]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/ircmanager.php
Initial 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
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_Bot;
71
72             $password = isset($this->plugin->password) ? $this->plugin->password : NULL;
73             $transport = isset($this->plugin->transport) ? $this->plugin->transport : 'tcp';
74             $encoding = isset($this->plugin->encoding) ? $this->plugin->encoding : 'ISO-8859-1';
75
76             $config = new Phergie_Extended_Config;
77             $config->readArray(
78                 array(
79                     // One array per connection, pretty self-explanatory
80                     'connections' => array(
81                         // Ex: All connection info for the Freenode network
82                         array(
83                             'host' => $this->plugin->host,
84                             'port' => $this->plugin->port,
85                             'username' => $this->plugin->username,
86                             'realname' => $this->plugin->realname,
87                             'nick' => $this->plugin->nickname,
88                             'password' => $password,
89                             'transport' => $transport,
90                             'encoding' => $encoding
91                         )
92                     ),
93
94                     'processor' => 'async',
95                     'processor.options' => array('usec' => 200000),
96                     // Time zone. See: http://www.php.net/manual/en/timezones.php
97                     'timezone' => 'UTC',
98
99     // Whitelist of plugins to load
100     'plugins' => array(
101         // To enable a plugin, simply add a string to this array containing
102         // the short name of the plugin as shown below.
103
104         // 'ShortPluginName',
105
106         // Below is an example of enabling the AutoJoin plugin, for which
107         // the corresponding PEAR package is Phergie_Plugin_AutoJoin. This
108         // plugin allows you to set a list of channels in this configuration
109         // file that the bot will automatically join when it connects to a
110         // server. If you'd like to enable this plugin, simply install it,
111         // uncomment the line below, and set a value for the setting
112         // autojoin.channels (examples for which are located further down in
113         // this file).
114
115         // 'AutoJoin',
116
117         // A few other recommended plugins:
118
119         // Servers randomly send PING events to clients to ensure that
120         // they're still connected and will eventually terminate the
121
122         // connection if a PONG response is not received. The Pong plugin
123         // handles sending these responses.
124
125         // 'Pong',
126
127         // It's sometimes difficult to distinguish between a lack of
128         // activity on a server and the client not receiving data even
129         // though a connection remains open. The Ping plugin performs a self
130         // CTCP PING sporadically to ensure that its connection is still
131         // functioning and, if not, terminates the bot.
132
133         // 'Ping',
134
135         // Sometimes it's desirable to have the bot disconnect gracefully
136         // when issued a command to do so via a PRIVMSG event. The Quit
137         // plugin implements this using the Command plugin to intercept the
138         // command.
139
140         // 'Quit',
141     ),
142
143     // If set to true, this allows any plugin dependencies for plugins
144     // listed in the 'plugins' option to be loaded even if they are not
145     // explicitly included in that list
146     'plugins.autoload' => true,
147
148     // Enables shell output describing bot events via Phergie_Ui_Console
149     'ui.enabled' => true,
150
151     // Examples of supported values for autojoins.channel:
152     // 'autojoin.channels' => '#channel1,#channel2',
153     // 'autojoin.channels' => array('#channel1', '#channel2'),
154     // 'autojoin.channels' => array(
155     //                            'host1' => '#channel1,#channel2',
156     //                            'host2' => array('#channel3', '#channel4')
157     //                        ),
158
159     // Examples of setting values for Ping plugin settings
160
161     // This is the amount of time in seconds that the Ping plugin will wait
162     // to receive an event from the server before it initiates a self-ping
163
164     // 'ping.event' => 300, // 5 minutes
165
166     // This is the amount of time in seconds that the Ping plugin will wait
167     // following a self-ping attempt before it assumes that a response will
168     // never be received and terminates the connection
169
170     // 'ping.ping' => 10, // 10 seconds
171
172 ));
173             $this->conn=new Aim($this->plugin->user,$this->plugin->password,4);
174             $this->conn->registerHandler("IMIn",array($this,"handle_aim_message"));
175             $this->conn->myServer="toc.oscar.aol.com";
176             $this->conn->signon();
177             $this->conn->setProfile(_m('Send me a message to post a notice'), false);
178         }
179         return $this->conn;
180     }
181
182     function handle_irc_message($data) {
183         $this->plugin->enqueue_incoming_raw($data);
184         return true;
185     }
186
187     function send_raw_message($data) {
188         $this->connect();
189         if (!$this->conn) {
190             return false;
191         }
192         $this->conn->sflapSend($data[0],$data[1],$data[2],$data[3]);
193         return true;
194     }
195 }