diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index 81121ab..ede1794 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -12,6 +12,7 @@ var Client = function(params, callback) { this.host = (params.host == null) ? this.jid.domain : params.host; this.rooms = {}; this._iq = 0; + this._iqHandler = {}; this._iqCallback = {}; this.presences = {}; this.roster = {}; @@ -36,6 +37,12 @@ var Client = function(params, callback) { } else { jabber._debug(('IQ: ' + stanza)[jabber.color]); jabber.emit('iq', stanza); + var q = stanza.getChild('query'); + if(q.attrs.xmlns != null && jabber._iqHandler[q.attrs.xmlns] != null) { + jabber._iqHandler[q.attrs.xmlns].call(jabber, stanza); + } else { + jabber.emit('iq:unknow', stanza); + } } } if(stanza.name == 'presence') { @@ -72,10 +79,10 @@ var Client = function(params, callback) { jabber.emit('online'); jabber.askForRoster(function(roster) { jabber._debug("ROSTER : "[jabber.color] + JSON.stringify(roster)); + if(callback != null) { + callback.call(jabber); + } }); - if(callback != null) { - callback.call(jabber); - } }); this.addListener('groupchat', function(from, stanza) { fromName = from.split('@')[0]; @@ -95,41 +102,38 @@ var Client = function(params, callback) { jabber.presences[from] = stanza.attrs.type; } }); + this.registerIqHandler('http://jabber.org/protocol/disco#info', function(stanza) { + sys.debug((stanza.attrs.from + " wont to disco!")[jabber.color]); + jabber.resultIq(stanza, new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#info'}) + .c('feature', {'var': 'http://jabber.org/protocol/disco#info'}).up() + .c('feature', {'var': 'http://jabber.org/protocol/disco#items'}).up() + .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up() + .c('identity', { + category: 'conference', + type: 'text', + name: 'Play-Specific Chatrooms' + }).up() + .tree() + ); + }); + this.registerIqHandler('jabber:iq:last', function(stanza) { + sys.debug((stanza.attrs.from + ' wonts last')[jabber.color]); + //[FIXME] giving a good last time + jabber.resultIq(stanza, new xmpp.Element('query', { + xmlns: 'jabber:iq:last', seconds:'1'}) + .tree() + ); + }); + this.registerIqHandler('jabber:iq:version', function(stanza) { + jabber.resultIq(stanza, new xmpp.Element('query', {xmlns:'jabber:iq:version'}) + .c('name').t('node-xmpp-client').up() + .c('version').t('0.0.1').up() + .c('os').t('mac').up() + .tree() + ); + }); this.addListener('iq', function(stanza) { sys.debug(stanza.getChild('query').toString().yellow); - var query = stanza.getChild('query', 'http://jabber.org/protocol/disco#info'); - if(query != null) { - sys.debug((stanza.attrs.from + " wont to disco!")[jabber.color]); - jabber.resultIq(stanza, new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#info'}) - .c('feature', {'var': 'http://jabber.org/protocol/disco#info'}).up() - .c('feature', {'var': 'http://jabber.org/protocol/disco#items'}).up() - .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up() - .c('identity', { - category: 'conference', - type: 'text', - name: 'Play-Specific Chatrooms' - }).up() - .tree() - ); - } - query = stanza.getChild('query', 'jabber:iq:last'); - if(query != null) { - sys.debug((stanza.attrs.from + ' wonts last')[jabber.color]); - //[FIXME] giving a good last time - jabber.resultIq(stanza, new xmpp.Element('query', { - xmlns: 'jabber:iq:last', seconds:'1'}) - .tree() - ); - } - query = stanza.getChild('query', 'jabber:iq:version'); - if(query != null) { - jabber.resultIq(stanza, new xmpp.Element('query', {xmlns:'jabber:iq:version'}) - .c('name').t('node-xmpp-client').up() - .c('version').t('0.0.1').up() - .c('os').t('mac').up() - .tree() - ); - } }); }; @@ -142,6 +146,10 @@ Client.prototype._debug = function(txt) { } }; +Client.prototype.registerIqHandler = function(xmlns, action) { + this._iqHandler[xmlns] = action; +}; + Client.prototype.message = function(to, message) { this.xmpp.send(new xmpp.Element('message', { to: to, diff --git a/test/test.js b/test/test.js index 2024672..85b9f6b 100644 --- a/test/test.js +++ b/test/test.js @@ -57,6 +57,7 @@ exports.testRoom = function(test) { sys.debug('a is connected'.green); var a_room = a.room(ROOM, function(status) { sys.debug(status); + sys.debug(this.role); test.done(); }); });