2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local setmetatable = setmetatable;
10 local pairs, ipairs = pairs, ipairs;
11 local tostring, type = tostring, type;
12 local t_concat = table.concat;
13 local st = require "util.stanza";
17 local xmlns_forms = 'jabber:x:data';
20 local form_mt = { __index = form_t };
23 return setmetatable(layout, form_mt);
26 function form_t.form(layout, data, formtype)
27 local form = st.stanza("x", { xmlns = xmlns_forms, type = formtype or "form" });
29 form:tag("title"):text(layout.title):up();
31 if layout.instructions then
32 form:tag("instructions"):text(layout.instructions):up();
34 for n, field in ipairs(layout) do
35 local field_type = field.type or "text-single";
37 form:tag("field", { type = field_type, var = field.name, label = field.label });
39 local value = (data and data[field.name]) or field.value;
42 -- Add value, depending on type
43 if field_type == "hidden" then
44 if type(value) == "table" then
45 -- Assume an XML snippet
50 form:tag("value"):text(tostring(value)):up();
52 elseif field_type == "boolean" then
53 form:tag("value"):text((value and "1") or "0"):up();
54 elseif field_type == "fixed" then
56 elseif field_type == "jid-multi" then
57 for _, jid in ipairs(value) do
58 form:tag("value"):text(jid):up();
60 elseif field_type == "jid-single" then
61 form:tag("value"):text(value):up();
62 elseif field_type == "text-single" or field_type == "text-private" then
63 form:tag("value"):text(value):up();
64 elseif field_type == "text-multi" then
65 -- Split into multiple <value> tags, one for each line
66 for line in value:gmatch("([^\r\n]+)\r?\n*") do
67 form:tag("value"):text(line):up();
69 elseif field_type == "list-single" then
70 for _, val in ipairs(value) do
71 if type(val) == "table" then
72 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
74 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
80 if field.required then
81 form:tag("required"):up();
84 -- Jump back up to list of fields
90 local field_readers = {};
92 function form_t.data(layout, stanza)
95 for field_tag in stanza:childtags() do
97 for n, field in ipairs(layout) do
98 if field.name == field_tag.attr.var then
99 field_type = field.type;
104 local reader = field_readers[field_type];
106 data[field_tag.attr.var] = reader(field_tag);
113 field_readers["text-single"] =
115 local value = field_tag:child_with_name("value");
121 field_readers["text-private"] =
122 field_readers["text-single"];
124 field_readers["jid-single"] =
125 field_readers["text-single"];
127 field_readers["jid-multi"] =
130 for value_tag in field_tag:childtags() do
131 if value_tag.name == "value" then
132 result[#result+1] = value_tag[1];
138 field_readers["text-multi"] =
141 for value_tag in field_tag:childtags() do
142 if value_tag.name == "value" then
143 result[#result+1] = value_tag[1];
146 return t_concat(result, "\n");
149 field_readers["list-single"] =
150 field_readers["text-single"];
152 field_readers["boolean"] =
154 local value = field_tag:child_with_name("value");
156 if value[1] == "1" or value[1] == "true" then
164 field_readers["hidden"] =
166 local value = field_tag:child_with_name("value");
180 title = "MUC Configuration",
181 instructions = [[Use this form to configure options for this MUC room.]],
183 { name = "FORM_TYPE", type = "hidden", required = true };
184 { name = "field-name", type = "field-type", required = false };