]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
a044f9c1b9043965f4cf659469027c4ce8481d7f
[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                 $resource = $matches[3];
58                 return strtolower($node.'@'.$server);
59         } else {
60                 return NULL;
61         }
62 }
63
64 function jabber_daemon_address() {
65         return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
66 }
67
68 function jabber_connect($resource=NULL) {
69         static $conn = NULL;
70         if (!$conn) {
71                 $conn = new Laconica_XMPP(common_config('xmpp', 'host') ?
72                                                                 common_config('xmpp', 'host') :
73                                                                 common_config('xmpp', 'server'),
74                                                                 common_config('xmpp', 'port'),
75                                                                 common_config('xmpp', 'user'),
76                                                                 common_config('xmpp', 'password'),
77                                                                 ($resource) ? $resource :
78                                                                 common_config('xmpp', 'resource'),
79                                                                 common_config('xmpp', 'server'),
80                                                                 common_config('xmpp', 'debug') ?
81                                                                 true : false,
82                                                                 common_config('xmpp', 'debug') ?
83                                                                 XMPPHP_Log::LEVEL_VERBOSE :  NULL
84                                                                 );
85                 $conn->autoSubscribe();
86
87                 if (!$conn) {
88                         return false;
89                 }
90                 $conn->connect(true); # true = persistent connection
91                 if ($conn->isDisconnected()) {
92                         return false;
93                 }
94         $conn->processUntil('session_start');
95         }
96         return $conn;
97 }
98
99 function jabber_send_notice($to, $notice) {
100         $conn = jabber_connect();
101         if (!$conn) {
102                 return false;
103         }
104         $profile = Profile::staticGet($notice->profile_id);
105         if (!$profile) {
106                 common_log(LOG_WARNING, 'Refusing to send notice with ' .
107                            'unknown profile ' . common_log_objstring($notice),
108                            __FILE__);
109                 return false;
110         }
111         $msg = jabber_format_notice($profile, $notice);
112         $entry = jabber_format_entry($profile, $notice);
113         $conn->messageplus($to, $msg, 'chat', NULL, $entry);
114         return true;
115 }
116
117 # Extra stuff defined by Twitter, needed by twitter clients
118
119 function jabber_format_entry($profile, $notice) {
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='" . $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         $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
140     $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
141         $event .= "node='" . common_local_url('public') . "'>\n";
142         $event .= "<item id='" . $notice->uri ."' />\n";
143         $event .= "</items>\n";
144         $event .= "</event>\n";
145         # FIXME: include the pubsub event, too.
146         return $entry;
147 #       return $entry . "\n" . $event;
148 }
149
150 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
151         $conn = jabber_connect();
152         if (!$conn) {
153                 return false;
154         }
155         $conn->message($to, $body, $type, $subject);
156         return true;
157 }
158
159 function jabber_send_presence($status, $show='available', $to=Null) {
160         $conn = jabber_connect();
161         if (!$conn) {
162                 return false;
163         }
164         $conn->presence($status, $show, $to);
165         return true;
166 }
167
168 function jabber_confirm_address($code, $nickname, $address) {
169         $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
170                         'has said that your Jabber ID belongs to them. ' .
171             'If that\'s true, you can confirm by clicking on this URL: ' .
172                 common_local_url('confirmaddress', array('code' => $code)) .
173                 ' . (If you cannot click it, copy-and-paste it into the ' .
174                 'address bar of your browser). If that user isn\'t you, ' .
175                 'or if you didn\'t request this confirmation, just ignore this message.';
176
177         return jabber_send_message($address, $body);
178 }
179
180 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
181         $conn = jabber_connect();
182
183         $to = htmlspecialchars($to);
184         $status = htmlspecialchars($status);
185         $out = "<presence";
186         if($to) $out .= " to='$to'";
187         if($type) $out .= " type='$type'";
188         if($show == 'available' and !$status) {
189                 $out .= "/>";
190         } else {
191                 $out .= ">";
192                 if($show && ($show != 'available')) $out .= "<show>$show</show>";
193                 if($status) $out .= "<status>$status</status>";
194                 $out .= "</presence>";
195         }
196         $conn->send($out);
197 }
198
199 function jabber_broadcast_notice($notice) {
200         $profile = Profile::staticGet($notice->profile_id);
201         if (!$profile) {
202                 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
203                            'unknown profile ' . common_log_objstring($notice),
204                            __FILE__);
205                 return false;
206         }
207         $sent_to = array();
208         # First, get users who this is a direct reply to
209         $reply = new Reply();
210         $reply->notice_id = $notice->id;
211         if ($reply->find()) {
212                 while ($reply->fetch()) {
213                         $user = User::staticGet($reply->profile_id);
214                         if ($user && $user->jabber && $user->jabbernotify && $user->jabberreplies) {
215                                 common_log(LOG_INFO,
216                                                    'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
217                                                    __FILE__);
218                                 $success = jabber_send_notice($user->jabber, $notice);
219                                 if ($success) {
220                                         # Remember so we don't send twice
221                                         $sent_to[$user->id] = true;
222                                 } else {
223                                         # XXX: Not sure, but I think that's the right thing to do
224                                         common_log(LOG_WARNING,
225                                                            'Sending reply notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
226                                                            __FILE__);
227                                         return false;
228                                 }
229                         }
230                 }
231         }
232     # Now, get users subscribed to this profile
233         # XXX: use a join here rather than looping through results
234         $sub = new Subscription();
235         $sub->subscribed = $notice->profile_id;
236         
237         if ($sub->find()) {
238                 while ($sub->fetch()) {
239                         $user = User::staticGet($sub->subscriber);
240                         if ($user && $user->jabber && $user->jabbernotify && !$sent_to[$user->id]) {
241                                 common_log(LOG_INFO,
242                                                    'Sending notice ' . $notice->id . ' to ' . $user->jabber,
243                                                    __FILE__);
244                                 $success = jabber_send_notice($user->jabber, $notice);
245                                 if ($success) {
246                                         $sent_to[$user->id] = true;
247                                 } else {
248                                         # XXX: Not sure, but I think that's the right thing to do
249                                         common_log(LOG_WARNING,
250                                                            'Sending notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.',
251                                                            __FILE__);
252                                         return false;
253                                 }
254                         }
255                 }
256         }
257
258         return true;
259 }
260
261 function jabber_format_notice(&$profile, &$notice) {
262         return $profile->nickname . ': ' . $notice->content;
263 }