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");
55 return !$this->conn->disconnected;
59 while(!$this->conn->disconnected) {
60 $payloads = $this->conn->processUntil(array('message', 'presence',
61 'end_stream', 'session_start'));
62 foreach($payloads as $event) {
66 $this->handle_message($pl);
69 $this->handle_presence($pl);
72 $this->handle_session($pl);
79 function handle_message(&$pl) {
80 if ($pl['type'] != 'chat') {
83 if (strlen($pl['body']) == 0) {
86 $from = jabber_normalize_jid($pl['from']);
87 $user = User::staticGet('jabber', $from);
89 $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
92 if ($this->handle_command($user, $pl['body'])) {
95 $this->add_notice($user, $pl);
99 function handle_command($user, $body) {
101 switch(trim($body)) {
103 $this->set_notify($user, true);
106 $this->set_notify($user, false);
113 function set_notify(&$user, $notify) {
114 $orig = clone($user);
115 $user->jabbernotify = $notify;
116 $result = $user->update($orig);
118 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
119 $this->log(LOG_ERROR,
120 'Could not set notify flag to ' . $notify .
121 ' for user ' . common_log_objstring($user) .
122 ': ' . $last_error->message);
125 'User ' . $user->nickname . ' set notify flag to ' . $notify);
129 function add_notice(&$user, &$pl) {
130 $notice = new Notice();
131 $notice->profile_id = $user->id;
132 $notice->content = trim(substr($pl['body'], 0, 140));
133 $notice->created = DB_DataObject_Cast::dateTime();
134 $notice->query('BEGIN');
135 $id = $notice->insert();
137 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
138 $this->log(LOG_ERROR,
139 'Could not insert ' . common_log_objstring($notice) .
140 ' for user ' . common_log_objstring($user) .
141 ': ' . $last_error->message);
144 $orig = clone($notice);
145 $notice->uri = common_notice_uri($notice);
146 $result = $notice->update($orig);
148 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
149 $this->log(LOG_ERROR,
150 'Could not add URI to ' . common_log_objstring($notice) .
151 ' for user ' . common_log_objstring($user) .
152 ': ' . $last_error->message);
155 $notice->query('COMMIT');
156 common_broadcast_notice($notice);
158 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
161 function handle_presence(&$pl) {
162 $from = jabber_normalize_jid($pl['from']);
163 switch ($pl['type']) {
165 # We let anyone subscribe
166 $this->subscribed($from);
175 $user = User::staticGet('jabber', $from);
177 $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
180 if ($user->updatefrompresence) {
181 $this->add_notice($user, $pl);
188 function log($level, $msg) {
189 common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
192 function subscribed($to) {
193 $this->special_presence('subscribed', $to);
196 function special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
197 $to = htmlspecialchars($to);
198 $status = htmlspecialchars($status);
200 if($to) $out .= " to='$to'";
201 if($type) $out .= " type='$type'";
202 if($show == 'available' and !$status) {
206 if($show && ($show != 'available')) $out .= "<show>$show</show>";
207 if($status) $out .= "<status>$status</status>";
208 $out .= "</presence>";
210 $this->conn->send($out);
214 $daemon = new XMPPDaemon();
216 if ($daemon->connect()) {