]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
bf0d7cf3166b9c88e9595dc0b54f1f5d64837514
[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         preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches);
31         $node = $matches[1];
32         $server = $matches[2];
33         $resource = $matches[3];
34         return strtolower($node.'@'.$server);
35 }
36
37 function jabber_connect($resource=NULL) {
38         static $conn = NULL;
39         if (!$conn) {
40                 $conn = new XMPP(common_config('xmpp', 'server'),
41                                              common_config('xmpp', 'port'),
42                                              common_config('xmpp', 'user'),
43                                              common_config('xmpp', 'password'),
44                                          ($resource) ? $resource : 
45                                                 common_config('xmpp', 'resource'));
46                                         
47                 if (!$conn) {
48                         return false;
49                 }
50                 $conn->connect(true); # try to get a persistent connection
51                 if ($conn->disconnected) {
52                         return false;
53                 }
54         $conn->processUntil('session_start');
55         }
56         return $conn;
57 }
58
59 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
60         $conn = jabber_connect();
61         if (!$conn) {
62                 return false;
63         }
64         $conn->message($to, $body, $type, $subject);
65         return true;
66 }
67
68 function jabber_send_presence($status=Null, $show='available', $to=Null) {
69         $conn = jabber_connect();
70         if (!$conn) {
71                 return false;
72         }
73         $conn->presence($status, $show, $to);
74         return true;
75 }
76
77 function jabber_confirm_address($code, $nickname, $address) {
78
79         # FIXME: do we have to request presence first?
80         
81         $body = "Hey, $nickname.";
82         $body .= "\n\n";
83         $body .= 'Someone just entered this IM address on ';
84         $body .= common_config('site', 'name') . '.';
85         $body .= "\n\n";
86         $body .= 'If it was you, and you want to confirm your entry, ';
87         $body .= 'use the URL below:';
88         $body .= "\n\n";
89         $body .= "\t".common_local_url('confirmaddress',
90                                                                    array('code' => $code));
91         $body .= "\n\n";
92         $body .= 'If not, just ignore this message.';
93         $body .= "\n\n";
94         $body .= 'Thanks for your time, ';
95         $body .= "\n";
96         $body .= common_config('site', 'name');
97         $body .= "\n";
98
99         jabber_send_message($address, $body);
100 }
101         
102 function jabber_broadcast_notice($notice) {
103         # First, get users subscribed to this profile
104         # XXX: use a join here rather than looping through results
105         $profile = Profile::staticGet($notice->profile_id);
106         if (!$profile) {
107                 common_log(LOG_WARNING, 'Refusing to broadcast notice with ' .
108                            'unknown profile ' . common_log_objstring($notice),
109                            __FILE__);
110                 return false;
111         }
112         $sub = new Subscription();
113         $sub->subscribed = $notice->profile_id;
114         if ($sub->find()) {
115                 $msg = jabber_format_notice($profile, $notice);
116                 while ($sub->fetch()) {
117                         $user = User::staticGet($sub->subscriber);
118                         if ($user && $user->jabber) {
119                                 jabber_send_message($user->jabber,
120                                                     $msg);
121                         }
122                 }
123         }
124 }
125
126 function jabber_format_notice(&$profile, &$notice) {
127         return = $profile->nickname . ': ' . $notice->content;
128 }