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