]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - xmppdaemon.php
add flags to db for jabber and sms notification
[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 set_notify(&$user, $notify) {
93                 $orig = clone($user);
94                 $user->jabbernotify = $notify;
95                 $result = $user->update($orig);
96                 if (!$id) {
97                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
98                         $this->log(LOG_ERROR, 
99                                            'Could not set notify flag to ' . $notify .
100                                            ' for user ' . common_log_objstring($user) . 
101                                            ': ' . $last_error->message);
102                 } else {
103                         $this->log(LOG_INFO,
104                                            'User ' . $user->nickname . ' set notify flag to ' . $notify);
105                 }
106         }
107         
108         function add_notice(&$user, &$pl) {
109                 $notice = new Notice();
110                 $notice->profile_id = $user->id;
111                 $notice->content = trim(substr($pl['body'], 0, 140));
112                 $notice->created = DB_DataObject_Cast::dateTime();
113                 $notice->query('BEGIN');
114                 $id = $notice->insert();
115                 if (!$id) {
116                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
117                         $this->log(LOG_ERROR, 
118                                            'Could not insert ' . common_log_objstring($notice) . 
119                                            ' for user ' . common_log_objstring($user) . 
120                                            ': ' . $last_error->message);
121                         return;
122                 }
123                 $orig = clone($notice);
124                 $notice->uri = common_notice_uri($notice);
125                 $result = $notice->update($orig);
126                 if (!$result) {
127                         $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
128                         $this->log(LOG_ERROR, 
129                                            'Could not add URI to ' . common_log_objstring($notice) . 
130                                            ' for user ' . common_log_objstring($user) . 
131                                            ': ' . $last_error->message);
132                         return;
133                 }
134                 common_broadcast_notice($notice);
135                 $this->log(LOG_INFO,
136                                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);
137         }
138         
139         function handle_presence(&$pl) {
140                 $user = User::staticGet('jabber', $pl['from']);
141                 if (!$user) {
142                         $this->log(LOG_WARNING, 'Message from unknown user ' . $pl['from']);
143                         return;
144                 }
145                 if ($user->updatefrompresence) {
146                         $this->add_notice($user, $pl);
147                 }
148         }
149         
150         function handle_session(&$pl) {
151                 $conn->presence($status="Send me a message to post a notice");
152         }
153         
154         function log($level, $msg) {
155                 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
156         }
157 }
158
159
160 ?>