]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Msn/msnmanager.php
Merge remote branch 'statusnet/1.0.x' into msn-plugin
[quix0rs-gnu-social.git] / plugins / Msn / msnmanager.php
1 <?php\r
2 /*\r
3  * StatusNet - the distributed open-source microblogging tool\r
4  * Copyright (C) 2008, 2009, StatusNet, Inc.\r
5  *\r
6  * This program is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU Affero General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * This program is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU Affero General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU Affero General Public License\r
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 \r
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }\r
21 \r
22 /**\r
23  * MSN background connection manager for MSN-using queue handlers,\r
24  * allowing them to send outgoing messages on the right connection.\r
25  *\r
26  * Input is handled during socket select loop, keepalive pings during idle.\r
27  * Any incoming messages will be handled.\r
28  *\r
29  * In a multi-site queuedaemon.php run, one connection will be instantiated\r
30  * for each site being handled by the current process that has MSN enabled.\r
31  */\r
32 \r
33 class MsnManager extends ImManager {\r
34     public $conn = null;\r
35     private $lastping = null;\r
36     private $pingInterval;\r
37 \r
38     /**\r
39      * Initialise connection to server.\r
40      *\r
41      * @return boolean true on success\r
42      */\r
43     public function start($master) {\r
44         if (parent::start($master)) {\r
45             $this->connect();\r
46             return true;\r
47         } else {\r
48             return false;\r
49         }\r
50     }\r
51 \r
52     /**\r
53     * Return any open sockets that the run loop should listen\r
54     * for input on.\r
55     *\r
56     * @return array Array of socket resources\r
57     */\r
58     public function getSockets() {\r
59         $this->connect();\r
60         if ($this->conn) {\r
61             return $this->conn->getSockets();\r
62         } else {\r
63             return array();\r
64         }\r
65     }\r
66 \r
67     /**\r
68      * Idle processing for io manager's execution loop.\r
69      * Send keepalive pings to server.\r
70      *\r
71      * @return void\r
72      */\r
73     public function idle($timeout = 0) {\r
74         if (empty($this->lastping) || time() - $this->lastping > $this->pingInterval) {\r
75             $this->send_ping();\r
76         }\r
77     }\r
78 \r
79     /**\r
80      * Message pump is triggered on socket input, so we only need an idle()\r
81      * call often enough to trigger our outgoing pings.\r
82      */\r
83     function timeout() {\r
84         return $this->pingInterval;\r
85     }\r
86 \r
87     /**\r
88      * Process MSN events that have come in over the wire.\r
89      *\r
90      * @param resource $socket Socket ready\r
91      * @return void\r
92      */\r
93     public function handleInput($socket) {\r
94         common_log(LOG_DEBUG, 'Servicing the MSN queue.');\r
95         $this->stats('msn_process');\r
96         $this->conn->receive();\r
97     }\r
98 \r
99     /**\r
100     * Initiate connection\r
101     *\r
102     * @return void\r
103     */\r
104     public function connect() {\r
105         if (!$this->conn) {\r
106             $this->conn = new MSN(\r
107                 array(\r
108                     'user' => $this->plugin->user,\r
109                     'password' => $this->plugin->password,\r
110                     'alias' => $this->plugin->nickname,\r
111                     'psm' => 'Send me a message to post a notice',\r
112                     'debug' => true\r
113                 )\r
114             );\r
115             $this->conn->registerHandler('IMin', array($this, 'handle_msn_message'));\r
116             $this->conn->registerHandler('SessionReady', array($this, 'handle_session_ready'));\r
117             $this->conn->registerHandler('Pong', array($this, 'update_ping_time'));\r
118             $this->conn->registerHandler('ConnectFailed', array($this, 'handle_connect_failed'));\r
119             $this->conn->registerHandler('Reconnect', array($this, 'handle_reconnect'));\r
120             $this->conn->signon();\r
121             $this->lastping = time();\r
122         }\r
123         return $this->conn;\r
124     }\r
125 \r
126     /**\r
127     * Called by the idle process to send a ping\r
128     * when necessary\r
129     *\r
130     * @return void\r
131     */\r
132     private function send_ping() {\r
133         $this->connect();\r
134         if (!$this->conn) {\r
135             return false;\r
136         }\r
137 \r
138         $this->conn->sendPing();\r
139         $this->lastping = time();\r
140         $this->pingInterval = 50;\r
141         return true;\r
142     }\r
143 \r
144     /**\r
145      * Update the time till the next ping\r
146      *\r
147      * @param $data Time till next ping\r
148      * @return void\r
149      */\r
150     public function update_ping_time($data) {\r
151         $this->pingInterval = $data;\r
152     }\r
153 \r
154     /**\r
155     * Called via a callback when a message is received\r
156     *\r
157     * Passes it back to the queuing system\r
158     *\r
159     * @param array $data Data\r
160     * @return boolean\r
161     */\r
162     public function handle_msn_message($data) {\r
163         $this->plugin->enqueue_incoming_raw($data);\r
164         return true;\r
165     }\r
166 \r
167     /**\r
168     * Called via a callback when a session becomes ready\r
169     *\r
170     * @param array $data Data\r
171     */\r
172     public function handle_session_ready($data) {\r
173         $sessionFailed = false;\r
174         $wm = Msn_waiting_message::top($data['to']);\r
175         while ($wm != NULL) {\r
176             if ($sessionFailed) {\r
177                 $this->plugin->send_message($wm->screenname, $wm->message);\r
178                 $sessionFailed = true;\r
179             } elseif (!$this->conn->sendMessage($wm->screenname, $wm->message, $ignore)) {\r
180                 $this->plugin->send_message($wm->screenname, $wm->message);\r
181             }\r
182 \r
183             $wm->delete();\r
184             $wm = Msn_waiting_message::top($data['to']);\r
185         }\r
186     }\r
187 \r
188     /**\r
189     * Called by callback to log failure during connect\r
190     *\r
191     * @param void $data Not used (there to keep callback happy)\r
192     * @return void\r
193     */\r
194     public function handle_connect_failed($data) {\r
195         common_log(LOG_NOTICE, 'MSN connect failed, retrying');\r
196     }\r
197 \r
198     /**\r
199     * Called by callback to log reconnection\r
200     *\r
201     * @param void $data Not used (there to keep callback happy)\r
202     * @return void\r
203     */\r
204     public function handle_reconnect($data) {\r
205         common_log(LOG_NOTICE, 'MSN reconnecting');\r
206     }\r
207 \r
208     /**\r
209     * Enters a message into the database for sending via a callback\r
210     * when the session is established\r
211     *\r
212     * @param string $to Intended recipient\r
213     * @param string $message Message\r
214     */\r
215     private function enqueue_waiting_message($to, $message) {\r
216         $wm = new Msn_waiting_message();\r
217 \r
218         $wm->screenname = $to;\r
219         $wm->message    = $message;\r
220         $wm->created    = common_sql_now();\r
221         $result         = $wm->insert();\r
222 \r
223         if (!$result) {\r
224             common_log_db_error($wm, 'INSERT', __FILE__);\r
225             throw new ServerException('DB error inserting queue item');\r
226         }\r
227 \r
228         return true;\r
229     }\r
230 \r
231     /**\r
232      * Send a message using the daemon\r
233      *\r
234      * @param $data Message data\r
235      * @return boolean true on success\r
236      */\r
237     public function send_raw_message($data) {\r
238         $this->connect();\r
239         if (!$this->conn) {\r
240             return false;\r
241         }\r
242 \r
243         $waitForSession = false;\r
244         if (!$this->conn->sendMessage($data['to'], $data['message'], $waitForSession)) {\r
245             if ($waitForSession) {\r
246                 $this->enqueue_waiting_message($data['to'], $data['message']);\r
247             } else {\r
248                 return false;\r
249             }\r
250         }\r
251 \r
252         // Sending a command updates the time till next ping\r
253         $this->lastping = time();\r
254         $this->pingInterval = 50;\r
255         return true;\r
256     }\r
257 }\r