view modules/mod_options.js @ 28:7cfcd7d5796c

Replace Paul Sowden’s ISO8601 functions by my own, since their licence is possibly incompatible with AGPL.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 01 Nov 2010 03:01:40 +0100
parents b80ab94da447
children b2faacfefb90
line wrap: on
line source

var config = require('../configuration');
var storage = require('../storage');
var forms = require('../forms');
var errors = require('../errors');
var makeError = errors.makeError;
var toBareJID = require('../util').toBareJID;
var NS = require('../namespaces');

// SECTION 6.3.2: Configure Subscription Options (Request)
exports.configureSub = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB,
	pschild: 'options',
	func: function(response, stanza, request, to) {
		if (!config.enabled('subscription-options'))
			return makeError(response, errors.sub.configure.subscription_options_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 jid = request.getAttribute('jid');
		if (!jid)
			return makeError(response, errors.sub.configure.subscriber_jid_required.n);
		if (toBareJID(jid) != toBareJID(to))
			return makeError(response, errors.sub.configure.insufficient_privileges.n);

		var subs = storage.getSubscription(jid, nodeID);
		if (!subs.subid) // FIXME: better test for empty object.
			return makeError(response, errors.sub.configure.no_such_subscriber.n);

		response.c('pubsub', {xmlns: NS.PUBSUB});
		response.c('options', {node: nodeID, jid: jid});

		var form = forms.build('form', 'subscribe_options', subs.options, true);
		response.cnode(form);

		return response;
	}
}

// SECTIONS 6.3.5: Form Submission
exports.configureSub = {
	type: 'set',
	child: 'pubsub',
	ns: NS.PUBSUB,
	pschild: 'options',
	func: function(response, stanza, request, to) {
		if (!config.enabled('subscription-options'))
			return makeError(response, errors.sub.subscribe.not_supported.n);

		var nodeID = unsubscribe.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 jid = unsubscribe.getAttribute('jid');
		if (!jid || toBareJID(jid) != toBareJID(to))
			return makeError(response, errors.sub.unsubscribe.insufficient_privileges.n);

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

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

		var set = storage.configureSubscription(nodeID, jid, form);
		if (typeof form == 'number')
			return makeError(response, form);

		return response;
	}
}

// SECTION 6.4: Request Default Subscription Configuration Options
exports.defaultSub = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB,
	pschild: 'default',
	func: function(response, stanza, request) {
		if (!config.enabled('retrieve-default-sub'))
			return makeError(response, errors.sub.default_options.default_subscription_configuration_retrieval_not_supported.n);

		var nodeID = request.getAttribute('node');
		if (nodeID && !storage.existsNode(nodeID))
			return makeError(response, errors.node_does_not_exist.n);

		response.c('pubsub', {xmlns: NS.PUBSUB});
		var s;
		if (nodeID)
			response.c('default', {node: nodeID});
		else
			response.c('default');

		var form = forms.build('form', 'subscribe_options', 'default', false);
		response.cnode(form);

		return response;
	}
}