[Pkg-javascript-commits] [ltx] 127/469: unify startStream()
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:01:13 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 206235db1844260cbb08f5137fc352684bfbaa50
Author: Astro <astro at spaceboyz.net>
Date: Thu Sep 9 18:52:32 2010 +0200
unify startStream()
---
lib/xmpp/client.js | 18 +++---------------
lib/xmpp/component.js | 14 +-------------
lib/xmpp/connection.js | 36 +++++++++++++++++++++++++++++-------
lib/xmpp/router.js | 2 +-
lib/xmpp/server.js | 21 +++------------------
5 files changed, 37 insertions(+), 54 deletions(-)
diff --git a/lib/xmpp/client.js b/lib/xmpp/client.js
index b46de89..e762516 100644
--- a/lib/xmpp/client.js
+++ b/lib/xmpp/client.js
@@ -35,7 +35,7 @@ function Client(params) {
else
this.jid = params.jid;
this.password = params.password;
- this.xmlns = NS_CLIENT;
+ this.xmlns[''] = NS_CLIENT;
this.xmppVersion = "1.0";
this.streamTo = this.jid.domain;
this.state = STATE_PREAUTH;
@@ -48,6 +48,7 @@ function Client(params) {
var attempt = SRV.connect(this.socket,
['_xmpp-client._tcp'], this.jid.domain, 5222);
attempt.addListener('connect', function() {
+ self.startParser();
self.startStream();
});
attempt.addListener('error', function(e) {
@@ -67,19 +68,6 @@ function Client(params) {
sys.inherits(Client, Connection.Connection);
exports.Client = Client;
-Client.prototype.startStream = function() {
- Connection.Connection.prototype.startStream.call(this);
-
- var tag = "<stream:stream xmlns='" + this.xmlns +
- "' xmlns:stream='" + Connection.NS_STREAM + "'" +
- " to='" + this.streamTo + "'";
- if (this.xmppVersion)
- tag += " version='" + this.xmppVersion + "'";
- tag += ">";
-console.log(tag);
- this.send(tag);
-};
-
Client.prototype.onRawStanza = function(stanza) {
/* Actually, we shouldn't wait for <stream:features/> if
this.streamAttrs.version is missing, but who uses pre-XMPP-1.0
@@ -99,7 +87,7 @@ Client.prototype.onRawStanza = function(stanza) {
} else if (stanza.is('success', NS_XMPP_SASL)) {
this.mech = null;
this.state = STATE_AUTHED;
- this.stopParser();
+ this.startParser();
this.startStream();
} else {
this.emit('error', 'XMPP authentication failure');
diff --git a/lib/xmpp/component.js b/lib/xmpp/component.js
index 5dd20ba..b9cc381 100644
--- a/lib/xmpp/component.js
+++ b/lib/xmpp/component.js
@@ -23,7 +23,7 @@ function Component(params) {
else
this.jid = params.jid;
this.password = params.password;
- this.xmlns = NS_COMPONENT;
+ this.xmlns[''] = NS_COMPONENT;
this.streamTo = this.jid.domain;
this.addListener('streamStart', function(streamAttrs) {
@@ -50,18 +50,6 @@ Component.prototype.onStreamStart = function(streamAttrs) {
this.send(new xml.Element('handshake').t(digest));
};
-Component.prototype.startStream = function() {
- Connection.Connection.prototype.startStream.call(this);
-
- var tag = "<stream:stream xmlns='" + this.xmlns +
- "' xmlns:stream='" + Connection.NS_STREAM + "'" +
- " to='" + this.streamTo + "'";
- if (this.xmppVersion)
- tag += " version='" + this.xmppVersion + "'";
- tag += ">";
- this.send(tag);
-};
-
Component.prototype.onRawStanza = function(stanza) {
if (stanza.is('handshake', NS_COMPONENT)) {
this.emit('online');
diff --git a/lib/xmpp/connection.js b/lib/xmpp/connection.js
index 73afdf4..121c78e 100644
--- a/lib/xmpp/connection.js
+++ b/lib/xmpp/connection.js
@@ -17,6 +17,7 @@ function Connection(socket) {
EventEmitter.call(this);
this.charset = 'UTF-8';
+ this.xmlns = { stream: NS_STREAM };
this.socket = socket || new net.Stream();
this.socket.addListener('data', function(data) {
@@ -57,17 +58,14 @@ Connection.prototype.send = function(stanza) {
var socket = this.socket;
el.write(function(s) { socket.write(s); });
return el;
- }
- else {
+ } else {
+ console.log({write:stanza});
this.socket.write(stanza);
return stanza;
}
};
Connection.prototype.startParser = function() {
- if (this.parser)
- return;
-
var self = this;
this.parser = new StreamParser.StreamParser(this.charset, this.maxStanzaSize);
@@ -81,7 +79,8 @@ Connection.prototype.startParser = function() {
self.streamNsAttrs[k] = attrs[k];
}
- /* Notify in case we don't wait for <stream:features/> (Component)
+ /* Notify in case we don't wait for <stream:features/>
+ (Component or non-1.0 streams)
*/
self.emit('streamStart', attrs);
});
@@ -101,10 +100,33 @@ Connection.prototype.stopParser = function() {
};
Connection.prototype.startStream = function() {
- this.startParser();
+ var attrs = {};
+ for(var k in this.xmlns) {
+ if (this.xmlns.hasOwnProperty(k)) {
+ if (!k)
+ attrs.xmlns = this.xmlns[k];
+ else
+ attrs['xmlns:' + k] = this.xmlns[k];
+ }
+ }
+ if (this.xmppVersion)
+ attrs.version = this.xmppVersion;
+ if (this.streamTo)
+ attrs.to = this.streamTo;
+ if (this.streamId)
+ attrs.id = this.streamId;
+
+ var el = new xml.Element('stream:stream', attrs);
+ // make it non-empty to cut the closing tag
+ el.t(' ');
+ var s = el.toString();
+ console.log({startStream:s.substr(0, s.indexOf(' </stream:stream>'))});
+
+ this.send(s.substr(0, s.indexOf(' </stream:stream>')));
};
Connection.prototype.onData = function(data) {
+ console.log({data:data.toString()});
if (this.parser)
this.parser.write(data);
};
diff --git a/lib/xmpp/router.js b/lib/xmpp/router.js
index 8fac1dc..f4a4737 100644
--- a/lib/xmpp/router.js
+++ b/lib/xmpp/router.js
@@ -160,7 +160,7 @@ DomainContext.prototype.getOutStream = function(destDomain) {
onStanza = function(stanza) {
console.log({external:{domain:destDomain,stanza:stanza.toString()}});
if (stanza.is('success', NS_XMPP_SASL)) {
- outStream.stopParser();
+ outStream.startParser();
outStream.startStream();
outStream.removeListener('stanza', onStanza);
var onStream;
diff --git a/lib/xmpp/server.js b/lib/xmpp/server.js
index 61310fe..cfe1156 100644
--- a/lib/xmpp/server.js
+++ b/lib/xmpp/server.js
@@ -19,7 +19,8 @@ function Server(socket) {
var self = this;
Connection.Connection.call(this, socket);
- this.xmlns = NS_SERVER;
+ this.xmlns[''] = NS_SERVER;
+ this.xmlns['db'] = NS_DIALBACK;
this.xmppVersion = '1.0';
console.log({Server:this});
@@ -62,22 +63,6 @@ console.log({Server:this});
}
sys.inherits(Server, Connection.Connection);
-Server.prototype.startStream = function() {
- Connection.Connection.prototype.startStream.call(this);
-
- var tag = "<stream:stream xmlns='" + this.xmlns +
- "' xmlns:stream='" + Connection.NS_STREAM +
- "' xmlns:db='" + NS_DIALBACK + "'";
- if (this.destDomain)
- tag += " to='" + this.destDomain + "'";
- if (this.streamId)
- tag += " id='" + this.streamId + "'";
- if (this.xmppVersion)
- tag += " version='" + this.xmppVersion + "'";
- tag += ">";
- this.send(tag);
-};
-
exports.dialbackKey = function(from, to, key) {
return new xml.Element('db:result', { to: to,
from: from }).
@@ -148,7 +133,7 @@ exports.OutgoingServer = function(srcDomain, destDomain, credentials) {
var self = this;
Server.call(this);
- this.destDomain = destDomain;
+ this.streamTo = destDomain;
// For outgoing, we only need our own cert & key
this.credentials = credentials;
// No credentials means we cannot <starttls/> on the server
--
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