[Pkg-javascript-commits] [ltx] 11/469: connection: break onStanza() into useFeatures()
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:00:51 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository ltx.
commit 7a1eaa5a042012f4cdc554975ef24beec063a13d
Author: Astro <astro at spaceboyz.net>
Date: Thu May 27 01:35:18 2010 +0200
connection: break onStanza() into useFeatures()
---
lib/xmpp/connection.js | 160 ++++++++++++++++++++++++++++++-------------------
1 file changed, 97 insertions(+), 63 deletions(-)
diff --git a/lib/xmpp/connection.js b/lib/xmpp/connection.js
index 3849b3a..8b477fb 100644
--- a/lib/xmpp/connection.js
+++ b/lib/xmpp/connection.js
@@ -12,11 +12,12 @@ var NS_XMPP_BIND = 'urn:ietf:params:xml:ns:xmpp-bind';
var NS_XMPP_SESSION = 'urn:ietf:params:xml:ns:xmpp-session';
var STATE_PREAUTH = 0,
- STATE_AUTH = 1,
- STATE_AUTHED = 2,
- STATE_BIND = 3,
- STATE_SESSION = 4,
- STATE_ONLINE = 5;
+ STATE_TLS = 1,
+ STATE_AUTH = 2,
+ STATE_AUTHED = 3,
+ STATE_BIND = 4,
+ STATE_SESSION = 5,
+ STATE_ONLINE = 6;
var IQID_SESSION = 'sess',
IQID_BIND = 'bind';
@@ -113,87 +114,59 @@ Connection.prototype.onData = function(data) {
Connection.prototype.onStanza = function(stanza) {
if (this.state == STATE_ONLINE) {
this.emit('stanza', this.element);
- } else if (this.state == STATE_PREAUTH &&
- stanza.name == 'stream:features') {
- if (this.allowTLS &&
- stanza.getChild('starttls', NS_XMPP_TLS)) {
- this.send(new xml.Element('starttls', { xmlns: NS_XMPP_TLS }));
- } else if (stanza.getChild('mechanisms', NS_XMPP_SASL)) {
- this.state = STATE_AUTH;
- var mechs = stanza.getChild('mechanisms', NS_XMPP_SASL).
- getChildren('mechanism', NS_XMPP_SASL).
- map(function(el) { return el.getText(); });
- var mech = selectAuthMechanism(mechs);
- if (mech) {
- mech.authzid = this.jid.bare().toString();
- mech.authcid = this.jid.user;
- mech.password = this.password;
- this.send(new xml.Element('auth',
- { xmlns: NS_XMPP_SASL,
- mechanism: mech.name
- }).t(b64.encode(mech.auth())));
- } else {
- this.emit('authFail');
- this.end();
- }
+ } else if (stanza.name == 'stream:features') {
+ this.streamFeatures = stanza;
+ this.useFeatures();
+ } else if (this.state == STATE_TLS) {
+ if (stanza.is('proceed', NS_XMPP_TLS)) {
+ this.setSecure();
+ this.addListener('secure',
+ function() {
+ this.state = STATE_PREAUTH;
+ this.startStream();
+ });
+ } else {
+ this.emit('error', 'Cannot begin TLS handshake');
+ this.end();
}
- } else if (stanza.name == 'proceed' &&
- stanza.getNS() == NS_XMPP_TLS) {
- this.setSecure();
- this.addListener('secure', this.startStream);
- } else if (this.state == STATE_AUTH &&
- stanza.getNS() == NS_XMPP_SASL) {
- if (stanza.name == 'success') {
+ } else if (this.state == STATE_AUTH) {
+ if (stanza.is('success', NS_XMPP_SASL)) {
this.state = STATE_AUTHED;
this.startStream();
} else {
this.emit('authFail');
this.end();
}
- } else if (this.state == STATE_AUTHED &&
- stanza.name == 'stream:features' &&
- stanza.getChild('bind', NS_XMPP_BIND)) {
- this.state = STATE_BIND;
- var bindEl = new xml.Element('iq',
- { type: 'set',
- id: IQID_BIND
- }).c('bind',
- { xmlns: NS_XMPP_BIND
- });
- if (this.jid.resource)
- bindEl.c('resource').t(this.jid.resource);
- this.send(bindEl.root());
} else if (this.state == STATE_BIND &&
- stanza.name == 'iq' &&
+ stanza.is('iq') &&
stanza.attrs.id == IQID_BIND) {
if (stanza.attrs.type == 'result') {
this.state = STATE_AUTHED;
+ this.did_bind = true;
+
var bindEl = stanza.getChild('bind', NS_XMPP_BIND);
if (bindEl && bindEl.getChild('jid')) {
this.jid = new JID(bindEl.getChild('jid').getText());
}
- // FIXME: move this to check stream:features again
- this.state = STATE_SESSION;
- this.send(new xml.Element('iq',
- { type: 'set',
- to: this.jid.domain,
- id: IQID_SESSION
- }).c('session',
- { xmlns: NS_XMPP_SESSION
- }));
+ /* no stream restart, but next feature */
+ this.useFeatures();
} else {
- this.emit('error', "Cannot bind resource");
+ this.emit('error', 'Cannot bind resource');
this.end();
}
} else if (this.state == STATE_SESSION &&
- stanza.name == 'iq' &&
+ stanza.is('iq') &&
stanza.attrs.id == IQID_SESSION) {
if (stanza.attrs.type == 'result') {
- this.state = STATE_ONLINE;
- this.emit('online');
+ this.state = STATE_AUTHED;
+ this.did_session = true;
+
+ /* no stream restart, but next feature (most probably
+ we'll go online next) */
+ this.useFeatures();
} else {
- this.emit('error', "Cannot establish session");
+ this.emit('error', 'Cannot bind resource');
this.end();
}
} else if (stanza.name == 'stream:error') {
@@ -202,6 +175,67 @@ Connection.prototype.onStanza = function(stanza) {
}
};
+/**
+ * Either onStanza just received <stream:features/>, or we just
+ * enabled a feature and are looking for the next.
+ */
+Connection.prototype.useFeatures = function() {
+ if (this.allowTLS &&
+ this.state == STATE_PREAUTH &&
+ this.streamFeatures.getChild('starttls', NS_XMPP_TLS)) {
+ this.state = STATE_TLS;
+ this.send(new xml.Element('starttls', { xmlns: NS_XMPP_TLS }));
+ } else if (this.state == STATE_PREAUTH &&
+ this.streamFeatures.getChild('mechanisms', NS_XMPP_SASL)) {
+ this.state = STATE_AUTH;
+ var mech = selectAuthMechanism(this.streamFeatures.
+ getChild('mechanisms', NS_XMPP_SASL).
+ getChildren('mechanism', NS_XMPP_SASL).
+ map(function(el) { return el.getText(); }));
+ if (mech) {
+ mech.authzid = this.jid.bare().toString();
+ mech.authcid = this.jid.user;
+ mech.password = this.password;
+ this.send(new xml.Element('auth',
+ { xmlns: NS_XMPP_SASL,
+ mechanism: mech.name
+ }).t(b64.encode(mech.auth())));
+ } else {
+ this.emit('authFail');
+ this.end();
+ }
+ } else if (this.state == STATE_AUTHED &&
+ !this.did_bind &&
+ this.streamFeatures.getChild('bind', NS_XMPP_BIND)) {
+ this.state = STATE_BIND;
+ var bindEl = new xml.Element('iq',
+ { type: 'set',
+ id: IQID_BIND
+ }).c('bind',
+ { xmlns: NS_XMPP_BIND
+ });
+ if (this.jid.resource)
+ bindEl.c('resource').t(this.jid.resource);
+ this.send(bindEl);
+ } else if (this.state == STATE_AUTHED &&
+ !this.did_session &&
+ this.streamFeatures.getChild('session', NS_XMPP_SESSION)) {
+ this.state = STATE_SESSION;
+ this.send(new xml.Element('iq',
+ { type: 'set',
+ to: this.jid.domain,
+ id: IQID_SESSION
+ }).c('session',
+ { xmlns: NS_XMPP_SESSION
+ }));
+ } else if (this.state == STATE_AUTHED) {
+ /* Ok, we're authenticated and all features have been
+ processed */
+ this.state = STATE_ONLINE;
+ this.emit('online');
+ }
+};
+
function selectAuthMechanism(mechs) {
/*if (mechs.indexOf("DIGEST-MD5") >= 0)
return "DIGEST-MD5";
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git
More information about the Pkg-javascript-commits
mailing list