view modules/mod_disco.js @ 43:023f767662d3

Fix compatibility with strict mode of node 0.4 and some files without licence header.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 01 Mar 2011 11:58:15 +0100
parents 07ca0263a53f
children 0d3f18bb1d36
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 5.1: Discover Features
exports.disco_info = {
	type: 'get',
	child: 'query',
	ns: NS.DISCO_INFO,
	func: function(response, stanza, request) {
		var nodeID = request.getAttribute('node');

		// SECTION 5.3: Discover Node Information
		if (nodeID && nodeID != '') {
			if (!storage.existsNode(nodeID))
				return makeError(response, errors.node_does_not_exist.n);

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

			var type = 'leaf'
			if (conf['pubsub#node_type'])
				type = conf['pubsub#node_type'];

			response.c('query', {xmlns: NS.DISCO_INFO, node: nodeID})
				.c('identity', {category: 'pubsub', type: type}).up()
				.c('feature', {'var': NS.PUBSUB}).up();

			// SECTION 5.4
			if (config.enabled('meta-data')) {
				var x = forms.build('result', 'node_metadata', storage.getMetadata(nodeID), true);
				if (x)
					response.cnode(x);
			}

		// SECTION 5.1: Discover Features
		} else {
			response.c('query', {xmlns: NS.DISCO_INFO})
				.c('identity', {category: 'pubsub', type: 'service', name: 'PubSub JavaScript Server'}).up()
				.c('feature', {'var': NS.DISCO_INFO}).up()
				.c('feature', {'var': NS.DISCO_ITEMS}).up()
				.c('feature', {'var': NS.PUBSUB}).up()
				.c('feature', {'var': 'jabber:iq:version'}).up()
				.c('feature', {'var': NS.COMMANDS}).up();

			for (var i in config.activated)
				if (typeof i == 'string')
					response.c('feature', {'var': 'http://jabber.org/protocol/pubsub#' + config.activated[i]}).up();
		}

		return response;
	}
}

// SECTION 5.2: Discover Nodes
exports.disco_items = {
	type: 'get',
	child: 'query',
	ns: NS.DISCO_ITEMS,
	func: function(response, stanza, request, to) {
		var children;
		var nodeID = request.getAttribute('node');
		if (nodeID && nodeID != '') {
			if (nodeID == NS.COMMANDS) {
				// XEP-0050: Ad-Hoc Commands

				response.c('query', {xmlns: NS.DISCO_ITEMS, node: nodeID})
					.c('item', {jid: config.jid, node: 'ping', name: 'Ping'}).up()
					.c('item', {jid: config.jid, node: 'reload', name: 'Reload'}).up();

				return response;
			} else {
				if (!storage.existsNode(nodeID))
					return makeError(response, errors.node_does_not_exist.n);

				response.c('query', {xmlns: NS.DISCO_ITEMS, node: nodeID});

				children = storage.getChildren(nodeID);
				if (typeof children == 'number')
					return makeError(response, children);
			}
		} else {
			response.c('query', {xmlns: NS.DISCO_ITEMS});

			children = storage.getChildren();
			if (typeof children == 'number')
				return makeError(response, children);
		}

		for (var nodeID in children) {
			var attr = {jid: config.jid};
			if (children[nodeID] == 'node') {
				if (config.enabled('meta-data')) {
					var metadata = storage.getMetadata(nodeID);
					if (metadata['pubsub#title'])
						attr.name = metadata['pubsub#title'];

					if (config.hideNonAccessibleNodes) {
						var configuration = storage.getConfiguration(nodeID);
						if (configuration['pubsub#access_model'] == 'whitelist') {
							var affils = storage.getAffiliationsFromNodeID(nodeID);
							var affil = affils[toBareJID(to)];
							if (!affil || affil == 'outcast')
								continue;
						}
					}
				}
				attr.node = nodeID;

			// SECTION 5.5: Discover Items for a Node
			} else
				attr.name = nodeID;

			response.c('item', attr).up();
		}

		return response;
	}
}