]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/xmppqueuehandler.php
Script to update laconica.pot from source, and the results of running it
[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                         if ($this->conn) {
55                                 $this->conn->processTime($timeout);
56                         }
57                 } catch (XMPPHP_Exception $e) {
58                         $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
59                         die($e->getMessage());
60                 }
61         }
62         
63         function forward_message(&$pl) {
64                 if ($pl['type'] != 'chat') {
65                     $this->log(LOG_DEBUG, 'Ignoring message of type ' . $pl['type'] . ' from ' . $pl['from']);
66                         return;
67                 }
68                 $listener = $this->listener();
69                 if (strtolower($listener) == strtolower($pl['from'])) {
70                         $this->log(LOG_WARNING, 'Ignoring loop message.');
71                         return;
72                 }
73                 $this->log(LOG_INFO, 'Forwarding message from ' . $pl['from'] . ' to ' . $listener);
74                 $this->conn->message($this->listener(), $pl['body'], 'chat', NULL, $this->ofrom($pl['from']));
75         }
76
77         function ofrom($from) {
78                 $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
79                 $address .= "<address type='ofrom' jid='$from' />\n";
80                 $address .= "</addresses>\n";
81                 return $address;
82         }
83
84         function listener() {
85                 if (common_config('xmpp', 'listener')) {
86                         return common_config('xmpp', 'listener');
87                 } else {
88                         return jabber_daemon_address() . '/' . common_config('xmpp','resource') . '-listener';
89                 }
90         }
91 }