]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
Script to update laconica.pot from source, and the results of running it
[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         $profile->free();
96         return true;
97 }
98
99 # Extra stuff defined by Twitter, needed by twitter clients
100
101 function jabber_format_entry($profile, $notice) {
102
103         # FIXME: notice url might be remote
104
105         $noticeurl = common_local_url('shownotice',
106                                                                   array('notice' => $notice->id));
107         $msg = jabber_format_notice($profile, $notice);
108         $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
109         $entry .= "<source>\n";
110         $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
111         $entry .= "<link href='" . htmlspecialchars($profile->profileurl) . "'/>\n";
112         $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
113         $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
114         $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
115         $entry .= "</source>\n";
116         $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
117         $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
118         $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
119         $entry .= "<id>". $notice->uri . "</id>\n";
120         $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
121         $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
122         $entry .= "</entry>\n";
123
124         $html = "\n<html xmlns='http://jabber.org/protocol/xhtml-im'>\n";
125         $html .= "<body xmlns='http://www.w3.org/1999/xhtml'>\n";
126         $html .= "<a href='".htmlspecialchars($profile->profileurl)."'>".$profile->nickname."</a>: ";
127         $html .= ($notice->rendered) ? $notice->rendered : common_render_content($notice->content, $notice);
128         $html .= "\n</body>\n";
129         $html .= "\n</html>\n";
130
131         $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
132         $address .= "<address type='replyto' jid='" . jabber_daemon_address() . "' />\n";
133         $address .= "</addresses>\n";
134
135         # FIXME: include a pubsub event, too.
136
137         return $html . $entry . $address;
138 }
139
140 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
141         $conn = jabber_connect();
142         if (!$conn) {
143                 return false;
144         }
145         $conn->message($to, $body, $type, $subject);
146         return true;
147 }
148
149 function jabber_send_presence($status, $show='available', $to=NULL,
150                                                           $type = 'available', $priority=NULL)
151 {
152         $conn = jabber_connect();
153         if (!$conn) {
154                 return false;
155         }
156         $conn->presence($status, $show, $to, $type, $priority);
157         return true;
158 }
159
160 function jabber_confirm_address($code, $nickname, $address) {
161         $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
162                         'has said that your Jabber ID belongs to them. ' .
163             'If that\'s true, you can confirm by clicking on this URL: ' .
164                 common_local_url('confirmaddress', array('code' => $code)) .
165                 ' . (If you cannot click it, copy-and-paste it into the ' .
166                 'address bar of your browser). If that user isn\'t you, ' .
167                 'or if you didn\'t request this confirmation, just ignore this message.';
168
169         return jabber_send_message($address, $body);
170 }
171
172 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
173         $conn = jabber_connect();
174
175         $to = htmlspecialchars($to);
176         $status = htmlspecialchars($status);
177         $out = "<presence";
178         if($to) $out .= " to='$to'";
179         if($type) $out .= " type='$type'";
180         if($show == 'available' and !$status) {
181                 $out .= "/>";
182         } else {
183                 $out .= ">";
184                 if($show && ($show != 'available')) $out .= "<show>$show</show>";
185                 if($status) $out .= "<status>$status</status>";
186                 $out .= "</presence>";
187         }
188         $conn->send($out);
189 }
190
191 function jabber_broadcast_notice($notice) {
192
193         if (!common_config('xmpp', 'enabled')) {
194                 return true;
195         }
196         $profile = Profile::staticGet($notice->profile_id);
197
198         if (!$profile) {
199                 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
200                            'unknown profile ' . common_log_objstring($notice),
201                            __FILE__);
202                 return false;
203         }
204
205         $msg = jabber_format_notice($profile, $notice);
206         $entry = jabber_format_entry($profile, $notice);
207
208         $profile->free();
209         unset($profile);
210         
211         $sent_to = array();
212         $conn = jabber_connect();
213
214         # First, get users to whom this is a direct reply
215         $user = new User();
216         $user->query('SELECT user.id, user.jabber ' .
217                                  'FROM user JOIN reply ON user.id = reply.profile_id ' .
218                                  'WHERE reply.notice_id = ' . $notice->id . ' ' .
219                                  'AND user.jabber is not null ' .
220                                  'AND user.jabbernotify = 1 ' .
221                                  'AND user.jabberreplies = 1 ');
222
223         while ($user->fetch()) {
224                 common_log(LOG_INFO,
225                                    'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
226                                    __FILE__);
227                 $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
228                 $conn->processTime(0);
229                 $sent_to[$user->id] = 1;
230         }
231
232         $user->free();
233         
234     # Now, get users subscribed to this profile
235
236         $user = new User();
237         $user->query('SELECT user.id, user.jabber ' .
238                                  'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
239                                  'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
240                                  'AND user.jabber is not null ' .
241                                  'AND user.jabbernotify = 1 ');
242
243         while ($user->fetch()) {
244                 if (!array_key_exists($user->id, $sent_to)) {
245                         common_log(LOG_INFO,
246                                            'Sending notice ' . $notice->id . ' to ' . $user->jabber,
247                                            __FILE__);
248                         $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
249                         # To keep the incoming queue from filling up, we service it after each send.
250                         $conn->processTime(0);
251                 }
252         }
253
254         $user->free();
255         
256         return true;
257 }
258
259 function jabber_public_notice($notice) {
260
261         # Now, users who want everything
262
263         $public = common_config('xmpp', 'public');
264
265         # FIXME PRIV don't send out private messages here
266         # XXX: should we send out non-local messages if public,localonly
267         # = false? I think not
268
269         if ($public && $notice->is_local) {
270                 $profile = Profile::staticGet($notice->profile_id);
271
272                 if (!$profile) {
273                         common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
274                                            'unknown profile ' . common_log_objstring($notice),
275                                            __FILE__);
276                         return false;
277                 }
278
279                 $msg = jabber_format_notice($profile, $notice);
280                 $entry = jabber_format_entry($profile, $notice);
281
282                 $conn = jabber_connect();
283
284                 foreach ($public as $address) {
285                         common_log(LOG_INFO,
286                                            'Sending notice ' . $notice->id . ' to public listener ' . $address,
287                                            __FILE__);
288                         $conn->message($address, $msg, 'chat', NULL, $entry);
289                         $conn->processTime(0);
290                 }
291                 $profile->free();
292         }
293
294         return true;
295 }
296
297 function jabber_format_notice(&$profile, &$notice) {
298         return $profile->nickname . ': ' . $notice->content;
299 }