diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index 9198c75..b0b60ab 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -2,7 +2,8 @@ var sys = require('sys'), xmpp = require('node-xmpp'), colors = require('colors'), events = require('events'), - Room = require('./room').Room; + Room = require('./room').Room, + Pubsub = require('./pubsub').Pubsub; var BasicClient = function(params) { events.EventEmitter.call(this); @@ -61,6 +62,7 @@ var Client = function(params, callback) { this._isReady = false; this._iqHandler = {}; this._iqCallback = {}; + this._pubSubCallback = {}; this.presences = {}; this.roster = {}; this.xmpp.addListener('stanza', function(stanza) { @@ -113,7 +115,7 @@ var Client = function(params, callback) { jabber._debug('MESSAGE: ' + stanza); var event_ = stanza.getChild('event', 'http://jabber.org/protocol/pubsub#event'); if(event_ != null) { - jabber.emit('pubsub:event', from, event_, stanza); + jabber.emit('pubsub:event', from, event_.getChild('items').attrs.node, event_, stanza); } else { jabber.emit('message', from, stanza.getChild('body').getText(), stanza); } @@ -138,6 +140,11 @@ var Client = function(params, callback) { this.addListener('iq:result', function(id, stanza){ jabber._iqCallback[id].call(jabber, stanza); }); + this.addListener('pubsub:event', function(from, node, event_, stanza) { + event_.getChildren('item').each(function(item) { + jabber._pubSubCallback[from + '#' + node].call(jabber, item); + }); + }); this.addListener('presence', function(from, stanza) { if(stanza.attrs.type == 'error') { var jfrom = new JID(stanza.attrs.from); @@ -270,23 +277,6 @@ 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.pubsub = function(to) { + return new Pubsub(this, to); }; - -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() - ); -}; - diff --git a/lib/xmpp-client/pubsub.js b/lib/xmpp-client/pubsub.js new file mode 100644 index 0000000..89cec74 --- /dev/null +++ b/lib/xmpp-client/pubsub.js @@ -0,0 +1,92 @@ +var sys = require('sys'), + xmpp = require('node-xmpp'), + colors = require('colors'), + events = require('events'); + +function Pubsub(client, to) { + this.client = client; + if(to == null) { + to = 'pubsub.' + this.client.jid.domain; + } + this.to = to; +}; + +sys.inherits(Pubsub, events.EventEmitter); + +exports.Pubsub = Pubsub; + +Pubsub.prototype.createTree = function() { + sys.debug(new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) + .c('create', {node: '/home/' + this.client.jid.domain + '/user'}).up() + .c('configure') + .tree()); + this.client.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) + .c('create', {node: '/home/' + this.client.jid.domain + '/user'}).up() + .c('configure') + .tree(), + function(stanza) { + sys.debug(stanza.toString().yellow); + sys.debug(stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub').toString.yellow); + } + ); +}; + +Pubsub.prototype.discoverNodes = function(callback) { + var jabber = this.client; + this.client.iq(this.to, + new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#items'}), + function(iq) { + callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#items').getChildren('item')); + } + ); +}; + +Pubsub.prototype.node = function(node, callback) { + var exist = false; + var pubsub = this; + this.discoverNodes(function(items) { + items.forEach(function(item) { + if(item.attrs.node == node) { exist = true; } + }); + if(! exist) { pubsub.createNode(node, callback); } + else { callback.call(pubsub.client); } + }); +}; + +Pubsub.prototype.createNode = function(node, callback) { + var jabber = this.client; + this.client.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) + .c('create', {node: node}).up() + .c('configure') + .tree(), + function(stanza) { + var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub'); + if(pubsub != null) { + sys.debug(pubsub.toString().yellow); + callback.call(jabber); + } + } + ); +}; + +Pubsub.prototype.publish = function(node, publish) { + this.client.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) + .cnode(publish) + .tree() + ); +}; + +Pubsub.prototype.suscribe = function(node, callback) { + var jabber = this.client; + jabber.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) + .c('subscription', { + node: node, + jid: jabber.jid.user + '@' + jabber.jid.domain + }) + .tree(), + function(stanza) { + sys.debug(stanza.toString().yellow); + jabber._pubSubCallback[to + '#' + node] = callback; + } + ); +}; diff --git a/test/test.js b/test/test.js index ac8d923..5d6122a 100644 --- a/test/test.js +++ b/test/test.js @@ -47,6 +47,7 @@ exports.testClient = function(test) { }; */ +/* exports.testRoom = function(test) { test.expect(1); var ROOM = 'mushroom@conference.' + conf.b.jid.split('@')[1]; @@ -79,16 +80,29 @@ exports.testRoom = function(test) { }); }); }; - +*/ exports.testPubSub = function(test) { + var POEMS = 'poems'; 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() { + var p = b.pubsub(); + p.node(POEMS, function() { + sys.debug('got my node'.yellow); + p.suscribe(POEMS, function(item) { + sys.debug(item.toString().yellow); + }); + //test.done(); + }); + /* + b.suscribe(null, POEMS, function(item) { + sys.debug(item.attrs.id.yellow); }); + */ + //b.publish(null, POEMS, ); }); };