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