]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
364282a1c34dbee1f7928b1c22763a866f5d1202
[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('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_connect($resource=NULL, $status=NULL) {
41         static $conn = NULL;
42         if (!$conn) {
43                 $conn = new XMPP(common_config('xmpp', 'server'),
44                                              common_config('xmpp', 'port'),
45                                              common_config('xmpp', 'user'),
46                                              common_config('xmpp', 'password'),
47                                          ($resource) ? $resource :
48                                                 common_config('xmpp', 'resource'),
49                                          common_config('xmpp', 'host') ?
50                                          common_config('xmpp', 'host') :
51                                          common_config('xmpp', 'server'));
52
53                 if (!$conn) {
54                         return false;
55                 }
56                 $conn->connect(true); # try to get a persistent connection
57                 if ($conn->disconnected) {
58                         return false;
59                 }
60         $conn->processUntil('session_start');
61         if ($status) {
62                 $conn->presence($status);
63                 }
64         }
65         return $conn;
66 }
67
68 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
69         $conn = jabber_connect();
70         if (!$conn) {
71                 return false;
72         }
73         $conn->message($to, $body, $type, $subject);
74         return true;
75 }
76
77 function jabber_send_presence($status=Null, $show='available', $to=Null) {
78         $conn = jabber_connect();
79         if (!$conn) {
80                 return false;
81         }
82         $conn->presence($status, $show, $to);
83         return true;
84 }
85
86 function jabber_confirm_address($code, $nickname, $address) {
87
88         # Fire off a subscription, just in case
89
90         jabber_special_presence('subscribe', $address);
91
92         # Hopefully this goes through if we're not already subscribed
93
94         $body = 'User "' . $nickname . '" on ' . common_config('site', 'name') .
95                         'has said that your Jabber ID belongs to them. ' .
96                 'If that\'s true, you can confirm by clicking on this URL: ' .
97                 common_local_url('confirmaddress', array('code' => $confirm->code)) .
98                 ' . (If you cannot click it, copy-and-paste it into the ' .
99                 'address bar of your browser). If that user isn\'t you, ' .
100                 'or if you didn\'t request this confirmation, just ignore this message.';
101
102         jabber_send_message($address, $body);
103 }
104
105 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
106         $conn = jabber_connect();
107
108         $to = htmlspecialchars($to);
109         $status = htmlspecialchars($status);
110         $out = "<presence";
111         if($to) $out .= " to='$to'";
112         if($type) $out .= " type='$type'";
113         if($show == 'available' and !$status) {
114                 $out .= "/>";
115         } else {
116                 $out .= ">";
117                 if($show && ($show != 'available')) $out .= "<show>$show</show>";
118                 if($status) $out .= "<status>$status</status>";
119                 $out .= "</presence>";
120         }
121         $conn->send($out);
122 }
123
124 function jabber_broadcast_notice($notice) {
125         # First, get users subscribed to this profile
126         # XXX: use a join here rather than looping through results
127         $profile = Profile::staticGet($notice->profile_id);
128         if (!$profile) {
129                 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
130                            'unknown profile ' . common_log_objstring($notice),
131                            __FILE__);
132                 return false;
133         }
134         $sub = new Subscription();
135         $sub->subscribed = $notice->profile_id;
136         if ($sub->find()) {
137                 $msg = jabber_format_notice($profile, $notice);
138                 while ($sub->fetch()) {
139                         $user = User::staticGet($sub->subscriber);
140                         if ($user && $user->jabber) {
141                                 jabber_send_message($user->jabber,
142                                                     $msg);
143                         }
144                 }
145         }
146 }
147
148 function jabber_format_notice(&$profile, &$notice) {
149         return $profile->nickname . ': ' . $notice->content;
150 }