diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index 1e00a7c..ec0a617 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -1,24 +1,23 @@ var sys = require('sys'), xmpp = require('xmpp'), + colors = require('colors'), events = require('events'); -var Client = function(_jid, password, host) { +var Client = function(params) { events.EventEmitter.call(this); + this.color = (params.color != null) ? params.color : 'blue'; this.debug = true; var jabber = this; - this.jid = new xmpp.JID(_jid); - this.host = (host == null) ? this.jid.domain : host; + this.jid = new xmpp.JID(params.jid); + this.host = (params.host == null) ? this.jid.domain : params.host; this.rooms = {}; this._iq = 0; this._iqCallback = {}; this.presences = {}; this.roster = {}; - this.xmpp = new xmpp.Client({ - host: this.host, - jid: '' + this.jid, - password: password }); + this.xmpp = new xmpp.Client(params); this.xmpp.addListener('rawStanza', function(stanza) { - sys.debug("RAW: " + stanza); + sys.debug("RAW: "[jabber.color] + stanza.toString().white); }); this.xmpp.addListener('authFail', function() { sys.error("[Error] Jabber : Authentication failure"); @@ -29,7 +28,7 @@ var Client = function(_jid, password, host) { process.exit(1); }); this.xmpp.addListener('stanza', function(stanza) { - sys.debug('STANZA: ' + stanza); + sys.debug('STANZA: '[jabber.color] + stanza); if(stanza.name == 'iq') { if(stanza.attrs.type == 'result') { jabber._debug('IQ result: ' + stanza); @@ -40,9 +39,9 @@ var Client = function(_jid, password, host) { } } if(stanza.name == 'presence') { - var fromm = stanza.attrs.from.split('/')[0].split('@'); - if(fromm[1] == 'conference.' + this.jid.domain) { - jabber.rooms[fromm[0]].emit('presence', stanza); + var fromm = new xmpp.JID(stanza.attrs.from); + if(fromm.domain == 'conference.' + this.jid.domain) { + jabber.rooms[fromm.user].emit('presence', stanza); } else { jabber.emit('presence', stanza.attrs.from, stanza); } diff --git a/test/test.js b/test/test.js index 0d4c0e6..ea35fba 100644 --- a/test/test.js +++ b/test/test.js @@ -18,7 +18,7 @@ exports.testJid = function(test) { }; exports.testClientInit = function(test) { - var c = new Client('mathieu@gtalk.com', 'toto'); + var c = new Client({jid: 'mathieu@gtalk.com', password:'toto'}); test.equals('gtalk.com', c.host); test.done(); }; @@ -26,7 +26,7 @@ exports.testClientInit = function(test) { exports.testClient = function(test) { test.expect(3); var MESSAGE = "Beuha de test!"; - var b = new Client(conf.b.jid, conf.b.password); + var b = new Client(conf.b); b.addListener('message', function(from, msg, stanza){ sys.debug('Message from ' + from.red + ' : ' + msg.yellow); test.equals(MESSAGE, msg); @@ -35,7 +35,7 @@ exports.testClient = function(test) { b.addListener('online', function() { sys.debug('b is connected'.red); test.ok(true); - var a = new Client(conf.a.jid, conf.a.password); + var a = new Client(conf.a); a.addListener('online', function() { sys.debug('a is connected'.green); sys.debug('a presences : ' + JSON.stringify(a.presences).green);