Changeset

3877:562b34050561

mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms) This allows calling simple commands like the one provided by mod_uptime
author Kim Alvefur <zash@zash.se>
date Tue, 04 Feb 2020 21:49:14 +0100
parents 3876:75b330d4fa6f
children 3878:9a3dfe0bf9fd
files mod_rest/jsonmap.lib.lua
diffstat 1 files changed, 45 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/jsonmap.lib.lua	Sat Feb 01 13:03:18 2020 +0100
+++ b/mod_rest/jsonmap.lib.lua	Tue Feb 04 21:49:14 2020 +0100
@@ -126,6 +126,50 @@
 		end;
 	};
 
+	-- XEP-0050: Ad-Hoc Commands
+	command = {"func", "http://jabber.org/protocol/commands", "command",
+		function (s)
+			local cmd = {
+				action = s.attr.action,
+				node = s.attr.node,
+				sessionid = s.attr.sessionid,
+				status = s.attr.status,
+			};
+			local actions = s:get_child("actions");
+			local note = s:get_child("note");
+			-- TODO -- local form = s:get_child("x", "jabber:x:data");
+			if actions then
+				cmd.actions = {
+					execute = actions.attr.execute,
+				};
+				for action in actions:childtags() do
+					cmd.actions[action.name] = true
+				end
+			elseif note then
+				cmd.note = {
+					type = note.attr.type;
+					text = note:get_text();
+				};
+			end
+			return cmd;
+		end;
+		function (s)
+			if type(s) == "table" and s ~= json.null then
+				local cmd = st.stanza("command", {
+						xmlns  = "http://jabber.org/protocol/commands",
+						action = s.action,
+						node = s.node,
+						sessionid = s.sessionid,
+						status = s.status,
+					});
+				return cmd;
+			elseif type(s) == "string" then -- assume node
+				return st.stanza("command", { xmlns  = "http://jabber.org/protocol/commands", node = s });
+			end
+			-- else .. missing required attribute
+		end;
+	};
+
 	-- XEP-0066: Out of Band Data
 	oob_url = {"func", "jabber:iq:oob", "query",
 		function (s)
@@ -165,6 +209,7 @@
 	items = "iq",
 	ping = "iq",
 	version = "iq",
+	command = "iq",
 
 	body = "message",
 	html = "message",