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 # XXX: something of a hack to work around problems with the XMPPHP lib
26 class Laconica_XMPP extends XMPPHP_XMPP {
28 function messageplus($to, $body, $type = 'chat', $subject = null, $payload = null) {
29 $to = htmlspecialchars($to);
30 $body = htmlspecialchars($body);
31 $subject = htmlspecialchars($subject);
33 $jid = jabber_daemon_address();
35 $out = "<message from='$jid' to='$to' type='$type'>";
36 if($subject) $out .= "<subject>$subject</subject>";
37 $out .= "<body>$body</body>";
38 if($payload) $out .= $payload;
42 common_log(LOG_DEBUG, "Sending $cnt chars to $to");
44 common_log(LOG_DEBUG, 'Done.');
47 public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=NULL) {
48 if($type == 'available') $type = '';
49 $to = htmlspecialchars($to);
50 $status = htmlspecialchars($status);
51 if($show == 'unavailable') $type = 'unavailable';
54 if($to) $out .= " to='$to'";
55 if($type) $out .= " type='$type'";
56 if($show == 'available' and !$status and is_null($priority)) {
60 if($show != 'available') $out .= "<show>$show</show>";
61 if($status) $out .= "<status>$status</status>";
62 if(!is_null($priority)) $out .= "<priority>$priority</priority>";
63 $out .= "</presence>";
70 function jabber_valid_base_jid($jid) {
72 return Validate::email($jid);
75 function jabber_normalize_jid($jid) {
76 if (preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches)) {
78 $server = $matches[2];
79 return strtolower($node.'@'.$server);
85 function jabber_daemon_address() {
86 return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
89 function jabber_connect($resource=NULL, $status=NULL, $priority=NULL) {
92 $conn = new Laconica_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
106 $conn->autoSubscribe();
107 $conn->useEncryption(common_config('xmpp', 'encryption'));
112 $conn->connect(true); # true = persistent connection
113 if ($conn->isDisconnected()) {
116 $conn->processUntil('session_start');
118 $conn->presence($presence, $priority);
123 function jabber_send_notice($to, $notice) {
124 $conn = jabber_connect();
128 $profile = Profile::staticGet($notice->profile_id);
130 common_log(LOG_WARNING, 'Refusing to send notice with ' .
131 'unknown profile ' . common_log_objstring($notice),
135 $msg = jabber_format_notice($profile, $notice);
136 $entry = jabber_format_entry($profile, $notice);
137 $conn->messageplus($to, $msg, 'chat', NULL, $entry);
141 # Extra stuff defined by Twitter, needed by twitter clients
143 function jabber_format_entry($profile, $notice) {
145 $noticeurl = common_local_url('shownotice',
146 array('notice' => $notice->id));
147 $msg = jabber_format_notice($profile, $notice);
148 $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
149 $entry .= "<source>\n";
150 $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
151 $entry .= "<link href='" . htmlspecialchars($profile->profileurl) . "'/>\n";
152 $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
153 $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
154 $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
155 $entry .= "</source>\n";
156 $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
157 $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
158 $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
159 $entry .= "<id>". $notice->uri . "</id>\n";
160 $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
161 $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
162 $entry .= "</entry>\n";
164 $html = "\n<html xmlns='http://jabber.org/protocol/xhtml-im'>\n";
165 $html .= "<body xmlns='http://www.w3.org/1999/xhtml'>\n";
166 $html .= "<a href='".common_profile_url($profile->nickname)."'>".$profile->nickname."</a>: ";
167 $html .= ($notice->rendered) ? $notice->rendered : common_render_content($notice->content, $notice);
168 $html .= "\n</body>\n";
169 $html .= "\n</html>\n";
171 $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
172 $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
173 $event .= "node='" . common_local_url('public') . "'>\n";
174 $event .= "<item id='" . $notice->uri ."' />\n";
175 $event .= "</items>\n";
176 $event .= "</event>\n";
177 # FIXME: include the pubsub event, too.
178 return $html . $entry;
179 # return $entry . "\n" . $event;
182 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
183 $conn = jabber_connect();
187 $conn->message($to, $body, $type, $subject);
191 function jabber_send_presence($status, $show='available', $to=Null) {
192 $conn = jabber_connect();
196 $conn->presence($status, $show, $to);
200 function jabber_confirm_address($code, $nickname, $address) {
201 $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
202 'has said that your Jabber ID belongs to them. ' .
203 'If that\'s true, you can confirm by clicking on this URL: ' .
204 common_local_url('confirmaddress', array('code' => $code)) .
205 ' . (If you cannot click it, copy-and-paste it into the ' .
206 'address bar of your browser). If that user isn\'t you, ' .
207 'or if you didn\'t request this confirmation, just ignore this message.';
209 return jabber_send_message($address, $body);
212 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
213 $conn = jabber_connect();
215 $to = htmlspecialchars($to);
216 $status = htmlspecialchars($status);
218 if($to) $out .= " to='$to'";
219 if($type) $out .= " type='$type'";
220 if($show == 'available' and !$status) {
224 if($show && ($show != 'available')) $out .= "<show>$show</show>";
225 if($status) $out .= "<status>$status</status>";
226 $out .= "</presence>";
231 function jabber_broadcast_notice($notice) {
232 if (!common_config('xmpp', 'enabled')) {
235 $profile = Profile::staticGet($notice->profile_id);
237 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
238 'unknown profile ' . common_log_objstring($notice),
243 # First, get users who this is a direct reply to
244 $reply = new Reply();
245 $reply->notice_id = $notice->id;
246 if ($reply->find()) {
247 while ($reply->fetch()) {
248 $user = User::staticGet($reply->profile_id);
249 if ($user && $user->jabber && $user->jabbernotify && $user->jabberreplies) {
251 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
253 $success = jabber_send_notice($user->jabber, $notice);
255 # Remember so we don't send twice
256 $sent_to[$user->id] = true;
258 # XXX: Not sure, but I think that's the right thing to do
259 common_log(LOG_WARNING,
260 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
267 # Now, get users subscribed to this profile
268 # XXX: use a join here rather than looping through results
269 $sub = new Subscription();
270 $sub->subscribed = $notice->profile_id;
273 while ($sub->fetch()) {
274 $user = User::staticGet($sub->subscriber);
275 if ($user && $user->jabber && $user->jabbernotify && !array_key_exists($user->id,$sent_to)) {
277 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
279 $success = jabber_send_notice($user->jabber, $notice);
281 $sent_to[$user->id] = true;
283 # XXX: Not sure, but I think that's the right thing to do
284 common_log(LOG_WARNING,
285 'Sending notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
293 # Now, users who want everything
295 $public = common_config('xmpp', 'public');
297 # FIXME PRIV don't send out private messages here
298 # XXX: should we send out non-local messages if public,localonly = false? I think not
300 if ($public && $notice->is_local) {
301 foreach ($public as $address) {
303 'Sending notice ' . $notice->id . ' to public listener ' . $address,
305 jabber_send_notice($address, $notice);
312 function jabber_format_notice(&$profile, &$notice) {
313 return $profile->nickname . ': ' . $notice->content;