view modules/mod_manage.js @ 36:26eb015a7c40

Really should learn that “hg addremove” is good.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 10 Nov 2010 01:43:25 +0100
parents 6697f394301f
children bc717575e66a
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');

// 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(toBareJID(to), nodeID);
		if (affil != 'super-owner' && affil != 'owner')
			return makeError(response, errors.forbidden.n);

		response.c('pubsub', {xmlns: NS.PUBSUB_OWNER});
		response.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[toBareJID(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});
		response.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(toBareJID(to), nodeID);
		if (affil != 'super-owner' && affil != 'owner')
			return makeError(response, errors.forbidden.n);

		var e = false;
		var tags2 = [];
		for (i in request.tags) {
			var tag = request.tags[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.tags = 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(toBareJID(to), nodeID);
		if (affil != 'super-owner' && affil != 'owner')
			return makeError(response, errors.forbidden.n);

		var e = false;
		for (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;
	}
}