]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Msn/msnmanager.php
Added some more error handling and commenting
[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      * Process MSN events that have come in over the wire.\r
81      *\r
82      * @param resource $socket Socket ready\r
83      * @return void\r
84      */\r
85     public function handleInput($socket) {\r
86         common_log(LOG_DEBUG, 'Servicing the MSN queue.');\r
87         $this->stats('msn_process');\r
88         $this->conn->receive();\r
89     }\r
90 \r
91     /**\r
92     * Initiate connection\r
93     *\r
94     * @return void\r
95     */\r
96     function connect() {\r
97         if (!$this->conn) {\r
98             $this->conn = new MSN(\r
99                 array(\r
100                     'user' => $this->plugin->user,\r
101                     'password' => $this->plugin->password,\r
102                     'alias' => $this->plugin->nickname,\r
103                     'psm' => 'Send me a message to post a notice',\r
104                     'debug' => true\r
105                 )\r
106             );\r
107             $this->conn->registerHandler("IMIn", array($this, 'handle_msn_message'));\r
108             $this->conn->registerHandler('Pong', array($this, 'update_ping_time'));\r
109             $this->conn->registerHandler('ConnectFailed', array($this, 'handle_connect_failed'));\r
110             $this->conn->registerHandler('Reconnect', array($this, 'handle_reconnect'));\r
111             $this->conn->signon();\r
112             $this->lastping = time();\r
113         }\r
114         return $this->conn;\r
115     }\r
116 \r
117     /**\r
118     * Called by the idle process to send a ping\r
119     * when necessary\r
120     *\r
121     * @return void\r
122     */\r
123     private function send_ping() {\r
124         $this->connect();\r
125         if (!$this->conn) {\r
126             return false;\r
127         }\r
128 \r
129         $this->conn->sendPing();\r
130         $this->lastping = time();\r
131         $this->pingInterval = 50;\r
132         return true;\r
133     }\r
134 \r
135     /**\r
136      * Update the time till the next ping\r
137      * \r
138      * @param $data Time till next ping\r
139      * @return void\r
140      */\r
141     private function update_ping_time($data) {\r
142         $pingInterval = $data;\r
143     }\r
144 \r
145     /**\r
146     * Called via a callback when a message is received\r
147     *\r
148     * Passes it back to the queuing system\r
149     *\r
150     * @param array $data Data\r
151     * @return void\r
152     */\r
153     private function handle_msn_message($data) {\r
154         $this->plugin->enqueue_incoming_raw($data);\r
155         return true;\r
156     }\r
157 \r
158     /**\r
159     * Called by callback to log failure during connect\r
160     *\r
161     * @param void $data Not used (there to keep callback happy)\r
162     * @return void\r
163     */\r
164     function handle_connect_failed($data) {\r
165         common_log(LOG_NOTICE, 'MSN connect failed, retrying');\r
166     }\r
167 \r
168     /**\r
169     * Called by callback to log reconnection\r
170     *\r
171     * @param void $data Not used (there to keep callback happy)\r
172     * @return void\r
173     */\r
174     function handle_reconnect($data) {\r
175         common_log(LOG_NOTICE, 'MSN reconnecting');\r
176     }\r
177 \r
178     /**\r
179      * Send a message using the daemon\r
180      * \r
181      * @param $data Message\r
182      * @return boolean true on success\r
183      */\r
184     function send_raw_message($data) {\r
185         $this->connect();\r
186         if (!$this->conn) {\r
187             return false;\r
188         }\r
189 \r
190         if (!$this->conn->sendMessage($data['to'], $data['message'])) {\r
191             return false;\r
192         }\r
193 \r
194         // Sending a command updates the time till next ping\r
195         $this->lastping = time();\r
196         $this->pingInterval = 50;\r
197         return true;\r
198     }\r
199 }\r