Skip to content

Commit

Permalink
c2s: reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
astro committed Sep 15, 2011
1 parent 86d19d2 commit 94015d3
Showing 1 changed file with 50 additions and 39 deletions.
89 changes: 50 additions & 39 deletions lib/xmpp/c2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,53 +51,54 @@ C2S.prototype.acceptConnection = function(socket, options) {
this.emit("connect", client);
socket.addListener('error', function() { });
client.addListener('error', function() { });
}
};

/**
* C2S Router */
C2S.prototype.route = function(stanza) {
var self = this;
if(stanza.attrs && stanza.attrs.to) {
if (stanza.attrs && stanza.attrs.to) {
var toJid = new JID(stanza.attrs.to);
if(self.sessions.hasOwnProperty(toJid.bare().toString())) {
if (self.sessions.hasOwnProperty(toJid.bare().toString())) {
// Now loop over all the sesssions and only send to the right jid(s)
var sent = false;
for(var resource in self.sessions[toJid.bare().toString()]) {
var sent = false, resource;
for(resource in self.sessions[toJid.bare().toString()]) {
if(toJid.bare().toString() === toJid.toString() || toJid.resource === resource) {
sent = true;
self.sessions[toJid.bare().toString()][resource].send(stanza);
}
}
// We couldn't find a connected jid that matches the destination. Let's send it to everyone
if(!sent) {
for(var resource in self.sessions[toJid.bare().toString()]) {
if (!sent) {
for(resource in self.sessions[toJid.bare().toString()]) {
self.sessions[toJid.bare().toString()][resource].send(stanza);
}
}
}
}
else {
// Huh? Who is it for? and why did it end up here?
// TODO: reply with error
}
}
};

/**
* Registers a route (jid => specific client connection)
*/
C2S.prototype.registerRoute = function(jid, client) {
// What if we have a conflict! TOFIX
if(!this.sessions.hasOwnProperty(jid.bare().toString()))
if (!this.sessions.hasOwnProperty(jid.bare().toString()))
this.sessions[jid.bare().toString()] = {};
this.sessions[jid.bare().toString()][jid.resource] = client;
return true;
}
};

/**
* Returns the list of jids connected for a specific jid.
*/
C2S.prototype.connectedClientsForJid = function(jid) {
jid = new JID(jid);
if(!this.sessions.hasOwnProperty(jid.bare().toString())) {
if (!this.sessions.hasOwnProperty(jid.bare().toString())) {
return [];
}
else {
Expand All @@ -107,19 +108,19 @@ C2S.prototype.connectedClientsForJid = function(jid) {
}
return jids;
}
},
};

/**
* Unregisters a route (jid => specific client connection)
*/
C2S.prototype.unregisterRoute = function(jid, client) {
if(!this.sessions.hasOwnProperty(jid.bare().toString())) {
if (!this.sessions.hasOwnProperty(jid.bare().toString())) {
// Hum. What? That can't be.
} else {
delete this.sessions[jid.bare().toString()][jid.resource];
}
return true;
}
};


function C2SClient(socket, c2s) {
Expand Down Expand Up @@ -182,14 +183,14 @@ C2SClient.prototype.startStream = function(streamAttrs) {
this.send(s.substr(0, s.indexOf(' </stream:stream>')));

this.sendFeatures();
}
};

C2SClient.prototype.sendFeatures = function() {
var features = new ltx.Element('stream:features');
if(!this.authentified) {
if (!this.authentified) {
// TLS
if(this.c2s.options.tls && !this.socket.encrypted) {
features.c("starttls", {xmlns: NS_XMPP_TLS}).c("required")
if (this.c2s.options.tls && !this.socket.encrypted) {
features.c("starttls", {xmlns: NS_XMPP_TLS}).c("required");
}
this.mechanisms = sasl.availableMechanisms();

Expand All @@ -203,16 +204,16 @@ C2SClient.prototype.sendFeatures = function() {
features.c("session", {xmlns: 'urn:ietf:params:xml:ns:xmpp-session'});
}
this.send(features);
}
};

C2SClient.prototype.onRawStanza = function(stanza) {
var bind, session;

if(this.jid) {
if (this.jid) {
stanza.attrs.from = this.jid.toString();
}

if(stanza.is('starttls', NS_XMPP_TLS)) {
if (stanza.is('starttls', NS_XMPP_TLS)) {
this.send(new ltx.Element('proceed', { xmlns: Connection.NS_XMPP_TLS }));
this.setSecure(this.c2s.credentials, true);
}
Expand All @@ -229,7 +230,7 @@ C2SClient.prototype.onRawStanza = function(stanza) {
(session = stanza.getChild('session', 'urn:ietf:params:xml:ns:xmpp-session'))) {
this.onSession(stanza);
}
else if (stanza.is('iq') && (inband = stanza.getChild('query', 'jabber:iq:register'))) {
else if (stanza.is('iq') && stanza.getChild('query', 'jabber:iq:register')) {
this.onRegistration(stanza);
}
else if (stanza.attrs.to &&
Expand All @@ -250,7 +251,7 @@ C2SClient.prototype.authenticate = function(username, password) {
var self = this;
var jid = new JID(username, this.c2s.options.domain);
this.c2s.emit('authenticate', jid, password, this, function(authenticated) {
if(authenticated) {
if (authenticated) {
self.emit('auth-success', jid);
self.jid = jid;
self.authentified = true;
Expand Down Expand Up @@ -280,48 +281,58 @@ C2SClient.prototype.onAuth = function(stanza) {

C2SClient.prototype.onRegistration = function(stanza) {
var self = this;
register = stanza.getChild('query', 'jabber:iq:register');
if(stanza.attrs.type === 'get') {
var register = stanza.getChild('query', 'jabber:iq:register');
if (stanza.attrs.type === 'get') {
stanza.attrs.type = "result";
register.c("instructions").t("Choose a username and password for use with this service. ");
register.c("username");
register.c("password");
self.send(stanza);
}
else if(stanza.attrs.type === 'set') {
self.c2s.emit('register', new JID(register.getChild('username', 'jabber:iq:register').getText(), this.c2s.options.domain), register.getChild('password', 'jabber:iq:register').getText(), self, function(registered, reason) {
if(registered) {
self.c2s.emit('register',
new JID(register.getChild('username', 'jabber:iq:register').getText(), this.c2s.options.domain),
register.getChild('password', 'jabber:iq:register').getText(),
self,
function(registered, reason) {
if (registered) {
self.emit('registration-success', self.jid);
self.send(new ltx.Element("iq", {type: "result", id: stanza.attrs.id}));
self.send(new ltx.Element("iq", { type: "result", id: stanza.attrs.id }));
}
else {
self.emit('registration-failure', jid);
stanza.attrs.type = "error";
stanza.c("error", {code: reason.code, type: reason.type}).c(reason.text, {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"});
stanza.c("error", { code: reason.code, type: reason.type }).
c(reason.text, { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" });
self.send(stanza);
}
});
}
}

C2SClient.prototype.onBind = function(stanza) {
var self = this;
var bind = stanza.getChild('bind', 'urn:ietf:params:xml:ns:xmpp-bind')
if(resource = bind.getChild("resource", 'urn:ietf:params:xml:ns:xmpp-bind')) {
self.jid.setResource(resource.getText());
var bind = stanza.getChild('bind', 'urn:ietf:params:xml:ns:xmpp-bind');
var resource;
if ((resource = bind.getChild("resource", 'urn:ietf:params:xml:ns:xmpp-bind'))) {
this.jid.setResource(resource.getText());
}
else {
self.jid.setResource(generateId());
this.jid.setResource(generateId());
}
if(self.c2s.registerRoute(self.jid, self)) {
self.send(new ltx.Element("iq", {type:"result", id: stanza.attrs.id}).c("bind", { xmlns: "urn:ietf:params:xml:ns:xmpp-bind"}).c("jid").t(self.jid.toString()));
if (this.c2s.registerRoute(this.jid, this)) {
this.send(new ltx.Element("iq", { type:"result", id: stanza.attrs.id }).
c("bind", { xmlns: "urn:ietf:params:xml:ns:xmpp-bind" }).
c("jid").t(this.jid.toString())
);
}
}
};

C2SClient.prototype.onSession = function(stanza) {
var self = this;
self.send(new ltx.Element("iq", {type:"result", id: stanza.attrs.id}).c("session", { xmlns: "urn:ietf:params:xml:ns:xmpp-session"}));
}
self.send(new ltx.Element("iq", { type:"result", id: stanza.attrs.id }).
c("session", { xmlns: "urn:ietf:params:xml:ns:xmpp-session"})
);
};


function generateId() {
Expand Down

0 comments on commit 94015d3

Please sign in to comment.