3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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 {
34 # Low priority; we don't want to receive messages
35 $this->log(LOG_INFO, "INITIALIZE");
36 $this->conn = jabber_connect($this->_id);
38 $this->conn->addEventHandler('message', 'forward_message', $this);
39 $this->conn->addEventHandler('reconnect', 'handle_reconnect', $this);
40 $this->conn->setReconnectTimeout(600);
41 jabber_send_presence("Send me a message to post a notice", 'available', NULL, 'available', -1);
43 return !is_null($this->conn);
46 function handle_reconnect(&$pl) {
47 $this->conn->processUntil('session_start');
48 $this->conn->presence(NULL, 'available', NULL, 'available', -1);
51 function idle($timeout=0) {
52 # Process the queue for as long as needed
54 $this->conn->processTime($timeout);
55 } catch (XMPPHP_Exception $e) {
56 $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
57 die($e->getMessage());
61 function forward_message(&$pl) {
62 if ($pl['type'] != 'chat') {
63 $this->log(LOG_DEBUG, 'Ignoring message of type ' . $pl['type'] . ' from ' . $pl['from']);
66 $listener = $this->listener();
67 if (strtolower($listener) == strtolower($pl['from'])) {
68 $this->log(LOG_WARNING, 'Ignoring loop message.');
71 $this->log(LOG_INFO, 'Forwarding message from ' . $pl['from'] . ' to ' . $listener);
72 $this->conn->message($this->listener(), $pl['body'], 'chat', NULL, $this->ofrom($pl['from']));
75 function ofrom($from) {
76 $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
77 $address .= "<address type='ofrom' jid='$from' />\n";
78 $address .= "</addresses>\n";
83 if (common_config('xmpp', 'listener')) {
84 return common_config('xmpp', 'listener');
86 return jabber_daemon_address() . '/' . common_config('xmpp','resource') . '-listener';