From b3f974ec941859d4d2f922103f11f3e8ee82fd26 Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Thu, 12 Aug 2010 13:56:15 +0200 Subject: [PATCH] node disco and subscribe --- lib/xmpp-client/client.js | 2 +- lib/xmpp-client/pubsub.js | 77 +++++++++++++++++++++++++++++++++++---- test/test.js | 12 ++++-- 3 files changed, 80 insertions(+), 11 deletions(-) diff --git a/lib/xmpp-client/client.js b/lib/xmpp-client/client.js index ade5f94..8f81f44 100644 --- a/lib/xmpp-client/client.js +++ b/lib/xmpp-client/client.js @@ -142,7 +142,7 @@ var Client = function(params, callback) { jabber._iqCallback[id].call(jabber, stanza); }); this.addListener('pubsub:event', function(from, node, event_, stanza) { - event_.getChildren('item').each(function(item) { + event_.getChildren('item').forEach(function(item) { jabber._pubSubCallback[from + '#' + node].call(jabber, item); }); }); diff --git a/lib/xmpp-client/pubsub.js b/lib/xmpp-client/pubsub.js index 2a662f8..c3565d1 100644 --- a/lib/xmpp-client/pubsub.js +++ b/lib/xmpp-client/pubsub.js @@ -31,7 +31,67 @@ Pubsub.prototype.createTree = function() { ); }; -Pubsub.prototype.discoverNodes = function(callback) { +Pubsub.prototype.disco = function(callback) { + var jabber = this.client; + this.client.iq(this.to, + new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#info'}), + function(iq) { + callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#info')); + } + ); +}; + +Pubsub.prototype.discoNode = function(node, callback) { + var jabber = this.client; + this.client.iq(this.to, + new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#info', node: node}), + function(iq) { + callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#info')); + } + ); +}; + +Pubsub.prototype.subscriptions = function(callback) { + var jabber = this.client; + this.client.iq(this.to, + new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}).c('subscriptions'), + function(iq) { + callback.call(jabber, iq + .getChild('pubsub', 'http://jabber.org/protocol/pubsub') + .getChild('subscriptions') + .getChildren('subscription') + ); + } + ); +}; + +Pubsub.prototype.nodeSubscriptions = function(node, callback) { + var jabber = this.client; + this.client.iq(this.to, + new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}).c('subscriptions', {node: node}), + function(iq) { + callback.call(jabber, iq + .getChild('pubsub', 'http://jabber.org/protocol/pubsub') + .getChild('subscriptions') + .getChildren('subscription') + ); + } + ); +}; + + +Pubsub.prototype.discoNodeItems = function(node, callback) { + var jabber = this.client; + this.client.iq(this.to, + new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: node}), + function(iq) { + callback.call(jabber, iq.getChild('query', 'http://jabber.org/protocol/disco#items').getChildren('item')); + } + ); +}; + + +Pubsub.prototype.discoNodes = function(callback) { var jabber = this.client; this.client.iq(this.to, new xmpp.Element('query', {xmlns: 'http://jabber.org/protocol/disco#items'}), @@ -44,7 +104,7 @@ Pubsub.prototype.discoverNodes = function(callback) { Pubsub.prototype.node = function(node, callback) { var exist = false; var pubsub = this; - this.discoverNodes(function(items) { + this.discoNodes(function(items) { items.forEach(function(item) { if(item.attrs.node == node) { exist = true; } }); @@ -82,17 +142,20 @@ Pubsub.prototype.publish = function(node, content) { ); }; -Pubsub.prototype.suscribe = function(node, callback) { +Pubsub.prototype.suscribe = function(node, onMessage, onSuscribed) { var jabber = this.client; + var pubsub = this; jabber.iqSet(this.to, new xmpp.Element('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) - .c('subscription', { + .c('subscribe', { node: node, jid: jabber.jid.user + '@' + jabber.jid.domain }) .tree(), - function(stanza) { - sys.debug(stanza.toString().yellow); - jabber._pubSubCallback[to + '#' + node] = callback; + function(iq) { + sys.debug(('Suscribe to ' + node).yellow); + jabber._pubSubCallback[pubsub.to + '#' + node] = onMessage; + var s = iq.getChild('pubsub', 'http://jabber.org/protocol/pubsub').attrs; + onSuscribed.call(jabber, s.subscription, s.subid); } ); }; diff --git a/test/test.js b/test/test.js index c54f553..7597db9 100644 --- a/test/test.js +++ b/test/test.js @@ -91,12 +91,18 @@ exports.testPubSub = function(test) { test.done(); }); var p = b.pubsub(); + /*p.discoNode(POEMS, function(r) { + sys.debug(r); + });*/ p.node(POEMS, function() { sys.debug('got my node'.yellow); - p.publish(POEMS, new xmpp.Element('entry', {xmlns: 'http://www.w3.org/2005/Atom'}).c('title').t('blab blah').tree()); - /*p.suscribe(POEMS, function(item) { + p.suscribe(POEMS, function(item) { sys.debug('SUSCRIBE : ' + item.toString().yellow); - });*/ + test.done(); + }, + function(subsription, id) { + p.publish(POEMS, new xmpp.Element('entry', {xmlns: 'http://www.w3.org/2005/Atom'}).c('title').t('blab blah').tree()); + }); //test.done(); }); /*