diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index 6b6d0e9..82e5fa2 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -3,7 +3,7 @@ var sys = require('sys'), colors = require('colors'), events = require('events'); -var Client = function(params) { +var Client = function(params, callback) { events.EventEmitter.call(this); this.color = (params.color != null) ? params.color : 'blue'; this.debug = true; @@ -39,11 +39,21 @@ var Client = function(params) { } } if(stanza.name == 'presence') { - var fromm = new xmpp.JID(stanza.attrs.from); - if(fromm.domain == 'conference.' + this.jid.domain) { - jabber.rooms[fromm.user].emit('presence', stanza); + var jfrom = new xmpp.JID(stanza.attrs.from); + var roomName = jfrom.user + '@' + jfrom.domain; + if(stanza.attrs.type == 'error') { + sys.error(stanza.toString().inverse); + if(jabber.rooms[roomName] != null) { + jabber.rooms[roomName].emit('presence:error', stanza.getChild('error'), stanza); + } else { + jabber.emit('presence:error', stanza.getChild('error'), stanza); + } } else { - jabber.emit('presence', stanza.attrs.from, stanza); + if(jabber.rooms[roomName] != null) { + jabber.rooms[roomName].emit('presence', stanza.attrs.from, stanza); + } else { + jabber.emit('presence', stanza.attrs.from, stanza); + } } } if(stanza.name == 'message') { @@ -61,6 +71,9 @@ var Client = function(params) { jabber.presence(); jabber.emit('online'); jabber.askForRoster(); + if(callback != null) { + callback.apply(jabber); + } }); this.addListener('groupchat', function(from, stanza) { fromName = from.split('@')[0]; @@ -70,7 +83,15 @@ var Client = function(params) { jabber._iqCallback[id].call(jabber, stanza); }); this.addListener('presence', function(from, stanza) { - jabber.presences[from] = stanza.attrs.type; + if(stanza.attrs.type == 'error') { + var jfrom = new JID(stanza.attrs.from); + var roomName = jfrom.user + '@' + jfrom.domain; + if(this.rooms[roomName] != null) { + + } + } else { + jabber.presences[from] = stanza.attrs.type; + } }); this.addListener('iq', function(stanza) { sys.debug(stanza.getChild('query').toString().yellow); @@ -164,11 +185,20 @@ Client.prototype.presence = function(type) { this.xmpp.send(new xmpp.Element('presence', (type != null) ? {type: type} : {}).tree()); }; +Client.prototype.canonicalRoomName = function(room) { + if(room.indexOf('@') > 0) { + return room; + } else { + return room + '@conference.' + this.client.jid.domain; + } +}; + Client.prototype.room = function(name) { - if(this.rooms[name] == null) { - this.rooms[name] = new Room(this, name); + var room = this.canonicalRoomName(name); + if(this.rooms[room] == null) { + this.rooms[room] = new Room(this, room); } - return this.rooms[name]; + return this.rooms[room]; }; Client.prototype.disconnect = function() { @@ -184,7 +214,7 @@ Client.prototype.disconnect = function() { sys.debug("disconnect from XMPP"); }; -var Room = function(client, room) { +var Room = function(client, room, callback) { events.EventEmitter.call(this); this.client = client; this.room = room; @@ -197,7 +227,7 @@ exports.Room = Room; Room.prototype.presence = function() { this.client.xmpp.send(new xmpp.Element('presence', { - to: this.room + '@conference.' + this.client.jid.domain + '/' + this.client.jid.user + to: this.room + '/' + this.client.jid.user }) .c('priority').t("5").up() .c('x', {xmlns:"http://jabber.org/protocol/muc"}) diff --git a/test/test.js b/test/test.js index 70324ba..b7b0276 100644 --- a/test/test.js +++ b/test/test.js @@ -49,16 +49,15 @@ exports.testClient = function(test) { exports.testRoom = function(test) { var ROOM = 'mushroom@conference.' + conf.b.jid.split('@')[1]; - var b = new Client(conf.b); - b.addListener('online', function() { + 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); - a.addListener('online', function() { + var a = new Client(conf.a, function() { sys.debug('a is connected'.green); var a_room = a.room(ROOM); }); - test.done(); + //test.done(); }); };