From d18443dbfff1464f2967636f21a306a3d9a57e02 Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Sat, 7 Aug 2010 22:00:13 +0200 Subject: [PATCH] muc with callback --- lib/xmpp-client/client.js | 26 +++++++++++++++++++++----- test/test.js | 14 +++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index 82e5fa2..dd2956a 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -72,7 +72,7 @@ var Client = function(params, callback) { jabber.emit('online'); jabber.askForRoster(); if(callback != null) { - callback.apply(jabber); + callback.call(jabber); } }); this.addListener('groupchat', function(from, stanza) { @@ -193,10 +193,10 @@ Client.prototype.canonicalRoomName = function(room) { } }; -Client.prototype.room = function(name) { +Client.prototype.room = function(name, callback) { var room = this.canonicalRoomName(name); if(this.rooms[room] == null) { - this.rooms[room] = new Room(this, room); + this.rooms[room] = new Room(this, room, callback); } return this.rooms[room]; }; @@ -214,10 +214,26 @@ Client.prototype.disconnect = function() { sys.debug("disconnect from XMPP"); }; -var Room = function(client, room, callback) { +var Room = function(client, name, callback) { events.EventEmitter.call(this); this.client = client; - this.room = room; + this.room = name; + var room = this; + this.addListener('presence', function(from, stanza) { + var jfrom = new xmpp.JID(from); + if(name == jfrom.user + '@' + jfrom.domain) { + var x = stanza.getChild('x', 'http://jabber.org/protocol/muc#user'); + if(x != null) { + var item = x.getChild('item'); + if(item != null) { + room.affiliation = item.attrs.affiliation; + room.role = item.attrs.role; + } + var status = x.getChild('status'); + callback.call(room, (status != null) ? status.attrs.code : '200'); + } + } + }); this.presence(); }; diff --git a/test/test.js b/test/test.js index b7b0276..2024672 100644 --- a/test/test.js +++ b/test/test.js @@ -52,12 +52,16 @@ exports.testRoom = function(test) { var b = new Client(conf.b, function() { sys.debug('b is connected'.red); sys.debug(('enter in ' + ROOM).green); - var b_room = b.room(ROOM); - var a = new Client(conf.a, function() { - sys.debug('a is connected'.green); - var a_room = a.room(ROOM); + var b_room = b.room(ROOM, function(status) { + var a = new Client(conf.a, function() { + sys.debug('a is connected'.green); + var a_room = a.room(ROOM, function(status) { + sys.debug(status); + test.done(); + }); + }); + }); - //test.done(); }); };