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('XMPPHP/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 return strtolower($node.'@'.$server);
39 function jabber_daemon_address() {
40 return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
43 function jabber_connect($resource=NULL, $status=NULL, $priority=NULL) {
46 $conn = new XMPPHP_XMPP(common_config('xmpp', 'host') ?
47 common_config('xmpp', 'host') :
48 common_config('xmpp', 'server'),
49 common_config('xmpp', 'port'),
50 common_config('xmpp', 'user'),
51 common_config('xmpp', 'password'),
52 ($resource) ? $resource :
53 common_config('xmpp', 'resource'),
54 common_config('xmpp', 'server'),
55 common_config('xmpp', 'debug') ?
57 common_config('xmpp', 'debug') ?
58 XMPPHP_Log::LEVEL_VERBOSE : NULL
60 $conn->autoSubscribe();
61 $conn->useEncryption(common_config('xmpp', 'encryption'));
66 $conn->connect(true); # true = persistent connection
67 if ($conn->isDisconnected()) {
70 $conn->processUntil('session_start');
72 $conn->presence($presence, 'available', NULL, 'available', $priority);
77 function jabber_send_notice($to, $notice) {
78 $conn = jabber_connect();
82 $profile = Profile::staticGet($notice->profile_id);
84 common_log(LOG_WARNING, 'Refusing to send notice with ' .
85 'unknown profile ' . common_log_objstring($notice),
89 $msg = jabber_format_notice($profile, $notice);
90 $entry = jabber_format_entry($profile, $notice);
91 $conn->message($to, $msg, 'chat', NULL, $entry);
95 # Extra stuff defined by Twitter, needed by twitter clients
97 function jabber_format_entry($profile, $notice) {
99 # FIXME: notice url might be remote
101 $noticeurl = common_local_url('shownotice',
102 array('notice' => $notice->id));
103 $msg = jabber_format_notice($profile, $notice);
104 $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
105 $entry .= "<source>\n";
106 $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
107 $entry .= "<link href='" . htmlspecialchars($profile->profileurl) . "'/>\n";
108 $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
109 $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
110 $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
111 $entry .= "</source>\n";
112 $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
113 $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
114 $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
115 $entry .= "<id>". $notice->uri . "</id>\n";
116 $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
117 $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
118 $entry .= "</entry>\n";
120 $html = "\n<html xmlns='http://jabber.org/protocol/xhtml-im'>\n";
121 $html .= "<body xmlns='http://www.w3.org/1999/xhtml'>\n";
122 $html .= "<a href='".common_profile_url($profile->nickname)."'>".$profile->nickname."</a>: ";
123 $html .= ($notice->rendered) ? $notice->rendered : common_render_content($notice->content, $notice);
124 $html .= "\n</body>\n";
125 $html .= "\n</html>\n";
127 $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
128 $address .= "<address type='replyto' jid='" . jabber_daemon_address() . "' />\n";
129 $address .= "</addresses>\n";
131 $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
132 $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
133 $event .= "node='" . common_local_url('public') . "'>\n";
134 $event .= "<item id='" . $notice->uri ."' />\n";
135 $event .= "</items>\n";
136 $event .= "</event>\n";
137 # FIXME: include the pubsub event, too.
138 # return $html . $entry . $address;
139 return $entry . $address;
140 # return $entry . "\n" . $event;
143 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
144 $conn = jabber_connect();
148 $conn->message($to, $body, $type, $subject);
152 function jabber_send_presence($status, $show='available', $to=Null) {
153 $conn = jabber_connect();
157 $conn->presence($status, $show, $to);
161 function jabber_confirm_address($code, $nickname, $address) {
162 $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
163 'has said that your Jabber ID belongs to them. ' .
164 'If that\'s true, you can confirm by clicking on this URL: ' .
165 common_local_url('confirmaddress', array('code' => $code)) .
166 ' . (If you cannot click it, copy-and-paste it into the ' .
167 'address bar of your browser). If that user isn\'t you, ' .
168 'or if you didn\'t request this confirmation, just ignore this message.';
170 return jabber_send_message($address, $body);
173 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
174 $conn = jabber_connect();
176 $to = htmlspecialchars($to);
177 $status = htmlspecialchars($status);
179 if($to) $out .= " to='$to'";
180 if($type) $out .= " type='$type'";
181 if($show == 'available' and !$status) {
185 if($show && ($show != 'available')) $out .= "<show>$show</show>";
186 if($status) $out .= "<status>$status</status>";
187 $out .= "</presence>";
192 function jabber_broadcast_notice($notice) {
194 if (!common_config('xmpp', 'enabled')) {
197 $profile = Profile::staticGet($notice->profile_id);
200 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
201 'unknown profile ' . common_log_objstring($notice),
206 $msg = jabber_format_notice($profile, $notice);
207 $entry = jabber_format_entry($profile, $notice);
210 $conn = jabber_connect();
212 # First, get users to whom this is a direct reply
214 $user->query('SELECT user.id, user.jabber ' .
215 'FROM user JOIN reply ON user.id = reply.profile_id ' .
216 'WHERE reply.notice_id = ' . $notice->id . ' ' .
217 'AND user.jabber is not null ' .
218 'AND user.jabbernotify = 1 ' .
219 'AND user.jabberreplies = 1 ');
221 while ($user->fetch()) {
223 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
225 $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
226 $sent_to[$user->id] = 1;
229 # Now, get users subscribed to this profile
232 $user->query('SELECT user.id, user.jabber ' .
233 'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
234 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
235 'AND user.jabber is not null ' .
236 'AND user.jabbernotify = 1 ');
238 while ($user->fetch()) {
239 if (!array_key_exists($user->id, $sent_to)) {
241 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
243 $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
250 function jabber_public_notice($notice) {
252 # Now, users who want everything
254 $public = common_config('xmpp', 'public');
256 # FIXME PRIV don't send out private messages here
257 # XXX: should we send out non-local messages if public,localonly
258 # = false? I think not
260 if ($public && $notice->is_local) {
261 $profile = Profile::staticGet($notice->profile_id);
264 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
265 'unknown profile ' . common_log_objstring($notice),
270 $msg = jabber_format_notice($profile, $notice);
271 $entry = jabber_format_entry($profile, $notice);
273 $conn = jabber_connect();
275 foreach ($public as $address) {
277 'Sending notice ' . $notice->id . ' to public listener ' . $address,
279 $conn->message($address, $msg, 'chat', NULL, $entry);
286 function jabber_format_notice(&$profile, &$notice) {
287 return $profile->nickname . ': ' . $notice->content;