util/adhoc.lua
author Kim Alvefur <zash@zash.se>
Wed, 27 Mar 2024 19:33:11 +0100
changeset 13471 c2a476f4712a
parent 11356 e10567199f02
permissions -rw-r--r--
util.startup: Fix exiting on pidfile trouble prosody.shutdown() relies on prosody.main_thread, which has not been set yet at this point. Doing a clean shutdown might actually be harmful in case it tears down things set up by the conflicting Prosody, such as the very pidfile we were looking at. Thanks again SigmaTel71 for noticing

-- luacheck: ignore 212/self

local function new_simple_form(form, result_handler)
	return function(self, data, state)
		if state or data.form then
			if data.action == "cancel" then
				return { status = "canceled" };
			end
			local fields, err = form:data(data.form);
			return result_handler(fields, err, data);
		else
			return { status = "executing", actions = {"next", "complete", default = "complete"}, form = form }, "executing";
		end
	end
end

local function new_initial_data_form(form, initial_data, result_handler)
	return function(self, data, state)
		if state or data.form then
			if data.action == "cancel" then
				return { status = "canceled" };
			end
			local fields, err = form:data(data.form);
			return result_handler(fields, err, data);
		else
			local values, err = initial_data(data);
			if type(err) == "table" then
				return {status = "error"; error = err}
			elseif type(err) == "string" then
				return {status = "error"; error = {type = "cancel"; condition = "internal-server-error", err}}
			end
			return { status = "executing", actions = {"next", "complete", default = "complete"},
				 form = { layout = form, values = values } }, "executing";
		end
	end
end

return { new_simple_form = new_simple_form,
	 new_initial_data_form = new_initial_data_form };