]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
171dff4df47cd2c0b4719f1a44c72bb4c3c8a498
[quix0rs-gnu-social.git] / lib / jabber.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once('XMPPHP/XMPP.php');
23
24 # XXX: something of a hack to work around problems with the XMPPHP lib
25
26 class Laconica_XMPP extends XMPPHP_XMPP {
27
28     function messageplus($to, $body, $type = 'chat', $subject = null, $payload = null) {
29                 $to       = htmlspecialchars($to);
30                 $body   = htmlspecialchars($body);
31                 $subject = htmlspecialchars($subject);
32
33                 $jid = jabber_daemon_address();
34
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;
39                 $out .= "</message>";
40
41                 $cnt = strlen($out);
42                 common_log(LOG_DEBUG, "Sending $cnt chars to $to");
43                 $this->send($out);
44                 common_log(LOG_DEBUG, 'Done.');
45     }
46
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';
52
53                 $out = "<presence";
54                 if($to) $out .= " to='$to'";
55                 if($type) $out .= " type='$type'";
56                 if($show == 'available' and !$status and is_null($priority)) {
57                         $out .= "/>";
58                 } else {
59                         $out .= ">";
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>";
64                 }
65
66                 $this->send($out);
67         }
68 }
69
70 function jabber_valid_base_jid($jid) {
71         # Cheap but effective
72         return Validate::email($jid);
73 }
74
75 function jabber_normalize_jid($jid) {
76         if (preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches)) {
77                 $node = $matches[1];
78                 $server = $matches[2];
79                 return strtolower($node.'@'.$server);
80         } else {
81                 return NULL;
82         }
83 }
84
85 function jabber_daemon_address() {
86         return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
87 }
88
89 function jabber_connect($resource=NULL, $status=NULL, $priority=NULL) {
90         static $conn = NULL;
91         if (!$conn) {
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') ?
102                                                                 true : false,
103                                                                 common_config('xmpp', 'debug') ?
104                                                                 XMPPHP_Log::LEVEL_VERBOSE :  NULL
105                                                                 );
106                 $conn->autoSubscribe();
107                 $conn->useEncryption(common_config('xmpp', 'encryption'));
108
109                 if (!$conn) {
110                         return false;
111                 }
112                 $conn->connect(true); # true = persistent connection
113                 if ($conn->isDisconnected()) {
114                         return false;
115                 }
116         $conn->processUntil('session_start');
117                 $conn->getRoster();
118                 $conn->presence($presence, 'available', NULL, 'available', $priority);
119         }
120         return $conn;
121 }
122
123 function jabber_send_notice($to, $notice) {
124         $conn = jabber_connect();
125         if (!$conn) {
126                 return false;
127         }
128         $profile = Profile::staticGet($notice->profile_id);
129         if (!$profile) {
130                 common_log(LOG_WARNING, 'Refusing to send notice with ' .
131                            'unknown profile ' . common_log_objstring($notice),
132                            __FILE__);
133                 return false;
134         }
135         $msg = jabber_format_notice($profile, $notice);
136         $entry = jabber_format_entry($profile, $notice);
137         $conn->messageplus($to, $msg, 'chat', NULL, $entry);
138         return true;
139 }
140
141 # Extra stuff defined by Twitter, needed by twitter clients
142
143 function jabber_format_entry($profile, $notice) {
144
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";
163
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";
170
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;
180 }
181
182 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
183         $conn = jabber_connect();
184         if (!$conn) {
185                 return false;
186         }
187         $conn->message($to, $body, $type, $subject);
188         return true;
189 }
190
191 function jabber_send_presence($status, $show='available', $to=Null) {
192         $conn = jabber_connect();
193         if (!$conn) {
194                 return false;
195         }
196         $conn->presence($status, $show, $to);
197         return true;
198 }
199
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.';
208
209         return jabber_send_message($address, $body);
210 }
211
212 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
213         $conn = jabber_connect();
214
215         $to = htmlspecialchars($to);
216         $status = htmlspecialchars($status);
217         $out = "<presence";
218         if($to) $out .= " to='$to'";
219         if($type) $out .= " type='$type'";
220         if($show == 'available' and !$status) {
221                 $out .= "/>";
222         } else {
223                 $out .= ">";
224                 if($show && ($show != 'available')) $out .= "<show>$show</show>";
225                 if($status) $out .= "<status>$status</status>";
226                 $out .= "</presence>";
227         }
228         $conn->send($out);
229 }
230
231 function jabber_broadcast_notice($notice) {
232
233         if (!common_config('xmpp', 'enabled')) {
234                 return true;
235         }
236         $profile = Profile::staticGet($notice->profile_id);
237         if (!$profile) {
238                 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
239                            'unknown profile ' . common_log_objstring($notice),
240                            __FILE__);
241                 return false;
242         }
243         $sent_to = array();
244         # First, get users who this is a direct reply to
245         $reply = new Reply();
246         $reply->notice_id = $notice->id;
247         if ($reply->find()) {
248                 while ($reply->fetch()) {
249                         $user = User::staticGet($reply->profile_id);
250                         if ($user && $user->jabber && $user->jabbernotify && $user->jabberreplies) {
251                                 common_log(LOG_INFO,
252                                                    'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
253                                                    __FILE__);
254                                 $success = jabber_send_notice($user->jabber, $notice);
255                                 if ($success) {
256                                         # Remember so we don't send twice
257                                         $sent_to[$user->id] = true;
258                                 } else {
259                                         # XXX: Not sure, but I think that's the right thing to do
260                                         common_log(LOG_WARNING,
261                                                            'Sending reply notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
262                                                            __FILE__);
263                                         return false;
264                                 }
265                         }
266                 }
267         }
268     # Now, get users subscribed to this profile
269         # XXX: use a join here rather than looping through results
270         $sub = new Subscription();
271         $sub->subscribed = $notice->profile_id;
272
273         if ($sub->find()) {
274                 while ($sub->fetch()) {
275                         $user = User::staticGet($sub->subscriber);
276                         if ($user && $user->jabber && $user->jabbernotify && !array_key_exists($user->id,$sent_to)) {
277                                 common_log(LOG_INFO,
278                                                    'Sending notice ' . $notice->id . ' to ' . $user->jabber,
279                                                    __FILE__);
280                                 $success = jabber_send_notice($user->jabber, $notice);
281                                 if ($success) {
282                                         $sent_to[$user->id] = true;
283                                 } else {
284                                         # XXX: Not sure, but I think that's the right thing to do
285                                         common_log(LOG_WARNING,
286                                                            'Sending notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
287                                                            __FILE__);
288                                         return false;
289                                 }
290                         }
291                 }
292         }
293         
294         return true;
295 }
296
297 function jabber_public_notice($notice) {
298
299         # Now, users who want everything
300
301         $public = common_config('xmpp', 'public');
302
303         # FIXME PRIV don't send out private messages here
304         # XXX: should we send out non-local messages if public,localonly
305         # = false? I think not
306
307         if ($public && $notice->is_local) {
308                 foreach ($public as $address) {
309                                 common_log(LOG_INFO,
310                                                    'Sending notice ' . $notice->id . ' to public listener ' . $address,
311                                                    __FILE__);
312                                 jabber_send_notice($address, $notice);
313                 }
314         }
315
316         return true;
317 }
318
319 function jabber_format_notice(&$profile, &$notice) {
320         return $profile->nickname . ': ' . $notice->content;
321 }