view modules/mod_retrieve.js @ 45:8b20f2efb939

Add an option to choose the host and port of the connection.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 08 Mar 2011 00:25:17 +0100
parents 023f767662d3
children 0d3f18bb1d36
line wrap: on
line source

/*
 *  Copyright (C) 2010  Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 *
 *  This file is part of PSĜS, a PubSub server written in JavaScript.
 *
 *  PSĜS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License.
 *
 *  PSĜS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with PSĜS.  If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

// 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), nodeID);
		} else
			var subs = storage.getSubscription(toBareJID(to));

		response.c('pubsub', {xmlns: NS.PUBSUB})
			.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})
			.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]})
					.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;
	}
}