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 $resource = $matches[3];
34 return strtolower($node.'@'.$server);
40 function jabber_daemon_address() {
41 return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
44 function jabber_connect($resource=NULL) {
47 $conn = new XMPPHP_XMPP(common_config('xmpp', 'host') ?
48 common_config('xmpp', 'host') :
49 common_config('xmpp', 'server'),
50 common_config('xmpp', 'port'),
51 common_config('xmpp', 'user'),
52 common_config('xmpp', 'password'),
53 ($resource) ? $resource :
54 common_config('xmpp', 'resource'),
55 common_config('xmpp', 'server'),
56 common_config('xmpp', 'debug') ?
58 common_config('xmpp', 'debug') ?
59 XMPPHP_Log::LEVEL_VERBOSE : NULL
61 $conn->autoSubscribe();
66 $conn->connect(true); # true = persistent connection
67 if ($conn->isDisconnected()) {
70 $conn->processUntil('session_start');
75 function jabber_send_notice($to, $notice) {
76 $conn = jabber_connect();
80 $profile = Profile::staticGet($notice->profile_id);
82 common_log(LOG_WARNING, 'Refusing to send notice with ' .
83 'unknown profile ' . common_log_objstring($notice),
87 $msg = jabber_format_notice($profile, $notice);
88 $entry = jabber_format_entry($profile, $notice);
89 $stanza = jabber_make_stanza($to, $msg, 'chat', NULL, $entry);
90 common_log(LOG_DEBUG, 'full stanza = ' . $stanza, __FILE__);
95 function jabber_make_stanza($to, $body, $type = 'chat', $subject = null, $payload = null) {
96 $to = htmlspecialchars($to);
97 $body = htmlspecialchars($body);
98 $subject = htmlspecialchars($subject);
100 $jid = jabber_daemon_address();
102 $out = "<message from='$jid' to='$to' type='$type'>";
103 if($subject) $out .= "<subject>$subject</subject>";
104 $out .= "<body>$body</body>";
105 if($payload) $out .= $payload;
106 $out .= "</message>";
111 # Extra stuff defined by Twitter, needed by twitter clients
113 function jabber_format_entry($profile, $notice) {
114 $noticeurl = common_local_url('shownotice',
115 array('notice' => $notice->id));
116 $msg = jabber_format_notice($profile, $notice);
117 $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
118 $entry .= "<source>\n";
119 $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
120 $entry .= "<link href='" . $profile->profileurl . "'/>\n";
121 $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
122 $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
123 $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
124 $entry .= "</source>\n";
125 $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
126 $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
127 $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
128 $entry .= "<id>". $notice->uri . "</id>\n";
129 $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
130 $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
131 $entry .= "</entry>\n";
133 $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
134 $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
135 $event .= "node='" . common_local_url('public') . "'>\n";
136 $event .= "<item id='" . $notice->uri ."' />\n";
137 $event .= "</items>\n";
138 $event .= "</event>\n";
139 # FIXME: include the pubsub event, too.
141 # return $entry . "\n" . $event;
144 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
145 $conn = jabber_connect();
149 $conn->message($to, $body, $type, $subject);
153 function jabber_send_presence($status, $show='available', $to=Null) {
154 $conn = jabber_connect();
158 $conn->presence($status, $show, $to);
162 function jabber_confirm_address($code, $nickname, $address) {
163 $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
164 'has said that your Jabber ID belongs to them. ' .
165 'If that\'s true, you can confirm by clicking on this URL: ' .
166 common_local_url('confirmaddress', array('code' => $code)) .
167 ' . (If you cannot click it, copy-and-paste it into the ' .
168 'address bar of your browser). If that user isn\'t you, ' .
169 'or if you didn\'t request this confirmation, just ignore this message.';
171 return jabber_send_message($address, $body);
174 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
175 $conn = jabber_connect();
177 $to = htmlspecialchars($to);
178 $status = htmlspecialchars($status);
180 if($to) $out .= " to='$to'";
181 if($type) $out .= " type='$type'";
182 if($show == 'available' and !$status) {
186 if($show && ($show != 'available')) $out .= "<show>$show</show>";
187 if($status) $out .= "<status>$status</status>";
188 $out .= "</presence>";
193 function jabber_broadcast_notice($notice) {
194 # First, get users subscribed to this profile
195 # XXX: use a join here rather than looping through results
196 $profile = Profile::staticGet($notice->profile_id);
198 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
199 'unknown profile ' . common_log_objstring($notice),
203 $sub = new Subscription();
204 $sub->subscribed = $notice->profile_id;
206 while ($sub->fetch()) {
207 $user = User::staticGet($sub->subscriber);
208 if ($user && $user->jabber && $user->jabbernotify) {
210 'Sending notice ' . $notice->id . ' to ' . $user->jabber,
212 $success = jabber_send_notice($user->jabber, $notice);
214 # XXX: Not sure, but I think that's the right thing to do
223 function jabber_format_notice(&$profile, &$notice) {
224 return $profile->nickname . ': ' . $notice->content;