diff options
Diffstat (limited to 'util/dataforms.lua')
-rw-r--r-- | util/dataforms.lua | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/util/dataforms.lua b/util/dataforms.lua index 469ce976..6152c9f3 100644 --- a/util/dataforms.lua +++ b/util/dataforms.lua @@ -8,12 +8,13 @@ local setmetatable = setmetatable; local ipairs = ipairs; -local tostring, type, next = tostring, type, next; +local type, next = type, next; local t_concat = table.concat; local st = require "util.stanza"; local jid_prep = require "util.jid".prep; local _ENV = nil; +-- luacheck: std none local xmlns_forms = 'jabber:x:data'; @@ -37,9 +38,18 @@ function form_t.form(layout, data, formtype) -- Add field tag form:tag("field", { type = field_type, var = field.name, label = field.label }); - local value = (data and data[field.name]) or field.value; + if field.desc then + form:text_tag("desc", field.desc); + end + + local value; + if data and data[field.name] ~= nil then + value = data[field.name]; + else + value = field.value; + end - if value then + if value ~= nil then -- Add value, depending on type if field_type == "hidden" then if type(value) == "table" then @@ -48,7 +58,7 @@ function form_t.form(layout, data, formtype) :add_child(value) :up(); else - form:tag("value"):text(tostring(value)):up(); + form:tag("value"):text(value):up(); end elseif field_type == "boolean" then form:tag("value"):text((value and "1") or "0"):up(); @@ -78,7 +88,7 @@ function form_t.form(layout, data, formtype) has_default = true; end else - form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up(); + form:tag("option", { label= val }):tag("value"):text(val):up():up(); end end end @@ -94,7 +104,7 @@ function form_t.form(layout, data, formtype) form:tag("value"):text(val.value):up(); end else - form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up(); + form:tag("option", { label= val }):tag("value"):text(val):up():up(); end end end @@ -145,7 +155,7 @@ function form_t.data(layout, stanza) if field.required then errors[field.name] = "Required value missing"; end - else + elseif field.name then present[field.name] = true; local reader = field_readers[field.type]; if reader then @@ -248,8 +258,24 @@ field_readers["hidden"] = return field_tag:get_child_text("value"); end + +local function get_form_type(form) + if not st.is_stanza(form) then + return nil, "not a stanza object"; + elseif form.attr.xmlns ~= "jabber:x:data" or form.name ~= "x" then + return nil, "not a dataform element"; + end + for field in form:childtags("field") do + if field.attr.var == "FORM_TYPE" then + return field:get_child_text("value"); + end + end + return ""; +end + return { new = new; + get_type = get_form_type; }; |