diff modules/mod_retrieve.js @ 24:b80ab94da447

Add new modules files.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 01 Nov 2010 00:02:27 +0100
parents
children b2faacfefb90
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/modules/mod_retrieve.js
@@ -0,0 +1,132 @@
+var config = require('../configuration');
+var storage = require('../storage');
+var errors = require('../errors');
+var makeError = errors.makeError;
+var toBareJID = require('../util').toBareJID;
+var NS = require('../namespaces');
+
+// SECTION 5.6: Retrieve Subscriptions
+exports.retrieveSubscriptions = {
+	type: 'get',
+	child: 'pubsub',
+	ns: NS.PUBSUB,
+	pschild: 'subscriptions',
+	func: function(response, stanza, request) {
+		if (!config.enabled('retrieve-subscriptions'))
+			return makeError(response, errors.subscriptions_retrieval_not_supported.n);
+
+		var nodeID = request.getAttribute('node');
+		if (nodeID && nodeID != '') {
+			if (!storage.existsNode(nodeID))
+				return makeError(response, errors.node_does_not_exist.n);
+			var subs = storage.getSubscription(toBareJID(to), node);
+		} else
+			var subs = storage.getSubscription(toBareJID(to));
+
+		response.c('pubsub', {xmlns: NS.PUBSUB});
+		response.c('subscriptions');
+
+		for (i in subs)
+			response.c('subscription', {node: i, jid: to, subscription: subs[i].type, subid: subs[i].subid}).up();
+
+		return response;
+	}
+}
+
+// SECTION 5.7: Retrieve Affiliations
+exports.retrieveAffiliations = {
+	type: 'get',
+	child: 'pubsub',
+	ns: NS.PUBSUB,
+	pschild: 'affiliations',
+	func: function(response, stanza, request) {
+		if (!config.enabled('retrieve-affiliations'))
+			return makeError(response, errors.affiliations_retrieval_not_supported.n);
+
+		var nodeID = request.getAttribute('node');
+		if (nodeID && nodeID != '') {
+			if (!storage.existsNode(nodeID))
+				return makeError(response, errors.node_does_not_exist.n);
+			var affils = {};
+			affils[nodeID] = storage.getAffiliation(toBareJID(to), nodeID);
+		} else
+			var affils = storage.getAffiliationsFromJID(toBareJID(to));
+
+		response.c('pubsub', {xmlns: NS.PUBSUB});
+		response.c('affiliations');
+
+		for (i in affils)
+			response.c('affiliation', {node: i, affiliation: affils[i]}).up();
+
+		return response;
+	}
+}
+
+// SECTION 6.5: Retrieve Items from a Node
+exports.retrieveItems = {
+	type: 'get',
+	child: 'pubsub',
+	ns: NS.PUBSUB,
+	pschild: 'items',
+	func: function(response, stanza, request) {
+		if (!config.enabled('retrieve-items'))
+			return makeError(response, errors.sub.default_options.node_configuration_not_supported.n);
+
+		var nodeID = request.getAttribute('node');
+		if (!nodeID || nodeID == '')
+			return makeError(response, errors.nodeid_required.n);
+		if (!storage.existsNode(nodeID))
+			return makeError(response, errors.node_does_not_exist.n);
+
+		var configuration = storage.getConfiguration(nodeID);
+		if (configuration['pubsub#access_model'] == 'whitelist') {
+			var affil = storage.getAffiliation(toBareJID(to), nodeID);
+			if (affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'member')
+				return makeError(response, errors.pub.publish.insufficient_privileges.n);
+		}
+
+		var item = [];
+		for (var i=0; i<request.children.length; i++) {
+			var j = request.children[i];
+			if (j.name == 'item' && j.attr['id'] && j.attr['id'] != '')
+				item.push(j.attr['id']);
+		}
+
+		response.c('pubsub', {xmlns: NS.PUBSUB});
+
+		var max_items = request.getAttribute('max_items');
+		if (max_items)
+			max_items = Number (max_items);
+
+		if (item.length) {
+			response.c('items', {node: nodeID});
+
+			for (var i=0; i<item.length; i++) {
+				var j = storage.getItem(nodeID, item[i]);
+				if (typeof j == 'number')
+					return makeError(response, j);
+				if (j == errors.success)
+					continue;
+
+				response.c('item', {id: item[i]})
+				response.cnode(j).up().up();
+			}
+		} else {
+			response.c('items', {node: nodeID});
+
+			var j;
+			if (max_items)
+				j = storage.getLastItem(nodeID, max_items);
+			else
+				j = storage.getItems(nodeID);
+			if (typeof j == 'number')
+				return makeError(response, j);
+
+			var k = 0;
+			for (var i in j)
+				response.c('item', {id: i}).t(j[i].content).up();
+		}
+
+		return response;
+	}
+}