3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23 * IRC background connection manager for IRC-using queue handlers,
24 * allowing them to send outgoing messages on the right connection.
26 * Input is handled during socket select loop, Any incoming messages will be handled.
28 * In a multi-site queuedaemon.php run, one connection will be instantiated
29 * for each site being handled by the current process that has IRC enabled.
32 class IrcManager extends ImManager {
33 protected $conn = null;
34 protected $regchecks = array();
35 protected $regchecksLookup = array();
38 * Initialize connection to server.
40 * @return boolean true on success
42 public function start($master) {
43 if (parent::start($master)) {
52 * Return any open sockets that the run loop should listen
55 * @return array Array of socket resources
57 public function getSockets() {
60 return $this->conn->getSockets();
67 * Process IRC events that have come in over the wire.
69 * @param resource $socket Socket to handle input on
72 public function handleInput($socket) {
73 common_log(LOG_DEBUG, 'Servicing the IRC queue.');
74 $this->stats('irc_process');
77 $this->conn->handleEvents();
78 } catch (Phergie_Driver_Exception $e) {
79 $this->conn->reconnect();
88 public function connect() {
90 $this->conn = new Phergie_StatusnetBot;
92 $config = new Phergie_Config;
95 'connections' => array(
97 'host' => $this->plugin->host,
98 'port' => $this->plugin->port,
99 'username' => $this->plugin->username,
100 'realname' => $this->plugin->realname,
101 'nick' => $this->plugin->nick,
102 'password' => $this->plugin->password,
103 'transport' => $this->plugin->transporttype,
104 'encoding' => $this->plugin->encoding
108 'driver' => 'statusnet',
110 'processor' => 'async',
111 'processor.options' => array('sec' => 0, 'usec' => 0),
120 'plugins.autoload' => true,
122 'ui.enabled' => true,
124 'nickserv.password' => $this->plugin->nickservpassword,
125 'nickserv.identify_message' => $this->plugin->nickservidentifyregexp,
127 'autojoin.channels' => $this->plugin->channels,
129 'statusnet.messagecallback' => array($this, 'handle_irc_message'),
130 'statusnet.regcallback' => array($this, 'handle_reg_response'),
131 'statusnet.unregregexp' => $this->plugin->unregregexp,
132 'statusnet.regregexp' => $this->plugin->regregexp
136 $this->conn->setConfig($config);
137 $this->conn->connect();
143 * Called via a callback when a message is received
144 * Passes it back to the queuing system
146 * @param array $data Data
149 public function handle_irc_message($data) {
150 $this->plugin->enqueue_incoming_raw($data);
155 * Called via a callback when NickServ responds to
156 * the bots query asking if a nick is registered
158 * @param array $data Data
161 public function handle_reg_response($data) {
163 $screenname = $data['screenname'];
164 $nickdata = $this->regchecks[$screenname];
165 $usernick = $nickdata['user']->nickname;
167 if (isset($this->regchecksLookup[$usernick])) {
168 if ($data['registered']) {
170 $this->plugin->send_confirmation_code($screenname, $nickdata['code'], $nickdata['user'], true);
172 $this->plugin->send_message($screenname, _m('Your nickname is not registered so IRC connectivity cannot be enabled'));
174 $confirm = new Confirm_address();
176 $confirm->user_id = $user->id;
177 $confirm->address_type = $this->plugin->transport;
179 if ($confirm->find(true)) {
180 $result = $confirm->delete();
183 common_log_db_error($confirm, 'DELETE', __FILE__);
184 // TRANS: Server error thrown on database error canceling IM address confirmation.
185 $this->serverError(_('Couldn\'t delete confirmation.'));
191 // Unset lookup value
192 unset($this->regchecksLookup[$usernick]);
195 unset($this->regchecks[$screename]);
200 * Send a message using the daemon
202 * @param $data Message data
203 * @return boolean true on success
205 public function send_raw_message($data) {
211 if ($data['type'] != 'message') {
213 $nickdata = $data['nickdata'];
214 $usernick = $nickdata['user']->nickname;
215 $screenname = $nickdata['screenname'];
217 // Cancel any existing checks for this user
218 if (isset($this->regchecksLookup[$usernick])) {
219 unset($this->regchecks[$this->regchecksLookup[$usernick]]);
222 $this->regchecks[$screenname] = $nickdata;
223 $this->regchecksLookup[$usernick] = $screenname;
227 $this->conn->send($data['data']['command'], $data['data']['args']);
228 } catch (Phergie_Driver_Exception $e) {
229 $this->conn->reconnect();