[Pkg-javascript-commits] [ltx] 70/469: router: refactor a bit, receive stanzas
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:01:02 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 bfd6d27c8c1082ffa71930f465a2d3b51ee72a65
Author: Astro <astro at spaceboyz.net>
Date: Sun Sep 5 18:37:43 2010 +0200
router: refactor a bit, receive stanzas
---
lib/xmpp/router.js | 75 +++++++++++++++++++++++++++++++++++++++++-------------
lib/xmpp/server.js | 11 ++++++++
2 files changed, 68 insertions(+), 18 deletions(-)
diff --git a/lib/xmpp/router.js b/lib/xmpp/router.js
index 77b7b1e..86b5be6 100644
--- a/lib/xmpp/router.js
+++ b/lib/xmpp/router.js
@@ -21,7 +21,7 @@ dbgStream = function(tag, stream) {
};
/**
- * Represents a domain we host with connections to federated services
+ * Represents a domain we host with connections to federated servers
*/
function DomainContext(domain) {
this.domain = domain;
@@ -85,13 +85,12 @@ DomainContext.prototype.getOutStream = function(domain) {
var outStream = this.s2sOut[domain] =
Server.makeOutgoingServer(domain);
dbgStream('outgoing', outStream);
+ this.setupStream(domain, outStream);
+
outStream.addListener('close', function() {
// TODO: purge queue
delete self.s2sOut[domain];
});
- outStream.addListener('error', function() {
- outStream.end();
- });
// Prepare dialback
outStream.addListener('online', function() {
@@ -117,20 +116,6 @@ DomainContext.prototype.getOutStream = function(domain) {
}
};
-DomainContext.prototype.verifyDialback = function(domain, id, key) {
- var outStream;
- if (this.s2sOut.hasOwnProperty(domain) &&
- (outStream = this.s2sOut[domain])) {
-
- var isValid = outStream.streamAttrs.id === id &&
- outStream.dbKey === key;
-
- //outStream.send(Server.dialbackResult(this.domain, domain, isValid));
- return isValid;
- } else
- return false;
-};
-
/**
* Called by router when verification is done
*/
@@ -143,6 +128,7 @@ DomainContext.prototype.addInStream = function(domain, stream) {
oldStream.end();
}
+ this.setupStream(domain, stream);
stream.isOnline = true;
stream.isVerified = true;
stream.addListener('close', function() {
@@ -152,6 +138,58 @@ DomainContext.prototype.addInStream = function(domain, stream) {
this.s2sIn[domain] = stream;
};
+DomainContext.prototype.setupStream = function(domain, stream) {
+ var self = this;
+
+ stream.addListener('stanza', function(stanza) {
+ // Before verified they can send whatever they want
+ if (!stream.isVerified)
+ return;
+
+ var allowed = typeof stanza.attrs.from === 'string' &&
+ typeof stanza.attrs.to === 'string';
+ if (allowed) {
+ var fromDomain = (new JID.JID(stanza.attrs.from)).domain;
+ allowed = fromDomain === domain;
+ }
+ if (allowed) {
+ var toDomain = (new JID.JID(stanza.attrs.to)).domain;
+ allowed = toDomain === self.domain;
+ }
+
+ if (allowed) {
+ self.receive(stanza);
+ } else {
+ console.log({'improper-addressing':stanza.toString()});
+ stream.send(Server.error('improper-addressing'));
+ stream.emit('error', new Error('improper-addressing'));
+ }
+ });
+ stream.addListener('error', function() {
+ if (stream.writable)
+ stream.send('</stream:stream>');
+ stream.end();
+ });
+};
+
+DomainContext.prototype.verifyDialback = function(domain, id, key) {
+ var outStream;
+ if (this.s2sOut.hasOwnProperty(domain) &&
+ (outStream = this.s2sOut[domain])) {
+
+ var isValid = outStream.streamAttrs.id === id &&
+ outStream.dbKey === key;
+
+ return isValid;
+ } else
+ return false;
+};
+
+DomainContext.prototype.receive = function(stanza) {
+ if (this.stanzaListener)
+ this.stanzaListener(stanza);
+};
+
/**
* TODO:
* * recv stanzas
@@ -188,6 +226,7 @@ Router.prototype.acceptConnection = function(inStream) {
});
// incoming connection wants to get verified
inStream.addListener('dialbackKey', function(from, to, key) {
+ // TODO: what if no context?
var ctx = self.getContext(to);
var outStream = ctx.sendRaw(Server.dialbackVerify(to, from, inStream.streamId, key),
from);
diff --git a/lib/xmpp/server.js b/lib/xmpp/server.js
index 52f74a6..83bd26c 100644
--- a/lib/xmpp/server.js
+++ b/lib/xmpp/server.js
@@ -4,6 +4,7 @@ var xml = require('./xml');
var NS_SERVER = 'jabber:server';
var NS_DIALBACK = 'jabber:server:dialback';
+var NS_XMPP_STREAMS = 'urn:ietf:params:xml:ns:xmpp-streams';
/**
* Dialback-specific events:
@@ -150,3 +151,13 @@ function generateId() {
}
return r.toString();
};
+
+exports.error = function(condition, text) {
+ var e = new xml.Element('stream:error');
+ e.c(condition, { xmlns: NS_XMPP_STREAMS });
+ if (text)
+ e.c('text', { xmlns: NS_XMPP_STREAMS,
+ 'xml:lang': 'en' }).
+ t(text);
+ return e;
+};
--
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