3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 define('INSTALLDIR', dirname(__FILE__));
21 define('LACONICA', true);
23 require_once(INSTALLDIR . '/lib/common.php');
24 require_once('xmpp.php');
28 function XMPPDaemon() {
29 foreach (array('server', 'port', 'user', 'password', 'resource') as $attr) {
30 $this->$attr = common_config('xmpp', $attr);
35 $this->conn = new XMPP($this->host, $this->port, $this->user,
36 $this->password, $this->resource);
40 $this->conn->connect();
41 return !$this->conn->disconnected;
45 while(!$this->conn->disconnected) {
46 $payloads = $this->conn->processUntil(array('message', 'presence',
47 'end_stream', 'session_start'));
48 foreach($payloads as $event) {
52 $this->handle_message($pl);
55 $this->handle_presence($pl);
58 $this->handle_session($pl);
65 function handle_message(&$pl) {
66 $user = User::staticGet('jabber', $pl['from']);
68 $this->log(LOG_WARNING, 'Message from unknown user ' . $pl['from']);
71 if ($this->handle_command($user, $pl['body'])) {
74 $this->add_notice($user, $pl);
78 function handle_command($user, $body) {
82 $this->set_notify($user, true);
85 $this->set_notify($user, false);
92 function set_notify(&$user, $notify) {
94 $user->jabbernotify = $notify;
95 $result = $user->update($orig);
97 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
99 'Could not set notify flag to ' . $notify .
100 ' for user ' . common_log_objstring($user) .
101 ': ' . $last_error->message);
104 'User ' . $user->nickname . ' set notify flag to ' . $notify);
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();
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);
123 $orig = clone($notice);
124 $notice->uri = common_notice_uri($notice);
125 $result = $notice->update($orig);
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);
134 common_broadcast_notice($notice);
136 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
139 function handle_presence(&$pl) {
140 $user = User::staticGet('jabber', $pl['from']);
142 $this->log(LOG_WARNING, 'Message from unknown user ' . $pl['from']);
145 if ($user->updatefrompresence) {
146 $this->add_notice($user, $pl);
150 function handle_session(&$pl) {
151 $conn->presence($status="Send me a message to post a notice");
154 function log($level, $msg) {
155 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);