3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/queuehandler.php');
24 define('PING_INTERVAL', 120);
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
33 class XmppQueueHandler extends QueueHandler
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());
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);
49 return !is_null($this->conn);
57 function handle_reconnect(&$pl)
59 $this->conn->processUntil('session_start');
60 $this->conn->presence(null, 'available', null, 'available', -1);
63 function idle($timeout=0)
65 # Process the queue for as long as needed
68 $this->log(LOG_DEBUG, "Servicing the XMPP queue.");
69 $this->conn->processTime($timeout);
71 if (empty($this->lastping) || $now - $this->lastping > PING_INTERVAL) {
73 $this->lastping = $now;
76 } catch (XMPPHP_Exception $e) {
77 $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
78 die($e->getMessage());
84 $jid = jabber_daemon_address().'/'.$this->_id.$this->transport();
85 $server = common_config('xmpp', 'server');
87 if (!isset($this->pingid)) {
93 $this->log(LOG_DEBUG, "Sending ping #{$this->pingid}");
95 $this->conn->send("<iq from='{$jid}' to='{$server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
98 function forward_message(&$pl)
100 if ($pl['type'] != 'chat') {
101 $this->log(LOG_DEBUG, 'Ignoring message of type ' . $pl['type'] . ' from ' . $pl['from']);
104 $listener = $this->listener();
105 if (strtolower($listener) == strtolower($pl['from'])) {
106 $this->log(LOG_WARNING, 'Ignoring loop message.');
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']));
113 function ofrom($from)
115 $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
116 $address .= "<address type='ofrom' jid='$from' />\n";
117 $address .= "</addresses>\n";
123 if (common_config('xmpp', 'listener')) {
124 return common_config('xmpp', 'listener');
126 return jabber_daemon_address() . '/' . common_config('xmpp','resource') . 'daemon';
130 function getSockets()
132 return array($this->conn->getSocket());