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, keepalive pings during idle.
27 * Any incoming messages will be handled.
29 * In a multi-site queuedaemon.php run, one connection will be instantiated
30 * for each site being handled by the current process that has IRC enabled.
33 class IrcManager extends ImManager {
35 public $regchecks = array();
36 public $regchecksLookup = array();
39 * Initialize connection to server.
41 * @return boolean true on success
43 public function start($master) {
44 if (parent::start($master)) {
53 * Return any open sockets that the run loop should listen
56 * @return array Array of socket resources
58 public function getSockets() {
61 return $this->conn->getSockets();
68 * Process IRC events that have come in over the wire.
70 * @param resource $socket
73 public function handleInput($socket) {
74 common_log(LOG_DEBUG, 'Servicing the IRC queue.');
75 $this->stats('irc_process');
76 $this->conn->receive();
84 public function connect() {
86 $this->conn = new Phergie_StatusnetBot;
88 $config = new Phergie_Config;
91 'connections' => array(
93 'host' => $this->plugin->host,
95 'username' => $this->plugin->username,
96 'realname' => $this->plugin->realname,
97 'nick' => $this->plugin->nick,
98 'password' => $this->plugin->password,
99 'transport' => $this->plugin->transporttype,
100 'encoding' => $this->plugin->encoding
104 'driver' => 'statusnet',
106 'processor' => 'async',
107 'processor.options' => array('usec' => 0),
116 'plugins.autoload' => true,
118 'ui.enabled' => true,
120 'nickserv.password' => $this->plugin->nickservpassword,
121 'autojoin.channels' => $this->plugin->channels,
122 'statusnet.messagecallback' => array($this, 'handle_irc_message'),
123 'statusnet.regcallback' => array($this, 'handle_reg_response')
127 $this->conn->setConfig($config);
128 $this->conn->connect();
134 * Called via a callback when a message is received
136 * Passes it back to the queuing system
138 * @param array $data Data
141 public function handle_irc_message($data) {
142 $this->plugin->enqueue_incoming_raw($data);
147 * Called via a callback when NickServ responds to
148 * the bots query asking if a nick is registered
150 * @param array $data Data
153 public function handle_reg_response($data) {
155 $screenname = $data['screenname'];
156 $nickdata = $this->regchecks[$screenname];
157 $usernick = $nickdata['user']->nickname;
159 if (isset($this->regchecksLookup[$usernick])) {
160 if ($data['registered']) {
162 $this->plugin->send_confirmation_code($screenname, $nickdata['code'], $nickdata['user'], true);
164 $this->plugin->send_message($screenname, _m('Your nickname is not registered so IRC connectivity cannot be enabled'));
166 $confirm = new Confirm_address();
168 $confirm->user_id = $user->id;
169 $confirm->address_type = $this->plugin->transport;
171 if ($confirm->find(true)) {
172 $result = $confirm->delete();
175 common_log_db_error($confirm, 'DELETE', __FILE__);
176 // TRANS: Server error thrown on database error canceling IM address confirmation.
177 $this->serverError(_('Couldn\'t delete confirmation.'));
183 // Unset lookup value
184 unset($this->regchecksLookup[$usernick]);
187 unset($this->regchecks[$screename]);
192 * Send a message using the daemon
194 * @param $data Message
195 * @return boolean true on success
197 public function send_raw_message($data) {
203 if ($data['type'] != 'message') {
205 $nickdata = $data['nickdata'];
206 $usernick = $nickdata['user']->nickname;
207 $screenname = $nickdata['screenname'];
209 // Cancel any existing checks for this user
210 if (isset($this->regchecksLookup[$usernick])) {
211 unset($this->regchecks[$this->regchecksLookup[$usernick]]);
214 $this->regchecks[$screenname] = $nickdata;
215 $this->regchecksLookup[$usernick] = $screenname;
219 $this->conn->send($data['data']['command'], $data['data']['args']);
220 } catch (Phergie_Driver_Exception $e) {
221 $this->conn->reconnect();