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 * Lazy-connect the configured Jabber account to the configured server;
90 * if already opened, the same connection will be returned.
92 * In a multi-site background process, each site configuration
93 * will get its own connection.
95 * @param string $resource Resource to connect (defaults to configured resource)
97 * @return XMPPHP connection to the configured server
100 function jabber_connect($resource=null)
102 static $connections = array();
103 $site = common_config('site', 'server');
104 if (empty($connections[$site])) {
105 if (empty($resource)) {
106 $resource = common_config('xmpp', 'resource');
108 $conn = new Sharing_XMPP(common_config('xmpp', 'host') ?
109 common_config('xmpp', 'host') :
110 common_config('xmpp', 'server'),
111 common_config('xmpp', 'port'),
112 common_config('xmpp', 'user'),
113 common_config('xmpp', 'password'),
115 common_config('xmpp', 'server'),
116 common_config('xmpp', 'debug') ?
118 common_config('xmpp', 'debug') ?
119 XMPPHP_Log::LEVEL_VERBOSE : null
125 $connections[$site] = $conn;
127 $conn->autoSubscribe();
128 $conn->useEncryption(common_config('xmpp', 'encryption'));
131 common_log(LOG_INFO, __METHOD__ . ": connecting " .
132 common_config('xmpp', 'user') . '/' . $resource);
133 //$conn->connect(true); // true = persistent connection
134 $conn->connect(); // persistent connections break multisite
135 } catch (XMPPHP_Exception $e) {
136 common_log(LOG_ERR, $e->getMessage());
140 $conn->processUntil('session_start');
142 return $connections[$site];
146 * send a single notice to a given Jabber address
148 * @param string $to JID to send the notice to
149 * @param Notice $notice notice to send
151 * @return boolean success value
154 function jabber_send_notice($to, $notice)
156 $conn = jabber_connect();
160 $profile = Profile::staticGet($notice->profile_id);
162 common_log(LOG_WARNING, 'Refusing to send notice with ' .
163 'unknown profile ' . common_log_objstring($notice),
167 $msg = jabber_format_notice($profile, $notice);
168 $entry = jabber_format_entry($profile, $notice);
169 $conn->message($to, $msg, 'chat', null, $entry);
175 * extra information for XMPP messages, as defined by Twitter
177 * @param Profile $profile Profile of the sending user
178 * @param Notice $notice Notice being sent
180 * @return string Extra information (Atom, HTML, addresses) in string format
183 function jabber_format_entry($profile, $notice)
185 $entry = $notice->asAtomEntry(true, true);
187 $xs = new XMLStringer();
188 $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im'));
189 $xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml'));
190 $xs->element('a', array('href' => $profile->profileurl),
193 if (!empty($notice->rendered)) {
194 $xs->raw($notice->rendered);
196 $xs->raw(common_render_content($notice->content, $notice));
199 $xs->element('a', array(
200 'href'=>common_local_url('conversation',
201 array('id' => $notice->conversation)).'#notice-'.$notice->id
202 ),sprintf(_('[%s]'),$notice->id));
203 $xs->elementEnd('body');
204 $xs->elementEnd('html');
206 $html = $xs->getString();
208 return $html . ' ' . $entry;
212 * sends a single text message to a given JID
214 * @param string $to JID to send the message to
215 * @param string $body body of the message
216 * @param string $type type of the message
217 * @param string $subject subject of the message
219 * @return boolean success flag
222 function jabber_send_message($to, $body, $type='chat', $subject=null)
224 $conn = jabber_connect();
228 $conn->message($to, $body, $type, $subject);
233 * sends a presence stanza on the Jabber network
235 * @param string $status current status, free-form string
236 * @param string $show structured status value
237 * @param string $to recipient of presence, null for general
238 * @param string $type type of status message, related to $show
239 * @param int $priority priority of the presence
241 * @return boolean success value
244 function jabber_send_presence($status, $show='available', $to=null,
245 $type = 'available', $priority=null)
247 $conn = jabber_connect();
251 $conn->presence($status, $show, $to, $type, $priority);
256 * sends a confirmation request to a JID
258 * @param string $code confirmation code for confirmation URL
259 * @param string $nickname nickname of confirming user
260 * @param string $address JID to send confirmation to
262 * @return boolean success flag
265 function jabber_confirm_address($code, $nickname, $address)
267 $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
268 'has said that your Jabber ID belongs to them. ' .
269 'If that\'s true, you can confirm by clicking on this URL: ' .
270 common_local_url('confirmaddress', array('code' => $code)) .
271 ' . (If you cannot click it, copy-and-paste it into the ' .
272 'address bar of your browser). If that user isn\'t you, ' .
273 'or if you didn\'t request this confirmation, just ignore this message.';
275 return jabber_send_message($address, $body);
279 * sends a "special" presence stanza on the Jabber network
281 * @param string $type Type of presence
282 * @param string $to JID to send presence to
283 * @param string $show show value for presence
284 * @param string $status status value for presence
286 * @return boolean success flag
288 * @see jabber_send_presence()
291 function jabber_special_presence($type, $to=null, $show=null, $status=null)
293 // FIXME: why use this instead of jabber_send_presence()?
294 $conn = jabber_connect();
296 $to = htmlspecialchars($to);
297 $status = htmlspecialchars($status);
304 $out .= " type='$type'";
306 if ($show == 'available' and !$status) {
310 if ($show && ($show != 'available')) {
311 $out .= "<show>$show</show>";
314 $out .= "<status>$status</status>";
316 $out .= "</presence>";
322 * broadcast a notice to all subscribers and reply recipients
324 * This function will send a notice to all subscribers on the local server
325 * who have Jabber addresses, and have Jabber notification enabled, and
326 * have this subscription enabled for Jabber. It also sends the notice to
327 * all recipients of @-replies who have Jabber addresses and Jabber notification
328 * enabled. This is really the heart of Jabber distribution in StatusNet.
330 * @param Notice $notice The notice to broadcast
332 * @return boolean success flag
335 function jabber_broadcast_notice($notice)
337 if (!common_config('xmpp', 'enabled')) {
340 $profile = Profile::staticGet($notice->profile_id);
343 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
344 'unknown profile ' . common_log_objstring($notice),
349 $msg = jabber_format_notice($profile, $notice);
350 $entry = jabber_format_entry($profile, $notice);
357 $conn = jabber_connect();
359 $ni = $notice->whoGets();
361 foreach ($ni as $user_id => $reason) {
362 $user = User::staticGet($user_id);
364 empty($user->jabber) ||
365 !$user->jabbernotify) {
366 // either not a local user, or just not found
370 case NOTICE_INBOX_SOURCE_REPLY:
371 if (!$user->jabberreplies) {
375 case NOTICE_INBOX_SOURCE_SUB:
376 $sub = Subscription::pkeyGet(array('subscriber' => $user->id,
377 'subscribed' => $notice->profile_id));
378 if (empty($sub) || !$sub->jabber) {
382 case NOTICE_INBOX_SOURCE_GROUP:
385 throw new Exception(sprintf(_("Unknown inbox source %d."), $reason));
389 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
391 $conn->message($user->jabber, $msg, 'chat', null, $entry);
392 $conn->processTime(0);
399 * send a notice to all public listeners
401 * For notices that are generated on the local system (by users), we can optionally
402 * forward them to remote listeners by XMPP.
404 * @param Notice $notice notice to broadcast
406 * @return boolean success flag
409 function jabber_public_notice($notice)
411 // Now, users who want everything
413 $public = common_config('xmpp', 'public');
415 // FIXME PRIV don't send out private messages here
416 // XXX: should we send out non-local messages if public,localonly
417 // = false? I think not
419 if ($public && $notice->is_local == Notice::LOCAL_PUBLIC) {
420 $profile = Profile::staticGet($notice->profile_id);
423 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
424 'unknown profile ' . common_log_objstring($notice),
429 $msg = jabber_format_notice($profile, $notice);
430 $entry = jabber_format_entry($profile, $notice);
432 $conn = jabber_connect();
434 foreach ($public as $address) {
436 'Sending notice ' . $notice->id .
437 ' to public listener ' . $address,
439 $conn->message($address, $msg, 'chat', null, $entry);
440 $conn->processTime(0);
449 * makes a plain-text formatted version of a notice, suitable for Jabber distribution
451 * @param Profile &$profile profile of the sending user
452 * @param Notice &$notice notice being sent
454 * @return string plain-text version of the notice, with user nickname prefixed
457 function jabber_format_notice(&$profile, &$notice)
459 return $profile->nickname . ': ' . $notice->content . ' [' . $notice->id . ']';