diff psgxs.js @ 9:a6429f48e403

First implementation of XEP-0050 (ad-hoc commands).
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 27 Aug 2010 00:39:03 +0200
parents efe8dbd34780
children 44889cfb2f8c
line wrap: on
line diff
--- a/psgxs.js
+++ b/psgxs.js
@@ -70,7 +70,7 @@ function onIq(stanza) {
 
 	if (type == 'get') {
 
-		// XEP-0092
+		// XEP-0092: Software Version
 		if (stanza.getChild('query', 'jabber:iq:version')) {
 			var query = xmpp.stanza('query', {xmlns: 'jabber:iq:version'})
 				.s('name').t('PSĜS')
@@ -116,10 +116,12 @@ function onIq(stanza) {
 					.s('feature', {'var': 'http://jabber.org/protocol/disco#items'})
 					.s('feature', {'var': 'http://jabber.org/protocol/pubsub'})
 					.s('feature', {'var': 'jabber:iq:version'})
-//					.s('feature', {'var': 'http://jabber.org/protocol/commands'})
-					for (var i in config.activated)
-						if (typeof i == 'string')
-							q.s('feature', {'var': 'http://jabber.org/protocol/pubsub#' + config.activated[i]});
+					.s('feature', {'var': 'http://jabber.org/protocol/commands'});
+
+				for (var i in config.activated)
+					if (typeof i == 'string')
+						q.s('feature', {'var': 'http://jabber.org/protocol/pubsub#' + config.activated[i]});
+
 				response.cnode(q);
 			}
 
@@ -130,14 +132,25 @@ function onIq(stanza) {
 			var children;
 			var nodeID = query.getAttribute('node');
 			if (nodeID && nodeID != '') {
-				if (!storage.existsNode(nodeID))
-					return makeError(response, errors.node_does_not_exist.n);
+				if (nodeID == 'http://jabber.org/protocol/commands') {
+					// XEP-0050: Ad-Hoc Commands
+
+					q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: nodeID})
+						.s('item', {jid: componentJID, node: 'ping', name: 'Ping'})
+						.s('item', {jid: componentJID, node: 'reload', name: 'Reload'});
 
-				q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: nodeID});
+					response.cnode(q);
+					return conn.send(response);
+				} else {
+					if (!storage.existsNode(nodeID))
+						return makeError(response, errors.node_does_not_exist.n);
 
-				children = storage.getChildren(nodeID);
-				if (typeof children == 'number')
-					return makeError(response, children);
+					q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: nodeID});
+
+					children = storage.getChildren(nodeID);
+					if (typeof children == 'number')
+						return makeError(response, children);
+				}
 			} else {
 				q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#items'});
 
@@ -427,7 +440,31 @@ function onIq(stanza) {
 		} else
 			return makeError(response, errors.feature_not_implemented.n);
 	} else if (type == 'set') {
-		if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub')) {
+		if (stanza.getChild('command', 'http://jabber.org/protocol/commands')) {
+			// XEP-0050: Ad-Hoc Commands
+			var command = stanza.getChild('command', 'http://jabber.org/protocol/commands');
+
+			var action = command.getAttribute('action');
+			if (action != 'execute')
+				return makeError(response, errors.bad_request.n);
+
+			var node = command.getAttribute('node');
+			if (node == 'ping') {
+				var cmd = xmpp.stanza('command', {xmlns: 'http://jabber.org/protocol/commands',
+//						      sessionid: 'list:20020923T213616Z-700',
+						      node: node,
+						      'status': 'completed'})
+					.c('note', {type: 'info'}).t('pong');
+				response.cnode(cmd);
+			} else if (node == 'reload') {
+				storage.load();
+				response.c('command', {xmlns: 'http://jabber.org/protocol/commands',
+						       node: node,
+						       'status': 'completed'})
+					.c('note', {type: 'info'}).t('The server has correctly reloaded.');
+			} else
+				return makeError(response, errors.bad_request.n);
+		} else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub')) {
 			var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub');
 
 			// SECTION 6.1