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