]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - xmppdaemon.php
dont let the daemon get called from a web server
[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('xmpp.php');
31
32 class XMPPDaemon {
33
34         function XMPPDaemon() {
35                 foreach (array('server', 'port', 'user', 'password', 'resource') as $attr) {
36                         $this->$attr = common_config('xmpp', $attr);
37                 }
38         }
39
40         function connect() {
41                 $this->conn = new XMPP($this->host, $this->port, $this->user,
42                                                            $this->password, $this->resource);
43                 if (!$this->conn) {
44                         return false;
45                 }
46                 $this->conn->connect();
47                 return !$this->conn->disconnected;
48         }
49
50         function handle() {
51                 while(!$this->conn->disconnected) {
52                         $payloads = $this->conn->processUntil(array('message', 'presence',
53                                                                                                                 'end_stream', 'session_start'));
54                         foreach($payloads as $event) {
55                                 $pl = $event[1];
56                                 switch($event[0]) {
57                                  case 'message':
58                                         $this->handle_message($pl);
59                                         break;
60                                  case 'presence':
61                                         $this->handle_presence($pl);
62                                         break;
63                                  case 'session_start':
64                                         $this->handle_session($pl);
65                                         break;
66                                 }
67                         }
68                 }
69         }
70
71         function handle_message(&$pl) {
72                 $user = User::staticGet('jabber', $pl['from']);
73                 if (!$user) {
74                         $this->log(LOG_WARNING, 'Message from unknown user ' . $pl['from']);
75                         return;
76                 }
77                 if ($this->handle_command($user, $pl['body'])) {
78                         return;
79                 } else {
80                         $this->add_notice($user, $pl);
81                 }
82         }
83
84         function handle_command($user, $body) {
85                 # XXX: localise
86                 switch(trim($body)) {
87                  case 'on':
88                         $this->set_notify($user, true);
89                         return true;
90                  case 'off':
91                         $this->set_notify($user, false);
92                         return true;
93                  default:
94                         return false;
95                 }
96         }
97
98         function set_notify(&$user, $notify) {
99                 $orig = clone($user);
100                 $user->jabbernotify = $notify;
101                 $result = $user->update($orig);
102                 if (!$id) {
103                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
104                         $this->log(LOG_ERROR,
105                                            'Could not set notify flag to ' . $notify .
106                                            ' for user ' . common_log_objstring($user) .
107                                            ': ' . $last_error->message);
108                 } else {
109                         $this->log(LOG_INFO,
110                                            'User ' . $user->nickname . ' set notify flag to ' . $notify);
111                 }
112         }
113
114         function add_notice(&$user, &$pl) {
115                 $notice = new Notice();
116                 $notice->profile_id = $user->id;
117                 $notice->content = trim(substr($pl['body'], 0, 140));
118                 $notice->created = DB_DataObject_Cast::dateTime();
119                 $notice->query('BEGIN');
120                 $id = $notice->insert();
121                 if (!$id) {
122                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
123                         $this->log(LOG_ERROR,
124                                            'Could not insert ' . common_log_objstring($notice) .
125                                            ' for user ' . common_log_objstring($user) .
126                                            ': ' . $last_error->message);
127                         return;
128                 }
129                 $orig = clone($notice);
130                 $notice->uri = common_notice_uri($notice);
131                 $result = $notice->update($orig);
132                 if (!$result) {
133                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
134                         $this->log(LOG_ERROR,
135                                            'Could not add URI to ' . common_log_objstring($notice) .
136                                            ' for user ' . common_log_objstring($user) .
137                                            ': ' . $last_error->message);
138                         return;
139                 }
140                 common_broadcast_notice($notice);
141                 $this->log(LOG_INFO,
142                                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
143         }
144
145         function handle_presence(&$pl) {
146                 $user = User::staticGet('jabber', $pl['from']);
147                 if (!$user) {
148                         $this->log(LOG_WARNING, 'Message from unknown user ' . $pl['from']);
149                         return;
150                 }
151                 if ($user->updatefrompresence) {
152                         $this->add_notice($user, $pl);
153                 }
154         }
155
156         function handle_session(&$pl) {
157                 $conn->presence($status="Send me a message to post a notice");
158         }
159
160         function log($level, $msg) {
161                 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
162         }
163 }
164
165 $daemon = new XMPPDaemon();
166
167 if ($daemon->connect()) {
168         $daemon->handle();
169 }
170 ?>