]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/xmppqueuehandler.php
change Laconica and Control Yourself to StatusNet in PHP files
[quix0rs-gnu-social.git] / lib / xmppqueuehandler.php
1 <?php
2 /*
3  * StatusNet - a 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('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/queuehandler.php');
23
24 define('PING_INTERVAL', 120);
25
26 /**
27  * Common superclass for all XMPP-using queue handlers. They all need to
28  * service their message queues on idle, and forward any incoming messages
29  * to the XMPP listener connection. So, we abstract out common code to a
30  * superclass.
31  */
32
33 class XmppQueueHandler extends QueueHandler
34 {
35     var $pingid = 0;
36     var $lastping = null;
37
38     function start()
39     {
40         # Low priority; we don't want to receive messages
41         $this->log(LOG_INFO, "INITIALIZE");
42         $this->conn = jabber_connect($this->_id.$this->transport());
43         if ($this->conn) {
44             $this->conn->addEventHandler('message', 'forward_message', $this);
45             $this->conn->addEventHandler('reconnect', 'handle_reconnect', $this);
46             $this->conn->setReconnectTimeout(600);
47             jabber_send_presence("Send me a message to post a notice", 'available', null, 'available', -1);
48         }
49         return !is_null($this->conn);
50     }
51
52     function timeout()
53     {
54         return 10;
55     }
56
57     function handle_reconnect(&$pl)
58     {
59         $this->conn->processUntil('session_start');
60         $this->conn->presence(null, 'available', null, 'available', -1);
61     }
62
63     function idle($timeout=0)
64     {
65         # Process the queue for as long as needed
66         try {
67             if ($this->conn) {
68                 $this->log(LOG_DEBUG, "Servicing the XMPP queue.");
69                 $this->conn->processTime($timeout);
70                 $now = time();
71                 if (empty($this->lastping) || $now - $this->lastping > PING_INTERVAL) {
72                     $this->sendPing();
73                     $this->lastping = $now;
74                 }
75             }
76         } catch (XMPPHP_Exception $e) {
77             $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
78             die($e->getMessage());
79         }
80     }
81
82     function sendPing()
83     {
84         $jid = jabber_daemon_address().'/'.$this->_id.$this->transport();
85         $server = common_config('xmpp', 'server');
86
87         if (!isset($this->pingid)) {
88             $this->pingid = 0;
89         } else {
90             $this->pingid++;
91         }
92
93         $this->log(LOG_DEBUG, "Sending ping #{$this->pingid}");
94
95                 $this->conn->send("<iq from='{$jid}' to='{$server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
96     }
97
98     function forward_message(&$pl)
99     {
100         if ($pl['type'] != 'chat') {
101             $this->log(LOG_DEBUG, 'Ignoring message of type ' . $pl['type'] . ' from ' . $pl['from']);
102             return;
103         }
104         $listener = $this->listener();
105         if (strtolower($listener) == strtolower($pl['from'])) {
106             $this->log(LOG_WARNING, 'Ignoring loop message.');
107             return;
108         }
109         $this->log(LOG_INFO, 'Forwarding message from ' . $pl['from'] . ' to ' . $listener);
110         $this->conn->message($this->listener(), $pl['body'], 'chat', null, $this->ofrom($pl['from']));
111     }
112
113     function ofrom($from)
114     {
115         $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
116         $address .= "<address type='ofrom' jid='$from' />\n";
117         $address .= "</addresses>\n";
118         return $address;
119     }
120
121     function listener()
122     {
123         if (common_config('xmpp', 'listener')) {
124             return common_config('xmpp', 'listener');
125         } else {
126             return jabber_daemon_address() . '/' . common_config('xmpp','resource') . 'daemon';
127         }
128     }
129
130     function getSockets()
131     {
132         return array($this->conn->getSocket());
133     }
134 }