diff modules/mod_disco.js @ 24:b80ab94da447

Add new modules files.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 01 Nov 2010 00:02:27 +0100
parents
children bc717575e66a
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/modules/mod_disco.js
@@ -0,0 +1,113 @@
+var config = require('../configuration');
+var storage = require('../storage');
+var forms = require('../forms');
+var errors = require('../errors');
+var makeError = errors.makeError;
+var NS = require('../namespaces');
+
+// 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) {
+		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 i in children) {
+			var attr = {jid: config.jid};
+			if (children[i] == 'node') {
+				if (config.enabled('meta-data')) {
+					var metadata = storage.getMetadata(i);
+					if (metadata['pubsub#title'])
+						attr.name = metadata['pubsub#title'];
+				}
+				attr.node = i;
+
+			// SECTION 5.5: Discover Items for a Node
+			} else
+				attr.name = i;
+
+			response.c('item', attr).up();
+		}
+
+		return response;
+	}
+}