]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/xmppqueuehandler.php
Remove more contractions
[quix0rs-gnu-social.git] / lib / xmppqueuehandler.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 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 do not want to receive messages
41
42         $this->log(LOG_INFO, "INITIALIZE");
43         $this->conn = jabber_connect($this->_id.$this->transport());
44
45         if (empty($this->conn)) {
46             $this->log(LOG_ERR, "Could not connect to server.");
47             return false;
48         }
49
50         $this->conn->addEventHandler('message', 'forward_message', $this);
51         $this->conn->addEventHandler('reconnect', 'handle_reconnect', $this);
52         $this->conn->setReconnectTimeout(600);
53         jabber_send_presence("Send me a message to post a notice", 'available', null, 'available', -1);
54
55         return !is_null($this->conn);
56     }
57
58     function timeout()
59     {
60         return 10;
61     }
62
63     function handle_reconnect(&$pl)
64     {
65         $this->log(LOG_NOTICE, 'reconnected');
66
67         $this->conn->processUntil('session_start');
68         $this->conn->presence(null, 'available', null, 'available', -1);
69     }
70
71     function idle($timeout=0)
72     {
73         # Process the queue for as long as needed
74         try {
75             if ($this->conn) {
76                 $this->log(LOG_DEBUG, "Servicing the XMPP queue.");
77                 $this->conn->processTime($timeout);
78                 $now = time();
79                 if (empty($this->lastping) || $now - $this->lastping > PING_INTERVAL) {
80                     $this->sendPing();
81                     $this->lastping = $now;
82                 }
83             }
84         } catch (XMPPHP_Exception $e) {
85             $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
86             die($e->getMessage());
87         }
88     }
89
90     function sendPing()
91     {
92         $jid = jabber_daemon_address().'/'.$this->_id.$this->transport();
93         $server = common_config('xmpp', 'server');
94
95         if (!isset($this->pingid)) {
96             $this->pingid = 0;
97         } else {
98             $this->pingid++;
99         }
100
101         $this->log(LOG_DEBUG, "Sending ping #{$this->pingid}");
102
103                 $this->conn->send("<iq from='{$jid}' to='{$server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
104     }
105
106     function forward_message(&$pl)
107     {
108         if ($pl['type'] != 'chat') {
109             $this->log(LOG_DEBUG, 'Ignoring message of type ' . $pl['type'] . ' from ' . $pl['from']);
110             return;
111         }
112         $listener = $this->listener();
113         if (strtolower($listener) == strtolower($pl['from'])) {
114             $this->log(LOG_WARNING, 'Ignoring loop message.');
115             return;
116         }
117         $this->log(LOG_INFO, 'Forwarding message from ' . $pl['from'] . ' to ' . $listener);
118         $this->conn->message($this->listener(), $pl['body'], 'chat', null, $this->ofrom($pl['from']));
119     }
120
121     function ofrom($from)
122     {
123         $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
124         $address .= "<address type='ofrom' jid='$from' />\n";
125         $address .= "</addresses>\n";
126         return $address;
127     }
128
129     function listener()
130     {
131         if (common_config('xmpp', 'listener')) {
132             return common_config('xmpp', 'listener');
133         } else {
134             return jabber_daemon_address() . '/' . common_config('xmpp','resource') . 'daemon';
135         }
136     }
137
138     function getSockets()
139     {
140         return array($this->conn->getSocket());
141     }
142 }