tools/form2table.lua
author Kim Alvefur <zash@zash.se>
Sat, 23 Mar 2024 20:48:19 +0100
changeset 13465 c673ff1075bd
parent 13146 879a6a33c21b
permissions -rw-r--r--
mod_posix: Move everything to util.startup This allows greater control over the order of events. Notably, the internal ordering between daemonization, initialization of libunbound and setup of signal handling is sensitive. libunbound starts a separate thread for processing DNS requests. If this thread is started before signal handling has been set up, it will not inherit the signal handlers and instead behave as it would have before signal handlers were set up, i.e. cause the whole process to immediately exit. libunbound is usually initialized on the first DNS request, usually triggered by an outgoing s2s connection attempt. If daemonization happens before signals have been set up, signals may not be processed at all.

-- Read an XML dataform and spit out a serialized Lua table of it
if not pcall(require, "prosody.loader") then
	pcall(require, "loader");
end

local function 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;
			type = tag.attr.type;
			label = tag.attr.label;
			desc = tag:get_child_text("desc");
			required = tag:get_child("required") and true or nil;
			value = tag:get_child_text("value");
			options = nil;
		};

		if field.type == "list-single" or field.type == "list-multi" then
			local options = {};
			for option in tag:childtags("option") do
				options[#options+1] = { label = option.attr.label, value = option:get_child_text("value") };
			end
			field.options = options;
		end

		if field.type == "jid-multi" or field.type == "list-multi" or field.type == "text-multi" then
			local values = {};
			for value in tag:childtags("value") do
				values[#values+1] = value:get_text();
			end
			if field.type == "text-multi" then
				values = table.concat(values, "\n");
			end
			field.value = values;
		end

		if field.type == "boolean" then
			field.value = field.value == "true" or field.value == "1";
		end

		layout[#layout+1] = field;

	end
	return layout;
end

print("dataforms.new " .. require"prosody.util.serialization".serialize(from_stanza(require"prosody.util.xml".parse(io.read("*a"))),
	{ unquoted = true }))