]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Msn/msnmanager.php
Added some more event handlers and corrected aADL scope
[quix0rs-gnu-social.git] / plugins / Msn / msnmanager.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  * AIM background connection manager for AIM-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 XMPP enabled.
31  */
32
33 class MsnManager extends ImManager
34 {
35     public $conn = null;
36     
37     protected $lastping = null;
38     
39     private $pingInterval;
40     
41     /**
42      * Initialize connection to server.
43      * @return boolean true on success
44      */
45     public function start($master)
46     {
47         if(parent::start($master))
48         {
49             $this->connect();
50             return true;
51         }else{
52             return false;
53         }
54     }
55
56     public function getSockets()
57     {
58         $this->connect();
59         if($this->conn){
60             return $this->conn->getSockets();
61         }else{
62             return array();
63         }
64     }
65     
66     /**
67      * Idle processing for io manager's execution loop.
68      * Send keepalive pings to server.
69      */
70     public function idle($timeout=0)
71     {
72         $now = time();
73         if (empty($this->lastping) || $now - $this->lastping > $pingInterval) {
74             $this->send_ping();
75         }
76     }
77     
78     /**
79      * Process MSN events that have come in over the wire.
80      * @param resource $socket
81      */
82     public function handleInput($socket)
83     {
84         common_log(LOG_DEBUG, "Servicing the MSN queue.");
85         $this->stats('msn_process');
86         $this->conn->receive();
87     }
88
89     function connect()
90     {
91         if (!$this->conn) {
92             $this->conn=new MSN(array(
93                                       'user' => $this->plugin->user,
94                                       'password' => $this->plugin->password,
95                                       'alias' => $this->plugin->nickname,
96                                       'psm' => 'Send me a message to post a notice',
97                                       'debug' => true
98                                 )
99                         );
100             $this->conn->registerHandler("IMIn", array($this, 'handle_msn_message'));
101             $this->conn->registerHandler('Pong', array($this, 'update_ping_time'));
102             $this->conn->registerHandler('ConnectFailed', array($this, 'handle_connect_failed'));
103             $this->conn->registerHandler('Reconnect', array($this, 'handle_reconnect'));
104             $this->conn->signon();
105             $this->lastping = time();
106         }
107         return $this->conn;
108     }
109     
110     function send_ping() {
111         $this->connect();
112         if (!$this->conn) {
113             return false;
114         }
115         
116         $now = time();
117         
118         $this->conn->send_ping();
119         $this->lastping = $now;
120         return true;
121     }
122     
123     /**
124      * Update the time till the next ping
125      * @param $data Time till next ping
126      */
127     function update_ping_time($data) {
128         $pingInterval = $data;
129     }
130     
131     function handle_msn_message($data)
132     {
133         $this->plugin->enqueue_incoming_raw($data);
134         return true;
135     }
136     
137     function handle_connect_failed($data) {
138         common_log(LOG_NOTICE, 'MSN connect failed, retrying');
139     }
140     
141     function handle_reconnect($data) {
142         common_log(LOG_NOTICE, 'MSN reconnecting');
143     }
144
145     function send_raw_message($data)
146     {
147         $this->connect();
148         if (!$this->conn) {
149             return false;
150         }
151         $this->conn->sflapSend($data[0],$data[1],$data[2],$data[3]);
152         
153         // Sending a command updates the time till next ping
154         $this->lastping = time();
155         $this->pingInterval = 50;
156         return true;
157     }
158 }