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 local has_default = false;
71 for _, val in ipairs(value) do
72 if type(val) == "table" then
73 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
74 if val.default and (not has_default) then
75 form:tag("value"):text(val.value):up();
79 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
82 elseif field_type == "list-multi" then
83 for _, val in ipairs(value) do
84 if type(val) == "table" then
85 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
87 form:tag("value"):text(val.value):up();
90 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
96 if field.required then
97 form:tag("required"):up();
100 -- Jump back up to list of fields
106 local field_readers = {};
108 function form_t.data(layout, stanza)
111 for field_tag in stanza:childtags() do
113 for n, field in ipairs(layout) do
114 if field.name == field_tag.attr.var then
115 field_type = field.type;
120 local reader = field_readers[field_type];
122 data[field_tag.attr.var] = reader(field_tag);
129 field_readers["text-single"] =
131 local value = field_tag:child_with_name("value");
137 field_readers["text-private"] =
138 field_readers["text-single"];
140 field_readers["jid-single"] =
141 field_readers["text-single"];
143 field_readers["jid-multi"] =
146 for value_tag in field_tag:childtags() do
147 if value_tag.name == "value" then
148 result[#result+1] = value_tag[1];
154 field_readers["text-multi"] =
157 for value_tag in field_tag:childtags() do
158 if value_tag.name == "value" then
159 result[#result+1] = value_tag[1];
162 return t_concat(result, "\n");
165 field_readers["list-single"] =
166 field_readers["text-single"];
168 field_readers["list-multi"] =
171 for value_tag in field_tag:childtags() do
172 if value_tag.name == "value" then
173 result[#result+1] = value_tag[1];
179 field_readers["boolean"] =
181 local value = field_tag:child_with_name("value");
183 if value[1] == "1" or value[1] == "true" then
191 field_readers["hidden"] =
193 local value = field_tag:child_with_name("value");
207 title = "MUC Configuration",
208 instructions = [[Use this form to configure options for this MUC room.]],
210 { name = "FORM_TYPE", type = "hidden", required = true };
211 { name = "field-name", type = "field-type", required = false };