]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - xmppdaemon.php
change host to port
[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                 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                 $user = User::staticGet('jabber', $pl['from']);
77                 if (!$user) {
78                         $this->log(LOG_WARNING, 'Message from unknown user ' . $pl['from']);
79                         return;
80                 }
81                 if ($this->handle_command($user, $pl['body'])) {
82                         return;
83                 } else {
84                         $this->add_notice($user, $pl);
85                 }
86         }
87
88         function handle_command($user, $body) {
89                 # XXX: localise
90                 switch(trim($body)) {
91                  case 'on':
92                         $this->set_notify($user, true);
93                         return true;
94                  case 'off':
95                         $this->set_notify($user, false);
96                         return true;
97                  default:
98                         return false;
99                 }
100         }
101
102         function set_notify(&$user, $notify) {
103                 $orig = clone($user);
104                 $user->jabbernotify = $notify;
105                 $result = $user->update($orig);
106                 if (!$id) {
107                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
108                         $this->log(LOG_ERROR,
109                                            'Could not set notify flag to ' . $notify .
110                                            ' for user ' . common_log_objstring($user) .
111                                            ': ' . $last_error->message);
112                 } else {
113                         $this->log(LOG_INFO,
114                                            'User ' . $user->nickname . ' set notify flag to ' . $notify);
115                 }
116         }
117
118         function add_notice(&$user, &$pl) {
119                 $notice = new Notice();
120                 $notice->profile_id = $user->id;
121                 $notice->content = trim(substr($pl['body'], 0, 140));
122                 $notice->created = DB_DataObject_Cast::dateTime();
123                 $notice->query('BEGIN');
124                 $id = $notice->insert();
125                 if (!$id) {
126                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
127                         $this->log(LOG_ERROR,
128                                            'Could not insert ' . common_log_objstring($notice) .
129                                            ' for user ' . common_log_objstring($user) .
130                                            ': ' . $last_error->message);
131                         return;
132                 }
133                 $orig = clone($notice);
134                 $notice->uri = common_notice_uri($notice);
135                 $result = $notice->update($orig);
136                 if (!$result) {
137                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
138                         $this->log(LOG_ERROR,
139                                            'Could not add URI to ' . common_log_objstring($notice) .
140                                            ' for user ' . common_log_objstring($user) .
141                                            ': ' . $last_error->message);
142                         return;
143                 }
144                 common_broadcast_notice($notice);
145                 $this->log(LOG_INFO,
146                                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
147         }
148
149         function handle_presence(&$pl) {
150                 $user = User::staticGet('jabber', $pl['from']);
151                 if (!$user) {
152                         $this->log(LOG_WARNING, 'Message from unknown user ' . $pl['from']);
153                         return;
154                 }
155                 if ($user->updatefrompresence) {
156                         $this->add_notice($user, $pl);
157                 }
158         }
159
160         function handle_session(&$pl) {
161                 $conn->presence($status="Send me a message to post a notice");
162         }
163
164         function log($level, $msg) {
165                 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
166         }
167 }
168
169 $daemon = new XMPPDaemon();
170
171 if ($daemon->connect()) {
172         $daemon->handle();
173 }
174 ?>