]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
b943b6f782937197229478d6925cc72045fd2ef2
[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         public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=NULL) {
29                 if($type == 'available') $type = '';
30                 $to      = htmlspecialchars($to);
31                 $status = htmlspecialchars($status);
32                 if($show == 'unavailable') $type = 'unavailable';
33
34                 $out = "<presence";
35                 if($to) $out .= " to='$to'";
36                 if($type) $out .= " type='$type'";
37                 if($show == 'available' and !$status and is_null($priority)) {
38                         $out .= "/>";
39                 } else {
40                         $out .= ">";
41                         if($show != 'available') $out .= "<show>$show</show>";
42                         if($status) $out .= "<status>$status</status>";
43                         if(!is_null($priority)) $out .= "<priority>$priority</priority>";
44                         $out .= "</presence>";
45                 }
46
47                 $this->send($out);
48         }
49 }
50
51 function jabber_valid_base_jid($jid) {
52         # Cheap but effective
53         return Validate::email($jid);
54 }
55
56 function jabber_normalize_jid($jid) {
57         if (preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches)) {
58                 $node = $matches[1];
59                 $server = $matches[2];
60                 return strtolower($node.'@'.$server);
61         } else {
62                 return NULL;
63         }
64 }
65
66 function jabber_daemon_address() {
67         return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
68 }
69
70 function jabber_connect($resource=NULL, $status=NULL, $priority=NULL) {
71         static $conn = NULL;
72         if (!$conn) {
73                 $conn = new Laconica_XMPP(common_config('xmpp', 'host') ?
74                                                                 common_config('xmpp', 'host') :
75                                                                 common_config('xmpp', 'server'),
76                                                                 common_config('xmpp', 'port'),
77                                                                 common_config('xmpp', 'user'),
78                                                                 common_config('xmpp', 'password'),
79                                                                 ($resource) ? $resource :
80                                                                 common_config('xmpp', 'resource'),
81                                                                 common_config('xmpp', 'server'),
82                                                                 common_config('xmpp', 'debug') ?
83                                                                 true : false,
84                                                                 common_config('xmpp', 'debug') ?
85                                                                 XMPPHP_Log::LEVEL_VERBOSE :  NULL
86                                                                 );
87                 $conn->autoSubscribe();
88                 $conn->useEncryption(common_config('xmpp', 'encryption'));
89
90                 if (!$conn) {
91                         return false;
92                 }
93                 $conn->connect(true); # true = persistent connection
94                 if ($conn->isDisconnected()) {
95                         return false;
96                 }
97         $conn->processUntil('session_start');
98                 $conn->getRoster();
99                 $conn->presence($presence, 'available', NULL, 'available', $priority);
100         }
101         return $conn;
102 }
103
104 function jabber_send_notice($to, $notice) {
105         $conn = jabber_connect();
106         if (!$conn) {
107                 return false;
108         }
109         $profile = Profile::staticGet($notice->profile_id);
110         if (!$profile) {
111                 common_log(LOG_WARNING, 'Refusing to send notice with ' .
112                            'unknown profile ' . common_log_objstring($notice),
113                            __FILE__);
114                 return false;
115         }
116         $msg = jabber_format_notice($profile, $notice);
117         $entry = jabber_format_entry($profile, $notice);
118         $conn->message($to, $msg, 'chat', NULL, $entry);
119         return true;
120 }
121
122 # Extra stuff defined by Twitter, needed by twitter clients
123
124 function jabber_format_entry($profile, $notice) {
125
126         # FIXME: notice url might be remote
127
128         $noticeurl = common_local_url('shownotice',
129                                                                   array('notice' => $notice->id));
130         $msg = jabber_format_notice($profile, $notice);
131         $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
132         $entry .= "<source>\n";
133         $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
134         $entry .= "<link href='" . htmlspecialchars($profile->profileurl) . "'/>\n";
135         $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
136         $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
137         $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
138         $entry .= "</source>\n";
139         $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
140         $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
141         $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
142         $entry .= "<id>". $notice->uri . "</id>\n";
143         $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
144         $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
145         $entry .= "</entry>\n";
146
147         $html = "\n<html xmlns='http://jabber.org/protocol/xhtml-im'>\n";
148         $html .= "<body xmlns='http://www.w3.org/1999/xhtml'>\n";
149         $html .= "<a href='".common_profile_url($profile->nickname)."'>".$profile->nickname."</a>: ";
150         $html .= ($notice->rendered) ? $notice->rendered : common_render_content($notice->content, $notice);
151         $html .= "\n</body>\n";
152         $html .= "\n</html>\n";
153
154         $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
155         $address .= "<address type='replyto' jid='" . jabber_daemon_address() . "' />\n";
156         $address .= "</addresses>\n";
157
158         $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
159     $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
160         $event .= "node='" . common_local_url('public') . "'>\n";
161         $event .= "<item id='" . $notice->uri ."' />\n";
162         $event .= "</items>\n";
163         $event .= "</event>\n";
164         # FIXME: include the pubsub event, too.
165         return $html . $entry . $address;
166 #       return $entry . "\n" . $event;
167 }
168
169 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
170         $conn = jabber_connect();
171         if (!$conn) {
172                 return false;
173         }
174         $conn->message($to, $body, $type, $subject);
175         return true;
176 }
177
178 function jabber_send_presence($status, $show='available', $to=Null) {
179         $conn = jabber_connect();
180         if (!$conn) {
181                 return false;
182         }
183         $conn->presence($status, $show, $to);
184         return true;
185 }
186
187 function jabber_confirm_address($code, $nickname, $address) {
188         $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
189                         'has said that your Jabber ID belongs to them. ' .
190             'If that\'s true, you can confirm by clicking on this URL: ' .
191                 common_local_url('confirmaddress', array('code' => $code)) .
192                 ' . (If you cannot click it, copy-and-paste it into the ' .
193                 'address bar of your browser). If that user isn\'t you, ' .
194                 'or if you didn\'t request this confirmation, just ignore this message.';
195
196         return jabber_send_message($address, $body);
197 }
198
199 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
200         $conn = jabber_connect();
201
202         $to = htmlspecialchars($to);
203         $status = htmlspecialchars($status);
204         $out = "<presence";
205         if($to) $out .= " to='$to'";
206         if($type) $out .= " type='$type'";
207         if($show == 'available' and !$status) {
208                 $out .= "/>";
209         } else {
210                 $out .= ">";
211                 if($show && ($show != 'available')) $out .= "<show>$show</show>";
212                 if($status) $out .= "<status>$status</status>";
213                 $out .= "</presence>";
214         }
215         $conn->send($out);
216 }
217
218 function jabber_broadcast_notice($notice) {
219
220         if (!common_config('xmpp', 'enabled')) {
221                 return true;
222         }
223         $profile = Profile::staticGet($notice->profile_id);
224
225         if (!$profile) {
226                 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
227                            'unknown profile ' . common_log_objstring($notice),
228                            __FILE__);
229                 return false;
230         }
231
232         $msg = jabber_format_notice($profile, $notice);
233         $entry = jabber_format_entry($profile, $notice);
234
235         $sent_to = array();
236         $conn = jabber_connect();
237
238         # First, get users to whom this is a direct reply
239         $user = new User();
240         $user->query('SELECT user.id, user.jabber ' .
241                                  'FROM user JOIN reply ON user.id = reply.profile_id ' .
242                                  'WHERE reply.notice_id = ' . $notice->id . ' ' .
243                                  'AND user.jabber is not null ' .
244                                  'AND user.jabbernotify = 1 ' .
245                                  'AND user.jabberreplies = 1 ');
246
247         while ($user->fetch()) {
248                 common_log(LOG_INFO,
249                                    'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
250                                    __FILE__);
251                 $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
252         }
253
254     # Now, get users subscribed to this profile
255
256         $user = new User();
257         $user->query('SELECT user.id, user.jabber ' .
258                                  'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
259                                  'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
260                                  'AND user.jabber is not null ' .
261                                  'AND user.jabbernotify = 1 ');
262
263         while ($user->fetch()) {
264                 if (!array_key_exists($user->id, $sent_to)) {
265                         common_log(LOG_INFO,
266                                            'Sending notice ' . $notice->id . ' to ' . $user->jabber,
267                                            __FILE__);
268                         $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
269                 }
270         }
271
272         return true;
273 }
274
275 function jabber_public_notice($notice) {
276
277         # Now, users who want everything
278
279         $public = common_config('xmpp', 'public');
280
281         # FIXME PRIV don't send out private messages here
282         # XXX: should we send out non-local messages if public,localonly
283         # = false? I think not
284
285         if ($public && $notice->is_local) {
286                 $msg = jabber_format_notice($profile, $notice);
287                 $entry = jabber_format_entry($profile, $notice);
288
289                 $sent_to = array();
290                 $conn = jabber_connect();
291
292                 foreach ($public as $address) {
293                         common_log(LOG_INFO,
294                                            'Sending notice ' . $notice->id . ' to public listener ' . $address,
295                                            __FILE__);
296                         $conn->message($address, $msg, 'chat', NULL, $entry);
297                 }
298         }
299
300         return true;
301 }
302
303 function jabber_format_notice(&$profile, &$notice) {
304         return $profile->nickname . ': ' . $notice->content;
305 }