From 4a0c4d0a8a5ec0e9e5413ebb410b1ecb1aa28911 Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Sun, 28 Nov 2010 21:48:49 +0100 Subject: [PATCH] iq version --- lib/xmpp-client/basic-client.js | 15 +++++++++------ lib/xmpp-client/client.js | 13 ++++++++++--- test/test-client.js | 1 + test/test-iq.js | 24 ++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 test/test-iq.js diff --git a/lib/xmpp-client/basic-client.js b/lib/xmpp-client/basic-client.js index 4e34e75..34b0795 100644 --- a/lib/xmpp-client/basic-client.js +++ b/lib/xmpp-client/basic-client.js @@ -14,8 +14,8 @@ var BasicClient = function(params, callback) { this._iqCallback = {}; this._iqHandler = {}; var jabber = this; - this.jid = new xmpp.JID(params.jid); - this.host = (params.host == null) ? this.jid.domain : params.host; + var jid = new xmpp.JID(params.jid); + this.host = (params.host == null) ? jid.domain : params.host; this.xmpp = new xmpp.Client(params); this.xmpp.addListener('rawStanza', function(stanza) { //sys.debug("RAW: "[jabber.color] + stanza.toString().white); @@ -33,10 +33,10 @@ var BasicClient = function(params, callback) { case 'iq': switch(stanza.attrs.type) { case 'error': - jabber._iqCallback[stanza.attrs.id][1].apply(jabber, [stanza]); + jabber._iqCallback[stanza.attrs.id].error.apply(jabber, [stanza]); break; case 'result': - jabber._iqCallback[stanza.attrs.id][0].apply(jabber, [stanza]); + jabber._iqCallback[stanza.attrs.id].success.apply(jabber, [stanza]); break; default: jabber.emit('iq', stanza); @@ -66,6 +66,7 @@ var BasicClient = function(params, callback) { } }); this.xmpp.addListener('online', function() { + jabber.jid = this.jid; jabber.emit('online'); callback.apply(jabber); }); @@ -86,9 +87,11 @@ BasicClient.prototype.presence = function(type) { }; BasicClient.prototype.iq = function(to, query, callback, error) { - error |= function(stanza) { sys.error((this.jid + " : " + stanza.toString()).red);}; + if(error == undefined) error = function(stanza) { sys.error((this.jid + " : " + stanza.toString()).red);}; var n = 'node' + this._iq++; - this._iqCallback[n] = [callback, error]; + this._iqCallback[n] = {}; + this._iqCallback[n].success = callback; + this._iqCallback[n].error = error; var attrs = { type: 'get', id: n diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index a8e8d39..3a7b877 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -9,7 +9,7 @@ var Client = function(params, callback) { this.presences = {}; BasicClient.call(this, params, function() { this.presence(); - this.askForRoster(function(roster) { + this.getRoster(function(roster) { callback.apply(this); }); }); @@ -38,7 +38,7 @@ var Client = function(params, callback) { 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('version').t('0.0.2').up() .c('os').t(process.platform).up() .tree() ); @@ -49,7 +49,7 @@ var Client = function(params, callback) { sys.inherits(Client, BasicClient); exports.Client = Client; -Client.prototype.askForRoster = function(callback) { +Client.prototype.getRoster = function(callback) { var jabber = this; this.iq(null, new xmpp.Element('query', {xmlns: 'jabber:iq:roster'}), function(iq) { iq.getChild('query', 'jabber:iq:roster').getChildren('item').forEach(function(child) { @@ -62,6 +62,13 @@ Client.prototype.askForRoster = function(callback) { }); }; +Client.prototype.getVersion = function(jid, callback, error) { + var jabber = this; + this.iq(jid, new xmpp.Element('query', {xmlns: 'jabber:iq:version'}), function(iq) { + callback.call(jabber, iq.getChild('query', 'jabber:iq:version') ); + }, error); +}; + Client.prototype.disconnect = function() { this.xmpp.send(new xmpp.Element('presence', {type: 'unavailable'}) .c('status').t('Logged out') diff --git a/test/test-client.js b/test/test-client.js index cf3c15f..27fb9b8 100644 --- a/test/test-client.js +++ b/test/test-client.js @@ -24,3 +24,4 @@ exports.testClient = function(test) { }); }); }; + diff --git a/test/test-iq.js b/test/test-iq.js new file mode 100644 index 0000000..7a1eaea --- /dev/null +++ b/test/test-iq.js @@ -0,0 +1,24 @@ +var sys = require('sys'), + colors = require('colors'), + xmpp = require('node-xmpp'), + Client = require('../lib/xmpp-client').Client, + conf = require('./conf').conf; + +exports.testIq = function(test) { + test.expect(1); + var a = new Client(conf.a, function() { + //sys.debug(this.jid.toString().blue); + var b = new Client(conf.b, function() { + //sys.debug(this.jid.toString().blue); + a.getVersion(b.jid, function(version) { + sys.debug(version.toString().yellow); + test.ok(true, 'version'); + test.done(); + }, + function(iq) { + sys.error(iq.toString().red); + test.done(); + }); + }); + }); +}; \ No newline at end of file