# in jabber.php, which create a new XMPP class. A more elegant (?) solution
# might be to use make this a subclass of XMPP.
-class XMPPDaemon extends Daemon {
+class XMPPDaemon extends Daemon
+{
- function XMPPDaemon($resource=null) {
+ function XMPPDaemon($resource=null)
+ {
static $attrs = array('server', 'port', 'user', 'password', 'host');
foreach ($attrs as $attr)
$this->log(LOG_INFO, "INITIALIZE XMPPDaemon {$this->user}@{$this->server}/{$this->resource}");
}
- function connect() {
+ function connect()
+ {
$connect_to = ($this->host) ? $this->host : $this->server;
return !$this->conn->isDisconnected();
}
- function name() {
+ function name()
+ {
return strtolower('xmppdaemon.'.$this->resource);
}
- function run() {
+ function run()
+ {
if ($this->connect()) {
$this->conn->addEventHandler('message', 'handle_message', $this);
}
}
- function handle_reconnect(&$pl) {
+ function handle_reconnect(&$pl)
+ {
$this->conn->processUntil('session_start');
$this->conn->presence('Send me a message to post a notice', 'available', null, 'available', 100);
}
- function get_user($from) {
+ function get_user($from)
+ {
$user = User::staticGet('jabber', jabber_normalize_jid($from));
return $user;
}
- function handle_message(&$pl) {
+ function handle_message(&$pl)
+ {
if ($pl['type'] != 'chat') {
return;
}
$body = preg_replace('/d[\ ]*('. $to .')[\ ]*/', '', $pl['body']);
$this->add_direct($user, $body, $to, $from);
} else {
- $len = mb_strlen($pl['body']);
- if($len > 140) {
- $this->from_site($from, 'Message too long - maximum is 140 characters, you sent ' . $len);
- return;
- }
$this->add_notice($user, $pl);
}
unset($user);
}
- function is_self($from) {
+ function is_self($from)
+ {
return preg_match('/^'.strtolower(jabber_daemon_address()).'/', strtolower($from));
}
- function get_ofrom($pl) {
+ function get_ofrom($pl)
+ {
$xml = $pl['xml'];
$addresses = $xml->sub('addresses');
if (!$addresses) {
return $jid;
}
- function is_autoreply($txt) {
+ function is_autoreply($txt)
+ {
if (preg_match('/[\[\(]?[Aa]uto[-\s]?[Rr]e(ply|sponse)[\]\)]/', $txt)) {
return true;
+ } else if (preg_match('/^System: Message wasn\'t delivered. Offline storage size was exceeded.$/', $txt)) {
+ return true;
} else {
return false;
}
}
- function is_otr($txt) {
+ function is_otr($txt)
+ {
if (preg_match('/^\?OTR/', $txt)) {
return true;
} else {
}
}
- function is_direct($txt) {
+ function is_direct($txt)
+ {
if (strtolower(substr($txt, 0, 2))=='d ') {
return true;
} else {
}
}
- function from_site($address, $msg) {
+ function from_site($address, $msg)
+ {
$text = '['.common_config('site', 'name') . '] ' . $msg;
jabber_send_message($address, $text);
}
- function handle_command($user, $body) {
+ function handle_command($user, $body)
+ {
$inter = new CommandInterpreter();
$cmd = $inter->handle_command($user, $body);
if ($cmd) {
}
}
- function add_notice(&$user, &$pl) {
+ function add_notice(&$user, &$pl)
+ {
$body = trim($pl['body']);
- $content_shortened = common_shorten_link($body);
+ $content_shortened = common_shorten_links($body);
if (mb_strlen($content_shortened) > 140) {
- $content = trim(mb_substr($body, 0, 140));
- $content_shortened = common_shorten_link($content);
- }
- else {
- $content = $body;
+ $from = jabber_normalize_jid($pl['from']);
+ $this->from_site($from, "Message too long - maximum is 140 characters, you sent ".mb_strlen($content_shortened));
+ return;
}
- $notice = Notice::saveNew($user->id, $content, 'xmpp');
+ $notice = Notice::saveNew($user->id, $content_shortened, 'xmpp');
if (is_string($notice)) {
$this->log(LOG_ERR, $notice);
return;
unset($notice);
}
- function handle_presence(&$pl) {
+ function handle_presence(&$pl)
+ {
$from = jabber_normalize_jid($pl['from']);
switch ($pl['type']) {
case 'subscribe':
}
}
- function log($level, $msg) {
+ function log($level, $msg)
+ {
common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
}
- function subscribed($to) {
+ function subscribed($to)
+ {
jabber_special_presence('subscribed', $to);
}
}
+// Abort immediately if xmpp is not enabled, otherwise the daemon chews up
+// lots of CPU trying to connect to unconfigured servers
+if (common_config('xmpp','enabled')==false) {
+ print "Aborting daemon - xmpp is disabled\n";
+ exit();
+}
+
ini_set("max_execution_time", "0");
ini_set("max_input_time", "0");
set_time_limit(0);