]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - xmppdaemon.php
add imsettings to menu
[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 normalize_jid($jid) {
55                 preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches);
56                 $node = $matches[1];
57                 $server = $matches[2];
58                 $resource = $matches[3];
59                 return strtolower($node.'@'.$server);
60         }
61
62         function handle() {
63                 while(!$this->conn->disconnected) {
64                         $payloads = $this->conn->processUntil(array('message', 'presence',
65                                                                                                                 'end_stream', 'session_start'));
66                         foreach($payloads as $event) {
67                                 $pl = $event[1];
68                                 switch($event[0]) {
69                                  case 'message':
70                                         $this->handle_message($pl);
71                                         break;
72                                  case 'presence':
73                                         $this->handle_presence($pl);
74                                         break;
75                                  case 'session_start':
76                                         $this->handle_session($pl);
77                                         break;
78                                 }
79                         }
80                 }
81         }
82
83         function handle_message(&$pl) {
84                 if ($pl['type'] != 'chat') {
85                         return;
86                 }
87                 if (strlen($pl['body']) == 0) {
88                         return;
89                 }
90                 $from = $this->normalize_jid($pl['from']);
91                 $user = User::staticGet('jabber', $from);
92                 if (!$user) {
93                         $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
94                         return;
95                 }
96                 if ($this->handle_command($user, $pl['body'])) {
97                         return;
98                 } else {
99                         $this->add_notice($user, $pl);
100                 }
101         }
102
103         function handle_command($user, $body) {
104                 # XXX: localise
105                 switch(trim($body)) {
106                  case 'on':
107                         $this->set_notify($user, true);
108                         return true;
109                  case 'off':
110                         $this->set_notify($user, false);
111                         return true;
112                  default:
113                         return false;
114                 }
115         }
116
117         function set_notify(&$user, $notify) {
118                 $orig = clone($user);
119                 $user->jabbernotify = $notify;
120                 $result = $user->update($orig);
121                 if (!$id) {
122                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
123                         $this->log(LOG_ERROR,
124                                            'Could not set notify flag to ' . $notify .
125                                            ' for user ' . common_log_objstring($user) .
126                                            ': ' . $last_error->message);
127                 } else {
128                         $this->log(LOG_INFO,
129                                            'User ' . $user->nickname . ' set notify flag to ' . $notify);
130                 }
131         }
132
133         function add_notice(&$user, &$pl) {
134                 $notice = new Notice();
135                 $notice->profile_id = $user->id;
136                 $notice->content = trim(substr($pl['body'], 0, 140));
137                 $notice->created = DB_DataObject_Cast::dateTime();
138                 $notice->query('BEGIN');
139                 $id = $notice->insert();
140                 if (!$id) {
141                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
142                         $this->log(LOG_ERROR,
143                                            'Could not insert ' . common_log_objstring($notice) .
144                                            ' for user ' . common_log_objstring($user) .
145                                            ': ' . $last_error->message);
146                         return;
147                 }
148                 $orig = clone($notice);
149                 $notice->uri = common_notice_uri($notice);
150                 $result = $notice->update($orig);
151                 if (!$result) {
152                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
153                         $this->log(LOG_ERROR,
154                                            'Could not add URI to ' . common_log_objstring($notice) .
155                                            ' for user ' . common_log_objstring($user) .
156                                            ': ' . $last_error->message);
157                         return;
158                 }
159                 common_broadcast_notice($notice);
160                 $this->log(LOG_INFO,
161                                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
162         }
163
164         function handle_presence(&$pl) {
165                 $from = $this->normalize_jid($pl['from']);
166                 $user = User::staticGet('jabber', $from);
167                 if (!$user) {
168                         $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
169                         return;
170                 }
171                 if ($user->updatefrompresence) {
172                         $this->add_notice($user, $pl);
173                 }
174         }
175
176         function handle_session(&$pl) {
177                 $this->conn->presence($status="Send me a message to post a notice");
178         }
179
180         function log($level, $msg) {
181                 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
182         }
183 }
184
185 $daemon = new XMPPDaemon();
186
187 if ($daemon->connect()) {
188         $daemon->handle();
189 }
190 ?>