]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
2a557bfbe50f9ba56ebd276dc6acb088903309bb
[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) {
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
50                 if (!$conn) {
51                         return false;
52                 }
53                 $conn->connect(true); # try to get a persistent connection
54                 if ($conn->disconnected) {
55                         return false;
56                 }
57         $conn->processUntil('session_start');
58         }
59         return $conn;
60 }
61
62 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
63         $conn = jabber_connect();
64         if (!$conn) {
65                 return false;
66         }
67         $conn->message($to, $body, $type, $subject);
68         return true;
69 }
70
71 function jabber_send_presence($status=Null, $show='available', $to=Null) {
72         $conn = jabber_connect();
73         if (!$conn) {
74                 return false;
75         }
76         $conn->presence($status, $show, $to);
77         return true;
78 }
79
80 function jabber_confirm_address($code, $nickname, $address) {
81
82         # FIXME: above arguments are unused, we start the process with a
83         # subscription
84         # XXX: no idea what we do if the update daemon is already subscribed.
85
86         jabber_special_presence('subscribe', $address);
87
88 }
89
90
91 function jabber_special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
92         $conn = jabber_connect();
93
94         $to = htmlspecialchars($to);
95         $status = htmlspecialchars($status);
96         $out = "<presence";
97         if($to) $out .= " to='$to'";
98         if($type) $out .= " type='$type'";
99         if($show == 'available' and !$status) {
100                 $out .= "/>";
101         } else {
102                 $out .= ">";
103                 if($show && ($show != 'available')) $out .= "<show>$show</show>";
104                 if($status) $out .= "<status>$status</status>";
105                 $out .= "</presence>";
106         }
107         $conn->send($out);
108 }
109
110 function jabber_broadcast_notice($notice) {
111         # First, get users subscribed to this profile
112         # XXX: use a join here rather than looping through results
113         $profile = Profile::staticGet($notice->profile_id);
114         if (!$profile) {
115                 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
116                            'unknown profile ' . common_log_objstring($notice),
117                            __FILE__);
118                 return false;
119         }
120         $sub = new Subscription();
121         $sub->subscribed = $notice->profile_id;
122         if ($sub->find()) {
123                 $msg = jabber_format_notice($profile, $notice);
124                 while ($sub->fetch()) {
125                         $user = User::staticGet($sub->subscriber);
126                         if ($user && $user->jabber) {
127                                 jabber_send_message($user->jabber,
128                                                     $msg);
129                         }
130                 }
131         }
132 }
133
134 function jabber_format_notice(&$profile, &$notice) {
135         return $profile->nickname . ': ' . $notice->content;
136 }