]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/ircmanager.php
ircmanager.php almost complete - Need to add exception catching
[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
36     /**
37      * Initialize connection to server.
38      *
39      * @return boolean true on success
40      */
41     public function start($master) {
42         if (parent::start($master)) {
43             $this->connect();
44             return true;
45         } else {
46             return false;
47         }
48     }
49
50     /**
51     * Return any open sockets that the run loop should listen
52     * for input on.
53     *
54     * @return array Array of socket resources
55     */
56     public function getSockets() {
57         $this->connect();
58         if ($this->conn) {
59             return $this->conn->getSockets();
60         } else {
61             return array();
62         }
63     }
64
65     /**
66      * Process IRC events that have come in over the wire.
67      *
68      * @param resource $socket
69      * @return void
70      */
71     public function handleInput($socket) {
72         common_log(LOG_DEBUG, 'Servicing the IRC queue.');
73         $this->stats('irc_process');
74         $this->conn->receive();
75     }
76
77     /**
78     * Initiate connection
79     *
80     * @return void
81     */
82     public function connect() {
83         if (!$this->conn) {
84             $this->conn = new Phergie_Extended_Bot;
85
86             $password = isset($this->plugin->password) ? $this->plugin->password : '';
87             $transport = isset($this->plugin->transport) ? $this->plugin->transport : 'tcp';
88             $encoding = isset($this->plugin->encoding) ? $this->plugin->encoding : 'ISO-8859-1';
89             $nickservpassword = isset($this->plugin->nickservpassword) ? $this->plugin->nickservpassword : '';
90             $channels = isset($this->plugin->channels) ? $this->plugin->channels : array();
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->nickname,
102                             'password' => $password,
103                             'transport' => $transport,
104                             'encoding' => $encoding
105                         )
106                     ),
107
108                     'driver' => 'statusnet',
109
110                     'processor' => 'statusnet',
111
112                     'plugins' => array(
113                         'Pong',
114                         'NickServ',
115                         'AutoJoin',
116                         'Statusnet_Callback',
117                     ),
118
119                     'plugins.autoload' => true,
120
121                     'ui.enabled' => true,
122
123                     'nickserv.password' => $nickservpassword,
124                     'autojoin.channels' => $channels,
125                     'statusnet_callback.callback' => array($this, 'handle_irc_message')
126                 )
127             );
128
129             $this->conn->setConfig($config);
130             $this->conn->connect();
131         }
132         return $this->conn;
133     }
134
135     /**
136     * Called via a callback when a message is received
137     *
138     * Passes it back to the queuing system
139     *
140     * @param array $data Data
141     * @return boolean
142     */
143     public function handle_irc_message($data) {
144         $this->plugin->enqueue_incoming_raw($data);
145         return true;
146     }
147
148     /**
149      * Send a message using the daemon
150      *
151      * @param $data Message
152      * @return boolean true on success
153      */
154     public function send_raw_message($data) {
155         $this->connect();
156         if (!$this->conn) {
157             return false;
158         }
159         $this->conn->send($data[0], $data[1]);
160         return true;
161     }
162 }