3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once('xmpp.php');
24 function jabber_valid_base_jid($jid) {
26 return Validate::email($jid);
29 function jabber_normalize_jid($jid) {
30 if (preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches)) {
32 $server = $matches[2];
33 $resource = $matches[3];
34 return strtolower($node.'@'.$server);
40 function jabber_connect($resource=NULL) {
43 $conn = new XMPP(common_config('xmpp', 'server'),
44 common_config('xmpp', 'port'),
45 common_config('xmpp', 'user'),
46 common_config('xmpp', 'password'),
47 ($resource) ? $resource :
48 common_config('xmpp', 'resource'));
53 $conn->connect(true); # try to get a persistent connection
54 if ($conn->disconnected) {
57 $conn->processUntil('session_start');
62 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
63 $conn = jabber_connect();
67 $conn->message($to, $body, $type, $subject);
71 function jabber_send_presence($status=Null, $show='available', $to=Null) {
72 $conn = jabber_connect();
76 $conn->presence($status, $show, $to);
80 function jabber_confirm_address($code, $nickname, $address) {
82 # FIXME: do we have to request presence first?
84 $body = "Hey, $nickname.";
86 $body .= 'Someone just entered this IM address on ';
87 $body .= common_config('site', 'name') . '.';
89 $body .= 'If it was you, and you want to confirm your entry, ';
90 $body .= 'use the URL below:';
92 $body .= "\t".common_local_url('confirmaddress',
93 array('code' => $code));
95 $body .= 'If not, just ignore this message.';
97 $body .= 'Thanks for your time, ';
99 $body .= common_config('site', 'name');
102 jabber_send_message($address, $body);
105 function jabber_broadcast_notice($notice) {
106 # First, get users subscribed to this profile
107 # XXX: use a join here rather than looping through results
108 $profile = Profile::staticGet($notice->profile_id);
110 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
111 'unknown profile ' . common_log_objstring($notice),
115 $sub = new Subscription();
116 $sub->subscribed = $notice->profile_id;
118 $msg = jabber_format_notice($profile, $notice);
119 while ($sub->fetch()) {
120 $user = User::staticGet($sub->subscriber);
121 if ($user && $user->jabber) {
122 jabber_send_message($user->jabber,
129 function jabber_format_notice(&$profile, &$notice) {
130 return $profile->nickname . ': ' . $notice->content;