XMLStream($host, $port); $this->user = $user; $this->password = $password; $this->resource = $resource; if(!$server) $server = $host; $this->stream_start = '\n'; $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'); } function message_handler($xml) { print "Message: {$xml->sub('body')->data}\n"; } function presence_handler($xml) { } function features_handler($xml) { if($xml->hassub('starttls')) { $this->send(""); } elseif($xml->hassub('bind')) { $id = $this->getId(); print "ok, we can bind $id\n"; $this->addIdHandler($id, 'resource_bind_handler'); $this->send("{$this->resource}"); } else { print "Attempting Auth...\n"; $this->send("" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . ""); } } function sasl_success_handler($xml) { print "Auth success!\n"; $this->reset(); } function sasl_failure_handler($xml) { print "Auth failed!\n"; } function resource_bind_handler($xml) { if($xml->attrs['type'] == 'result') print "Bound to " . $xml->sub('bind')->sub('jid')->data . "\n"; $id = $this->getId(); $this->addIdHandler($id, 'session_start_handler'); $this->send(""); } function session_start_handler($xml) { print "session started\n"; $this->event('session_start'); } function tls_proceed_handler($xml) { print "Starting TLS connection\n"; stream_socket_enable_crypto($this->socket, True, STREAM_CRYPTO_METHOD_TLS_CLIENT); print stream_socket_get_name($this->socket, True) . "\n"; $this->reset(); } } ?>