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 * Build an XMPP proxy connection that'll save outgoing messages
90 * to the 'xmppout' queue to be picked up by xmppdaemon later.
92 function jabber_proxy()
94 $proxy = new Queued_XMPP(common_config('xmpp', 'host') ?
95 common_config('xmpp', 'host') :
96 common_config('xmpp', 'server'),
97 common_config('xmpp', 'port'),
98 common_config('xmpp', 'user'),
99 common_config('xmpp', 'password'),
100 common_config('xmpp', 'resource') . 'daemon',
101 common_config('xmpp', 'server'),
102 common_config('xmpp', 'debug') ?
104 common_config('xmpp', 'debug') ?
105 XMPPHP_Log::LEVEL_VERBOSE : null);
110 * Lazy-connect the configured Jabber account to the configured server;
111 * if already opened, the same connection will be returned.
113 * In a multi-site background process, each site configuration
114 * will get its own connection.
116 * @param string $resource Resource to connect (defaults to configured resource)
118 * @return XMPPHP connection to the configured server
121 function jabber_connect($resource=null)
123 static $connections = array();
124 $site = common_config('site', 'server');
125 if (empty($connections[$site])) {
126 if (empty($resource)) {
127 $resource = common_config('xmpp', 'resource');
129 $conn = new Sharing_XMPP(common_config('xmpp', 'host') ?
130 common_config('xmpp', 'host') :
131 common_config('xmpp', 'server'),
132 common_config('xmpp', 'port'),
133 common_config('xmpp', 'user'),
134 common_config('xmpp', 'password'),
136 common_config('xmpp', 'server'),
137 common_config('xmpp', 'debug') ?
139 common_config('xmpp', 'debug') ?
140 XMPPHP_Log::LEVEL_VERBOSE : null
146 $connections[$site] = $conn;
148 $conn->autoSubscribe();
149 $conn->useEncryption(common_config('xmpp', 'encryption'));
152 common_log(LOG_INFO, __METHOD__ . ": connecting " .
153 common_config('xmpp', 'user') . '/' . $resource);
154 //$conn->connect(true); // true = persistent connection
155 $conn->connect(); // persistent connections break multisite
156 } catch (XMPPHP_Exception $e) {
157 common_log(LOG_ERR, $e->getMessage());
161 $conn->processUntil('session_start');
163 return $connections[$site];
167 * Queue send for a single notice to a given Jabber address
169 * @param string $to JID to send the notice to
170 * @param Notice $notice notice to send
172 * @return boolean success value
175 function jabber_send_notice($to, $notice)
177 $conn = jabber_proxy();
178 $profile = Profile::staticGet($notice->profile_id);
180 common_log(LOG_WARNING, 'Refusing to send notice with ' .
181 'unknown profile ' . common_log_objstring($notice),
185 $msg = jabber_format_notice($profile, $notice);
186 $entry = jabber_format_entry($profile, $notice);
187 $conn->message($to, $msg, 'chat', null, $entry);
193 * extra information for XMPP messages, as defined by Twitter
195 * @param Profile $profile Profile of the sending user
196 * @param Notice $notice Notice being sent
198 * @return string Extra information (Atom, HTML, addresses) in string format
201 function jabber_format_entry($profile, $notice)
203 $entry = $notice->asAtomEntry(true, true);
205 $xs = new XMLStringer();
206 $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im'));
207 $xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml'));
208 $xs->element('a', array('href' => $profile->profileurl),
211 if (!empty($notice->rendered)) {
212 $xs->raw($notice->rendered);
214 $xs->raw(common_render_content($notice->content, $notice));
217 $xs->element('a', array(
218 'href'=>common_local_url('conversation',
219 array('id' => $notice->conversation)).'#notice-'.$notice->id
220 ),sprintf(_('[%s]'),$notice->id));
221 $xs->elementEnd('body');
222 $xs->elementEnd('html');
224 $html = $xs->getString();
226 return $html . ' ' . $entry;
230 * sends a single text message to a given JID
232 * @param string $to JID to send the message to
233 * @param string $body body of the message
234 * @param string $type type of the message
235 * @param string $subject subject of the message
237 * @return boolean success flag
240 function jabber_send_message($to, $body, $type='chat', $subject=null)
242 $conn = jabber_proxy();
243 $conn->message($to, $body, $type, $subject);
248 * sends a presence stanza on the Jabber network
250 * @param string $status current status, free-form string
251 * @param string $show structured status value
252 * @param string $to recipient of presence, null for general
253 * @param string $type type of status message, related to $show
254 * @param int $priority priority of the presence
256 * @return boolean success value
259 function jabber_send_presence($status, $show='available', $to=null,
260 $type = 'available', $priority=null)
262 $conn = jabber_connect();
266 $conn->presence($status, $show, $to, $type, $priority);
271 * sends a confirmation request to a JID
273 * @param string $code confirmation code for confirmation URL
274 * @param string $nickname nickname of confirming user
275 * @param string $address JID to send confirmation to
277 * @return boolean success flag
280 function jabber_confirm_address($code, $nickname, $address)
282 $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
283 'has said that your Jabber ID belongs to them. ' .
284 'If that\'s true, you can confirm by clicking on this URL: ' .
285 common_local_url('confirmaddress', array('code' => $code)) .
286 ' . (If you cannot click it, copy-and-paste it into the ' .
287 'address bar of your browser). If that user isn\'t you, ' .
288 'or if you didn\'t request this confirmation, just ignore this message.';
290 return jabber_send_message($address, $body);
294 * sends a "special" presence stanza on the Jabber network
296 * @param string $type Type of presence
297 * @param string $to JID to send presence to
298 * @param string $show show value for presence
299 * @param string $status status value for presence
301 * @return boolean success flag
303 * @see jabber_send_presence()
306 function jabber_special_presence($type, $to=null, $show=null, $status=null)
308 // FIXME: why use this instead of jabber_send_presence()?
309 $conn = jabber_connect();
311 $to = htmlspecialchars($to);
312 $status = htmlspecialchars($status);
319 $out .= " type='$type'";
321 if ($show == 'available' and !$status) {
325 if ($show && ($show != 'available')) {
326 $out .= "<show>$show</show>";
329 $out .= "<status>$status</status>";
331 $out .= "</presence>";
337 * Queue broadcast of a notice to all subscribers and reply recipients
339 * This function will send a notice to all subscribers on the local server
340 * who have Jabber addresses, and have Jabber notification enabled, and
341 * have this subscription enabled for Jabber. It also sends the notice to
342 * all recipients of @-replies who have Jabber addresses and Jabber notification
343 * enabled. This is really the heart of Jabber distribution in StatusNet.
345 * @param Notice $notice The notice to broadcast
347 * @return boolean success flag
350 function jabber_broadcast_notice($notice)
352 if (!common_config('xmpp', 'enabled')) {
355 $profile = Profile::staticGet($notice->profile_id);
358 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
359 'unknown profile ' . common_log_objstring($notice),
361 return true; // not recoverable; discard.
364 $msg = jabber_format_notice($profile, $notice);
365 $entry = jabber_format_entry($profile, $notice);
372 $conn = jabber_proxy();
374 $ni = $notice->whoGets();
376 foreach ($ni as $user_id => $reason) {
377 $user = User::staticGet($user_id);
379 empty($user->jabber) ||
380 !$user->jabbernotify) {
381 // either not a local user, or just not found
385 case NOTICE_INBOX_SOURCE_REPLY:
386 if (!$user->jabberreplies) {
390 case NOTICE_INBOX_SOURCE_SUB:
391 $sub = Subscription::pkeyGet(array('subscriber' => $user->id,
392 'subscribed' => $notice->profile_id));
393 if (empty($sub) || !$sub->jabber) {
397 case NOTICE_INBOX_SOURCE_GROUP:
400 throw new Exception(sprintf(_("Unknown inbox source %d."), $reason));
404 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
406 $conn->message($user->jabber, $msg, 'chat', null, $entry);
413 * Queue send of a notice to all public listeners
415 * For notices that are generated on the local system (by users), we can optionally
416 * forward them to remote listeners by XMPP.
418 * @param Notice $notice notice to broadcast
420 * @return boolean success flag
423 function jabber_public_notice($notice)
425 // Now, users who want everything
427 $public = common_config('xmpp', 'public');
429 // FIXME PRIV don't send out private messages here
430 // XXX: should we send out non-local messages if public,localonly
431 // = false? I think not
433 if ($public && $notice->is_local == Notice::LOCAL_PUBLIC) {
434 $profile = Profile::staticGet($notice->profile_id);
437 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
438 'unknown profile ' . common_log_objstring($notice),
440 return true; // not recoverable; discard.
443 $msg = jabber_format_notice($profile, $notice);
444 $entry = jabber_format_entry($profile, $notice);
446 $conn = jabber_proxy();
448 foreach ($public as $address) {
450 'Sending notice ' . $notice->id .
451 ' to public listener ' . $address,
453 $conn->message($address, $msg, 'chat', null, $entry);
462 * makes a plain-text formatted version of a notice, suitable for Jabber distribution
464 * @param Profile &$profile profile of the sending user
465 * @param Notice &$notice notice being sent
467 * @return string plain-text version of the notice, with user nickname prefixed
470 function jabber_format_notice(&$profile, &$notice)
472 return $profile->nickname . ': ' . $notice->content . ' [' . $notice->id . ']';