]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Xmpp/lib/xmppmanager.php
[TRANSLATION] Update POTs and normalize files
[quix0rs-gnu-social.git] / plugins / Xmpp / lib / xmppmanager.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) {
21     exit(1);
22 }
23
24 use XMPPHP\Log;
25
26 /**
27  * XMPP background connection manager for XMPP-using queue handlers,
28  * allowing them to send outgoing messages on the right connection.
29  *
30  * Input is handled during socket select loop, keepalive pings during idle.
31  * Any incoming messages will be handled.
32  *
33  * In a multi-site queuedaemon.php run, one connection will be instantiated
34  * for each site being handled by the current process that has XMPP enabled.
35  */
36 class XmppManager extends ImManager
37 {
38     const PING_INTERVAL = 120;
39     public $conn = null;
40     protected $lastping = null;
41     protected $pingid = null;
42
43     /**
44      * Initialize connection to server.
45      * @param $master
46      * @return boolean true on success
47      */
48     public function start($master)
49     {
50         if (parent::start($master)) {
51             $this->connect();
52             return true;
53         } else {
54             return false;
55         }
56     }
57
58     function connect()
59     {
60         if (!$this->conn || $this->conn->isDisconnected()) {
61             $resource = 'queue' . posix_getpid();
62             $this->conn = new SharingXMPP($this->plugin->host ?
63                 $this->plugin->host :
64                 $this->plugin->server,
65                 $this->plugin->port,
66                 $this->plugin->user,
67                 $this->plugin->password,
68                 $this->plugin->resource,
69                 $this->plugin->server,
70                 $this->plugin->debug ?
71                     true : false,
72                 $this->plugin->debug ?
73                     Log::LEVEL_VERBOSE : null
74             );
75
76             if (!$this->conn) {
77                 return false;
78             }
79             $this->conn->addEventHandler('message', 'handle_xmpp_message', $this);
80             $this->conn->addEventHandler('reconnect', 'handle_xmpp_reconnect', $this);
81             $this->conn->setReconnectTimeout(600);
82
83             $this->conn->autoSubscribe();
84             $this->conn->useEncryption($this->plugin->encryption);
85
86             $this->conn->connect(true);
87
88             $this->conn->processUntil('session_start');
89             // TRANS: Presence announcement for XMPP.
90             $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
91         }
92         return $this->conn;
93     }
94
95     /**
96      * sends a presence stanza on the XMPP network
97      *
98      * @param string $status current status, free-form string
99      * @param string $show structured status value
100      * @param string $to recipient of presence, null for general
101      * @param string $type type of status message, related to $show
102      * @param int $priority priority of the presence
103      *
104      * @return boolean success value
105      */
106
107     function send_presence($status, $show = 'available', $to = null,
108                            $type = 'available', $priority = null)
109     {
110         $this->connect();
111         if (!$this->conn || $this->conn->isDisconnected()) {
112             return false;
113         }
114         $this->conn->presence($status, $show, $to, $type, $priority);
115         return true;
116     }
117
118     function send_raw_message($data)
119     {
120         $this->connect();
121         if (!$this->conn || $this->conn->isDisconnected()) {
122             return false;
123         }
124         $this->conn->send($data);
125         return true;
126     }
127
128     /**
129      * Message pump is triggered on socket input, so we only need an idle()
130      * call often enough to trigger our outgoing pings.
131      */
132     function timeout()
133     {
134         return self::PING_INTERVAL;
135     }
136
137     /**
138      * Process XMPP events that have come in over the wire.
139      * @fixme may kill process on XMPP error
140      * @param resource $socket
141      */
142     public function handleInput($socket)
143     {
144         // Process the queue for as long as needed
145         common_log(LOG_DEBUG, "Servicing the XMPP queue.");
146         $this->stats('xmpp_process');
147         $this->conn->processTime(0);
148     }
149
150     /**
151      * Lists the IM connection socket to allow i/o master to wake
152      * when input comes in here as well as from the queue source.
153      *
154      * @return array of resources
155      */
156     public function getSockets()
157     {
158         $this->connect();
159         if ($this->conn) {
160             return array($this->conn->getSocket());
161         } else {
162             return array();
163         }
164     }
165
166     /**
167      * Idle processing for io manager's execution loop.
168      * Send keepalive pings to server.
169      *
170      * Side effect: kills process on exception from XMPP library.
171      *
172      * @param int $timeout
173      * @todo FIXME: non-dying error handling
174      */
175     public function idle($timeout = 0)
176     {
177         $now = time();
178         if (empty($this->lastping) || $now - $this->lastping > self::PING_INTERVAL) {
179             $this->send_ping();
180         }
181     }
182
183     function send_ping()
184     {
185         $this->connect();
186         if (!$this->conn || $this->conn->isDisconnected()) {
187             return false;
188         }
189         $now = time();
190         if (!isset($this->pingid)) {
191             $this->pingid = 0;
192         } else {
193             $this->pingid++;
194         }
195
196         common_log(LOG_DEBUG, "Sending ping #{$this->pingid}");
197         $this->conn->send("<iq from='{$this->plugin->daemonScreenname()}' to='{$this->plugin->server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
198         $this->lastping = $now;
199         return true;
200     }
201
202     function handle_xmpp_message(&$pl)
203     {
204         $this->plugin->enqueueIncomingRaw($pl);
205         return true;
206     }
207
208     /**
209      * Callback for Jabber reconnect event
210      * @param $pl
211      */
212     function handle_xmpp_reconnect(&$pl)
213     {
214         common_log(LOG_NOTICE, 'XMPP reconnected');
215
216         $this->conn->processUntil('session_start');
217         // TRANS: Message for XMPP reconnect.
218         $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
219     }
220
221     /**
222      * sends a "special" presence stanza on the XMPP network
223      *
224      * @param string $type Type of presence
225      * @param string $to JID to send presence to
226      * @param string $show show value for presence
227      * @param string $status status value for presence
228      *
229      * @return boolean success flag
230      *
231      * @see send_presence()
232      */
233
234     function special_presence($type, $to = null, $show = null, $status = null)
235     {
236         // @todo FIXME: why use this instead of send_presence()?
237         $this->connect();
238         if (!$this->conn || $this->conn->isDisconnected()) {
239             return false;
240         }
241
242         $to = htmlspecialchars($to);
243         $status = htmlspecialchars($status);
244
245         $out = "<presence";
246         if ($to) {
247             $out .= " to='$to'";
248         }
249         if ($type) {
250             $out .= " type='$type'";
251         }
252         if ($show == 'available' and !$status) {
253             $out .= "/>";
254         } else {
255             $out .= ">";
256             if ($show && ($show != 'available')) {
257                 $out .= "<show>$show</show>";
258             }
259             if ($status) {
260                 $out .= "<status>$status</status>";
261             }
262             $out .= "</presence>";
263         }
264         $this->conn->send($out);
265         return true;
266     }
267 }