Skip to content

Commit

Permalink
Start sending some status codes for proper MUC support
Browse files Browse the repository at this point in the history
  • Loading branch information
Aredridel authored and aredridel committed Nov 1, 2013
1 parent 1050ded commit 8959386
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions lib/xmpp-muc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ Channel.prototype.send = function send(stanza) {
}
};

Channel.prototype.sendButFor = function sendButFor(stanza, user) {
for (var k in this.users) {
if (this.users[k].jid == user.jid) continue;
stanza.attrs.to = this.users[k];
console.log("XMPP> "+stanza.toString());
this.router.send(stanza);
}
};

var MUC = function(router) {
var Channels = {};
var muc = this;
Expand All @@ -42,6 +51,7 @@ var MUC = function(router) {
var to = new xmpp.JID(stanza.attrs.to);
var channeljid = new xmpp.JID(to.user + '@' + to.domain);
var channel = muc.getChannel(channeljid);
var origUser = stanza.attrs.from;

if(stanza.name == 'presence') {
if(stanza.attrs.type !=='unavailable') {
Expand All @@ -64,28 +74,47 @@ var MUC = function(router) {
channel.by_jid[stanza.attrs.from] = to.resource;
channel.emit('join', channel.users[to.resource], to.resource);
}
}
}

stanza.attrs.from = to.user + '@' + to.domain +'/'+(channel.by_jid[stanza.attrs.from]);
channel.send(stanza);
// FIXME: propagate all presences back to joiner
stanza.attrs.from = to.user + '@' + to.domain +'/'+(channel.by_jid[stanza.attrs.from]);
channel.sendButFor(stanza, stanza.from);

if(stanza.attrs.type =='unavailable') {
// Clear presence
channel.emit('part', channel.users[to.resource], stanza.attrs.from);
// FIXME: can be spoofed:
delete channel.users[to.resource];
delete channel.by_jid[stanza.attrs.from];
}
stanza = (new xmpp.Element('presence', {xmlns: 'jabber:client', id: stanza.attrs.id, to: origUser, from: to.user + '@' + to.domain + '/' + (channel.by_jid[origUser])}))
.c('x', {xmlns: 'http://jabber.org/protocol/muc#user'})
.c('item', {affiliation: 'owner', role: 'moderator'}).up()
.c('status', {code: 110}).up()
.c('status', {code: 201}).up()
.up();
stanza.attrs.to = origUser;
console.log("XMPP> "+stanza.toString());
router.send(stanza);
} else {
// Clear presence
channel.emit('part', channel.users[to.resource], stanza.attrs.from);

}
stanza.attrs.from = to.user + '@' + to.domain +'/'+(channel.by_jid[stanza.attrs.from]);
channel.send(stanza);

// FIXME: can be spoofed:
delete channel.users[to.resource];
delete channel.by_jid[stanza.attrs.from];
}
} else {
stanza.attrs.from = to.user + '@' + to.domain +'/'+(channel.by_jid[stanza.attrs.from]);
channel.send(stanza);
}
}
};
};

util.inherits(MUC, events.EventEmitter);

function createMUCService(router) {
return new MUC(router);
var n = new MUC(router);
router.on('stanza', function (stanza) {
n.handler(stanza);
});
return n;
}

exports.createMUCService = createMUCService;

0 comments on commit 8959386

Please sign in to comment.