1 local setmetatable = setmetatable;
2 local pairs, ipairs = pairs, ipairs;
3 local st = require "util.stanza";
7 local xmlns_forms = 'jabber:x:data';
10 local form_mt = { __index = form_t };
13 return setmetatable(layout, form_mt);
16 local form_x_attr = { xmlns = xmlns_forms };
18 function form_t.form(layout, data)
19 local form = st.stanza("x", form_x_attr);
21 form:tag("title"):text(layout.title):up();
23 if layout.instructions then
24 form:tag("instructions"):text(layout.instructions):up();
26 for n, field in ipairs(layout) do
27 local field_type = field.type or "text-single";
29 form:tag("field", { type = field_type, var = field.name, label = field.label });
31 local value = data[field.name];
33 -- Add value, depending on type
34 if field_type == "hidden" then
35 if type(value) == "table" then
36 -- Assume an XML snippet
37 form:add_child(value);
39 form:text(tostring(value));
41 elseif field_type == "boolean" then
42 form:tag("value"):text((value and "1") or "0");
43 elseif field_type == "fixed" then
45 elseif field_type == "jid-multi" then
46 for _, jid in ipairs(value) do
47 form:tag("value"):text(jid):up();
49 elseif field_type == "jid-single" then
50 form:tag("value"):text(value):up();
51 elseif field_type == "text-single" or field_type == "text-private" then
52 form:tag("value"):text(value):up();
53 elseif field_type == "text-multi" then
54 -- Split into multiple <value> tags, one for each line
55 for line in value:gmatch("([^\r\n]+)\r?\n*") do
56 form:tag("value"):text(line):up();
60 -- Jump back up to list of fields
66 function form_t.data(layout, stanza)
78 title = "MUC Configuration",
79 instructions = [[Use this form to configure options for this MUC room.]],
81 { name = "FORM_TYPE", type = "hidden", required = true };
82 { name = "field-name", type = "field-type", required = false };