]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/xmppqueuehandler.php
Merge branch '0.8.x' of git@gitorious.org:+laconica-developers/laconica/dev into...
[quix0rs-gnu-social.git] / lib / xmppqueuehandler.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, 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('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/queuehandler.php');
23
24 /**
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
28  * superclass.
29  */
30
31 class XmppQueueHandler extends QueueHandler
32 {
33     function start()
34     {
35         # Low priority; we don't want to receive messages
36         $this->log(LOG_INFO, "INITIALIZE");
37         $this->conn = jabber_connect($this->_id.$this->transport());
38         if ($this->conn) {
39             $this->conn->addEventHandler('message', 'forward_message', $this);
40             $this->conn->addEventHandler('reconnect', 'handle_reconnect', $this);
41             $this->conn->setReconnectTimeout(600);
42             jabber_send_presence("Send me a message to post a notice", 'available', null, 'available', -1);
43         }
44         return !is_null($this->conn);
45     }
46
47     function handle_reconnect(&$pl)
48     {
49         $this->conn->processUntil('session_start');
50         $this->conn->presence(null, 'available', null, 'available', -1);
51     }
52
53     function idle($timeout=0)
54     {
55         # Process the queue for as long as needed
56         try {
57             if ($this->conn) {
58                 $this->conn->processTime($timeout);
59             }
60         } catch (XMPPHP_Exception $e) {
61             $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
62             die($e->getMessage());
63         }
64     }
65
66     function forward_message(&$pl)
67     {
68         if ($pl['type'] != 'chat') {
69             $this->log(LOG_DEBUG, 'Ignoring message of type ' . $pl['type'] . ' from ' . $pl['from']);
70             return;
71         }
72         $listener = $this->listener();
73         if (strtolower($listener) == strtolower($pl['from'])) {
74             $this->log(LOG_WARNING, 'Ignoring loop message.');
75             return;
76         }
77         $this->log(LOG_INFO, 'Forwarding message from ' . $pl['from'] . ' to ' . $listener);
78         $this->conn->message($this->listener(), $pl['body'], 'chat', null, $this->ofrom($pl['from']));
79     }
80
81     function ofrom($from)
82     {
83         $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
84         $address .= "<address type='ofrom' jid='$from' />\n";
85         $address .= "</addresses>\n";
86         return $address;
87     }
88
89     function listener()
90     {
91         if (common_config('xmpp', 'listener')) {
92             return common_config('xmpp', 'listener');
93         } else {
94             return jabber_daemon_address() . '/' . common_config('xmpp','resource') . 'daemon';
95         }
96     }
97 }