view modules/mod_owner.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 8.1: Create a Node
exports.create = {
	type: 'set',
	child: 'pubsub',
	ns: NS.PUBSUB,
	child2: 'create',
	func: function(response, stanza, request, to) {
		if (!config.enabled('create-nodes'))
			return makeError(response, errors.owner.create.node_creation_not_supported.n);

		var instant = false;

		var nodeID = request.getAttribute('node');
		if (!nodeID || nodeID == '') {
			if (!config.enabled('instant-nodes'))
				return makeError(response, errors.owner.create.instant_nodes_not_supported.n);
			nodeID = fdsq.makeRandomId();
			instant = true;
		}
		if (storage.existsNode(nodeID))
			return makeError(response, errors.owner.create.nodeid_already_exists.n);

		var bare = toBareJID(to);
		var right = false;

		// Check for super-owner
		for (var i in config.owner)
			if (config.owner[i] == bare)
				right = true;

		// Check for authorized user
		for (var i in config.allowCreateNode)
			if (config.allowCreateNode[i].exec(bare))
				right = true;

		if (!right)
			return makeError(response, errors.forbidden.n);

		conf = {};
		conf['pubsub#creator'] = bare;

		var r = storage.createNode(nodeID, conf);
		if (typeof r == 'number')
			return makeError(response, r);

		if (instant)
			response.c('pubsub', {xmlns: NS.PUBSUB})
				.c('create', {node: nodeID});

		return response;
	}
}

// SECTION 8.4: Delete a Node
exports['delete'] = {
	type: 'set',
	child: 'pubsub',
	ns: NS.PUBSUB_OWNER,
	child2: 'delete',
	func: function(response, stanza, request, to) {
		if (!config.enabled('delete-nodes'))
			return makeError(response, errors.feature_not_implemented.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 notifications = storage.deleteNode(nodeID);
		if (typeof notifications == 'number')
			return makeError(response, r);

		notifs.send(notifications, 'delete', nodeID);

		return response;
	}
}

// SECTION 8.5: Purge All Node Items
exports.purge = {
	type: 'set',
	child: 'pubsub',
	ns: NS.PUBSUB_OWNER,
	child2: 'purge',
	func: function(response, stanza, request, to) {
		if (!config.enabled('purge-nodes'))
			return makeError(response, errors.owner.purge.node_purging_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);

		if (!config.enabled('persistent-items')) //FIXME: autre condition, supporté par le node
			return makeError(response, errors.owner.purge.node_does_not_persist_items.n);

		var notifications = storage.purgeNode(nodeID);
		if (typeof notifications == 'number')
			return makeError(response, r);

		notifs.send(notifications, 'purge', nodeID);

		return response;
	}
}