]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
add extra Twitter cruft to jabber messages
[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                 $conn->autoSubscribe();
57
58                 if (!$conn) {
59                         return false;
60                 }
61                 $conn->connect(true); # true = persistent connection
62                 if ($conn->isDisconnected()) {
63                         return false;
64                 }
65         $conn->processUntil('session_start');
66         }
67         return $conn;
68 }
69
70 function jabber_send_notice($to, $notice) {
71         $conn = jabber_connect();
72         if (!$conn) {
73                 return false;
74         }
75         $profile = Profile::staticGet($notice->profile_id);
76         if (!$profile) {
77                 common_log(LOG_WARNING, 'Refusing to send notice with ' .
78                            'unknown profile ' . common_log_objstring($notice),
79                            __FILE__);
80                 return false;
81         }
82         $msg = jabber_format_notice($profile, $notice);
83         $entry = jabber_format_entry($profile, $notice);
84         $conn->message($to, $msg, 'chat', $entry);
85         return true;
86 }
87
88 # Extra stuff defined by Twitter, needed by twitter clients
89
90 function jabber_format_entry($profile, $notice) {
91         $noticeurl = common_local_url('shownotice',
92                                                                   array('notice' => $notice->id));
93         $entry = "<entry xmlns=\'http://www.w3.org/2005/Atom\'>\n";
94         $entry .= "<source>\n";
95         $entry .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
96         $entry .= "<link href='" . $profile->profileurl . "'/>\n";
97         $entry .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
98         $entry .= "<author><name>" . $profile->nickname . "</name></author>\n";
99         $entry .= "<icon>" . common_profile_avatar_url($profile, AVATAR_STREAM_SIZE) . "</icon>\n";
100         $entry .= "</source>\n";
101         $entry .= "<title>" . $msg . "</title>\n";
102         $entry .= "<summary>" . $msg . "</summary>\n";
103         $entry .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
104         $entry .= "<id>". $notice->uri . "</id>\n";
105         $entry .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
106         $entry .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
107         $entry .= "</entry>\n";
108         $entry .= "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
109     $entry .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
110         $entry .= "node='" . common_local_url('public') . "'>\n";
111         $entry .= "<item id='" . $notice->uri ."' />\n";
112         $entry .= "</items>\n";
113         $entry .= "</event>\n";
114         return $entry;
115 }
116
117 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
118         $conn = jabber_connect();
119         if (!$conn) {
120                 return false;
121         }
122         $conn->message($to, $body, $type, $subject);
123         return true;
124 }
125
126 function jabber_send_presence($status, $show='available', $to=Null) {
127         $conn = jabber_connect();
128         if (!$conn) {
129                 return false;
130         }
131         $conn->presence($status, $show, $to);
132         return true;
133 }
134
135 function jabber_confirm_address($code, $nickname, $address) {
136         $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') . ' ' .
137                         'has said that your Jabber ID belongs to them. ' .
138             'If that\'s true, you can confirm by clicking on this URL: ' .
139                 common_local_url('confirmaddress', array('code' => $code)) .
140                 ' . (If you cannot click it, copy-and-paste it into the ' .
141                 'address bar of your browser). If that user isn\'t you, ' .
142                 'or if you didn\'t request this confirmation, just ignore this message.';
143
144         return jabber_send_message($address, $body);
145 }
146
147 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
148         $conn = jabber_connect();
149
150         $to = htmlspecialchars($to);
151         $status = htmlspecialchars($status);
152         $out = "<presence";
153         if($to) $out .= " to='$to'";
154         if($type) $out .= " type='$type'";
155         if($show == 'available' and !$status) {
156                 $out .= "/>";
157         } else {
158                 $out .= ">";
159                 if($show && ($show != 'available')) $out .= "<show>$show</show>";
160                 if($status) $out .= "<status>$status</status>";
161                 $out .= "</presence>";
162         }
163         $conn->send($out);
164 }
165
166 function jabber_broadcast_notice($notice) {
167         # First, get users subscribed to this profile
168         # XXX: use a join here rather than looping through results
169         $profile = Profile::staticGet($notice->profile_id);
170         if (!$profile) {
171                 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
172                            'unknown profile ' . common_log_objstring($notice),
173                            __FILE__);
174                 return false;
175         }
176         $sub = new Subscription();
177         $sub->subscribed = $notice->profile_id;
178         if ($sub->find()) {
179                 $msg = jabber_format_notice($profile, $notice);
180                 while ($sub->fetch()) {
181                         $user = User::staticGet($sub->subscriber);
182                         if ($user && $user->jabber && $user->jabbernotify) {
183                                 common_log(LOG_INFO,
184                                                    'Sending notice ' . $notice->id . ' to ' . $user->jabber,
185                                                    __FILE__);
186                                 $success = jabber_send_message($user->jabber, $msg);
187                                 if (!$success) {
188                                         # XXX: Not sure, but I think that's the right thing to do
189                                         return false;
190                                 }
191                         }
192                 }
193         }
194         return true;
195 }
196
197 function jabber_format_notice(&$profile, &$notice) {
198         return $profile->nickname . ': ' . $notice->content;
199 }