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