1
0
Fork 0
master
Matthieu Lalonde 14 years ago committed by Mathieu Lecarme
parent 8583829d43
commit 4a0c4d0a8a

@ -14,8 +14,8 @@ var BasicClient = function(params, callback) {
this._iqCallback = {}; this._iqCallback = {};
this._iqHandler = {}; this._iqHandler = {};
var jabber = this; var jabber = this;
this.jid = new xmpp.JID(params.jid); var jid = new xmpp.JID(params.jid);
this.host = (params.host == null) ? this.jid.domain : params.host; this.host = (params.host == null) ? jid.domain : params.host;
this.xmpp = new xmpp.Client(params); this.xmpp = new xmpp.Client(params);
this.xmpp.addListener('rawStanza', function(stanza) { this.xmpp.addListener('rawStanza', function(stanza) {
//sys.debug("RAW: "[jabber.color] + stanza.toString().white); //sys.debug("RAW: "[jabber.color] + stanza.toString().white);
@ -33,10 +33,10 @@ var BasicClient = function(params, callback) {
case 'iq': case 'iq':
switch(stanza.attrs.type) { switch(stanza.attrs.type) {
case 'error': case 'error':
jabber._iqCallback[stanza.attrs.id][1].apply(jabber, [stanza]); jabber._iqCallback[stanza.attrs.id].error.apply(jabber, [stanza]);
break; break;
case 'result': case 'result':
jabber._iqCallback[stanza.attrs.id][0].apply(jabber, [stanza]); jabber._iqCallback[stanza.attrs.id].success.apply(jabber, [stanza]);
break; break;
default: default:
jabber.emit('iq', stanza); jabber.emit('iq', stanza);
@ -66,6 +66,7 @@ var BasicClient = function(params, callback) {
} }
}); });
this.xmpp.addListener('online', function() { this.xmpp.addListener('online', function() {
jabber.jid = this.jid;
jabber.emit('online'); jabber.emit('online');
callback.apply(jabber); callback.apply(jabber);
}); });
@ -86,9 +87,11 @@ BasicClient.prototype.presence = function(type) {
}; };
BasicClient.prototype.iq = function(to, query, callback, error) { 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++; 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 = { var attrs = {
type: 'get', type: 'get',
id: n id: n

@ -9,7 +9,7 @@ var Client = function(params, callback) {
this.presences = {}; this.presences = {};
BasicClient.call(this, params, function() { BasicClient.call(this, params, function() {
this.presence(); this.presence();
this.askForRoster(function(roster) { this.getRoster(function(roster) {
callback.apply(this); callback.apply(this);
}); });
}); });
@ -38,7 +38,7 @@ var Client = function(params, callback) {
this.registerIqHandler('jabber:iq:version', function(stanza) { this.registerIqHandler('jabber:iq:version', function(stanza) {
jabber.resultIq(stanza, new xmpp.Element('query', {xmlns:'jabber:iq:version'}) jabber.resultIq(stanza, new xmpp.Element('query', {xmlns:'jabber:iq:version'})
.c('name').t('node-xmpp-client').up() .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() .c('os').t(process.platform).up()
.tree() .tree()
); );
@ -49,7 +49,7 @@ var Client = function(params, callback) {
sys.inherits(Client, BasicClient); sys.inherits(Client, BasicClient);
exports.Client = Client; exports.Client = Client;
Client.prototype.askForRoster = function(callback) { Client.prototype.getRoster = function(callback) {
var jabber = this; var jabber = this;
this.iq(null, new xmpp.Element('query', {xmlns: 'jabber:iq:roster'}), function(iq) { this.iq(null, new xmpp.Element('query', {xmlns: 'jabber:iq:roster'}), function(iq) {
iq.getChild('query', 'jabber:iq:roster').getChildren('item').forEach(function(child) { 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() { Client.prototype.disconnect = function() {
this.xmpp.send(new xmpp.Element('presence', {type: 'unavailable'}) this.xmpp.send(new xmpp.Element('presence', {type: 'unavailable'})
.c('status').t('Logged out') .c('status').t('Logged out')

@ -24,3 +24,4 @@ exports.testClient = function(test) {
}); });
}); });
}; };

@ -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();
});
});
});
};
Loading…
Cancel
Save