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 * Idle processing for io manager's execution loop.
69 * Send keepalive pings to server.
73 public function idle() {
74 // Call Phergie's doTick methods if necessary
75 $this->conn->handleEvents();
79 * Process IRC events that have come in over the wire.
81 * @param resource $socket
84 public function handleInput($socket) {
85 common_log(LOG_DEBUG, 'Servicing the IRC queue.');
86 $this->stats('irc_process');
87 $this->conn->handleEvents();
95 public function connect() {
97 $this->conn = new Phergie_StatusnetBot;
99 $config = new Phergie_Config;
102 'connections' => array(
104 'host' => $this->plugin->host,
105 'port' => $this->plugin->port,
106 'username' => $this->plugin->username,
107 'realname' => $this->plugin->realname,
108 'nick' => $this->plugin->nick,
109 'password' => $this->plugin->password,
110 'transport' => $this->plugin->transporttype,
111 'encoding' => $this->plugin->encoding
115 'driver' => 'statusnet',
117 'processor' => 'async',
118 'processor.options' => array('sec' => 0, 'usec' => 0),
127 'plugins.autoload' => true,
129 'ui.enabled' => true,
131 'nickserv.password' => $this->plugin->nickservpassword,
132 'nickserv.identify_message' => $this->plugin->nickservidentifyregexp,
134 'autojoin.channels' => $this->plugin->channels,
136 'statusnet.messagecallback' => array($this, 'handle_irc_message'),
137 'statusnet.regcallback' => array($this, 'handle_reg_response'),
138 'statusnet.unregregexp' => $this->plugin->unregregexp,
139 'statusnet.regregexp' => $this->plugin->regregexp
143 $this->conn->setConfig($config);
144 $this->conn->connect();
150 * Called via a callback when a message is received
152 * Passes it back to the queuing system
154 * @param array $data Data
157 public function handle_irc_message($data) {
158 $this->plugin->enqueue_incoming_raw($data);
163 * Called via a callback when NickServ responds to
164 * the bots query asking if a nick is registered
166 * @param array $data Data
169 public function handle_reg_response($data) {
171 $screenname = $data['screenname'];
172 $nickdata = $this->regchecks[$screenname];
173 $usernick = $nickdata['user']->nickname;
175 if (isset($this->regchecksLookup[$usernick])) {
176 if ($data['registered']) {
178 $this->plugin->send_confirmation_code($screenname, $nickdata['code'], $nickdata['user'], true);
180 $this->plugin->send_message($screenname, _m('Your nickname is not registered so IRC connectivity cannot be enabled'));
182 $confirm = new Confirm_address();
184 $confirm->user_id = $user->id;
185 $confirm->address_type = $this->plugin->transport;
187 if ($confirm->find(true)) {
188 $result = $confirm->delete();
191 common_log_db_error($confirm, 'DELETE', __FILE__);
192 // TRANS: Server error thrown on database error canceling IM address confirmation.
193 $this->serverError(_('Couldn\'t delete confirmation.'));
199 // Unset lookup value
200 unset($this->regchecksLookup[$usernick]);
203 unset($this->regchecks[$screename]);
208 * Send a message using the daemon
210 * @param $data Message
211 * @return boolean true on success
213 public function send_raw_message($data) {
219 if ($data['type'] != 'message') {
221 $nickdata = $data['nickdata'];
222 $usernick = $nickdata['user']->nickname;
223 $screenname = $nickdata['screenname'];
225 // Cancel any existing checks for this user
226 if (isset($this->regchecksLookup[$usernick])) {
227 unset($this->regchecks[$this->regchecksLookup[$usernick]]);
230 $this->regchecks[$screenname] = $nickdata;
231 $this->regchecksLookup[$usernick] = $screenname;
234 $args = $data['data']['args'];
235 $lines = explode("\n", $args[1]);
237 foreach ($lines as $line) {
238 $this->conn->send($data['data']['command'], array($args[0], $line));
240 } catch (Phergie_Driver_Exception $e) {
241 $this->conn->reconnect();