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.');
48 function jabber_valid_base_jid($jid) {
50 return Validate::email($jid);
53 function jabber_normalize_jid($jid) {
54 if (preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches)) {
56 $server = $matches[2];
57 return strtolower($node.'@'.$server);
63 function jabber_daemon_address() {
64 return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
67 function jabber_connect($resource=NULL) {
70 $conn = new Laconica_XMPP(common_config('xmpp', 'host') ?
71 common_config('xmpp', 'host') :
72 common_config('xmpp', 'server'),
73 common_config('xmpp', 'port'),
74 common_config('xmpp', 'user'),
75 common_config('xmpp', 'password'),
76 ($resource) ? $resource :
77 common_config('xmpp', 'resource'),
78 common_config('xmpp', 'server'),
79 common_config('xmpp', 'debug') ?
81 common_config('xmpp', 'debug') ?
82 XMPPHP_Log::LEVEL_VERBOSE : NULL
84 $conn->autoSubscribe();
85 $conn->useEncryption(common_config('xmpp', 'encryption'));
90 $conn->connect(true); # true = persistent connection
91 if ($conn->isDisconnected()) {
94 $conn->processUntil('session_start');
99 function jabber_send_notice($to, $notice) {
100 $conn = jabber_connect();
104 $profile = Profile::staticGet($notice->profile_id);
106 common_log(LOG_WARNING, 'Refusing to send notice with ' .
107 'unknown profile ' . common_log_objstring($notice),
111 $msg = jabber_format_notice($profile, $notice);
112 $entry = jabber_format_entry($profile, $notice);
113 $conn->messageplus($to, $msg, 'chat', NULL, $entry);
117 # Extra stuff defined by Twitter, needed by twitter clients
119 function jabber_format_entry($profile, $notice) {
121 $noticeurl = common_local_url('shownotice',
122 array('notice' => $notice->id));
123 $msg = jabber_format_notice($profile, $notice);
124 $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
125 $entry .= "<source>\n";
126 $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
127 $entry .= "<link href='" . htmlspecialchars($profile->profileurl) . "'/>\n";
128 $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
129 $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
130 $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
131 $entry .= "</source>\n";
132 $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
133 $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
134 $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
135 $entry .= "<id>". $notice->uri . "</id>\n";
136 $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
137 $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
138 $entry .= "</entry>\n";
140 $html = "\n<html xmlns='http://jabber.org/protocol/xhtml-im'>\n";
141 $html .= "<body xmlns='http://www.w3.org/1999/xhtml'>\n";
142 $html .= "<a href='".common_profile_url($profile->nickname)."'>".$profile->nickname."</a>: ";
143 $html .= ($notice->rendered) ? $notice->rendered : common_render_content($notice->content, $notice);
144 $html .= "\n</body>\n";
145 $html .= "\n</html>\n";
147 $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
148 $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
149 $event .= "node='" . common_local_url('public') . "'>\n";
150 $event .= "<item id='" . $notice->uri ."' />\n";
151 $event .= "</items>\n";
152 $event .= "</event>\n";
153 # FIXME: include the pubsub event, too.
154 return $html . $entry;
155 # return $entry . "\n" . $event;
158 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
159 $conn = jabber_connect();
163 $conn->message($to, $body, $type, $subject);
167 function jabber_send_presence($status, $show='available', $to=Null) {
168 $conn = jabber_connect();
172 $conn->presence($status, $show, $to);
176 function jabber_confirm_address($code, $nickname, $address) {
177 $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
178 'has said that your Jabber ID belongs to them. ' .
179 'If that\'s true, you can confirm by clicking on this URL: ' .
180 common_local_url('confirmaddress', array('code' => $code)) .
181 ' . (If you cannot click it, copy-and-paste it into the ' .
182 'address bar of your browser). If that user isn\'t you, ' .
183 'or if you didn\'t request this confirmation, just ignore this message.';
185 return jabber_send_message($address, $body);
188 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
189 $conn = jabber_connect();
191 $to = htmlspecialchars($to);
192 $status = htmlspecialchars($status);
194 if($to) $out .= " to='$to'";
195 if($type) $out .= " type='$type'";
196 if($show == 'available' and !$status) {
200 if($show && ($show != 'available')) $out .= "<show>$show</show>";
201 if($status) $out .= "<status>$status</status>";
202 $out .= "</presence>";
207 function jabber_broadcast_notice($notice) {
208 if (!common_config('xmpp', 'enabled')) {
211 $profile = Profile::staticGet($notice->profile_id);
213 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
214 'unknown profile ' . common_log_objstring($notice),
219 # First, get users who this is a direct reply to
220 $reply = new Reply();
221 $reply->notice_id = $notice->id;
222 if ($reply->find()) {
223 while ($reply->fetch()) {
224 $user = User::staticGet($reply->profile_id);
225 if ($user && $user->jabber && $user->jabbernotify && $user->jabberreplies) {
227 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
229 $success = jabber_send_notice($user->jabber, $notice);
231 # Remember so we don't send twice
232 $sent_to[$user->id] = true;
234 # XXX: Not sure, but I think that's the right thing to do
235 common_log(LOG_WARNING,
236 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
243 # Now, get users subscribed to this profile
244 # XXX: use a join here rather than looping through results
245 $sub = new Subscription();
246 $sub->subscribed = $notice->profile_id;
249 while ($sub->fetch()) {
250 $user = User::staticGet($sub->subscriber);
251 if ($user && $user->jabber && $user->jabbernotify && !array_key_exists($user->id,$sent_to)) {
253 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
255 $success = jabber_send_notice($user->jabber, $notice);
257 $sent_to[$user->id] = true;
259 # XXX: Not sure, but I think that's the right thing to do
260 common_log(LOG_WARNING,
261 'Sending notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
269 # Now, users who want everything
271 $public = common_config('xmpp', 'public');
273 # FIXME PRIV don't send out private messages here
274 # XXX: should we send out non-local messages if public,localonly = false? I think not
276 if ($public && $notice->is_local) {
277 foreach ($public as $address) {
279 'Sending notice ' . $notice->id . ' to public listener ' . $address,
281 jabber_send_notice($address, $notice);
288 function jabber_format_notice(&$profile, &$notice) {
289 return $profile->nickname . ': ' . $notice->content;