clix.adhoc: Move stanza to dataform converter here default tip

Sat, 24 Jun 2023 09:59:07 +0200

author
Kim Alvefur <zash@zash.se>
date
Sat, 24 Jun 2023 09:59:07 +0200
changeset 170
0d561f921c13
parent 169
4e67d34c1298

clix.adhoc: Move stanza to dataform converter here

Removes the need for verse to have a custom util.dataforms fork only for
this

clix/adhoc.lua file | annotate | diff | comparison | revisions
--- a/clix/adhoc.lua	Sat Jun 24 09:49:03 2023 +0200
+++ b/clix/adhoc.lua	Sat Jun 24 09:59:07 2023 +0200
@@ -1,5 +1,54 @@
 local dataforms = require "prosody.util.dataforms";
 
+local xmlns_validate = 'http://jabber.org/protocol/xdata-validate';
+local function dataform_from_stanza(stanza)
+	local layout = {
+		title = stanza:get_child_text("title");
+		instructions = stanza:get_child_text("instructions");
+	};
+	for tag in stanza:childtags("field") do
+		local field = {
+			name = tag.attr.var;
+			label = tag.attr.label;
+			type = tag.attr.type;
+			required = tag:get_child("required") and true or nil;
+			value = tag:get_child_text("value");
+		};
+		layout[#layout+1] = field;
+		if field.type then
+			local value = {};
+			if field.type:match"list%-" then
+				for tag in tag:childtags("option") do
+					value[#value+1] = { label = tag.attr.label, value = tag:get_child_text("value") };
+				end
+				for tag in tag:childtags("value") do
+					value[#value+1] = { label = tag.attr.label, value = tag:get_text(), default = true };
+				end
+			elseif field.type:match"%-multi" then
+				for tag in tag:childtags("value") do
+					value[#value+1] = tag.attr.label and { label = tag.attr.label, value = tag:get_text() } or tag:get_text();
+				end
+				if field.type == "text-multi" then
+					field.value = table.concat(value, "\n");
+				else
+					field.value = value;
+				end
+			end
+		end
+		local datatype_tag = tag:get_child("validate", xmlns_validate);
+		if datatype_tag then
+			field.datatype = datatype_tag.attr.datatype;
+			local range_tag = datatype_tag:get_child("range");
+			if range_tag then
+				field.range_min = tonumber(range_tag.attr.min);
+				field.range_max = tonumber(range_tag.attr.max);
+			end
+		end
+
+	end
+	return dataforms.new(layout);
+end
+
 -- TODO Cleanup, commit
 return function (opts, arg)
 	if opts.short_help then
@@ -22,7 +71,7 @@
 							data[k] = v; --FIXME multiple
 						end
 					end
-					local command_form_layout = dataforms.from_stanza(cmd.form)
+					local command_form_layout = dataform_from_stanza(cmd.form)
 					if opts.interactive then
 						for i=1,#command_form_layout do
 							local item = command_form_layout[i];
@@ -56,7 +105,7 @@
 					cmd:next(command_form_layout:form(data, "submit"));
 				elseif cmd.status == "completed" then
 					if cmd.form then
-						local command_form_layout = dataforms.from_stanza(cmd.form)
+						local command_form_layout = dataform_from_stanza(cmd.form)
 						local data = command_form_layout:data(cmd.form);
 						if data.title then
 							print("= " .. data.title .. " =");

mercurial