]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - xmppdaemon.php
4e451125962104c4b3bdb1408ad876fdef39841e
[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($resource=NULL) {
35                 static $attrs = array('server', 'port', 'user', 'password',
36                                            'resource', 'host');
37
38                 foreach ($attrs as $attr)
39                 {
40                         $this->$attr = common_config('xmpp', $attr);
41                 }
42
43                 if ($resource) {
44                         $this->resource = $resource;
45                 }
46         }
47
48         function connect() {
49                 $this->conn = jabber_connect($this->resource,
50                                                                      "Send me a message to post a notice");
51                 );
52                 if (!$this->conn) {
53                         return false;
54                 }
55                 return !$this->conn->disconnected;
56         }
57
58         function handle() {
59                 while(!$this->conn->disconnected) {
60                         $payloads = $this->conn->processUntil(array('message', 'presence',
61                                                                                                                 'end_stream', 'session_start'));
62                         foreach($payloads as $event) {
63                                 $pl = $event[1];
64                                 switch($event[0]) {
65                                  case 'message':
66                                         $this->handle_message($pl);
67                                         break;
68                                  case 'presence':
69                                         $this->handle_presence($pl);
70                                         break;
71                                  case 'session_start':
72                                         $this->handle_session($pl);
73                                         break;
74                                 }
75                         }
76                 }
77         }
78
79         function handle_message(&$pl) {
80                 if ($pl['type'] != 'chat') {
81                         return;
82                 }
83                 if (strlen($pl['body']) == 0) {
84                         return;
85                 }
86                 $from = jabber_normalize_jid($pl['from']);
87                 $user = User::staticGet('jabber', $from);
88                 if (!$user) {
89                         $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
90                         return;
91                 }
92                 if ($this->handle_command($user, $pl['body'])) {
93                         return;
94                 } else {
95                         $this->add_notice($user, $pl);
96                 }
97         }
98
99         function handle_command($user, $body) {
100                 # XXX: localise
101                 switch(trim($body)) {
102                  case 'on':
103                         $this->set_notify($user, true);
104                         return true;
105                  case 'off':
106                         $this->set_notify($user, false);
107                         return true;
108                  default:
109                         return false;
110                 }
111         }
112
113         function set_notify(&$user, $notify) {
114                 $orig = clone($user);
115                 $user->jabbernotify = $notify;
116                 $result = $user->update($orig);
117                 if (!$id) {
118                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
119                         $this->log(LOG_ERROR,
120                                            'Could not set notify flag to ' . $notify .
121                                            ' for user ' . common_log_objstring($user) .
122                                            ': ' . $last_error->message);
123                 } else {
124                         $this->log(LOG_INFO,
125                                            'User ' . $user->nickname . ' set notify flag to ' . $notify);
126                 }
127         }
128
129         function add_notice(&$user, &$pl) {
130                 $notice = new Notice();
131                 $notice->profile_id = $user->id;
132                 $notice->content = trim(substr($pl['body'], 0, 140));
133                 $notice->created = DB_DataObject_Cast::dateTime();
134                 $notice->query('BEGIN');
135                 $id = $notice->insert();
136                 if (!$id) {
137                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
138                         $this->log(LOG_ERROR,
139                                            'Could not insert ' . common_log_objstring($notice) .
140                                            ' for user ' . common_log_objstring($user) .
141                                            ': ' . $last_error->message);
142                         return;
143                 }
144                 $orig = clone($notice);
145                 $notice->uri = common_notice_uri($notice);
146                 $result = $notice->update($orig);
147                 if (!$result) {
148                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
149                         $this->log(LOG_ERROR,
150                                            'Could not add URI to ' . common_log_objstring($notice) .
151                                            ' for user ' . common_log_objstring($user) .
152                                            ': ' . $last_error->message);
153                         return;
154                 }
155                 $notice->query('COMMIT');
156                 common_broadcast_notice($notice);
157                 $this->log(LOG_INFO,
158                                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
159         }
160
161         function handle_presence(&$pl) {
162                 $from = jabber_normalize_jid($pl['from']);
163                 switch ($pl['type']) {
164                         case 'subscribe':
165                             # We let anyone subscribe
166                                 $this->subscribed($from);
167                                 break;
168                         case 'subscribed':
169                         case 'unsubscribe':
170                         case 'unsubscribed':
171                                 # XXX: do we care?
172                                 break;
173                         default:
174                                 if (!$pl['type']) {
175                                         $user = User::staticGet('jabber', $from);
176                                         if (!$user) {
177                                                 $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
178                                                 return;
179                                         }
180                                         if ($user->updatefrompresence) {
181                                                 $this->add_notice($user, $pl);
182                                         }
183                                 }
184                                 break;
185                 }
186         }
187
188         function log($level, $msg) {
189                 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
190         }
191
192         function subscribed($to) {
193                 $this->special_presence('subscribed', $to);
194         }
195
196         function special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
197                 $to = htmlspecialchars($to);
198                 $status = htmlspecialchars($status);
199                 $out = "<presence";
200                 if($to) $out .= " to='$to'";
201                 if($type) $out .= " type='$type'";
202                 if($show == 'available' and !$status) {
203                         $out .= "/>";
204                 } else {
205                         $out .= ">";
206                         if($show && ($show != 'available')) $out .= "<show>$show</show>";
207                         if($status) $out .= "<status>$status</status>";
208                         $out .= "</presence>";
209                 }
210                 $this->conn->send($out);
211         }
212 }
213
214 $daemon = new XMPPDaemon();
215
216 if ($daemon->connect()) {
217         $daemon->handle();
218 }
219 ?>