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 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();
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);
107 $orig = clone($notice);
108 $notice->uri = common_notice_uri($notice);
109 $result = $notice->update($orig);
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);
118 common_broadcast_notice($notice);
121 function handle_presence(&$pl) {
122 $user = User::staticGet('jabber', $pl['from']);
124 $this->log(LOG_WARNING, 'Message from unknown user ' . $pl['from']);
127 if ($user->updatefrompresence) {
128 $this->add_notice($user, $pl);
132 function handle_session(&$pl) {
133 $conn->presence($status="Send me a message to post a notice");
136 function log($level, $msg) {
137 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);