diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index df71396..fc9df63 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -32,18 +32,25 @@ var Client = function(params, callback) { this.xmpp.addListener('stanza', function(stanza) { //sys.debug('STANZA: '[jabber.color] + ('<' + stanza.name + '> ').bold[jabber.color] + stanza); if(stanza.name == 'iq') { - if(stanza.attrs.type == 'result') { - jabber._debug('IQ result: ' + stanza); - jabber.emit('iqResult', stanza.attrs.id, stanza); - } else { - 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); - } + switch(stanza.attrs.type) { + case 'result': + jabber._debug('IQ result: ' + stanza); + jabber.emit('iq:result', stanza.attrs.id, stanza); + break; + case 'error': + jabber._debug('IQ error :' + stanza); + jabber.emit('iq:error', stanza.attrs.id, stanza); + break; + default: + 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') { @@ -70,7 +77,12 @@ var Client = function(params, callback) { jabber.emit('groupchat', from, stanza.getChild('body').getText(), stanza); } else { 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]; 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); }); this.addListener('presence', function(from, stanza) { @@ -97,7 +109,7 @@ var Client = function(params, callback) { var jfrom = new JID(stanza.attrs.from); var roomName = jfrom.user + '@' + jfrom.domain; if(this.rooms[roomName] != null) { - + //[FIXME] } } else { jabber.presences[from] = stanza.attrs.type; @@ -136,6 +148,9 @@ var Client = function(params, callback) { this.addListener('iq', function(stanza) { 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); @@ -189,11 +204,19 @@ Client.prototype.iq = function(to, query, callback) { this.xmpp.send(new xmpp.Element('iq', attrs).cnode(query).tree()); }; -Client.prototype.iqSet = function(query) { - this.xmpp.send(new xmpp.Element('iq', { +Client.prototype.iqSet = function(to, query, callback) { + var n = this._iq++; + if(callback != null) { + this._iqCallback[n] = callback; + } + var attrs = { type:"set", - id: this._iq++ - }).cnode(query).tree()); + id: n + }; + if(to != null) { + attrs.to = to; + } + this.xmpp.send(new xmpp.Element('iq', attrs).cnode(query).tree()); }; Client.prototype.resultIq = function(iqGet, result) { @@ -237,6 +260,26 @@ Client.prototype.disconnect = function() { 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) { events.EventEmitter.call(this); this._isReady = false; diff --git a/test/test.js b/test/test.js index f3c534b..6b4edff 100644 --- a/test/test.js +++ b/test/test.js @@ -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 == '.') { var testrunner = require('nodeunit').testrunner; testrunner.run([__filename]);