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');
25 * Common superclass for all XMPP-using queue handlers. They all need to
26 * service their message queues on idle, and forward any incoming messages
27 * to the XMPP listener connection. So, we abstract out common code to a
31 class XmppQueueHandler extends QueueHandler
37 # Low priority; we don't want to receive messages
38 $this->log(LOG_INFO, "INITIALIZE");
39 $this->conn = jabber_connect($this->_id.$this->transport());
41 $this->conn->addEventHandler('message', 'forward_message', $this);
42 $this->conn->addEventHandler('reconnect', 'handle_reconnect', $this);
43 $this->conn->setReconnectTimeout(600);
44 jabber_send_presence("Send me a message to post a notice", 'available', null, 'available', -1);
46 return !is_null($this->conn);
54 function handle_reconnect(&$pl)
56 $this->conn->processUntil('session_start');
57 $this->conn->presence(null, 'available', null, 'available', -1);
60 function idle($timeout=0)
62 # Process the queue for as long as needed
65 $this->log(LOG_DEBUG, "Servicing the XMPP queue.");
66 $this->conn->processTime($timeout);
69 } catch (XMPPHP_Exception $e) {
70 $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
71 die($e->getMessage());
77 $jid = jabber_daemon_address().'/'.$this->_id.$this->transport();
78 $server = common_config('xmpp', 'server');
80 if (!isset($this->pingid)) {
86 $this->log(LOG_DEBUG, "Sending ping #{$this->pingid}");
88 $this->conn->send("<iq from='{$jid}' to='{$server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
91 function forward_message(&$pl)
93 if ($pl['type'] != 'chat') {
94 $this->log(LOG_DEBUG, 'Ignoring message of type ' . $pl['type'] . ' from ' . $pl['from']);
97 $listener = $this->listener();
98 if (strtolower($listener) == strtolower($pl['from'])) {
99 $this->log(LOG_WARNING, 'Ignoring loop message.');
102 $this->log(LOG_INFO, 'Forwarding message from ' . $pl['from'] . ' to ' . $listener);
103 $this->conn->message($this->listener(), $pl['body'], 'chat', null, $this->ofrom($pl['from']));
106 function ofrom($from)
108 $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
109 $address .= "<address type='ofrom' jid='$from' />\n";
110 $address .= "</addresses>\n";
116 if (common_config('xmpp', 'listener')) {
117 return common_config('xmpp', 'listener');
119 return jabber_daemon_address() . '/' . common_config('xmpp','resource') . 'daemon';