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', 'host') ?
44 common_config('xmpp', 'host') :
45 common_config('xmpp', 'server'),
46 common_config('xmpp', 'port'),
47 common_config('xmpp', 'user'),
48 common_config('xmpp', 'password'),
49 ($resource) ? $resource :
50 common_config('xmpp', 'resource'),
51 common_config('xmpp', 'server'));
56 $conn->connect(true); # true = persistent connection
57 if ($conn->disconnected) {
60 $conn->processUntil('session_start');
65 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
66 $conn = jabber_connect();
70 $conn->message($to, $body, $type, $subject);
74 function jabber_send_presence($status, $show='available', $to=Null) {
75 $conn = jabber_connect();
79 $conn->presence($status, $show, $to);
83 function jabber_confirm_address($code, $nickname, $address) {
84 $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
85 'has said that your Jabber ID belongs to them. ' .
86 'If that\'s true, you can confirm by clicking on this URL: ' .
87 common_local_url('confirmaddress', array('code' => $code)) .
88 ' . (If you cannot click it, copy-and-paste it into the ' .
89 'address bar of your browser). If that user isn\'t you, ' .
90 'or if you didn\'t request this confirmation, just ignore this message.';
92 jabber_send_message($address, $body);
95 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
96 $conn = jabber_connect();
98 $to = htmlspecialchars($to);
99 $status = htmlspecialchars($status);
101 if($to) $out .= " to='$to'";
102 if($type) $out .= " type='$type'";
103 if($show == 'available' and !$status) {
107 if($show && ($show != 'available')) $out .= "<show>$show</show>";
108 if($status) $out .= "<status>$status</status>";
109 $out .= "</presence>";
114 function jabber_broadcast_notice($notice) {
115 # First, get users subscribed to this profile
116 # XXX: use a join here rather than looping through results
117 $profile = Profile::staticGet($notice->profile_id);
119 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
120 'unknown profile ' . common_log_objstring($notice),
124 $sub = new Subscription();
125 $sub->subscribed = $notice->profile_id;
127 $msg = jabber_format_notice($profile, $notice);
128 while ($sub->fetch()) {
129 $user = User::staticGet($sub->subscriber);
130 if ($user && $user->jabber) {
131 jabber_send_message($user->jabber,
138 function jabber_format_notice(&$profile, &$notice) {
139 return $profile->nickname . ': ' . $notice->content;