]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - xmppdaemon.php
commit queries in xmppdaemon::add_notice
[quix0rs-gnu-social.git] / xmppdaemon.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 # Abort if called from a web server
21 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
22         print "This script must be run from the command line\n";
23         exit();
24 }
25
26 define('INSTALLDIR', dirname(__FILE__));
27 define('LACONICA', true);
28
29 require_once(INSTALLDIR . '/lib/common.php');
30 require_once(INSTALLDIR . '/lib/jabber.php');
31
32 class XMPPDaemon {
33
34         function XMPPDaemon() {
35                 static $attrs = array('server', 'port', 'user', 'password',
36                                            'resource');
37
38                 foreach ($attrs as $attr)
39                 {
40                         $this->$attr = common_config('xmpp', $attr);
41                 }
42         }
43
44         function connect() {
45                 $this->conn = new XMPP($this->server, $this->port, $this->user,
46                                                            $this->password, $this->resource);
47                 if (!$this->conn) {
48                         return false;
49                 }
50                 $this->conn->connect();
51                 return !$this->conn->disconnected;
52         }
53
54         function handle() {
55                 while(!$this->conn->disconnected) {
56                         $payloads = $this->conn->processUntil(array('message', 'presence',
57                                                                                                                 'end_stream', 'session_start'));
58                         foreach($payloads as $event) {
59                                 $pl = $event[1];
60                                 switch($event[0]) {
61                                  case 'message':
62                                         $this->handle_message($pl);
63                                         break;
64                                  case 'presence':
65                                         $this->handle_presence($pl);
66                                         break;
67                                  case 'session_start':
68                                         $this->handle_session($pl);
69                                         break;
70                                 }
71                         }
72                 }
73         }
74
75         function handle_message(&$pl) {
76                 if ($pl['type'] != 'chat') {
77                         return;
78                 }
79                 if (strlen($pl['body']) == 0) {
80                         return;
81                 }
82                 $from = jabber_normalize_jid($pl['from']);
83                 $user = User::staticGet('jabber', $from);
84                 if (!$user) {
85                         $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
86                         return;
87                 }
88                 if ($this->handle_command($user, $pl['body'])) {
89                         return;
90                 } else {
91                         $this->add_notice($user, $pl);
92                 }
93         }
94
95         function handle_command($user, $body) {
96                 # XXX: localise
97                 switch(trim($body)) {
98                  case 'on':
99                         $this->set_notify($user, true);
100                         return true;
101                  case 'off':
102                         $this->set_notify($user, false);
103                         return true;
104                  default:
105                         return false;
106                 }
107         }
108
109         function set_notify(&$user, $notify) {
110                 $orig = clone($user);
111                 $user->jabbernotify = $notify;
112                 $result = $user->update($orig);
113                 if (!$id) {
114                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
115                         $this->log(LOG_ERROR,
116                                            'Could not set notify flag to ' . $notify .
117                                            ' for user ' . common_log_objstring($user) .
118                                            ': ' . $last_error->message);
119                 } else {
120                         $this->log(LOG_INFO,
121                                            'User ' . $user->nickname . ' set notify flag to ' . $notify);
122                 }
123         }
124
125         function add_notice(&$user, &$pl) {
126                 $notice = new Notice();
127                 $notice->profile_id = $user->id;
128                 $notice->content = trim(substr($pl['body'], 0, 140));
129                 $notice->created = DB_DataObject_Cast::dateTime();
130                 $notice->query('BEGIN');
131                 $id = $notice->insert();
132                 if (!$id) {
133                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
134                         $this->log(LOG_ERROR,
135                                            'Could not insert ' . common_log_objstring($notice) .
136                                            ' for user ' . common_log_objstring($user) .
137                                            ': ' . $last_error->message);
138                         return;
139                 }
140                 $orig = clone($notice);
141                 $notice->uri = common_notice_uri($notice);
142                 $result = $notice->update($orig);
143                 if (!$result) {
144                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
145                         $this->log(LOG_ERROR,
146                                            'Could not add URI to ' . common_log_objstring($notice) .
147                                            ' for user ' . common_log_objstring($user) .
148                                            ': ' . $last_error->message);
149                         return;
150                 }
151                 $notice->query('COMMIT');
152                 common_broadcast_notice($notice);
153                 $this->log(LOG_INFO,
154                                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
155         }
156
157         function handle_presence(&$pl) {
158                 $from = jabber_normalize_jid($pl['from']);
159                 switch ($pl['type']) {
160                         case 'subscribe':
161                             # We let anyone subscribe
162                                 $this->subscribed($from);
163                                 break;
164                         case 'subscribed':
165                             # Are we trying to confirm this address?
166                 $confirm = Confirm_address::staticGet('address', $from);
167                 if ($confirm) {
168                                         $this->send_confirmation_code($from, $confirm);
169                                 }
170                                 # Otherwise, silently ignore
171                                 break;
172                         case 'unsubscribe':
173                         case 'unsubscribed':
174                                 # XXX: do we care?
175                                 break;
176                         default:
177                                 if (!$pl['type']) {
178                                         $user = User::staticGet('jabber', $from);
179                                         if (!$user) {
180                                                 $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
181                                                 return;
182                                         }
183                                         if ($user->updatefrompresence) {
184                                                 $this->add_notice($user, $pl);
185                                         }
186                                 }
187                                 break;
188                 }
189         }
190
191         function handle_session(&$pl) {
192                 $this->conn->presence($status="Send me a message to post a notice");
193         }
194
195         function log($level, $msg) {
196                 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
197         }
198
199         function subscribed($to) {
200                 $this->special_presence('subscribed', $to);
201         }
202
203         function special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
204                 $to = htmlspecialchars($to);
205                 $status = htmlspecialchars($status);
206                 $out = "<presence";
207                 if($to) $out .= " to='$to'";
208                 if($type) $out .= " type='$type'";
209                 if($show == 'available' and !$status) {
210                         $out .= "/>";
211                 } else {
212                         $out .= ">";
213                         if($show && ($show != 'available')) $out .= "<show>$show</show>";
214                         if($status) $out .= "<status>$status</status>";
215                         $out .= "</presence>";
216                 }
217                 $this->conn->send($out);
218         }
219
220         function send_confirmation_code($to, &$confirm) {
221                 $body = 'Someone has asked to add this Jabber ID to their ' .
222                         'account on ' . common_config('site', 'name') . '. ' .
223                         'If it was you, you can confirm by clicking on this URL: ' .
224                         common_local_url('confirmaddress', array('code' => $confirm->code)) .
225                         ' . (If you cannot click it, copy-and-paste it into the ' .
226                         'address bar of your browser). If it wasn\'t you, ' .
227                         'just ignore this message.';
228
229                 $this->conn->message($to, $body);
230         }
231 }
232
233 $daemon = new XMPPDaemon();
234
235 if ($daemon->connect()) {
236         $daemon->handle();
237 }
238 ?>