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