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 $lastPing = null;
36 protected $regChecks = array();
37 protected $regChecksLookup = array();
40 * Initialize connection to server.
42 * @return boolean true on success
44 public function start($master) {
45 if (parent::start($master)) {
54 * Return any open sockets that the run loop should listen
57 * @return array Array of socket resources
59 public function getSockets() {
62 return $this->conn->getSockets();
69 * Idle processing for io manager's execution loop.
70 * Send keepalive pings to server.
74 public function idle() {
75 if (empty($this->lastPing) || time() - $this->lastPing > 120) {
81 * Process IRC events that have come in over the wire.
83 * @param resource $socket Socket to handle input on
86 public function handleInput($socket) {
87 common_log(LOG_DEBUG, 'Servicing the IRC queue.');
88 $this->stats('irc_process');
91 $this->conn->handleEvents();
92 } catch (Phergie_Driver_Exception $e) {
93 $this->conn->reconnect();
102 public function connect() {
104 $this->conn = new Phergie_StatusnetBot;
106 $config = new Phergie_Config;
109 'connections' => array(
111 'host' => $this->plugin->host,
112 'port' => $this->plugin->port,
113 'username' => $this->plugin->username,
114 'realname' => $this->plugin->realname,
115 'nick' => $this->plugin->nick,
116 'password' => $this->plugin->password,
117 'transport' => $this->plugin->transporttype,
118 'encoding' => $this->plugin->encoding
122 'driver' => 'statusnet',
124 'processor' => 'async',
125 'processor.options' => array('sec' => 0, 'usec' => 0),
134 'plugins.autoload' => true,
136 'ui.enabled' => true,
138 'nickserv.password' => $this->plugin->nickservpassword,
139 'nickserv.identify_message' => $this->plugin->nickservidentifyregexp,
141 'autojoin.channels' => $this->plugin->channels,
143 'statusnet.messagecallback' => array($this, 'handle_irc_message'),
144 'statusnet.regcallback' => array($this, 'handle_reg_response'),
145 'statusnet.unregregexp' => $this->plugin->unregregexp,
146 'statusnet.regregexp' => $this->plugin->regregexp
150 $this->conn->setConfig($config);
151 $this->lastPing = time();
152 $this->conn->connect();
158 * Called via a callback when a message is received
159 * Passes it back to the queuing system
161 * @param array $data Data
164 public function handle_irc_message($data) {
165 $this->plugin->enqueue_incoming_raw($data);
170 * Called via a callback when NickServ responds to
171 * the bots query asking if a nick is registered
173 * @param array $data Data
176 public function handle_reg_response($data) {
178 $screenname = $data['screenname'];
179 $nickdata = $this->regChecks[$screenname];
180 $usernick = $nickdata['user']->nickname;
182 if (isset($this->regChecksLookup[$usernick])) {
183 if ($data['registered']) {
185 $this->plugin->send_confirmation_code($screenname, $nickdata['code'], $nickdata['user'], true);
187 $this->plugin->send_message($screenname, _m('Your nickname is not registered so IRC connectivity cannot be enabled'));
189 $confirm = new Confirm_address();
191 $confirm->user_id = $user->id;
192 $confirm->address_type = $this->plugin->transport;
194 if ($confirm->find(true)) {
195 $result = $confirm->delete();
198 common_log_db_error($confirm, 'DELETE', __FILE__);
199 // TRANS: Server error thrown on database error canceling IM address confirmation.
200 $this->serverError(_('Couldn\'t delete confirmation.'));
206 // Unset lookup value
207 unset($this->regChecksLookup[$usernick]);
210 unset($this->regChecks[$screename]);
215 * Send a message using the daemon
217 * @param $data Message data
218 * @return boolean true on success
220 public function send_raw_message($data) {
226 if ($data['type'] != 'message') {
228 $nickdata = $data['nickdata'];
229 $usernick = $nickdata['user']->nickname;
230 $screenname = $nickdata['screenname'];
232 // Cancel any existing checks for this user
233 if (isset($this->regChecksLookup[$usernick])) {
234 unset($this->regChecks[$this->regChecksLookup[$usernick]]);
237 $this->regChecks[$screenname] = $nickdata;
238 $this->regChecksLookup[$usernick] = $screenname;
242 $this->conn->send($data['data']['command'], $data['data']['args']);
243 } catch (Phergie_Driver_Exception $e) {
244 $this->conn->reconnect();
256 protected function sendPing() {
257 $this->lastPing = time();
258 $this->conn->send('PING', $this->lastPing);