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