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 # 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";
26 define('INSTALLDIR', dirname(__FILE__));
27 define('LACONICA', true);
29 require_once(INSTALLDIR . '/lib/common.php');
30 require_once('xmpp.php');
34 function XMPPDaemon() {
35 static $attrs = array('server', 'port', 'user', 'password',
38 foreach ($attrs as $attr)
40 $this->$attr = common_config('xmpp', $attr);
45 $this->conn = new XMPP($this->server, $this->port, $this->user,
46 $this->password, $this->resource);
50 $this->conn->connect();
51 return !$this->conn->disconnected;
55 while(!$this->conn->disconnected) {
56 $payloads = $this->conn->processUntil(array('message', 'presence',
57 'end_stream', 'session_start'));
58 foreach($payloads as $event) {
62 $this->handle_message($pl);
65 $this->handle_presence($pl);
68 $this->handle_session($pl);
75 function handle_message(&$pl) {
76 if ($pl['type'] != 'chat') {
79 if (strlen($pl['body']) == 0) {
82 $from = jabber_normalize_jid($pl['from']);
83 $user = User::staticGet('jabber', $from);
85 $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
88 if ($this->handle_command($user, $pl['body'])) {
91 $this->add_notice($user, $pl);
95 function handle_command($user, $body) {
99 $this->set_notify($user, true);
102 $this->set_notify($user, false);
109 function set_notify(&$user, $notify) {
110 $orig = clone($user);
111 $user->jabbernotify = $notify;
112 $result = $user->update($orig);
114 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
115 $this->log(LOG_ERROR,
116 'Could not set notify flag to ' . $notify .
117 ' for user ' . common_log_objstring($user) .
118 ': ' . $last_error->message);
121 'User ' . $user->nickname . ' set notify flag to ' . $notify);
125 function add_notice(&$user, &$pl) {
126 $notice = new Notice();
127 $notice->profile_id = $user->id;
128 $notice->content = trim(substr($pl['body'], 0, 140));
129 $notice->created = DB_DataObject_Cast::dateTime();
130 $notice->query('BEGIN');
131 $id = $notice->insert();
133 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
134 $this->log(LOG_ERROR,
135 'Could not insert ' . common_log_objstring($notice) .
136 ' for user ' . common_log_objstring($user) .
137 ': ' . $last_error->message);
140 $orig = clone($notice);
141 $notice->uri = common_notice_uri($notice);
142 $result = $notice->update($orig);
144 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
145 $this->log(LOG_ERROR,
146 'Could not add URI to ' . common_log_objstring($notice) .
147 ' for user ' . common_log_objstring($user) .
148 ': ' . $last_error->message);
151 common_broadcast_notice($notice);
153 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
156 function handle_presence(&$pl) {
157 $from = jabber_normalize_jid($pl['from']);
158 $user = User::staticGet('jabber', $from);
160 $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
163 if ($user->updatefrompresence) {
164 $this->add_notice($user, $pl);
168 function handle_session(&$pl) {
169 $this->conn->presence($status="Send me a message to post a notice");
172 function log($level, $msg) {
173 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
177 $daemon = new XMPPDaemon();
179 if ($daemon->connect()) {