]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jabber.php
262fbcbb907321dc5e81b316a4e3408dfc532f7d
[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 function jabber_valid_base_jid($jid) {
23         # Cheap but effective
24         return Validate::email($jid);
25 }
26
27 function jabber_normalize_jid($jid) {
28         preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches);
29         $node = $matches[1];
30         $server = $matches[2];
31         $resource = $matches[3];
32         return strtolower($node.'@'.$server);
33 }
34
35 function jabber_connect($resource=NULL) {
36         static $conn = NULL;
37         if (!$conn) {
38                 $conn = new XMPP(common_config('xmpp', 'server'),
39                                              common_config('xmpp', 'port'),
40                                              common_config('xmpp', 'user'),
41                                              common_config('xmpp', 'password'),
42                                          ($resource) ? $resource : 
43                                                 common_config('xmpp', 'resource'));
44                                         
45                 if (!$conn) {
46                         return false;
47                 }
48                 $conn->connect(true); # try to get a persistent connection
49                 if ($conn->disconnected) {
50                         return false;
51                 }
52         $conn->processUntil('session_start');
53         }
54         return $conn;
55 }
56
57 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
58         $conn = jabber_connect();
59         if (!$conn) {
60                 return false;
61         }
62         $conn->message($to, $body, $type, $subject);
63         return true;
64 }
65
66 function jabber_send_presence($status=Null, $show='available', $to=Null) {
67         $conn = jabber_connect();
68         if (!$conn) {
69                 return false;
70         }
71         $conn->presence($status, $show, $to);
72         return true;
73 }
74
75 function jabber_confirm_address($code, $nickname, $address) {
76
77         # FIXME: do we have to request presence first?
78         
79         $body = "Hey, $nickname.";
80         $body .= "\n\n";
81         $body .= 'Someone just entered this IM address on ';
82         $body .= common_config('site', 'name') . '.';
83         $body .= "\n\n";
84         $body .= 'If it was you, and you want to confirm your entry, ';
85         $body .= 'use the URL below:';
86         $body .= "\n\n";
87         $body .= "\t".common_local_url('confirmaddress',
88                                                                    array('code' => $code));
89         $body .= "\n\n";
90         $body .= 'If not, just ignore this message.';
91         $body .= "\n\n";
92         $body .= 'Thanks for your time, ';
93         $body .= "\n";
94         $body .= common_config('site', 'name');
95         $body .= "\n";
96
97         jabber_send_message($address, $body);
98 }
99