view modules/mod_retrieve.js @ 37:45d0cd9388ea

Fix mod_retrieve.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 10 Nov 2010 18:31:02 +0100
parents 6697f394301f
children 4c808bfe0658
line wrap: on
line source

var config = require('../configuration');
var storage = require('../storage');
var errors = require('../errors');
var makeError = errors.makeError;
var toBareJID = require('../fdsq').toBareJID;
var NS = require('../namespaces');

console.log(require('sys').inspect(toBareJID, false, null));

// SECTION 5.6: Retrieve Subscriptions
exports.retrieveSubscriptions = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB,
	child2: 'subscriptions',
	func: function(response, stanza, request, to) {
		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,
	child2: 'affiliations',
	func: function(response, stanza, request, to) {
		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,
	child2: 'items',
	func: function(response, stanza, request, to) {
		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;
	}
}