3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !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
42 $this->log(LOG_INFO, "INITIALIZE");
43 $this->conn = jabber_connect($this->_id.$this->transport());
45 if (empty($this->conn)) {
46 $this->log(LOG_ERR, "Couldn't connect to server.");
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);
55 return !is_null($this->conn);
63 function handle_reconnect(&$pl)
65 $this->log(LOG_NOTICE, 'reconnected');
67 $this->conn->processUntil('session_start');
68 $this->conn->presence(null, 'available', null, 'available', -1);
71 function idle($timeout=0)
73 # Process the queue for as long as needed
76 $this->log(LOG_DEBUG, "Servicing the XMPP queue.");
77 $this->conn->processTime($timeout);
79 if (empty($this->lastping) || $now - $this->lastping > PING_INTERVAL) {
81 $this->lastping = $now;
84 } catch (XMPPHP_Exception $e) {
85 $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
86 die($e->getMessage());
92 $jid = jabber_daemon_address().'/'.$this->_id.$this->transport();
93 $server = common_config('xmpp', 'server');
95 if (!isset($this->pingid)) {
101 $this->log(LOG_DEBUG, "Sending ping #{$this->pingid}");
103 $this->conn->send("<iq from='{$jid}' to='{$server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
106 function forward_message(&$pl)
108 if ($pl['type'] != 'chat') {
109 $this->log(LOG_DEBUG, 'Ignoring message of type ' . $pl['type'] . ' from ' . $pl['from']);
112 $listener = $this->listener();
113 if (strtolower($listener) == strtolower($pl['from'])) {
114 $this->log(LOG_WARNING, 'Ignoring loop message.');
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']));
121 function ofrom($from)
123 $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
124 $address .= "<address type='ofrom' jid='$from' />\n";
125 $address .= "</addresses>\n";
131 if (common_config('xmpp', 'listener')) {
132 return common_config('xmpp', 'listener');
134 return jabber_daemon_address() . '/' . common_config('xmpp','resource') . 'daemon';
138 function getSockets()
140 return array($this->conn->getSocket());