3 * StatusNet, the distributed open-source microblogging tool
5 * utility functions for Jabber/GTalk/XMPP messages
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2008 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once 'XMPPHP/XMPP.php';
37 * checks whether a string is a syntactically valid Jabber ID (JID)
39 * @param string $jid string to check
41 * @return boolean whether the string is a valid JID
44 function jabber_valid_base_jid($jid)
46 // Cheap but effective
47 return Validate::email($jid);
51 * normalizes a Jabber ID for comparison
53 * @param string $jid JID to check
55 * @return string an equivalent JID in normalized (lowercase) form
58 function jabber_normalize_jid($jid)
60 if (preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches)) {
62 $server = $matches[2];
63 return strtolower($node.'@'.$server);
70 * the JID of the Jabber daemon for this StatusNet instance
72 * @return string JID of the Jabber daemon
75 function jabber_daemon_address()
77 return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
80 class Sharing_XMPP extends XMPPHP_XMPP
89 * connect the configured Jabber account to the configured server
91 * @param string $resource Resource to connect (defaults to configured resource)
93 * @return XMPPHP connection to the configured server
96 function jabber_connect($resource=null)
100 $conn = new Sharing_XMPP(common_config('xmpp', 'host') ?
101 common_config('xmpp', 'host') :
102 common_config('xmpp', 'server'),
103 common_config('xmpp', 'port'),
104 common_config('xmpp', 'user'),
105 common_config('xmpp', 'password'),
106 ($resource) ? $resource :
107 common_config('xmpp', 'resource'),
108 common_config('xmpp', 'server'),
109 common_config('xmpp', 'debug') ?
111 common_config('xmpp', 'debug') ?
112 XMPPHP_Log::LEVEL_VERBOSE : null
119 $conn->autoSubscribe();
120 $conn->useEncryption(common_config('xmpp', 'encryption'));
123 $conn->connect(true); // true = persistent connection
124 } catch (XMPPHP_Exception $e) {
125 common_log(LOG_ERR, $e->getMessage());
129 $conn->processUntil('session_start');
135 * send a single notice to a given Jabber address
137 * @param string $to JID to send the notice to
138 * @param Notice $notice notice to send
140 * @return boolean success value
143 function jabber_send_notice($to, $notice)
145 $conn = jabber_connect();
149 $profile = Profile::staticGet($notice->profile_id);
151 common_log(LOG_WARNING, 'Refusing to send notice with ' .
152 'unknown profile ' . common_log_objstring($notice),
156 $msg = jabber_format_notice($profile, $notice);
157 $entry = jabber_format_entry($profile, $notice);
158 $conn->message($to, $msg, 'chat', null, $entry);
164 * extra information for XMPP messages, as defined by Twitter
166 * @param Profile $profile Profile of the sending user
167 * @param Notice $notice Notice being sent
169 * @return string Extra information (Atom, HTML, addresses) in string format
172 function jabber_format_entry($profile, $notice)
174 $entry = $notice->asAtomEntry(true, true);
176 $xs = new XMLStringer();
177 $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im'));
178 $xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml'));
179 $xs->element('a', array('href' => $profile->profileurl),
182 if (!empty($notice->rendered)) {
183 $xs->raw($notice->rendered);
185 $xs->raw(common_render_content($notice->content, $notice));
188 $xs->element('a', array(
189 'href'=>common_local_url('conversation',
190 array('id' => $notice->conversation)).'#notice-'.$notice->id
191 ),sprintf(_('[%s]'),$notice->id));
192 $xs->elementEnd('body');
193 $xs->elementEnd('html');
195 $html = $xs->getString();
197 return $html . ' ' . $entry;
201 * sends a single text message to a given JID
203 * @param string $to JID to send the message to
204 * @param string $body body of the message
205 * @param string $type type of the message
206 * @param string $subject subject of the message
208 * @return boolean success flag
211 function jabber_send_message($to, $body, $type='chat', $subject=null)
213 $conn = jabber_connect();
217 $conn->message($to, $body, $type, $subject);
222 * sends a presence stanza on the Jabber network
224 * @param string $status current status, free-form string
225 * @param string $show structured status value
226 * @param string $to recipient of presence, null for general
227 * @param string $type type of status message, related to $show
228 * @param int $priority priority of the presence
230 * @return boolean success value
233 function jabber_send_presence($status, $show='available', $to=null,
234 $type = 'available', $priority=null)
236 $conn = jabber_connect();
240 $conn->presence($status, $show, $to, $type, $priority);
245 * sends a confirmation request to a JID
247 * @param string $code confirmation code for confirmation URL
248 * @param string $nickname nickname of confirming user
249 * @param string $address JID to send confirmation to
251 * @return boolean success flag
254 function jabber_confirm_address($code, $nickname, $address)
256 $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
257 'has said that your Jabber ID belongs to them. ' .
258 'If that\'s true, you can confirm by clicking on this URL: ' .
259 common_local_url('confirmaddress', array('code' => $code)) .
260 ' . (If you cannot click it, copy-and-paste it into the ' .
261 'address bar of your browser). If that user isn\'t you, ' .
262 'or if you didn\'t request this confirmation, just ignore this message.';
264 return jabber_send_message($address, $body);
268 * sends a "special" presence stanza on the Jabber network
270 * @param string $type Type of presence
271 * @param string $to JID to send presence to
272 * @param string $show show value for presence
273 * @param string $status status value for presence
275 * @return boolean success flag
277 * @see jabber_send_presence()
280 function jabber_special_presence($type, $to=null, $show=null, $status=null)
282 // FIXME: why use this instead of jabber_send_presence()?
283 $conn = jabber_connect();
285 $to = htmlspecialchars($to);
286 $status = htmlspecialchars($status);
293 $out .= " type='$type'";
295 if ($show == 'available' and !$status) {
299 if ($show && ($show != 'available')) {
300 $out .= "<show>$show</show>";
303 $out .= "<status>$status</status>";
305 $out .= "</presence>";
311 * broadcast a notice to all subscribers and reply recipients
313 * This function will send a notice to all subscribers on the local server
314 * who have Jabber addresses, and have Jabber notification enabled, and
315 * have this subscription enabled for Jabber. It also sends the notice to
316 * all recipients of @-replies who have Jabber addresses and Jabber notification
317 * enabled. This is really the heart of Jabber distribution in StatusNet.
319 * @param Notice $notice The notice to broadcast
321 * @return boolean success flag
324 function jabber_broadcast_notice($notice)
326 if (!common_config('xmpp', 'enabled')) {
329 $profile = Profile::staticGet($notice->profile_id);
332 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
333 'unknown profile ' . common_log_objstring($notice),
338 $msg = jabber_format_notice($profile, $notice);
339 $entry = jabber_format_entry($profile, $notice);
346 $conn = jabber_connect();
348 // First, get users to whom this is a direct reply
350 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
351 $user->query("SELECT $UT.id, $UT.jabber " .
352 "FROM $UT JOIN reply ON $UT.id = reply.profile_id " .
353 'WHERE reply.notice_id = ' . $notice->id . ' ' .
354 "AND $UT.jabber is not null " .
355 "AND $UT.jabbernotify = 1 " .
356 "AND $UT.jabberreplies = 1 ");
358 while ($user->fetch()) {
360 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
362 $conn->message($user->jabber, $msg, 'chat', null, $entry);
363 $conn->processTime(0);
364 $sent_to[$user->id] = 1;
369 // Now, get users subscribed to this profile
372 $user->query("SELECT $UT.id, $UT.jabber " .
373 "FROM $UT JOIN subscription " .
374 "ON $UT.id = subscription.subscriber " .
375 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
376 "AND $UT.jabber is not null " .
377 "AND $UT.jabbernotify = 1 " .
378 'AND subscription.jabber = 1 ');
380 while ($user->fetch()) {
381 if (!array_key_exists($user->id, $sent_to)) {
383 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
385 $conn->message($user->jabber, $msg, 'chat', null, $entry);
386 // To keep the incoming queue from filling up,
387 // we service it after each send.
388 $conn->processTime(0);
389 $sent_to[$user->id] = 1;
393 // Now, get users who have it in their inbox because of groups
396 $user->query("SELECT $UT.id, $UT.jabber " .
397 "FROM $UT JOIN notice_inbox " .
398 "ON $UT.id = notice_inbox.user_id " .
399 'WHERE notice_inbox.notice_id = ' . $notice->id . ' ' .
400 'AND notice_inbox.source = 2 ' .
401 "AND $UT.jabber is not null " .
402 "AND $UT.jabbernotify = 1 ");
404 while ($user->fetch()) {
405 if (!array_key_exists($user->id, $sent_to)) {
407 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
409 $conn->message($user->jabber, $msg, 'chat', null, $entry);
410 // To keep the incoming queue from filling up,
411 // we service it after each send.
412 $conn->processTime(0);
413 $sent_to[$user->id] = 1;
423 * send a notice to all public listeners
425 * For notices that are generated on the local system (by users), we can optionally
426 * forward them to remote listeners by XMPP.
428 * @param Notice $notice notice to broadcast
430 * @return boolean success flag
433 function jabber_public_notice($notice)
435 // Now, users who want everything
437 $public = common_config('xmpp', 'public');
439 // FIXME PRIV don't send out private messages here
440 // XXX: should we send out non-local messages if public,localonly
441 // = false? I think not
443 if ($public && $notice->is_local) {
444 $profile = Profile::staticGet($notice->profile_id);
447 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
448 'unknown profile ' . common_log_objstring($notice),
453 $msg = jabber_format_notice($profile, $notice);
454 $entry = jabber_format_entry($profile, $notice);
456 $conn = jabber_connect();
458 foreach ($public as $address) {
460 'Sending notice ' . $notice->id .
461 ' to public listener ' . $address,
463 $conn->message($address, $msg, 'chat', null, $entry);
464 $conn->processTime(0);
473 * makes a plain-text formatted version of a notice, suitable for Jabber distribution
475 * @param Profile &$profile profile of the sending user
476 * @param Notice &$notice notice being sent
478 * @return string plain-text version of the notice, with user nickname prefixed
481 function jabber_format_notice(&$profile, &$notice)
483 return $profile->nickname . ': ' . $notice->content . ' [' . $notice->id . ']';