1
0
Fork 0

iq error and pubsub poc

master
Matthieu Lalonde 14 years ago committed by Mathieu Lecarme
parent c610c7345e
commit 26cd9eb128

@ -32,18 +32,25 @@ var Client = function(params, callback) {
this.xmpp.addListener('stanza', function(stanza) { 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.name == 'iq') {
if(stanza.attrs.type == 'result') { switch(stanza.attrs.type) {
jabber._debug('IQ result: ' + stanza); case 'result':
jabber.emit('iqResult', stanza.attrs.id, stanza); jabber._debug('IQ result: ' + stanza);
} else { jabber.emit('iq:result', stanza.attrs.id, stanza);
jabber._debug(('IQ: ' + stanza)[jabber.color]); break;
jabber.emit('iq', stanza); case 'error':
var q = stanza.getChild('query'); jabber._debug('IQ error :' + stanza);
if(q.attrs.xmlns != null && jabber._iqHandler[q.attrs.xmlns] != null) { jabber.emit('iq:error', stanza.attrs.id, stanza);
jabber._iqHandler[q.attrs.xmlns].call(jabber, stanza); break;
} else { default:
jabber.emit('iq:unknow', stanza); jabber._debug(('IQ: ' + stanza)[jabber.color]);
} jabber.emit('iq', stanza);
var q = stanza.getChild('query');
if(q.attrs.xmlns != null && jabber._iqHandler[q.attrs.xmlns] != null) {
jabber._iqHandler[q.attrs.xmlns].call(jabber, stanza);
} else {
jabber.emit('iq:unknow', stanza);
}
break;
} }
} }
if(stanza.name == 'presence') { if(stanza.name == 'presence') {
@ -70,7 +77,12 @@ var Client = function(params, callback) {
jabber.emit('groupchat', from, stanza.getChild('body').getText(), stanza); jabber.emit('groupchat', from, stanza.getChild('body').getText(), stanza);
} else { } else {
jabber._debug('MESSAGE: ' + stanza); jabber._debug('MESSAGE: ' + stanza);
jabber.emit('message', from, stanza.getChild('body').getText(), stanza); var event_ = stanza.getChild('event', 'http://jabber.org/protocol/pubsub#event');
if(event_ != null) {
jabber.emit('pubsub:event', from, event_, stanza);
} else {
jabber.emit('message', from, stanza.getChild('body').getText(), stanza);
}
} }
} }
}); });
@ -89,7 +101,7 @@ var Client = function(params, callback) {
fromName = from.split('/')[0]; fromName = from.split('/')[0];
jabber.rooms[fromName].emit('message', from, msg, stanza); jabber.rooms[fromName].emit('message', from, msg, stanza);
}); });
this.addListener('iqResult', function(id, stanza){ this.addListener('iq:result', function(id, stanza){
jabber._iqCallback[id].call(jabber, stanza); jabber._iqCallback[id].call(jabber, stanza);
}); });
this.addListener('presence', function(from, stanza) { this.addListener('presence', function(from, stanza) {
@ -97,7 +109,7 @@ var Client = function(params, callback) {
var jfrom = new JID(stanza.attrs.from); var jfrom = new JID(stanza.attrs.from);
var roomName = jfrom.user + '@' + jfrom.domain; var roomName = jfrom.user + '@' + jfrom.domain;
if(this.rooms[roomName] != null) { if(this.rooms[roomName] != null) {
//[FIXME]
} }
} else { } else {
jabber.presences[from] = stanza.attrs.type; jabber.presences[from] = stanza.attrs.type;
@ -136,6 +148,9 @@ var Client = function(params, callback) {
this.addListener('iq', function(stanza) { this.addListener('iq', function(stanza) {
sys.debug(stanza.getChild('query').toString().yellow); sys.debug(stanza.getChild('query').toString().yellow);
}); });
this.addListener('iq:error', function(id, stanza) {
this._debug(stanza.toString().red.invert);
});
}; };
sys.inherits(Client, events.EventEmitter); sys.inherits(Client, events.EventEmitter);
@ -189,11 +204,19 @@ Client.prototype.iq = function(to, query, callback) {
this.xmpp.send(new xmpp.Element('iq', attrs).cnode(query).tree()); this.xmpp.send(new xmpp.Element('iq', attrs).cnode(query).tree());
}; };
Client.prototype.iqSet = function(query) { Client.prototype.iqSet = function(to, query, callback) {
this.xmpp.send(new xmpp.Element('iq', { var n = this._iq++;
if(callback != null) {
this._iqCallback[n] = callback;
}
var attrs = {
type:"set", type:"set",
id: this._iq++ id: n
}).cnode(query).tree()); };
if(to != null) {
attrs.to = to;
}
this.xmpp.send(new xmpp.Element('iq', attrs).cnode(query).tree());
}; };
Client.prototype.resultIq = function(iqGet, result) { Client.prototype.resultIq = function(iqGet, result) {
@ -237,6 +260,26 @@ Client.prototype.disconnect = function() {
sys.debug("disconnect from XMPP"); sys.debug("disconnect from XMPP");
}; };
Client.prototype.publish = function(to, node, publish) {
this.iqSet(to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
.cnode(publish)
.tree()
);
};
Client.prototype.suscribe = function(to, node, callback) {
if(to == null) {
to = 'pubsub.' + this.jid.domain;
}
this.iqSet(to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
.c('subscription', {
node: node,
jid: this.jid.user + '@' + this.jid.domain
})
.tree()
);
};
var Room = function(client, name, callback) { var Room = function(client, name, callback) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
this._isReady = false; this._isReady = false;

@ -80,6 +80,18 @@ exports.testRoom = function(test) {
}); });
}; };
exports.testPubSub = function(test) {
var b = new Client(conf.b, function() {
sys.debug('b is connected'.red);
this.addListener('iq:error', function(id, stanza) {
sys.debug(stanza.toString().yellow);
test.done();
});
b.suscribe(null, 'poems', function() {
});
});
};
if(module.id == '.') { if(module.id == '.') {
var testrunner = require('nodeunit').testrunner; var testrunner = require('nodeunit').testrunner;
testrunner.run([__filename]); testrunner.run([__filename]);

Loading…
Cancel
Save