use jabber.php more from xmppdaemon

darcs-hash:20080626150336-34904-bfa5ec8740ba1edf60c34e1ef6aafdfd73fc99a2.gz
This commit is contained in:
Evan Prodromou 2008-06-26 11:03:36 -04:00
parent 6910737760
commit d195c49d96
2 changed files with 23 additions and 28 deletions

View File

@ -37,7 +37,7 @@ function jabber_normalize_jid($jid) {
} }
} }
function jabber_connect($resource=NULL, $status=NULL) { function jabber_connect($resource=NULL) {
static $conn = NULL; static $conn = NULL;
if (!$conn) { if (!$conn) {
$conn = new XMPP(common_config('xmpp', 'host') ? $conn = new XMPP(common_config('xmpp', 'host') ?
@ -53,14 +53,11 @@ function jabber_connect($resource=NULL, $status=NULL) {
if (!$conn) { if (!$conn) {
return false; return false;
} }
$conn->connect(true); # try to get a persistent connection $conn->connect(true); # true = persistent connection
if ($conn->disconnected) { if ($conn->disconnected) {
return false; return false;
} }
$conn->processUntil('session_start'); $conn->processUntil('session_start');
if ($status) {
$conn->presence($status);
}
} }
return $conn; return $conn;
} }
@ -74,7 +71,7 @@ function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
return true; return true;
} }
function jabber_send_presence($status=Null, $show='available', $to=Null) { function jabber_send_presence($status, $show='available', $to=Null) {
$conn = jabber_connect(); $conn = jabber_connect();
if (!$conn) { if (!$conn) {
return false; return false;

40
xmppdaemon.php Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php <?php
/* /*
* Laconica - a distributed open-source microblogging tool * Laconica - a distributed open-source microblogging tool
@ -32,8 +33,7 @@ require_once(INSTALLDIR . '/lib/jabber.php');
class XMPPDaemon { class XMPPDaemon {
function XMPPDaemon($resource=NULL) { function XMPPDaemon($resource=NULL) {
static $attrs = array('server', 'port', 'user', 'password', static $attrs = array('server', 'port', 'user', 'password', 'host');
'resource', 'host');
foreach ($attrs as $attr) foreach ($attrs as $attr)
{ {
@ -42,12 +42,20 @@ class XMPPDaemon {
if ($resource) { if ($resource) {
$this->resource = $resource; $this->resource = $resource;
} else {
$this->resource = common_config('xmpp', 'resource') . 'daemon';
} }
$this->log(LOG_INFO, "{$this->user}@{$this->server}/{$this->resource}");
} }
function connect() { function connect() {
$this->conn = jabber_connect($this->resource, $connect_to = ($this->host) ? $this->host : $this->server;
"Send me a message to post a notice");
$this->log(LOG_INFO, "Connecting to $connect_to on port $this->port");
$this->conn = jabber_connect($this->resource);
if (!$this->conn) { if (!$this->conn) {
return false; return false;
} }
@ -189,30 +197,20 @@ class XMPPDaemon {
} }
function subscribed($to) { function subscribed($to) {
$this->special_presence('subscribed', $to); jabber_special_presence('subscribed', $to);
} }
function special_presence($type, $to=NULL, $show=NULL, $status=NULL) { function set_status($status) {
$to = htmlspecialchars($to); jabber_send_presence($status);
$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);
} }
} }
$daemon = new XMPPDaemon(); $resource = ($argc > 1) ? $argv[1] : NULL;
$daemon = new XMPPDaemon($resource);
if ($daemon->connect()) { if ($daemon->connect()) {
$daemon->set_status("Send me a message to post a notice");
$daemon->handle(); $daemon->handle();
} }
?> ?>