]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/ircmanager.php
c404c751ca526277623e303f2bdac56b59806674
[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
35     public $conn = null;
36     /**
37      * Initialize connection to server.
38      * @return boolean true on success
39      */
40     public function start($master) {
41         if (parent::start($master)) {
42             $this->connect();
43             return true;
44         } else {
45             return false;
46         }
47     }
48
49     public function getSockets() {
50         $this->connect();
51         if ($this->conn) {
52             return array($this->conn->myConnection);
53         } else {
54             return array();
55         }
56     }
57
58     /**
59      * Process IRC events that have come in over the wire.
60      * @param resource $socket
61      */
62     public function handleInput($socket) {
63         common_log(LOG_DEBUG, 'Servicing the IRC queue.');
64         $this->stats('irc_process');
65         $this->conn->receive();
66     }
67
68     function connect() {
69         if (!$this->conn) {
70             $this->conn = new Phergie_Extended_Bot;
71
72             $password = isset($this->plugin->password) ? $this->plugin->password : '';
73             $transport = isset($this->plugin->transport) ? $this->plugin->transport : 'tcp';
74             $encoding = isset($this->plugin->encoding) ? $this->plugin->encoding : 'ISO-8859-1';
75             $nickservpassword = isset($this->plugin->nickservpassword) ? $this->plugin->nickservpassword : '';
76             $channels = isset($this->plugin->channels) ? $this->plugin->channels : array();
77
78             $config = new Phergie_Config;
79             $config->readArray(
80                 array(
81                     // One array per connection, pretty self-explanatory
82                     'connections' => array(
83                         array(
84                             'host' => $this->plugin->host,
85                             'port' => $this->plugin->port,
86                             'username' => $this->plugin->username,
87                             'realname' => $this->plugin->realname,
88                             'nick' => $this->plugin->nickname,
89                             'password' => $password,
90                             'transport' => $transport,
91                             'encoding' => $encoding
92                         )
93                     ),
94
95                     'driver' => 'statusnet',
96
97                     'processor' => 'statusnet',
98
99                     'plugins' => array(
100                         'Pong',
101                         'NickServ',
102                         'AutoJoin',
103                         'Statusnet_Callback',
104                     ),
105
106                     'plugins.autoload' => true,
107
108                     'ui.enabled' => true,
109
110                     'nickserv.password' => $nickservpassword,
111                     'autojoin.channels' => $channels,
112                     'statusnet_callback.callback' => array($this, 'handle_irc_message')
113                 )
114             );
115
116             $this->conn->setConfig($config);
117             $this->conn->connect();
118         }
119         return $this->conn;
120     }
121
122     function handle_irc_message($data) {
123         $this->plugin->enqueue_incoming_raw($data);
124         return true;
125     }
126
127     function send_raw_message($data) {
128         $this->connect();
129         if (!$this->conn) {
130             return false;
131         }
132         $this->conn->sflapSend($data[0],$data[1],$data[2],$data[3]);
133         return true;
134     }
135 }