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 foreach (array('server', 'port', 'user', 'password', 'resource') as $attr) {
36 $this->$attr = common_config('xmpp', $attr);
41 $this->conn = new XMPP($this->host, $this->port, $this->user,
42 $this->password, $this->resource);
46 $this->conn->connect();
47 return !$this->conn->disconnected;
51 while(!$this->conn->disconnected) {
52 $payloads = $this->conn->processUntil(array('message', 'presence',
53 'end_stream', 'session_start'));
54 foreach($payloads as $event) {
58 $this->handle_message($pl);
61 $this->handle_presence($pl);
64 $this->handle_session($pl);
71 function handle_message(&$pl) {
72 $user = User::staticGet('jabber', $pl['from']);
74 $this->log(LOG_WARNING, 'Message from unknown user ' . $pl['from']);
77 if ($this->handle_command($user, $pl['body'])) {
80 $this->add_notice($user, $pl);
84 function handle_command($user, $body) {
88 $this->set_notify($user, true);
91 $this->set_notify($user, false);
98 function set_notify(&$user, $notify) {
100 $user->jabbernotify = $notify;
101 $result = $user->update($orig);
103 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
104 $this->log(LOG_ERROR,
105 'Could not set notify flag to ' . $notify .
106 ' for user ' . common_log_objstring($user) .
107 ': ' . $last_error->message);
110 'User ' . $user->nickname . ' set notify flag to ' . $notify);
114 function add_notice(&$user, &$pl) {
115 $notice = new Notice();
116 $notice->profile_id = $user->id;
117 $notice->content = trim(substr($pl['body'], 0, 140));
118 $notice->created = DB_DataObject_Cast::dateTime();
119 $notice->query('BEGIN');
120 $id = $notice->insert();
122 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
123 $this->log(LOG_ERROR,
124 'Could not insert ' . common_log_objstring($notice) .
125 ' for user ' . common_log_objstring($user) .
126 ': ' . $last_error->message);
129 $orig = clone($notice);
130 $notice->uri = common_notice_uri($notice);
131 $result = $notice->update($orig);
133 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
134 $this->log(LOG_ERROR,
135 'Could not add URI to ' . common_log_objstring($notice) .
136 ' for user ' . common_log_objstring($user) .
137 ': ' . $last_error->message);
140 common_broadcast_notice($notice);
142 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
145 function handle_presence(&$pl) {
146 $user = User::staticGet('jabber', $pl['from']);
148 $this->log(LOG_WARNING, 'Message from unknown user ' . $pl['from']);
151 if ($user->updatefrompresence) {
152 $this->add_notice($user, $pl);
156 function handle_session(&$pl) {
157 $conn->presence($status="Send me a message to post a notice");
160 function log($level, $msg) {
161 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
165 $daemon = new XMPPDaemon();
167 if ($daemon->connect()) {