]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/xmppqueuehandler.php
Notice attachments are enclosures in feeds (Atom, RSS 1.0/RDF, and RSS 2.0).
[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     var $pingid = 0;
34
35     function start()
36     {
37         # Low priority; we don't want to receive messages
38         $this->log(LOG_INFO, "INITIALIZE");
39         $this->conn = jabber_connect($this->_id.$this->transport());
40         if ($this->conn) {
41             $this->conn->addEventHandler('message', 'forward_message', $this);
42             $this->conn->addEventHandler('reconnect', 'handle_reconnect', $this);
43             $this->conn->setReconnectTimeout(600);
44             jabber_send_presence("Send me a message to post a notice", 'available', null, 'available', -1);
45         }
46         return !is_null($this->conn);
47     }
48
49     function timeout()
50     {
51         return 10;
52     }
53
54     function handle_reconnect(&$pl)
55     {
56         $this->conn->processUntil('session_start');
57         $this->conn->presence(null, 'available', null, 'available', -1);
58     }
59
60     function idle($timeout=0)
61     {
62         # Process the queue for as long as needed
63         try {
64             if ($this->conn) {
65                 $this->log(LOG_DEBUG, "Servicing the XMPP queue.");
66                 $this->conn->processTime(max($timeout, 1));
67                 $this->sendPing();
68             }
69         } catch (XMPPHP_Exception $e) {
70             $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
71             die($e->getMessage());
72         }
73     }
74
75     function sendPing()
76     {
77         $jid = jabber_daemon_address().'/'.$this->_id.$this->transport();
78         $server = common_config('xmpp', 'server');
79
80         if (!isset($this->pingid)) {
81             $this->pingid = 0;
82         } else {
83             $this->pingid++;
84         }
85
86         $this->log(LOG_DEBUG, "Sending ping #{$this->pingid}");
87
88                 $this->conn->send("<iq from='{$jid}' to='{$server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
89     }
90
91     function forward_message(&$pl)
92     {
93         if ($pl['type'] != 'chat') {
94             $this->log(LOG_DEBUG, 'Ignoring message of type ' . $pl['type'] . ' from ' . $pl['from']);
95             return;
96         }
97         $listener = $this->listener();
98         if (strtolower($listener) == strtolower($pl['from'])) {
99             $this->log(LOG_WARNING, 'Ignoring loop message.');
100             return;
101         }
102         $this->log(LOG_INFO, 'Forwarding message from ' . $pl['from'] . ' to ' . $listener);
103         $this->conn->message($this->listener(), $pl['body'], 'chat', null, $this->ofrom($pl['from']));
104     }
105
106     function ofrom($from)
107     {
108         $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
109         $address .= "<address type='ofrom' jid='$from' />\n";
110         $address .= "</addresses>\n";
111         return $address;
112     }
113
114     function listener()
115     {
116         if (common_config('xmpp', 'listener')) {
117             return common_config('xmpp', 'listener');
118         } else {
119             return jabber_daemon_address() . '/' . common_config('xmpp','resource') . 'daemon';
120         }
121     }
122 }