]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/ircmanager.php
Rename Phergie_ExtendedBot to Phergie_StatusnetBot
[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_StatusnetBot;
85
86             $port = empty($this->plugin->port) ? 6667 : $this->plugin->port;
87             $password = empty($this->plugin->password) ? '' : $this->plugin->password;
88             $transport = empty($this->plugin->transporttype) ? 'tcp' : $this->plugin->transporttype;
89             $encoding = empty($this->plugin->encoding) ? 'UTF-8' : $this->plugin->encoding;
90             $nickservpassword = empty($this->plugin->nickservpassword) ? '' : $this->plugin->nickservpassword;
91             $channels = empty($this->plugin->channels) ? array() : $this->plugin->channels;
92
93             $config = new Phergie_Config;
94             $config->readArray(
95                 array(
96                     'connections' => array(
97                         array(
98                             'host' => $this->plugin->host,
99                             'port' => $port,
100                             'username' => $this->plugin->username,
101                             'realname' => $this->plugin->realname,
102                             'nick' => $this->plugin->nick,
103                             'password' => $password,
104                             'transport' => $transport,
105                             'encoding' => $encoding
106                         )
107                     ),
108
109                     'driver' => 'statusnet',
110
111                     'processor' => 'statusnet',
112
113                     'plugins' => array(
114                         'Pong',
115                         'NickServ',
116                         'AutoJoin',
117                         'Statusnet_Callback',
118                     ),
119
120                     'plugins.autoload' => true,
121
122                     'ui.enabled' => true,
123
124                     'nickserv.password' => $nickservpassword,
125                     'autojoin.channels' => $channels,
126                     'statusnet_callback.callback' => array($this, 'handle_irc_message')
127                 )
128             );
129
130             $this->conn->setConfig($config);
131             $this->conn->connect();
132         }
133         return $this->conn;
134     }
135
136     /**
137     * Called via a callback when a message is received
138     *
139     * Passes it back to the queuing system
140     *
141     * @param array $data Data
142     * @return boolean
143     */
144     public function handle_irc_message($data) {
145         $this->plugin->enqueue_incoming_raw($data);
146         return true;
147     }
148
149     /**
150      * Send a message using the daemon
151      *
152      * @param $data Message
153      * @return boolean true on success
154      */
155     public function send_raw_message($data) {
156         $this->connect();
157         if (!$this->conn) {
158             return false;
159         }
160         $this->conn->send($data[0], $data[1]);
161         return true;
162     }
163 }