From d7c1994ffb06d6f3a7b16c0e4d543e495f5e8cea Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Mon, 9 Aug 2010 17:12:46 +0200 Subject: [PATCH] broken room --- lib/xmpp-client/client.js | 34 ++++++++++++++++++++-------------- test/test.js | 11 ++++++++++- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index 8b44bb2..73f7f32 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -29,7 +29,7 @@ var Client = function(params, callback) { process.exit(1); }); this.xmpp.addListener('stanza', function(stanza) { - sys.debug('STANZA: '[jabber.color] + ('<' + stanza.name + '> ').bold[jabber.color] + stanza); + //sys.debug('STANZA: '[jabber.color] + ('<' + stanza.name + '> ').bold[jabber.color] + stanza); if(stanza.name == 'iq') { if(stanza.attrs.type == 'result') { jabber._debug('IQ result: ' + stanza); @@ -66,7 +66,7 @@ var Client = function(params, callback) { if(stanza.name == 'message') { var from = stanza.attrs.from; if(stanza.attrs.type == 'groupchat') { - jabber.emit('groupchat', from, stanza); + jabber.emit('groupchat', from, stanza.getChild('body').getText(), stanza); } else { jabber._debug('MESSAGE: ' + stanza); jabber.emit('message', from, stanza.getChild('body').getText(), stanza); @@ -78,15 +78,15 @@ var Client = function(params, callback) { jabber.presence(); jabber.emit('online'); jabber.askForRoster(function(roster) { - jabber._debug("ROSTER : "[jabber.color] + JSON.stringify(roster)); + //jabber._debug("ROSTER : "[jabber.color] + JSON.stringify(roster)); if(callback != null) { callback.call(jabber); } }); }); - this.addListener('groupchat', function(from, stanza) { - fromName = from.split('@')[0]; - jabber.rooms[fromName].emit('message', stanza); + this.addListener('groupchat', function(from, msg, stanza) { + fromName = from.split('/')[0]; + jabber.rooms[fromName].emit('message', msg, stanza); }); this.addListener('iqResult', function(id, stanza){ jabber._iqCallback[id].call(jabber, stanza); @@ -153,9 +153,8 @@ Client.prototype.registerIqHandler = function(xmlns, action) { Client.prototype.message = function(to, message) { this.xmpp.send(new xmpp.Element('message', { to: to, - type: 'chat'}). - c('body'). - t(message)); + type: 'chat'}) + .c('body').t(message)); }; Client.prototype.askForRoster = function(callback) { @@ -210,8 +209,7 @@ Client.prototype.room = function(name, callback) { Client.prototype.disconnect = function() { this.xmpp.send(new xmpp.Element('presence', {type: 'unavailable'}) - .c('status') - .t('Logged out') + .c('status').t('Logged out') .tree()); var jabber = this; /* Object.keys(this.rooms).forEach(function(room) { @@ -225,6 +223,7 @@ var Room = function(client, name, callback) { events.EventEmitter.call(this); this.client = client; this.room = name; + this.to = this.room + '/' + this.client.jid.user; var room = this; this.addListener('presence', function(from, stanza) { var jfrom = new xmpp.JID(from); @@ -249,8 +248,9 @@ sys.inherits(Room, events.EventEmitter); exports.Room = Room; Room.prototype.presence = function() { + sys.debug(('presence: ' + this.room)[this.client.color]); this.client.xmpp.send(new xmpp.Element('presence', { - to: this.room + '/' + this.client.jid.user + to: this.to }) .c('priority').t("5").up() .c('x', {xmlns:"http://jabber.org/protocol/muc"}) @@ -258,6 +258,12 @@ Room.prototype.presence = function() { ); }; -Room.prototype.message = function(msg) { - +Room.prototype.message = function(message) { + this.client.xmpp.send(new xmpp.Element('message', { + to: this.to, + type: 'groupchat'}) + .c('body').t(message).up() +// .c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(this.client.jid.username) + .tree() + ); }; \ No newline at end of file diff --git a/test/test.js b/test/test.js index 85b9f6b..c0f564c 100644 --- a/test/test.js +++ b/test/test.js @@ -48,7 +48,9 @@ exports.testClient = function(test) { */ exports.testRoom = function(test) { + test.expect(1); var ROOM = 'mushroom@conference.' + conf.b.jid.split('@')[1]; + var MESSAGE = "Hello everybody"; var b = new Client(conf.b, function() { sys.debug('b is connected'.red); sys.debug(('enter in ' + ROOM).green); @@ -58,7 +60,14 @@ exports.testRoom = function(test) { var a_room = a.room(ROOM, function(status) { sys.debug(status); sys.debug(this.role); - test.done(); + this.addListener('message', function(from, msg, stanza) { + sys.debug('message : ' + msg); + test.equals(MESSAGE, msg); + if(MESSAGE == msg) { + test.done(); + } + }); + b_room.message(MESSAGE); }); });