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(INSTALLDIR . '/lib/jabber.php');
34 function XMPPDaemon($resource=NULL) {
35 static $attrs = array('server', 'port', 'user', 'password',
38 foreach ($attrs as $attr)
40 $this->$attr = common_config('xmpp', $attr);
44 $this->resource = $resource;
49 $this->conn = jabber_connect($this->resource,
50 "Send me a message to post a notice");
54 return !$this->conn->disconnected;
58 while(!$this->conn->disconnected) {
59 $payloads = $this->conn->processUntil(array('message', 'presence',
60 'end_stream', 'session_start'));
61 foreach($payloads as $event) {
65 $this->handle_message($pl);
68 $this->handle_presence($pl);
71 $this->handle_session($pl);
78 function handle_message(&$pl) {
79 if ($pl['type'] != 'chat') {
82 if (strlen($pl['body']) == 0) {
85 $from = jabber_normalize_jid($pl['from']);
86 $user = User::staticGet('jabber', $from);
88 $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
91 if ($this->handle_command($user, $pl['body'])) {
94 $this->add_notice($user, $pl);
98 function handle_command($user, $body) {
100 switch(trim($body)) {
102 $this->set_notify($user, true);
105 $this->set_notify($user, false);
112 function set_notify(&$user, $notify) {
113 $orig = clone($user);
114 $user->jabbernotify = $notify;
115 $result = $user->update($orig);
117 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
118 $this->log(LOG_ERROR,
119 'Could not set notify flag to ' . $notify .
120 ' for user ' . common_log_objstring($user) .
121 ': ' . $last_error->message);
124 'User ' . $user->nickname . ' set notify flag to ' . $notify);
128 function add_notice(&$user, &$pl) {
129 $notice = new Notice();
130 $notice->profile_id = $user->id;
131 $notice->content = trim(substr($pl['body'], 0, 140));
132 $notice->created = DB_DataObject_Cast::dateTime();
133 $notice->query('BEGIN');
134 $id = $notice->insert();
136 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
137 $this->log(LOG_ERROR,
138 'Could not insert ' . common_log_objstring($notice) .
139 ' for user ' . common_log_objstring($user) .
140 ': ' . $last_error->message);
143 $orig = clone($notice);
144 $notice->uri = common_notice_uri($notice);
145 $result = $notice->update($orig);
147 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
148 $this->log(LOG_ERROR,
149 'Could not add URI to ' . common_log_objstring($notice) .
150 ' for user ' . common_log_objstring($user) .
151 ': ' . $last_error->message);
154 $notice->query('COMMIT');
155 common_broadcast_notice($notice);
157 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
160 function handle_presence(&$pl) {
161 $from = jabber_normalize_jid($pl['from']);
162 switch ($pl['type']) {
164 # We let anyone subscribe
165 $this->subscribed($from);
174 $user = User::staticGet('jabber', $from);
176 $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
179 if ($user->updatefrompresence) {
180 $this->add_notice($user, $pl);
187 function log($level, $msg) {
188 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
191 function subscribed($to) {
192 $this->special_presence('subscribed', $to);
195 function special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
196 $to = htmlspecialchars($to);
197 $status = htmlspecialchars($status);
199 if($to) $out .= " to='$to'";
200 if($type) $out .= " type='$type'";
201 if($show == 'available' and !$status) {
205 if($show && ($show != 'available')) $out .= "<show>$show</show>";
206 if($status) $out .= "<status>$status</status>";
207 $out .= "</presence>";
209 $this->conn->send($out);
213 $daemon = new XMPPDaemon();
215 if ($daemon->connect()) {