view modules/mod_owner.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 forms = require('../forms');
var notifs = require('../notifs');
var errors = require('../errors');
var makeError = errors.makeError;
var fdsq = require('../fdsq');
var toBareJID = fdsq.toBareJID;
var NS = require('../namespaces');

// 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;
	}
}