]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/ircmanager.php
Messages with new lines split at plugin stage for reliability
[quix0rs-gnu-social.git] / plugins / Irc / ircmanager.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 /**
23  * IRC background connection manager for IRC-using queue handlers,
24  * allowing them to send outgoing messages on the right connection.
25  *
26  * Input is handled during socket select loop, keepalive pings during idle.
27  * Any incoming messages will be handled.
28  *
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.
31  */
32
33 class IrcManager extends ImManager {
34     public $conn = null;
35     public $regchecks = array();
36     public $regchecksLookup = array();
37
38     /**
39      * Initialize connection to server.
40      *
41      * @return boolean true on success
42      */
43     public function start($master) {
44         if (parent::start($master)) {
45             $this->connect();
46             return true;
47         } else {
48             return false;
49         }
50     }
51
52     /**
53     * Return any open sockets that the run loop should listen
54     * for input on.
55     *
56     * @return array Array of socket resources
57     */
58     public function getSockets() {
59         $this->connect();
60         if ($this->conn) {
61             return $this->conn->getSockets();
62         } else {
63             return array();
64         }
65     }
66
67     /**
68      * Idle processing for io manager's execution loop.
69      * Send keepalive pings to server.
70      *
71      * @return void
72      */
73     public function idle() {
74         // Call Phergie's doTick methods if necessary
75         try {
76             $this->conn->handleEvents();
77         } catch (Phergie_Driver_Exception $e) {
78             $this->conn->reconnect();
79         }
80     }
81
82     /**
83      * Process IRC events that have come in over the wire.
84      *
85      * @param resource $socket
86      * @return void
87      */
88     public function handleInput($socket) {
89         common_log(LOG_DEBUG, 'Servicing the IRC queue.');
90         $this->stats('irc_process');
91
92         try {
93             $this->conn->handleEvents();
94         } catch (Phergie_Driver_Exception $e) {
95             $this->conn->reconnect();
96         }
97     }
98
99     /**
100     * Initiate connection
101     *
102     * @return void
103     */
104     public function connect() {
105         if (!$this->conn) {
106             $this->conn = new Phergie_StatusnetBot;
107
108             $config = new Phergie_Config;
109             $config->readArray(
110                 array(
111                     'connections' => array(
112                         array(
113                             'host' => $this->plugin->host,
114                             'port' => $this->plugin->port,
115                             'username' => $this->plugin->username,
116                             'realname' => $this->plugin->realname,
117                             'nick' => $this->plugin->nick,
118                             'password' => $this->plugin->password,
119                             'transport' => $this->plugin->transporttype,
120                             'encoding' => $this->plugin->encoding
121                         )
122                     ),
123
124                     'driver' => 'statusnet',
125
126                     'processor' => 'async',
127                     'processor.options' => array('sec' => 0, 'usec' => 0),
128
129                     'plugins' => array(
130                         'Pong',
131                         'NickServ',
132                         'AutoJoin',
133                         'Statusnet',
134                     ),
135
136                     'plugins.autoload' => true,
137
138                     'ui.enabled' => true,
139
140                     'nickserv.password' => $this->plugin->nickservpassword,
141                     'nickserv.identify_message' => $this->plugin->nickservidentifyregexp,
142
143                     'autojoin.channels' => $this->plugin->channels,
144
145                     'statusnet.messagecallback' => array($this, 'handle_irc_message'),
146                     'statusnet.regcallback' => array($this, 'handle_reg_response'),
147                     'statusnet.unregregexp' => $this->plugin->unregregexp,
148                     'statusnet.regregexp' => $this->plugin->regregexp
149                 )
150             );
151
152             $this->conn->setConfig($config);
153             $this->conn->connect();
154         }
155         return $this->conn;
156     }
157
158     /**
159     * Called via a callback when a message is received
160     *
161     * Passes it back to the queuing system
162     *
163     * @param array $data Data
164     * @return boolean
165     */
166     public function handle_irc_message($data) {
167         $this->plugin->enqueue_incoming_raw($data);
168         return true;
169     }
170
171     /**
172     * Called via a callback when NickServ responds to
173     * the bots query asking if a nick is registered
174     *
175     * @param array $data Data
176     * @return void
177     */
178     public function handle_reg_response($data) {
179         // Retrieve data
180         $screenname = $data['screenname'];
181         $nickdata = $this->regchecks[$screenname];
182         $usernick = $nickdata['user']->nickname;
183
184         if (isset($this->regchecksLookup[$usernick])) {
185             if ($data['registered']) {
186                 // Send message
187                 $this->plugin->send_confirmation_code($screenname, $nickdata['code'], $nickdata['user'], true);
188             } else {
189                 $this->plugin->send_message($screenname, _m('Your nickname is not registered so IRC connectivity cannot be enabled'));
190
191                 $confirm = new Confirm_address();
192
193                 $confirm->user_id      = $user->id;
194                 $confirm->address_type = $this->plugin->transport;
195
196                 if ($confirm->find(true)) {
197                     $result = $confirm->delete();
198
199                     if (!$result) {
200                         common_log_db_error($confirm, 'DELETE', __FILE__);
201                         // TRANS: Server error thrown on database error canceling IM address confirmation.
202                         $this->serverError(_('Couldn\'t delete confirmation.'));
203                         return;
204                     }
205                 }
206             }
207
208             // Unset lookup value
209             unset($this->regchecksLookup[$usernick]);
210
211             // Unset data
212             unset($this->regchecks[$screename]);
213         }
214     }
215
216     /**
217      * Send a message using the daemon
218      *
219      * @param $data Message
220      * @return boolean true on success
221      */
222     public function send_raw_message($data) {
223         $this->connect();
224         if (!$this->conn) {
225             return false;
226         }
227
228         if ($data['type'] != 'message') {
229             // Nick checking
230             $nickdata = $data['nickdata'];
231             $usernick = $nickdata['user']->nickname;
232             $screenname = $nickdata['screenname'];
233
234             // Cancel any existing checks for this user
235             if (isset($this->regchecksLookup[$usernick])) {
236                 unset($this->regchecks[$this->regchecksLookup[$usernick]]);
237             }
238
239             $this->regchecks[$screenname] = $nickdata;
240             $this->regchecksLookup[$usernick] = $screenname;
241         }
242
243         try {
244             $this->conn->send($data['data']['command'], $data['data']['args']);
245         } catch (Phergie_Driver_Exception $e) {
246             $this->conn->reconnect();
247             return false;
248         }
249
250         return true;
251     }
252 }