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();
85 if field.required then
86 form:tag("required"):up();
89 -- Jump back up to list of fields
95 local field_readers = {};
97 function form_t.data(layout, stanza)
100 for field_tag in stanza:childtags() do
102 for n, field in ipairs(layout) do
103 if field.name == field_tag.attr.var then
104 field_type = field.type;
109 local reader = field_readers[field_type];
111 data[field_tag.attr.var] = reader(field_tag);
118 field_readers["text-single"] =
120 local value = field_tag:child_with_name("value");
126 field_readers["text-private"] =
127 field_readers["text-single"];
129 field_readers["jid-single"] =
130 field_readers["text-single"];
132 field_readers["jid-multi"] =
135 for value_tag in field_tag:childtags() do
136 if value_tag.name == "value" then
137 result[#result+1] = value_tag[1];
143 field_readers["text-multi"] =
146 for value_tag in field_tag:childtags() do
147 if value_tag.name == "value" then
148 result[#result+1] = value_tag[1];
151 return t_concat(result, "\n");
154 field_readers["list-single"] =
155 field_readers["text-single"];
157 field_readers["boolean"] =
159 local value = field_tag:child_with_name("value");
161 if value[1] == "1" or value[1] == "true" then
169 field_readers["hidden"] =
171 local value = field_tag:child_with_name("value");
185 title = "MUC Configuration",
186 instructions = [[Use this form to configure options for this MUC room.]],
188 { name = "FORM_TYPE", type = "hidden", required = true };
189 { name = "field-name", type = "field-type", required = false };