view modules/mod_options.js @ 38:5395a501c991

Use internal toISOString method of Data.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 16 Nov 2010 19:00:15 +0100
parents 6697f394301f
children bc717575e66a
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('../fdsq').toBareJID;
var NS = require('../namespaces');

// SECTION 6.3.2: Configure Subscription Options (Request)
exports.getConfigureSub = {
	type: 'get',
	child: 'pubsub',
	ns: NS.PUBSUB,
	child2: '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,
	child2: '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,
	child2: '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;
	}
}