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) {
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
65 $conn->autoSubscribe();
66 $conn->useEncryption(common_config('xmpp', 'encryption'));
69 $conn->connect(true); # true = persistent connection
70 } catch (XMPPHP_Exception $e) {
71 common_log(LOG_ERROR, $e->getMessage());
75 $conn->processUntil('session_start');
80 function jabber_send_notice($to, $notice) {
81 $conn = jabber_connect();
85 $profile = Profile::staticGet($notice->profile_id);
87 common_log(LOG_WARNING, 'Refusing to send notice with ' .
88 'unknown profile ' . common_log_objstring($notice),
92 $msg = jabber_format_notice($profile, $notice);
93 $entry = jabber_format_entry($profile, $notice);
94 $conn->message($to, $msg, 'chat', NULL, $entry);
99 # Extra stuff defined by Twitter, needed by twitter clients
101 function jabber_format_entry($profile, $notice) {
103 # FIXME: notice url might be remote
105 $noticeurl = common_local_url('shownotice',
106 array('notice' => $notice->id));
107 $msg = jabber_format_notice($profile, $notice);
108 $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
109 $entry .= "<source>\n";
110 $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
111 $entry .= "<link href='" . htmlspecialchars($profile->profileurl) . "'/>\n";
112 $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
113 $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
114 $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
115 $entry .= "</source>\n";
116 $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
117 $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
118 $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
119 $entry .= "<id>". $notice->uri . "</id>\n";
120 $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
121 $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
122 $entry .= "</entry>\n";
124 $html = "\n<html xmlns='http://jabber.org/protocol/xhtml-im'>\n";
125 $html .= "<body xmlns='http://www.w3.org/1999/xhtml'>\n";
126 $html .= "<a href='".common_profile_url($profile->nickname)."'>".$profile->nickname."</a>: ";
127 $html .= ($notice->rendered) ? $notice->rendered : common_render_content($notice->content, $notice);
128 $html .= "\n</body>\n";
129 $html .= "\n</html>\n";
131 $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
132 $address .= "<address type='replyto' jid='" . jabber_daemon_address() . "' />\n";
133 $address .= "</addresses>\n";
135 # FIXME: include a pubsub event, too.
137 return $html . $entry . $address;
140 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
141 $conn = jabber_connect();
145 $conn->message($to, $body, $type, $subject);
149 function jabber_send_presence($status, $show='available', $to=NULL,
150 $type = 'available', $priority=NULL)
152 $conn = jabber_connect();
156 $conn->presence($status, $show, $to, $type, $priority);
160 function jabber_confirm_address($code, $nickname, $address) {
161 $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
162 'has said that your Jabber ID belongs to them. ' .
163 'If that\'s true, you can confirm by clicking on this URL: ' .
164 common_local_url('confirmaddress', array('code' => $code)) .
165 ' . (If you cannot click it, copy-and-paste it into the ' .
166 'address bar of your browser). If that user isn\'t you, ' .
167 'or if you didn\'t request this confirmation, just ignore this message.';
169 return jabber_send_message($address, $body);
172 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
173 $conn = jabber_connect();
175 $to = htmlspecialchars($to);
176 $status = htmlspecialchars($status);
178 if($to) $out .= " to='$to'";
179 if($type) $out .= " type='$type'";
180 if($show == 'available' and !$status) {
184 if($show && ($show != 'available')) $out .= "<show>$show</show>";
185 if($status) $out .= "<status>$status</status>";
186 $out .= "</presence>";
191 function jabber_broadcast_notice($notice) {
193 if (!common_config('xmpp', 'enabled')) {
196 $profile = Profile::staticGet($notice->profile_id);
199 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
200 'unknown profile ' . common_log_objstring($notice),
205 $msg = jabber_format_notice($profile, $notice);
206 $entry = jabber_format_entry($profile, $notice);
212 $conn = jabber_connect();
214 # First, get users to whom this is a direct reply
216 $user->query('SELECT user.id, user.jabber ' .
217 'FROM user JOIN reply ON user.id = reply.profile_id ' .
218 'WHERE reply.notice_id = ' . $notice->id . ' ' .
219 'AND user.jabber is not null ' .
220 'AND user.jabbernotify = 1 ' .
221 'AND user.jabberreplies = 1 ');
223 while ($user->fetch()) {
225 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
227 $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
228 $conn->processTime(0);
229 $sent_to[$user->id] = 1;
234 # Now, get users subscribed to this profile
237 $user->query('SELECT user.id, user.jabber ' .
238 'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
239 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
240 'AND user.jabber is not null ' .
241 'AND user.jabbernotify = 1 ');
243 while ($user->fetch()) {
244 if (!array_key_exists($user->id, $sent_to)) {
246 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
248 $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
249 # To keep the incoming queue from filling up, we service it after each send.
250 $conn->processTime(0);
259 function jabber_public_notice($notice) {
261 # Now, users who want everything
263 $public = common_config('xmpp', 'public');
265 # FIXME PRIV don't send out private messages here
266 # XXX: should we send out non-local messages if public,localonly
267 # = false? I think not
269 if ($public && $notice->is_local) {
270 $profile = Profile::staticGet($notice->profile_id);
273 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
274 'unknown profile ' . common_log_objstring($notice),
279 $msg = jabber_format_notice($profile, $notice);
280 $entry = jabber_format_entry($profile, $notice);
282 $conn = jabber_connect();
284 foreach ($public as $address) {
286 'Sending notice ' . $notice->id . ' to public listener ' . $address,
288 $conn->message($address, $msg, 'chat', NULL, $entry);
289 $conn->processTime(0);
297 function jabber_format_notice(&$profile, &$notice) {
298 return $profile->nickname . ': ' . $notice->content;