view modules/mod_options.js @ 48:c979c4d0932b

Little fixes for strict mode compatibility.
author Sonny Piers <sonny.piers@gmail.com>
date Sat, 19 Mar 2011 14:57:13 +0100
parents 0d3f18bb1d36
children
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 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 (JID.toBare(jid) != JID.toBare(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})
			.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 || JID.toBare(jid) != JID.toBare(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;
	}
}