3 * Laconica, 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@controlyourself.ca>
25 * @copyright 2008 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!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 Laconica instance
72 * @return string JID of the Jabber daemon
75 function jabber_daemon_address()
77 return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
81 * connect the configured Jabber account to the configured server
83 * @param string $resource Resource to connect (defaults to configured resource)
85 * @return XMPPHP connection to the configured server
88 function jabber_connect($resource=null)
92 $conn = new XMPPHP_XMPP(common_config('xmpp', 'host') ?
93 common_config('xmpp', 'host') :
94 common_config('xmpp', 'server'),
95 common_config('xmpp', 'port'),
96 common_config('xmpp', 'user'),
97 common_config('xmpp', 'password'),
98 ($resource) ? $resource :
99 common_config('xmpp', 'resource'),
100 common_config('xmpp', 'server'),
101 common_config('xmpp', 'debug') ?
103 common_config('xmpp', 'debug') ?
104 XMPPHP_Log::LEVEL_VERBOSE : null
111 $conn->autoSubscribe();
112 $conn->useEncryption(common_config('xmpp', 'encryption'));
115 $conn->connect(true); // true = persistent connection
116 } catch (XMPPHP_Exception $e) {
117 common_log(LOG_ERROR, $e->getMessage());
121 $conn->processUntil('session_start');
127 * send a single notice to a given Jabber address
129 * @param string $to JID to send the notice to
130 * @param Notice $notice notice to send
132 * @return boolean success value
135 function jabber_send_notice($to, $notice)
137 $conn = jabber_connect();
141 $profile = Profile::staticGet($notice->profile_id);
143 common_log(LOG_WARNING, 'Refusing to send notice with ' .
144 'unknown profile ' . common_log_objstring($notice),
148 $msg = jabber_format_notice($profile, $notice);
149 $entry = jabber_format_entry($profile, $notice);
150 $conn->message($to, $msg, 'chat', null, $entry);
156 * extra information for XMPP messages, as defined by Twitter
158 * @param Profile $profile Profile of the sending user
159 * @param Notice $notice Notice being sent
161 * @return string Extra information (Atom, HTML, addresses) in string format
164 function jabber_format_entry($profile, $notice)
166 // FIXME: notice url might be remote
168 $noticeurl = common_local_url('shownotice',
169 array('notice' => $notice->id));
171 $msg = jabber_format_notice($profile, $notice);
173 $self_url = common_local_url('userrss', array('nickname' => $profile->nickname));
175 $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
176 $entry .= "<source>\n";
177 $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
178 $entry .= "<link href='" . htmlspecialchars($profile->profileurl) . "'/>\n";
179 $entry .= "<link rel='self' type='application/rss+xml' href='" . htmlspecialchars($self_url) . "'/>\n";
180 $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
181 $entry .= "<icon>" . $profile->avatarUrl(AVATAR_PROFILE_SIZE) . "</icon>\n";
182 $entry .= "</source>\n";
183 $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
184 $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
185 $entry .= "<link rel='alternate' href='" . htmlspecialchars($noticeurl) . "' />\n";
186 $entry .= "<id>". htmlspecialchars($notice->uri) . "</id>\n";
187 $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
188 $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
189 $entry .= "</entry>\n";
191 $html = "\n<html xmlns='http://jabber.org/protocol/xhtml-im'>\n";
192 $html .= "<body xmlns='http://www.w3.org/1999/xhtml'>\n";
193 $html .= "<a href='".htmlspecialchars($profile->profileurl)."'>".$profile->nickname."</a>: ";
194 $html .= ($notice->rendered) ? $notice->rendered : common_render_content($notice->content, $notice);
195 $html .= "\n</body>\n";
196 $html .= "\n</html>\n";
198 $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
199 $address .= "<address type='replyto' jid='" . jabber_daemon_address() . "' />\n";
200 $address .= "</addresses>\n";
202 // FIXME: include a pubsub event, too.
204 return $html . $entry . $address;
208 * sends a single text message to a given JID
210 * @param string $to JID to send the message to
211 * @param string $body body of the message
212 * @param string $type type of the message
213 * @param string $subject subject of the message
215 * @return boolean success flag
218 function jabber_send_message($to, $body, $type='chat', $subject=null)
220 $conn = jabber_connect();
224 $conn->message($to, $body, $type, $subject);
229 * sends a presence stanza on the Jabber network
231 * @param string $status current status, free-form string
232 * @param string $show structured status value
233 * @param string $to recipient of presence, null for general
234 * @param string $type type of status message, related to $show
235 * @param int $priority priority of the presence
237 * @return boolean success value
240 function jabber_send_presence($status, $show='available', $to=null,
241 $type = 'available', $priority=null)
243 $conn = jabber_connect();
247 $conn->presence($status, $show, $to, $type, $priority);
252 * sends a confirmation request to a JID
254 * @param string $code confirmation code for confirmation URL
255 * @param string $nickname nickname of confirming user
256 * @param string $address JID to send confirmation to
258 * @return boolean success flag
261 function jabber_confirm_address($code, $nickname, $address)
263 $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
264 'has said that your Jabber ID belongs to them. ' .
265 'If that\'s true, you can confirm by clicking on this URL: ' .
266 common_local_url('confirmaddress', array('code' => $code)) .
267 ' . (If you cannot click it, copy-and-paste it into the ' .
268 'address bar of your browser). If that user isn\'t you, ' .
269 'or if you didn\'t request this confirmation, just ignore this message.';
271 return jabber_send_message($address, $body);
275 * sends a "special" presence stanza on the Jabber network
277 * @param string $type Type of presence
278 * @param string $to JID to send presence to
279 * @param string $show show value for presence
280 * @param string $status status value for presence
282 * @return boolean success flag
284 * @see jabber_send_presence()
287 function jabber_special_presence($type, $to=null, $show=null, $status=null)
289 // FIXME: why use this instead of jabber_send_presence()?
290 $conn = jabber_connect();
292 $to = htmlspecialchars($to);
293 $status = htmlspecialchars($status);
300 $out .= " type='$type'";
302 if ($show == 'available' and !$status) {
306 if ($show && ($show != 'available')) {
307 $out .= "<show>$show</show>";
310 $out .= "<status>$status</status>";
312 $out .= "</presence>";
318 * broadcast a notice to all subscribers and reply recipients
320 * This function will send a notice to all subscribers on the local server
321 * who have Jabber addresses, and have Jabber notification enabled, and
322 * have this subscription enabled for Jabber. It also sends the notice to
323 * all recipients of @-replies who have Jabber addresses and Jabber notification
324 * enabled. This is really the heart of Jabber distribution in Laconica.
326 * @param Notice $notice The notice to broadcast
328 * @return boolean success flag
331 function jabber_broadcast_notice($notice)
333 if (!common_config('xmpp', 'enabled')) {
336 $profile = Profile::staticGet($notice->profile_id);
339 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
340 'unknown profile ' . common_log_objstring($notice),
345 $msg = jabber_format_notice($profile, $notice);
346 $entry = jabber_format_entry($profile, $notice);
353 $conn = jabber_connect();
355 // First, get users to whom this is a direct reply
357 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
358 $user->query("SELECT $UT.id, $UT.jabber " .
359 "FROM $UT JOIN reply ON $UT.id = reply.profile_id " .
360 'WHERE reply.notice_id = ' . $notice->id . ' ' .
361 "AND $UT.jabber is not null " .
362 "AND $UT.jabbernotify = 1 " .
363 "AND $UT.jabberreplies = 1 ");
365 while ($user->fetch()) {
367 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
369 $conn->message($user->jabber, $msg, 'chat', null, $entry);
370 $conn->processTime(0);
371 $sent_to[$user->id] = 1;
376 // Now, get users subscribed to this profile
379 $user->query("SELECT $UT.id, $UT.jabber " .
380 "FROM $UT JOIN subscription " .
381 "ON $UT.id = subscription.subscriber " .
382 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
383 "AND $UT.jabber is not null " .
384 "AND $UT.jabbernotify = 1 " .
385 'AND subscription.jabber = 1 ');
387 while ($user->fetch()) {
388 if (!array_key_exists($user->id, $sent_to)) {
390 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
392 $conn->message($user->jabber, $msg, 'chat', null, $entry);
393 // To keep the incoming queue from filling up,
394 // we service it after each send.
395 $conn->processTime(0);
396 $sent_to[$user->id] = 1;
400 // Now, get users who have it in their inbox because of groups
403 $user->query("SELECT $UT.id, $UT.jabber " .
404 "FROM $UT JOIN notice_inbox " .
405 "ON $UT.id = notice_inbox.user_id " .
406 'WHERE notice_inbox.notice_id = ' . $notice->id . ' ' .
407 'AND notice_inbox.source = 2 ' .
408 'AND user.jabber is not null ' .
409 'AND user.jabbernotify = 1 ');
411 while ($user->fetch()) {
412 if (!array_key_exists($user->id, $sent_to)) {
414 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
416 $conn->message($user->jabber, $msg, 'chat', null, $entry);
417 // To keep the incoming queue from filling up,
418 // we service it after each send.
419 $conn->processTime(0);
420 $sent_to[$user->id] = 1;
430 * send a notice to all public listeners
432 * For notices that are generated on the local system (by users), we can optionally
433 * forward them to remote listeners by XMPP.
435 * @param Notice $notice notice to broadcast
437 * @return boolean success flag
440 function jabber_public_notice($notice)
442 // Now, users who want everything
444 $public = common_config('xmpp', 'public');
446 // FIXME PRIV don't send out private messages here
447 // XXX: should we send out non-local messages if public,localonly
448 // = false? I think not
450 if ($public && $notice->is_local) {
451 $profile = Profile::staticGet($notice->profile_id);
454 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
455 'unknown profile ' . common_log_objstring($notice),
460 $msg = jabber_format_notice($profile, $notice);
461 $entry = jabber_format_entry($profile, $notice);
463 $conn = jabber_connect();
465 foreach ($public as $address) {
467 'Sending notice ' . $notice->id .
468 ' to public listener ' . $address,
470 $conn->message($address, $msg, 'chat', null, $entry);
471 $conn->processTime(0);
480 * makes a plain-text formatted version of a notice, suitable for Jabber distribution
482 * @param Profile &$profile profile of the sending user
483 * @param Notice &$notice notice being sent
485 * @return string plain-text version of the notice, with user nickname prefixed
488 function jabber_format_notice(&$profile, &$notice)
490 return $profile->nickname . ': ' . $notice->content;