view modules/mod_manage.js @ 58:b98e545a94f7 default tip

Always use children instead of tags. Might break something.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 13 Sep 2011 00:54:55 +0200
parents c2c9c06d6e9d
children
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 8.8.1: Retrieve Subscriptions List
exports.manageRetrieveSub = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB_OWNER,
	child2: 'subscriptions',
	func: function(response, stanza, request, to) {
		if (!config.enabled('manage-subscriptions'))
			return makeError(response, errors.owner.manage_subscriptions.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 affil = storage.getAffiliation(JID.toBare(to), nodeID);
		if (affil != 'super-owner' && affil != 'owner')
			return makeError(response, errors.forbidden.n);

		response.c('pubsub', {xmlns: NS.PUBSUB_OWNER})
			.c('subscriptions', {node: nodeID});

		var subs = storage.getSubscriptionsFromNodeID(nodeID)
		for (var jid in subs)
			response.c('subscription', {jid: jid, subscription: subs[jid].type, subid: subs[jid].subid}).up();

		return response;
	}
}

// SECTION 8.9.1: Retrieve Affiliations List
exports.manageRetrieveAff = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB_OWNER,
	child2: 'affiliations',
	func: function(response, stanza, request, to) {
		if (!config.enabled('modify-affiliations'))
			return makeError(response, errors.owner.manage_affiliations.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 affils = storage.getAffiliationsFromNodeID(nodeID);
		var affil = affils[JID.toBare(to)];
		if (affil != 'super-owner' && affil != 'owner')
			return makeError(response, errors.owner.manage_affiliations.retrieve_list.entity_is_not_an_owner.n);

		response.c('pubsub', {xmlns: NS.PUBSUB_OWNER})
			.c('affiliations', {node: nodeID});

		for (var jid in affils)
			response.c('affiliation', {jid: jid, affiliation: affils[jid]}).up();

		return response;
	}
}

// SECTION 8.8.2: Modify Subscriptions
exports.modifySub = {
	type: 'set',
	child: 'pubsub',
	ns: NS.PUBSUB_OWNER,
	child2: 'subscriptions',
	func: function(response, stanza, request, to) {
		if (!config.enabled('manage-subscriptions'))
			return makeError(response, errors.owner.manage_subscriptions.not_supported.n); //XXX

		var nodeID = request.getAttribute('node');
		if (!nodeID)
			return makeError(response, errors.nodeid_required.n);
		if (!storage.existsNode(nodeID))
			return makeError(response, errors.node_does_not_exist.n);

		var affil = storage.getAffiliation(JID.toBare(to), nodeID);
		if (affil != 'super-owner' && affil != 'owner')
			return makeError(response, errors.forbidden.n);

		var e = false;
		var tags2 = [];
		for (i in request.children) {
			var tag = request.children[i];
			var jid = tag.getAttribute('jid');
			var sub = tag.getAttribute('subscription');

			if (sub == 'none' || sub == 'pending' || sub == 'subscribed' || sub == 'unconfigured') {
				var set = storage.subscribe(nodeID, jid, sub);

				if (typeof set == 'number') {
					e = true;
					tags2.push(tag);
				} else {
					// SECTION 8.8.4
					notifs.send(jid, 'subscription', nodeID, {jid: jid, subscription: sub});
				}
			} else {
				e = true;
				tags2.push(tag);
			}
		}

		request.children = tags2;

		if (e)
			return makeError(response, errors.owner.manage_subscriptions.modify.multiple_simultaneous_modifications.n, pubsub);

		return response;
	}
}

// SECTION 8.9.2: Modify Affiliation
exports.modifyAff = {
	type: 'set',
	child: 'pubsub',
	ns: NS.PUBSUB_OWNER,
	child2: 'affiliations',
	func: function(response, stanza, request, to) {
		if (!config.enabled('modify-affiliations'))
			return makeError(response, errors.owner.manage_affiliations.not_supported.n); //XXX

		var nodeID = request.getAttribute('node');
		if (!nodeID)
			return makeError(response, errors.nodeid_required.n);
		if (!storage.existsNode(nodeID))
			return makeError(response, errors.node_does_not_exist.n);

		var affil = storage.getAffiliation(JID.toBare(to), nodeID);
		if (affil != 'super-owner' && affil != 'owner')
			return makeError(response, errors.forbidden.n);

		var e = false;
		for (var i in request.children) {
			var jid = request.children[i].getAttribute('jid');
			var affiliation = request.children[i].getAttribute('affiliation');

			var set = storage.setAffiliation(nodeID, jid, affiliation);
			if (typeof set == 'number')
				e = true;
			else {
				// SECTION 8.9.4
				notifs.send(jid, 'affiliations', nodeID, {jid: jid, affiliation: affiliation});
				request.children.splice(i, 1);
			}
		}

		if (e)
			return makeError(response, errors.owner.manage_affiliations.modify.multiple_simultaneous_modifications.n, pubsub);

		return response;
	}
}