1
0
Fork 0

params and color debug

master
Matthieu Lalonde 15 years ago committed by Mathieu Lecarme
parent 69041e4c07
commit 72348a51dc

@ -1,24 +1,23 @@
var sys = require('sys'), var sys = require('sys'),
xmpp = require('xmpp'), xmpp = require('xmpp'),
colors = require('colors'),
events = require('events'); events = require('events');
var Client = function(_jid, password, host) { var Client = function(params) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
this.color = (params.color != null) ? params.color : 'blue';
this.debug = true; this.debug = true;
var jabber = this; var jabber = this;
this.jid = new xmpp.JID(_jid); this.jid = new xmpp.JID(params.jid);
this.host = (host == null) ? this.jid.domain : host; this.host = (params.host == null) ? this.jid.domain : params.host;
this.rooms = {}; this.rooms = {};
this._iq = 0; this._iq = 0;
this._iqCallback = {}; this._iqCallback = {};
this.presences = {}; this.presences = {};
this.roster = {}; this.roster = {};
this.xmpp = new xmpp.Client({ this.xmpp = new xmpp.Client(params);
host: this.host,
jid: '' + this.jid,
password: password });
this.xmpp.addListener('rawStanza', function(stanza) { this.xmpp.addListener('rawStanza', function(stanza) {
sys.debug("RAW: " + stanza); sys.debug("RAW: "[jabber.color] + stanza.toString().white);
}); });
this.xmpp.addListener('authFail', function() { this.xmpp.addListener('authFail', function() {
sys.error("[Error] Jabber : Authentication failure"); sys.error("[Error] Jabber : Authentication failure");
@ -29,7 +28,7 @@ var Client = function(_jid, password, host) {
process.exit(1); process.exit(1);
}); });
this.xmpp.addListener('stanza', function(stanza) { this.xmpp.addListener('stanza', function(stanza) {
sys.debug('STANZA: ' + stanza); sys.debug('STANZA: '[jabber.color] + stanza);
if(stanza.name == 'iq') { if(stanza.name == 'iq') {
if(stanza.attrs.type == 'result') { if(stanza.attrs.type == 'result') {
jabber._debug('IQ result: ' + stanza); jabber._debug('IQ result: ' + stanza);
@ -40,9 +39,9 @@ var Client = function(_jid, password, host) {
} }
} }
if(stanza.name == 'presence') { if(stanza.name == 'presence') {
var fromm = stanza.attrs.from.split('/')[0].split('@'); var fromm = new xmpp.JID(stanza.attrs.from);
if(fromm[1] == 'conference.' + this.jid.domain) { if(fromm.domain == 'conference.' + this.jid.domain) {
jabber.rooms[fromm[0]].emit('presence', stanza); jabber.rooms[fromm.user].emit('presence', stanza);
} else { } else {
jabber.emit('presence', stanza.attrs.from, stanza); jabber.emit('presence', stanza.attrs.from, stanza);
} }

@ -18,7 +18,7 @@ exports.testJid = function(test) {
}; };
exports.testClientInit = 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.equals('gtalk.com', c.host);
test.done(); test.done();
}; };
@ -26,7 +26,7 @@ exports.testClientInit = function(test) {
exports.testClient = function(test) { exports.testClient = function(test) {
test.expect(3); test.expect(3);
var MESSAGE = "Beuha de test!"; 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){ b.addListener('message', function(from, msg, stanza){
sys.debug('Message from ' + from.red + ' : ' + msg.yellow); sys.debug('Message from ' + from.red + ' : ' + msg.yellow);
test.equals(MESSAGE, msg); test.equals(MESSAGE, msg);
@ -35,7 +35,7 @@ exports.testClient = function(test) {
b.addListener('online', function() { b.addListener('online', function() {
sys.debug('b is connected'.red); sys.debug('b is connected'.red);
test.ok(true); test.ok(true);
var a = new Client(conf.a.jid, conf.a.password); var a = new Client(conf.a);
a.addListener('online', function() { a.addListener('online', function() {
sys.debug('a is connected'.green); sys.debug('a is connected'.green);
sys.debug('a presences : ' + JSON.stringify(a.presences).green); sys.debug('a presences : ' + JSON.stringify(a.presences).green);

Loading…
Cancel
Save