]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Xmpp/xmppmanager.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Xmpp / xmppmanager.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  * XMPP background connection manager for XMPP-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 XmppManager extends ImManager
34 {
35     protected $lastping = null;
36     protected $pingid = null;
37
38     public $conn = null;
39     
40     const PING_INTERVAL = 120;
41     
42
43     /**
44      * Initialize connection to server.
45      * @return boolean true on success
46      */
47     public function start($master)
48     {
49         if(parent::start($master))
50         {
51             $this->connect();
52             return true;
53         }else{
54             return false;
55         }
56     }
57
58     function send_raw_message($data)
59     {
60         $this->connect();
61         if (!$this->conn || $this->conn->isDisconnected()) {
62             return false;
63         }
64         $this->conn->send($data);
65         return true;
66     }
67
68     /**
69      * Message pump is triggered on socket input, so we only need an idle()
70      * call often enough to trigger our outgoing pings.
71      */
72     function timeout()
73     {
74         return self::PING_INTERVAL;
75     }
76
77     /**
78      * Process XMPP events that have come in over the wire.
79      * @fixme may kill process on XMPP error
80      * @param resource $socket
81      */
82     public function handleInput($socket)
83     {
84         # Process the queue for as long as needed
85         try {
86             common_log(LOG_DEBUG, "Servicing the XMPP queue.");
87             $this->stats('xmpp_process');
88             $this->conn->processTime(0);
89         } catch (XMPPHP_Exception $e) {
90             common_log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
91             die($e->getMessage());
92         }
93     }
94
95     /**
96      * Lists the IM connection socket to allow i/o master to wake
97      * when input comes in here as well as from the queue source.
98      *
99      * @return array of resources
100      */
101     public function getSockets()
102     {
103         $this->connect();
104         if($this->conn){
105             return array($this->conn->getSocket());
106         }else{
107             return array();
108         }
109     }
110
111     /**
112      * Idle processing for io manager's execution loop.
113      * Send keepalive pings to server.
114      *
115      * Side effect: kills process on exception from XMPP library.
116      *
117      * @fixme non-dying error handling
118      */
119     public function idle($timeout=0)
120     {
121         $now = time();
122         if (empty($this->lastping) || $now - $this->lastping > self::PING_INTERVAL) {
123             try {
124                 $this->send_ping();
125             } catch (XMPPHP_Exception $e) {
126                 common_log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
127                 die($e->getMessage());
128             }
129         }
130     }
131
132     function connect()
133     {
134         if (!$this->conn || $this->conn->isDisconnected()) {
135             $resource = 'queue' . posix_getpid();
136             $this->conn = new Sharing_XMPP($this->plugin->host ?
137                                     $this->plugin->host :
138                                     $this->plugin->server,
139                                     $this->plugin->port,
140                                     $this->plugin->user,
141                                     $this->plugin->password,
142                                     $this->plugin->resource,
143                                     $this->plugin->server,
144                                     $this->plugin->debug ?
145                                     true : false,
146                                     $this->plugin->debug ?
147                                     XMPPHP_Log::LEVEL_VERBOSE :  null
148                                     );
149
150             if (!$this->conn) {
151                 return false;
152             }
153             $this->conn->addEventHandler('message', 'handle_xmpp_message', $this);
154             $this->conn->addEventHandler('reconnect', 'handle_xmpp_reconnect', $this);
155             $this->conn->setReconnectTimeout(600);
156
157             $this->conn->autoSubscribe();
158             $this->conn->useEncryption($this->plugin->encryption);
159
160             try {
161                 $this->conn->connect(true); // true = persistent connection
162             } catch (XMPPHP_Exception $e) {
163                 common_log(LOG_ERR, $e->getMessage());
164                 return false;
165             }
166
167             $this->conn->processUntil('session_start');
168             $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
169         }
170         return $this->conn;
171     }
172
173     function send_ping()
174     {
175         $this->connect();
176         if (!$this->conn || $this->conn->isDisconnected()) {
177             return false;
178         }
179         $now = time();
180         if (!isset($this->pingid)) {
181             $this->pingid = 0;
182         } else {
183             $this->pingid++;
184         }
185
186         common_log(LOG_DEBUG, "Sending ping #{$this->pingid}");
187                 $this->conn->send("<iq from='{" . $this->plugin->daemon_screenname() . "}' to='{$this->plugin->server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
188         $this->lastping = $now;
189         return true;
190     }
191
192     function handle_xmpp_message(&$pl)
193     {
194         $this->plugin->enqueue_incoming_raw($pl);
195         return true;
196     }
197
198     /**
199      * Callback for Jabber reconnect event
200      * @param $pl
201      */
202     function handle_xmpp_reconnect(&$pl)
203     {
204         common_log(LOG_NOTICE, 'XMPP reconnected');
205
206         $this->conn->processUntil('session_start');
207         $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
208     }
209
210     /**
211      * sends a presence stanza on the XMPP network
212      *
213      * @param string $status   current status, free-form string
214      * @param string $show     structured status value
215      * @param string $to       recipient of presence, null for general
216      * @param string $type     type of status message, related to $show
217      * @param int    $priority priority of the presence
218      *
219      * @return boolean success value
220      */
221
222     function send_presence($status, $show='available', $to=null,
223                                   $type = 'available', $priority=null)
224     {
225         $this->connect();
226         if (!$this->conn || $this->conn->isDisconnected()) {
227             return false;
228         }
229         $this->conn->presence($status, $show, $to, $type, $priority);
230         return true;
231     }
232
233     /**
234      * sends a "special" presence stanza on the XMPP network
235      *
236      * @param string $type   Type of presence
237      * @param string $to     JID to send presence to
238      * @param string $show   show value for presence
239      * @param string $status status value for presence
240      *
241      * @return boolean success flag
242      *
243      * @see send_presence()
244      */
245
246     function special_presence($type, $to=null, $show=null, $status=null)
247     {
248         // FIXME: why use this instead of send_presence()?
249         $this->connect();
250         if (!$this->conn || $this->conn->isDisconnected()) {
251             return false;
252         }
253
254         $to     = htmlspecialchars($to);
255         $status = htmlspecialchars($status);
256
257         $out = "<presence";
258         if ($to) {
259             $out .= " to='$to'";
260         }
261         if ($type) {
262             $out .= " type='$type'";
263         }
264         if ($show == 'available' and !$status) {
265             $out .= "/>";
266         } else {
267             $out .= ">";
268             if ($show && ($show != 'available')) {
269                 $out .= "<show>$show</show>";
270             }
271             if ($status) {
272                 $out .= "<status>$status</status>";
273             }
274             $out .= "</presence>";
275         }
276         $this->conn->send($out);
277         return true;
278     }
279 }