]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
f89def25e9c629ec74db35ffe77181520ba327f8
[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, $status=NULL, $priority=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                 $conn->autoSubscribe();
61                 $conn->useEncryption(common_config('xmpp', 'encryption'));
62
63                 if (!$conn) {
64                         return false;
65                 }
66                 $conn->connect(true); # true = persistent connection
67                 if ($conn->isDisconnected()) {
68                         return false;
69                 }
70         $conn->processUntil('session_start');
71 #               $conn->getRoster();
72                 $conn->presence($presence, 'available', NULL, 'available');
73         }
74         return $conn;
75 }
76
77 function jabber_send_notice($to, $notice) {
78         $conn = jabber_connect();
79         if (!$conn) {
80                 return false;
81         }
82         $profile = Profile::staticGet($notice->profile_id);
83         if (!$profile) {
84                 common_log(LOG_WARNING, 'Refusing to send notice with ' .
85                            'unknown profile ' . common_log_objstring($notice),
86                            __FILE__);
87                 return false;
88         }
89         $msg = jabber_format_notice($profile, $notice);
90         $entry = jabber_format_entry($profile, $notice);
91         $conn->message($to, $msg, 'chat', NULL, $entry);
92         return true;
93 }
94
95 # Extra stuff defined by Twitter, needed by twitter clients
96
97 function jabber_format_entry($profile, $notice) {
98
99         # FIXME: notice url might be remote
100
101         $noticeurl = common_local_url('shownotice',
102                                                                   array('notice' => $notice->id));
103         $msg = jabber_format_notice($profile, $notice);
104         $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
105         $entry .= "<source>\n";
106         $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
107         $entry .= "<link href='" . htmlspecialchars($profile->profileurl) . "'/>\n";
108         $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
109         $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
110         $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
111         $entry .= "</source>\n";
112         $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
113         $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
114         $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
115         $entry .= "<id>". $notice->uri . "</id>\n";
116         $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
117         $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
118         $entry .= "</entry>\n";
119
120         $html = "\n<html xmlns='http://jabber.org/protocol/xhtml-im'>\n";
121         $html .= "<body xmlns='http://www.w3.org/1999/xhtml'>\n";
122         $html .= "<a href='".common_profile_url($profile->nickname)."'>".$profile->nickname."</a>: ";
123         $html .= ($notice->rendered) ? $notice->rendered : common_render_content($notice->content, $notice);
124         $html .= "\n</body>\n";
125         $html .= "\n</html>\n";
126
127         $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
128         $address .= "<address type='replyto' jid='" . jabber_daemon_address() . "' />\n";
129         $address .= "</addresses>\n";
130
131         $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
132     $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
133         $event .= "node='" . common_local_url('public') . "'>\n";
134         $event .= "<item id='" . $notice->uri ."' />\n";
135         $event .= "</items>\n";
136         $event .= "</event>\n";
137         # FIXME: include the pubsub event, too.
138         return $html . $entry . $address;
139 #       return $entry . "\n" . $event;
140 }
141
142 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
143         $conn = jabber_connect();
144         if (!$conn) {
145                 return false;
146         }
147         $conn->message($to, $body, $type, $subject);
148         return true;
149 }
150
151 function jabber_send_presence($status, $show='available', $to=Null) {
152         $conn = jabber_connect();
153         if (!$conn) {
154                 return false;
155         }
156         $conn->presence($status, $show, $to);
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         $sent_to = array();
209         $conn = jabber_connect();
210
211         # First, get users to whom this is a direct reply
212         $user = new User();
213         $user->query('SELECT user.id, user.jabber ' .
214                                  'FROM user JOIN reply ON user.id = reply.profile_id ' .
215                                  'WHERE reply.notice_id = ' . $notice->id . ' ' .
216                                  'AND user.jabber is not null ' .
217                                  'AND user.jabbernotify = 1 ' .
218                                  'AND user.jabberreplies = 1 ');
219
220         while ($user->fetch()) {
221                 common_log(LOG_INFO,
222                                    'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
223                                    __FILE__);
224                 $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
225         }
226
227     # Now, get users subscribed to this profile
228
229         $user = new User();
230         $user->query('SELECT user.id, user.jabber ' .
231                                  'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
232                                  'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
233                                  'AND user.jabber is not null ' .
234                                  'AND user.jabbernotify = 1 ');
235
236         while ($user->fetch()) {
237                 if (!array_key_exists($user->id, $sent_to)) {
238                         common_log(LOG_INFO,
239                                            'Sending notice ' . $notice->id . ' to ' . $user->jabber,
240                                            __FILE__);
241                         $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
242                 }
243         }
244
245         return true;
246 }
247
248 function jabber_public_notice($notice) {
249
250         # Now, users who want everything
251
252         $public = common_config('xmpp', 'public');
253
254         # FIXME PRIV don't send out private messages here
255         # XXX: should we send out non-local messages if public,localonly
256         # = false? I think not
257
258         if ($public && $notice->is_local) {
259                 $profile = Profile::staticGet($notice->profile_id);
260
261                 if (!$profile) {
262                         common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
263                                            'unknown profile ' . common_log_objstring($notice),
264                                            __FILE__);
265                         return false;
266                 }
267
268                 $msg = jabber_format_notice($profile, $notice);
269                 $entry = jabber_format_entry($profile, $notice);
270
271                 $conn = jabber_connect();
272
273                 foreach ($public as $address) {
274                         common_log(LOG_INFO,
275                                            'Sending notice ' . $notice->id . ' to public listener ' . $address,
276                                            __FILE__);
277                         $conn->message($address, $msg, 'chat', NULL, $entry);
278                 }
279         }
280
281         return true;
282 }
283
284 function jabber_format_notice(&$profile, &$notice) {
285         return $profile->nickname . ': ' . $notice->content;
286 }