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