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