]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
build stanza ourselves, without library
[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                 $resource = $matches[3];
34                 return strtolower($node.'@'.$server);
35         } else {
36                 return NULL;
37         }
38 }
39
40 function jabber_daemon_address() {
41         return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
42 }
43
44 function jabber_connect($resource=NULL) {
45         static $conn = NULL;
46         if (!$conn) {
47                 $conn = new XMPPHP_XMPP(common_config('xmpp', 'host') ?
48                                                                 common_config('xmpp', 'host') :
49                                                                 common_config('xmpp', 'server'),
50                                                                 common_config('xmpp', 'port'),
51                                                                 common_config('xmpp', 'user'),
52                                                                 common_config('xmpp', 'password'),
53                                                                 ($resource) ? $resource :
54                                                                 common_config('xmpp', 'resource'),
55                                                                 common_config('xmpp', 'server'),
56                                                                 common_config('xmpp', 'debug') ?
57                                                                 true : false,
58                                                                 common_config('xmpp', 'debug') ?
59                                                                 XMPPHP_Log::LEVEL_VERBOSE :  NULL
60                                                                 );
61                 $conn->autoSubscribe();
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         $stanza = jabber_make_stanza($to, $msg, 'chat', NULL, $entry);
90         common_log(LOG_DEBUG, 'full stanza = ' . $stanza, __FILE__);
91         $conn->send($stanza);
92         return true;
93 }
94
95 function jabber_make_stanza($to, $body, $type = 'chat', $subject = null, $payload = null) {
96         $to       = htmlspecialchars($to);
97         $body   = htmlspecialchars($body);
98         $subject = htmlspecialchars($subject);
99
100         $jid = jabber_daemon_address();
101         
102         $out = "<message from='$jid' to='$to' type='$type'>";
103         if($subject) $out .= "<subject>$subject</subject>";
104         $out .= "<body>$body</body>";
105         if($payload) $out .= $payload;
106         $out .= "</message>";
107         
108         return $out;
109 }
110
111 # Extra stuff defined by Twitter, needed by twitter clients
112
113 function jabber_format_entry($profile, $notice) {
114         $noticeurl = common_local_url('shownotice',
115                                                                   array('notice' => $notice->id));
116         $msg = jabber_format_notice($profile, $notice);
117         $entry = "\n<entry xmlns='http://www.w3.org/2005/Atom'>\n";
118         $entry .= "<source>\n";
119         $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
120         $entry .= "<link href='" . $profile->profileurl . "'/>\n";
121         $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
122         $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
123         $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
124         $entry .= "</source>\n";
125         $entry .= "<title>" . htmlspecialchars($msg) . "</title>\n";
126         $entry .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
127         $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
128         $entry .= "<id>". $notice->uri . "</id>\n";
129         $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
130         $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
131         $entry .= "</entry>\n";
132         
133         $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
134     $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
135         $event .= "node='" . common_local_url('public') . "'>\n";
136         $event .= "<item id='" . $notice->uri ."' />\n";
137         $event .= "</items>\n";
138         $event .= "</event>\n";
139         # FIXME: include the pubsub event, too.
140         return $entry;
141 #       return $entry . "\n" . $event;
142 }
143
144 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
145         $conn = jabber_connect();
146         if (!$conn) {
147                 return false;
148         }
149         $conn->message($to, $body, $type, $subject);
150         return true;
151 }
152
153 function jabber_send_presence($status, $show='available', $to=Null) {
154         $conn = jabber_connect();
155         if (!$conn) {
156                 return false;
157         }
158         $conn->presence($status, $show, $to);
159         return true;
160 }
161
162 function jabber_confirm_address($code, $nickname, $address) {
163         $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
164                         'has said that your Jabber ID belongs to them. ' .
165             'If that\'s true, you can confirm by clicking on this URL: ' .
166                 common_local_url('confirmaddress', array('code' => $code)) .
167                 ' . (If you cannot click it, copy-and-paste it into the ' .
168                 'address bar of your browser). If that user isn\'t you, ' .
169                 'or if you didn\'t request this confirmation, just ignore this message.';
170
171         return jabber_send_message($address, $body);
172 }
173
174 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
175         $conn = jabber_connect();
176
177         $to = htmlspecialchars($to);
178         $status = htmlspecialchars($status);
179         $out = "<presence";
180         if($to) $out .= " to='$to'";
181         if($type) $out .= " type='$type'";
182         if($show == 'available' and !$status) {
183                 $out .= "/>";
184         } else {
185                 $out .= ">";
186                 if($show && ($show != 'available')) $out .= "<show>$show</show>";
187                 if($status) $out .= "<status>$status</status>";
188                 $out .= "</presence>";
189         }
190         $conn->send($out);
191 }
192
193 function jabber_broadcast_notice($notice) {
194         # First, get users subscribed to this profile
195         # XXX: use a join here rather than looping through results
196         $profile = Profile::staticGet($notice->profile_id);
197         if (!$profile) {
198                 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
199                            'unknown profile ' . common_log_objstring($notice),
200                            __FILE__);
201                 return false;
202         }
203         $sub = new Subscription();
204         $sub->subscribed = $notice->profile_id;
205         if ($sub->find()) {
206                 while ($sub->fetch()) {
207                         $user = User::staticGet($sub->subscriber);
208                         if ($user && $user->jabber && $user->jabbernotify) {
209                                 common_log(LOG_INFO,
210                                                    'Sending notice ' . $notice->id . ' to ' . $user->jabber,
211                                                    __FILE__);
212                                 $success = jabber_send_notice($user->jabber, $notice);
213                                 if (!$success) {
214                                         # XXX: Not sure, but I think that's the right thing to do
215                                         return false;
216                                 }
217                         }
218                 }
219         }
220         return true;
221 }
222
223 function jabber_format_notice(&$profile, &$notice) {
224         return $profile->nickname . ': ' . $notice->content;
225 }