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