2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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)
27 local form = st.stanza("x", { xmlns = xmlns_forms, type = "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;
41 -- Add value, depending on type
42 if field_type == "hidden" then
43 if type(value) == "table" then
44 -- Assume an XML snippet
49 form:tag("value"):text(tostring(value)):up();
51 elseif field_type == "boolean" then
52 form:tag("value"):text((value and "1") or "0"):up();
53 elseif field_type == "fixed" then
55 elseif field_type == "jid-multi" then
56 for _, jid in ipairs(value) do
57 form:tag("value"):text(jid):up();
59 elseif field_type == "jid-single" then
60 form:tag("value"):text(value):up();
61 elseif field_type == "text-single" or field_type == "text-private" then
62 form:tag("value"):text(value):up();
63 elseif field_type == "text-multi" then
64 -- Split into multiple <value> tags, one for each line
65 for line in value:gmatch("([^\r\n]+)\r?\n*") do
66 form:tag("value"):text(line):up();
68 elseif field_type == "list-single" then
69 for _, val in ipairs(value) do
70 if type(val) == "table" then
71 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
73 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
78 if field.required then
79 form:tag("required"):up();
82 -- Jump back up to list of fields
88 local field_readers = {};
90 function form_t.data(layout, stanza)
93 for field_tag in stanza:childtags() do
94 local field_type = field_tag.attr.type;
96 local reader = field_readers[field_type];
98 data[field_tag.attr.var] = reader(field_tag);
105 field_readers["text-single"] =
107 local value = field_tag:child_with_name("value");
113 field_readers["text-private"] =
114 field_readers["text-single"];
116 field_readers["jid-single"] =
117 field_readers["text-single"];
119 field_readers["text-multi"] =
122 for value_tag in field_tag:childtags() do
123 if value_tag.name == "value" then
124 result[#result+1] = value_tag[1];
127 return t_concat(result, "\n");
130 field_readers["list-single"] =
131 field_readers["text-single"];
133 field_readers["boolean"] =
135 local value = field_tag:child_with_name("value");
137 if value[1] == "1" or value[1] == "true" then
145 field_readers["hidden"] =
147 local value = field_tag:child_with_name("value");
161 title = "MUC Configuration",
162 instructions = [[Use this form to configure options for this MUC room.]],
164 { name = "FORM_TYPE", type = "hidden", required = true };
165 { name = "field-name", type = "field-type", required = false };