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