view modules/mod_configure.js @ 57:addbf6bbfaa8

Various fixes for the migration to ltx.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 12 Sep 2011 23:45:00 +0200
parents 0d3f18bb1d36
children b98e545a94f7
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.2: Configure a Node
exports.getConfigure = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB_OWNER,
	child2: 'configure',
	func: function(response, stanza, request, to) {
		if (!config.enabled('config-node'))
			return makeError(response, errors.owner.configure.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 affil = storage.getAffiliation(JID.toBare(to), nodeID);
		if (affil != 'super-owner' && affil != 'owner' && affil != 'publish-only')
			return makeError(response, errors.pub.publish.insufficient_privileges.n);

		var conf = storage.getConfiguration(nodeID);
		if (!conf)
			return makeError(response, 42); // FIXME

		response.c('pubsub', {xmlns: NS.PUBSUB_OWNER})
			.c('configure', {node: nodeID})
			.cnode(forms.build('form', 'node_config', conf, true));


		return response;
	}
}

// SECTION 8.3: Request Default Node Configuration Options
exports.default = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB_OWNER,
	child2: 'default',
	func: function(response) {
		if (!config.enabled('config-node'))
			return makeError(response, errors.owner.default_options.node_configuration_not_supported.n);

		response.c('pubsub', {xmlns: NS.PUBSUB_OWNER})
			.c('default');

		var form = forms.build('node_config', service_configuration.node_config, null, true);
		response.cnode(form);

		return response;
	}
}

// SECTION 8.2.4: Form Submission
exports.setConfigure = {
	type: 'set',
	child: 'pubsub',
	ns: NS.PUBSUB_OWNER,
	child2: 'configure',
	func: function(response, stanza, request, to) {
		if (!config.enabled('config-node'))
			return makeError(response, errors.owner.configure.node_configuration_not_supported.n);

		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' && affil != 'publish-only')
			return makeError(response, errors.forbidden.n);

		var x = request.getChild('x', 'jabber:x:data');
		if (!x)
			return makeError(response, errors.bad_request.n);

		var type = x.getAttribute('type');
		if (type == 'cancel') {
			conn.send(response);
			return;
		}
		if (type != 'submit')
			return makeError(response, errors.bad_request.n);

		var form = forms.parse(x, true);
		if (typeof form == 'number')
			return makeError(response, form);

		// TODO: verify the form.
		var conf = form.fields;

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

		return response;
	}
}

// SECTION 8.1.3: Create and Configure a Node (configure)
exports.setConfigure2 = {
	type: 'set',
	child: 'pubsub',
	ns: NS.PUBSUB,
	child2: 'configure',
	number: 1,
	func: function(response, stanza, request, to) {
		if (!config.enabled('config-node'))
			return makeError(response, errors.owner.configure.node_configuration_not_supported.n);

		var nodeID = stanza.tags[0].tags[0].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' && affil != 'publish-only')
			return makeError(response, errors.forbidden.n);

		var x = request.getChild('x', 'jabber:x:data');
		if (!x)
			return makeError(response, errors.bad_request.n);

		var type = x.getAttribute('type');
		if (type == 'cancel') {
			conn.send(response);
			return;
		}
		if (type != 'submit')
			return makeError(response, errors.bad_request.n);

		var form = forms.parse(x, true);
		if (typeof form == 'number')
			return makeError(response, form);

		// TODO: verify the form.
		var conf = form.fields;

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

		return null;
	}
}