}
}
-function jabber_connect($resource=NULL, $status=NULL) {
+function jabber_connect($resource=NULL) {
static $conn = NULL;
if (!$conn) {
$conn = new XMPP(common_config('xmpp', 'host') ?
if (!$conn) {
return false;
}
- $conn->connect(true); # try to get a persistent connection
+ $conn->connect(true); # true = persistent connection
if ($conn->disconnected) {
return false;
}
- $conn->processUntil('session_start');
- if ($status) {
- $conn->presence($status);
- }
+ $conn->processUntil('session_start');
}
return $conn;
}
return true;
}
-function jabber_send_presence($status=Null, $show='available', $to=Null) {
+function jabber_send_presence($status, $show='available', $to=Null) {
$conn = jabber_connect();
if (!$conn) {
return false;
+#!/usr/bin/env php
<?php
/*
* Laconica - a distributed open-source microblogging tool
class XMPPDaemon {
function XMPPDaemon($resource=NULL) {
- static $attrs = array('server', 'port', 'user', 'password',
- 'resource', 'host');
+ static $attrs = array('server', 'port', 'user', 'password', 'host');
foreach ($attrs as $attr)
{
if ($resource) {
$this->resource = $resource;
+ } else {
+ $this->resource = common_config('xmpp', 'resource') . 'daemon';
}
+
+ $this->log(LOG_INFO, "{$this->user}@{$this->server}/{$this->resource}");
}
function connect() {
- $this->conn = jabber_connect($this->resource,
- "Send me a message to post a notice");
+ $connect_to = ($this->host) ? $this->host : $this->server;
+
+ $this->log(LOG_INFO, "Connecting to $connect_to on port $this->port");
+
+ $this->conn = jabber_connect($this->resource);
+
if (!$this->conn) {
return false;
}
}
function subscribed($to) {
- $this->special_presence('subscribed', $to);
+ jabber_special_presence('subscribed', $to);
}
- function special_presence($type, $to=NULL, $show=NULL, $status=NULL) {
- $to = htmlspecialchars($to);
- $status = htmlspecialchars($status);
- $out = "<presence";
- if($to) $out .= " to='$to'";
- if($type) $out .= " type='$type'";
- if($show == 'available' and !$status) {
- $out .= "/>";
- } else {
- $out .= ">";
- if($show && ($show != 'available')) $out .= "<show>$show</show>";
- if($status) $out .= "<status>$status</status>";
- $out .= "</presence>";
- }
- $this->conn->send($out);
+ function set_status($status) {
+ jabber_send_presence($status);
}
}
-$daemon = new XMPPDaemon();
+$resource = ($argc > 1) ? $argv[1] : NULL;
+
+$daemon = new XMPPDaemon($resource);
if ($daemon->connect()) {
+ $daemon->set_status("Send me a message to post a notice");
$daemon->handle();
}
?>