view modules/mod_configure.js @ 41:bc717575e66a

Much better handling of modules.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 20 Feb 2011 15:51:12 +0100
parents 6697f394301f
children 023f767662d3
line wrap: on
line source

// 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(toBareJID(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});

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

		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(toBareJID(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(toBareJID(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;
	}
}