aboutsummaryrefslogtreecommitdiffstats
path: root/util/dataforms.lua
blob: ba751f6f7dbc0bf8f52e3768f01d645ab3942d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

module "dataforms"

local xmlns_forms = 'jabber:x:data';

local form_t = {};
local form_mt = { __index = form_t };

function new(layout)
	return setmetatable(layout, form_mt);
end

local form_x_attr = { xmlns = xmlns_forms };

function form_t.form(layout, data)
	local form = st.tag("x", form_x_attr);
	for n, field in ipairs(layout) do
		local field_type = field.type;
		-- Add field tag
		form:tag("field", { type = field_type, var = field.name });

		local value = data[field.name];
		
		-- Add value, depending on type
		if field_type == "hidden" then
			if type(value) == "table" then
				-- Assume an XML snippet
				form:add_child(value);
			elseif value then
				form:text(tostring(value));
			end
		elseif field_type == "boolean" then
			form:text((value and "1") or "0");
		elseif field_type == "fixed" then
			
		elseif field_type == "jid-multi" then
			for _, jid in ipairs(value) do
				form:tag("value"):text(jid):up();
			end
		elseif field_type == "jid-single" then
			form:tag("value"):text(value):up();
			
		end
		
		-- Jump back up to list of fields
		form:up();
	end
end

function form_t.data(layout, stanza)
	
end



--[[

Layout:
{

	title = "MUC Configuration",
	instructions = [[Use this form to configure options for this MUC room.]],

	{ name = "FORM_TYPE", type = "hidden", required = true };
	{ name = "field-name", type = "field-type", required = false };
}


--]]