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