1
0
Fork 0

online is now a callback

master
Matthieu Lalonde 15 years ago committed by Mathieu Lecarme
parent 2e4be0d8d2
commit 70eb95508a

@ -3,7 +3,7 @@ var sys = require('sys'),
colors = require('colors'), colors = require('colors'),
events = require('events'); events = require('events');
var Client = function(params) { var Client = function(params, callback) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
this.color = (params.color != null) ? params.color : 'blue'; this.color = (params.color != null) ? params.color : 'blue';
this.debug = true; this.debug = true;
@ -39,13 +39,23 @@ var Client = function(params) {
} }
} }
if(stanza.name == 'presence') { if(stanza.name == 'presence') {
var fromm = new xmpp.JID(stanza.attrs.from); var jfrom = new xmpp.JID(stanza.attrs.from);
if(fromm.domain == 'conference.' + this.jid.domain) { var roomName = jfrom.user + '@' + jfrom.domain;
jabber.rooms[fromm.user].emit('presence', stanza); 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 {
if(jabber.rooms[roomName] != null) {
jabber.rooms[roomName].emit('presence', stanza.attrs.from, stanza);
} else { } else {
jabber.emit('presence', stanza.attrs.from, stanza); jabber.emit('presence', stanza.attrs.from, stanza);
} }
} }
}
if(stanza.name == 'message') { if(stanza.name == 'message') {
var from = stanza.attrs.from; var from = stanza.attrs.from;
if(stanza.attrs.type == 'groupchat') { if(stanza.attrs.type == 'groupchat') {
@ -61,6 +71,9 @@ var Client = function(params) {
jabber.presence(); jabber.presence();
jabber.emit('online'); jabber.emit('online');
jabber.askForRoster(); jabber.askForRoster();
if(callback != null) {
callback.apply(jabber);
}
}); });
this.addListener('groupchat', function(from, stanza) { this.addListener('groupchat', function(from, stanza) {
fromName = from.split('@')[0]; fromName = from.split('@')[0];
@ -70,7 +83,15 @@ var Client = function(params) {
jabber._iqCallback[id].call(jabber, stanza); jabber._iqCallback[id].call(jabber, stanza);
}); });
this.addListener('presence', function(from, stanza) { this.addListener('presence', function(from, stanza) {
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; jabber.presences[from] = stanza.attrs.type;
}
}); });
this.addListener('iq', function(stanza) { this.addListener('iq', function(stanza) {
sys.debug(stanza.getChild('query').toString().yellow); 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()); 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) { Client.prototype.room = function(name) {
if(this.rooms[name] == null) { var room = this.canonicalRoomName(name);
this.rooms[name] = new Room(this, 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() { Client.prototype.disconnect = function() {
@ -184,7 +214,7 @@ Client.prototype.disconnect = function() {
sys.debug("disconnect from XMPP"); sys.debug("disconnect from XMPP");
}; };
var Room = function(client, room) { var Room = function(client, room, callback) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
this.client = client; this.client = client;
this.room = room; this.room = room;
@ -197,7 +227,7 @@ exports.Room = Room;
Room.prototype.presence = function() { Room.prototype.presence = function() {
this.client.xmpp.send(new xmpp.Element('presence', { 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('priority').t("5").up()
.c('x', {xmlns:"http://jabber.org/protocol/muc"}) .c('x', {xmlns:"http://jabber.org/protocol/muc"})

@ -49,16 +49,15 @@ exports.testClient = function(test) {
exports.testRoom = function(test) { exports.testRoom = function(test) {
var ROOM = 'mushroom@conference.' + conf.b.jid.split('@')[1]; var ROOM = 'mushroom@conference.' + conf.b.jid.split('@')[1];
var b = new Client(conf.b); var b = new Client(conf.b, function() {
b.addListener('online', function() {
sys.debug('b is connected'.red); sys.debug('b is connected'.red);
sys.debug(('enter in ' + ROOM).green);
var b_room = b.room(ROOM); var b_room = b.room(ROOM);
var a = new Client(conf.a); var a = new Client(conf.a, function() {
a.addListener('online', function() {
sys.debug('a is connected'.green); sys.debug('a is connected'.green);
var a_room = a.room(ROOM); var a_room = a.room(ROOM);
}); });
test.done(); //test.done();
}); });
}; };

Loading…
Cancel
Save