diff options
-rw-r--r-- | util/dataforms.lua | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/util/dataforms.lua b/util/dataforms.lua index c10a0244..73a78f08 100644 --- a/util/dataforms.lua +++ b/util/dataforms.lua @@ -1,3 +1,6 @@ +local setmetatable = setmetatable; +local pairs, ipairs = pairs, ipairs; +local st = require "util.stanza"; module "dataforms" @@ -13,11 +16,17 @@ end local form_x_attr = { xmlns = xmlns_forms }; function form_t.form(layout, data) - local form = st.tag("x", form_x_attr); + local form = st.stanza("x", form_x_attr); + if layout.title then + form:tag("title"):text(layout.title):up(); + end + if layout.instructions then + form:tag("instructions"):text(layout.instructions):up(); + end for n, field in ipairs(layout) do - local field_type = field.type; + local field_type = field.type or "text-single"; -- Add field tag - form:tag("field", { type = field_type, var = field.name }); + form:tag("field", { type = field_type, var = field.name, label = field.label }); local value = data[field.name]; @@ -30,7 +39,7 @@ function form_t.form(layout, data) form:text(tostring(value)); end elseif field_type == "boolean" then - form:text((value and "1") or "0"); + form:tag("value"):text((value and "1") or "0"); elseif field_type == "fixed" then elseif field_type == "jid-multi" then @@ -39,7 +48,13 @@ function form_t.form(layout, data) end elseif field_type == "jid-single" then form:tag("value"):text(value):up(); - + elseif field_type == "text-single" or field_type == "text-private" then + form:tag("value"):text(value):up(); + elseif field_type == "text-multi" then + -- Split into multiple <value> tags, one for each line + for line in value:gmatch("([^\r\n]+)\r?\n*") do + form:tag("value"):text(line):up(); + end end -- Jump back up to list of fields @@ -52,9 +67,10 @@ function form_t.data(layout, stanza) end +return _M; ---[[ +--[=[ Layout: { @@ -67,4 +83,4 @@ Layout: } ---]] +--]=] |