]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/xmppqueuehandler.php
Added option to add disable attr to common_checkbox()
[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         function start() {
34                 # Low priority; we don't want to receive messages
35                 $this->log(LOG_INFO, "INITIALIZE");
36                 $this->conn = jabber_connect($this->_id);
37                 if ($this->conn) {
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);
42                 }
43                 return !is_null($this->conn);
44         }
45         
46         function handle_reconnect(&$pl) {
47                 $this->conn->processUntil('session_start');
48                 $this->conn->presence(NULL, 'available', NULL, 'available', -1);
49         }
50
51         function idle($timeout=0) {
52                 # Process the queue for as long as needed
53                 try {
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());
58                 }
59         }
60         
61         function forward_message(&$pl) {
62                 if ($pl['type'] != 'chat') {
63                     $this->log(LOG_DEBUG, 'Ignoring message of type ' . $pl['type'] . ' from ' . $pl['from']);
64                         return;
65                 }
66                 $listener = $this->listener();
67                 if (strtolower($listener) == strtolower($pl['from'])) {
68                         $this->log(LOG_WARNING, 'Ignoring loop message.');
69                         return;
70                 }
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']));
73         }
74
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";
79                 return $address;
80         }
81
82         function listener() {
83                 if (common_config('xmpp', 'listener')) {
84                         return common_config('xmpp', 'listener');
85                 } else {
86                         return jabber_daemon_address() . '/' . common_config('xmpp','resource') . '-listener';
87                 }
88         }
89 }