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