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