XMLStream($host, $port, $printlog, $loglevel);
$this->user = $user;
$this->password = $password;
$this->resource = $resource;
if(!$server) $server = $host;
$this->stream_start = '';
$this->stream_end = '';
$this->addHandler('features', 'http://etherx.jabber.org/streams', 'features_handler');
$this->addHandler('success', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_success_handler');
$this->addHandler('failure', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_failure_handler');
$this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler');
$this->default_ns = 'jabber:client';
$this->addHandler('message', 'jabber:client', 'message_handler');
$this->addHandler('presence', 'jabber:client', 'presence_handler');
$this->authed = False;
}
function message_handler($xml) {
$payload['type'] = $xml->attrs['type'];
if(!$paytload['type']) $payload['type'] = 'chat';
$payload['from'] = $xml->attrs['from'];
$payload['body'] = $xml->sub('body')->data;
$this->log->log("Message: {$xml->sub('body')->data}", LOGGING_DEBUG);
$this->event('message', $payload);
}
function message($to, $body, $type='chat', $subject=Null) {
$to = htmlspecialchars($to);
$body = htmlspecialchars($body);
$subject = htmlspecialchars($subject);
$out = "";
if($subject) $out .= "$subject";
$out .= "$body";
$this->send($out);
}
function presence($status=Null, $show='available', $to=Null) {
$to = htmlspecialchars($to);
$status = htmlspecialchars($status);
if($show == 'unavailable') $type = 'unavailable';
$out = "send($out);
}
function presence_handler($xml) {
$payload['type'] = $xml->attrs['type'];
if(!$payload['type']) $payload['type'] = 'available';
$payload['show'] = $xml->sub('show')->data;
if(!$payload['show']) $payload['show'] = $payload['type'];
$payload['from'] = $xml->attrs['from'];
$payload['status'] = $xml->sub('status')->data;
$this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", LOGGING_DEBUG);
$this->event('presence', $payload);
}
function features_handler($xml) {
if($xml->hassub('starttls')) {
$this->send("");
} elseif($xml->hassub('bind')) {
$id = $this->getId();
$this->addIdHandler($id, 'resource_bind_handler');
$this->send("{$this->resource}");
} else {
$this->log->log("Attempting Auth...");
$this->send("" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . "");
}
}
function sasl_success_handler($xml) {
$this->log->log("Auth success!");
$this->authed = True;
$this->reset();
}
function sasl_failure_handler($xml) {
$this->log->log("Auth failed!", LOGGING_ERROR);
$this->disconnect();
}
function resource_bind_handler($xml) {
if($xml->attrs['type'] == 'result') {
$this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data);
$this->fulljid = $xml->sub('bind')->sub('jid')->data;
}
$id = $this->getId();
$this->addIdHandler($id, 'session_start_handler');
$this->send("");
}
function session_start_handler($xml) {
$this->log->log("Session started");
$this->event('session_start');
}
function tls_proceed_handler($xml) {
$this->log->log("Starting TLS encryption");
stream_socket_enable_crypto($this->socket, True, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
$this->reset();
}
}
?>