Software /
code /
verse
Annotate
util/dataforms.lua @ 477:b0a8d4e9934e
sasl: Add oauthbearer mechanism
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 17 Mar 2023 12:25:58 +0000 |
parent | 442:b2ae91f4fec9 |
child | 490:6b2f31da9610 |
rev | line source |
---|---|
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 -- Prosody IM |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
4 -- |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 -- COPYING file in the source package for more information. |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 -- |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local setmetatable = setmetatable; |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
10 local ipairs = ipairs; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
11 local type, next = type, next; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
12 local tonumber = tonumber; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
13 local tostring = tostring; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local t_concat = table.concat; |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local st = require "util.stanza"; |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
16 local jid_prep = require "util.jid".prep; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
18 local _ENV = nil; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
19 -- luacheck: std none |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local xmlns_forms = 'jabber:x:data'; |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
22 local xmlns_validate = 'http://jabber.org/protocol/xdata-validate'; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 local form_t = {}; |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 local form_mt = { __index = form_t }; |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
27 local function new(layout) |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 return setmetatable(layout, form_mt); |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
31 local function from_stanza(stanza) |
335
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
32 local layout = { |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
33 title = stanza:get_child_text("title"); |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
34 instructions = stanza:get_child_text("instructions"); |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
35 }; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
36 for tag in stanza:childtags("field") do |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
37 local field = { |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
38 name = tag.attr.var; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
39 label = tag.attr.label; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
40 type = tag.attr.type; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
41 required = tag:get_child("required") and true or nil; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
42 value = tag:get_child_text("value"); |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
43 }; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
44 layout[#layout+1] = field; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
45 if field.type then |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
46 local value = {}; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
47 if field.type:match"list%-" then |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
48 for tag in tag:childtags("option") do |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
49 value[#value+1] = { label = tag.attr.label, value = tag:get_child_text("value") }; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
50 end |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
51 for tag in tag:childtags("value") do |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
52 value[#value+1] = { label = tag.attr.label, value = tag:get_text(), default = true }; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
53 end |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
54 elseif field.type:match"%-multi" then |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
55 for tag in tag:childtags("value") do |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
56 value[#value+1] = tag.attr.label and { label = tag.attr.label, value = tag:get_text() } or tag:get_text(); |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
57 end |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
58 if field.type == "text-multi" then |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
59 field.value = t_concat(value, "\n"); |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
60 else |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
61 field.value = value; |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
62 end |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
63 end |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
64 end |
442
b2ae91f4fec9
util.dataforms: Support XEP-0122 validation
Kim Alvefur <zash@zash.se>
parents:
441
diff
changeset
|
65 local datatype_tag = tag:get_child("validate", xmlns_validate); |
b2ae91f4fec9
util.dataforms: Support XEP-0122 validation
Kim Alvefur <zash@zash.se>
parents:
441
diff
changeset
|
66 if datatype_tag then |
b2ae91f4fec9
util.dataforms: Support XEP-0122 validation
Kim Alvefur <zash@zash.se>
parents:
441
diff
changeset
|
67 field.datatype = datatype.attr.datatype; |
b2ae91f4fec9
util.dataforms: Support XEP-0122 validation
Kim Alvefur <zash@zash.se>
parents:
441
diff
changeset
|
68 local range_tag = datatype_tag:get_child("range"); |
b2ae91f4fec9
util.dataforms: Support XEP-0122 validation
Kim Alvefur <zash@zash.se>
parents:
441
diff
changeset
|
69 if range_tag then |
b2ae91f4fec9
util.dataforms: Support XEP-0122 validation
Kim Alvefur <zash@zash.se>
parents:
441
diff
changeset
|
70 field.range_min = tonumber(range_tag.attr.min); |
b2ae91f4fec9
util.dataforms: Support XEP-0122 validation
Kim Alvefur <zash@zash.se>
parents:
441
diff
changeset
|
71 field.range_max = tonumber(range_tag.attr.max); |
b2ae91f4fec9
util.dataforms: Support XEP-0122 validation
Kim Alvefur <zash@zash.se>
parents:
441
diff
changeset
|
72 end |
b2ae91f4fec9
util.dataforms: Support XEP-0122 validation
Kim Alvefur <zash@zash.se>
parents:
441
diff
changeset
|
73 end |
b2ae91f4fec9
util.dataforms: Support XEP-0122 validation
Kim Alvefur <zash@zash.se>
parents:
441
diff
changeset
|
74 |
335
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
75 end |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
76 return new(layout); |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
77 end |
9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents:
334
diff
changeset
|
78 |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 function form_t.form(layout, data, formtype) |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
80 if not formtype then formtype = "form" end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
81 local form = st.stanza("x", { xmlns = xmlns_forms, type = formtype }); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
82 if formtype == "cancel" then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
83 return form; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
85 if formtype ~= "submit" then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
86 if layout.title then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
87 form:tag("title"):text(layout.title):up(); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
88 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
89 if layout.instructions then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
90 form:tag("instructions"):text(layout.instructions):up(); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
91 end |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 end |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
93 for _, field in ipairs(layout) do |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 local field_type = field.type or "text-single"; |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 -- Add field tag |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
96 form:tag("field", { type = field_type, var = field.var or field.name, label = formtype ~= "submit" and field.label or nil }); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
97 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
98 if formtype ~= "submit" then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
99 if field.desc then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
100 form:text_tag("desc", field.desc); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
101 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
102 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
103 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
104 if formtype == "form" and field.datatype then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
105 form:tag("validate", { xmlns = xmlns_validate, datatype = field.datatype }); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
106 if field.range_min or field.range_max then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
107 form:tag("range", { |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
108 min = field.range_min and tostring(field.range_min), |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
109 max = field.range_max and tostring(field.range_max), |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
110 }):up(); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
111 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
112 -- <basic/> assumed |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
113 form:up(); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
114 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
115 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
116 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
117 local value = field.value; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
118 local options = field.options; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
119 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
120 if data and data[field.name] ~= nil then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
121 value = data[field.name]; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
123 if formtype == "form" and type(value) == "table" |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
124 and (field_type == "list-single" or field_type == "list-multi") then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
125 -- Allow passing dynamically generated options as values |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
126 options, value = value, nil; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
127 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
128 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
129 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
130 if formtype == "form" and options then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
131 local defaults = {}; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
132 for _, val in ipairs(options) do |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
133 if type(val) == "table" then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
134 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up(); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
135 if val.default then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
136 defaults[#defaults+1] = val.value; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
137 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
138 else |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
139 form:tag("option", { label= val }):tag("value"):text(val):up():up(); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
140 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
141 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
142 if not value then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
143 if field_type == "list-single" then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
144 value = defaults[1]; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
145 elseif field_type == "list-multi" then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
146 value = defaults; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
147 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
148 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
149 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
150 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
151 if value ~= nil then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
152 if type(value) == "number" then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
153 -- TODO validate that this is ok somehow, eg check field.datatype |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
154 value = ("%g"):format(value); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
155 end |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 -- Add value, depending on type |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 if field_type == "hidden" then |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 if type(value) == "table" then |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 -- Assume an XML snippet |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 form:tag("value") |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 :add_child(value) |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 :up(); |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 else |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
164 form:tag("value"):text(value):up(); |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 elseif field_type == "boolean" then |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 form:tag("value"):text((value and "1") or "0"):up(); |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 elseif field_type == "fixed" then |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
169 form:tag("value"):text(value):up(); |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 elseif field_type == "jid-multi" then |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 for _, jid in ipairs(value) do |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 form:tag("value"):text(jid):up(); |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 elseif field_type == "jid-single" then |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 form:tag("value"):text(value):up(); |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 elseif field_type == "text-single" or field_type == "text-private" then |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 form:tag("value"):text(value):up(); |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 elseif field_type == "text-multi" then |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 -- Split into multiple <value> tags, one for each line |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 for line in value:gmatch("([^\r\n]+)\r?\n*") do |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 form:tag("value"):text(line):up(); |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 elseif field_type == "list-single" then |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
184 form:tag("value"):text(value):up(); |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 elseif field_type == "list-multi" then |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 for _, val in ipairs(value) do |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
187 form:tag("value"):text(val):up(); |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 end |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
191 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
192 local media = field.media; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
193 if media then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
194 form:tag("media", { xmlns = "urn:xmpp:media-element", height = ("%g"):format(media.height), width = ("%g"):format(media.width) }); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
195 for _, val in ipairs(media) do |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
196 form:tag("uri", { type = val.type }):text(val.uri):up() |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
197 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
198 form:up(); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
199 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
200 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
201 if formtype == "form" and field.required then |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 form:tag("required"):up(); |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 end |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
204 |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 -- Jump back up to list of fields |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 form:up(); |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 return form; |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 local field_readers = {}; |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
212 local data_validators = {}; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
214 function form_t.data(layout, stanza, current) |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 local data = {}; |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
216 local errors = {}; |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
217 local present = {}; |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
218 |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
219 for _, field in ipairs(layout) do |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
220 local tag; |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
221 for field_tag in stanza:childtags("field") do |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
222 if (field.var or field.name) == field_tag.attr.var then |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
223 tag = field_tag; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 break; |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 end |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
227 |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
228 if not tag then |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
229 if current and current[field.name] ~= nil then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
230 data[field.name] = current[field.name]; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
231 elseif field.required then |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
232 errors[field.name] = "Required value missing"; |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
233 end |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
234 elseif field.name then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
235 present[field.name] = true; |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
236 local reader = field_readers[field.type]; |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
237 if reader then |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
238 local value, err = reader(tag, field.required); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
239 local validator = field.datatype and data_validators[field.datatype]; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
240 if value ~= nil and validator then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
241 local valid, ret = validator(value, field); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
242 if valid then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
243 value = ret; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
244 else |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
245 value, err = nil, ret or ("Invalid value for data of type " .. field.datatype); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
246 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
247 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
248 data[field.name], errors[field.name] = value, err; |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
249 end |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 end |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
251 end |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
252 if next(errors) then |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
253 return data, errors, present; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 end |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
255 return data, nil, present; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
258 local function simple_text(field_tag, required) |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
259 local data = field_tag:get_child_text("value"); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
260 -- XEP-0004 does not say if an empty string is acceptable for a required value |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
261 -- so we will follow HTML5 which says that empty string means missing |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
262 if required and (data == nil or data == "") then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
263 return nil, "Required value missing"; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 end |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
265 return data; -- Return whatever get_child_text returned, even if empty string |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
266 end |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
268 field_readers["text-single"] = simple_text; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
269 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
270 field_readers["text-private"] = simple_text; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 field_readers["jid-single"] = |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
273 function (field_tag, required) |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
274 local raw_data, err = simple_text(field_tag, required); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
275 if not raw_data then return raw_data, err; end |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
276 local data = jid_prep(raw_data); |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
277 if not data then |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
278 return nil, "Invalid JID: " .. raw_data; |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
279 end |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
280 return data; |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
281 end |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
282 |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
283 field_readers["jid-multi"] = |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
284 function (field_tag, required) |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 local result = {}; |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
286 local err = {}; |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
287 for value_tag in field_tag:childtags("value") do |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
288 local raw_value = value_tag:get_text(); |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
289 local value = jid_prep(raw_value); |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
290 result[#result+1] = value; |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
291 if raw_value and not value then |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
292 err[#err+1] = ("Invalid JID: " .. raw_value); |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
294 end |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
295 if #result > 0 then |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
296 return result, (#err > 0 and t_concat(err, "\n") or nil); |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
297 elseif required then |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
298 return nil, "Required value missing"; |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
299 end |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
300 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
301 |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
302 field_readers["list-multi"] = |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
303 function (field_tag, required) |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
304 local result = {}; |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
305 for value in field_tag:childtags("value") do |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
306 result[#result+1] = value:get_text(); |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
307 end |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
308 if #result > 0 then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
309 return result; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
310 elseif required then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
311 return nil, "Required value missing"; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
312 end |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
313 end |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
314 |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
315 field_readers["text-multi"] = |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
316 function (field_tag, required) |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
317 local data, err = field_readers["list-multi"](field_tag, required); |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
318 if data then |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
319 data = t_concat(data, "\n"); |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
320 end |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
321 return data, err; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
322 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
323 |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
324 field_readers["list-single"] = simple_text; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
325 |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
326 local boolean_values = { |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
327 ["1"] = true, ["true"] = true, |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
328 ["0"] = false, ["false"] = false, |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
329 }; |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
330 |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
331 field_readers["boolean"] = |
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
332 function (field_tag, required) |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
333 local raw_value, err = simple_text(field_tag, required); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
334 if not raw_value then return raw_value, err; end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
335 local value = boolean_values[raw_value]; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
336 if value == nil then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
337 return nil, "Invalid boolean representation:" .. raw_value; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
338 end |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
339 return value; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
340 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
341 |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
342 field_readers["hidden"] = |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
343 function (field_tag) |
334
34c52f3b21c4
util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents:
123
diff
changeset
|
344 return field_tag:get_child_text("value"); |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
345 end |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
346 |
441
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
347 data_validators["xs:integer"] = |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
348 function (data, field) |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
349 local n = tonumber(data); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
350 if not n then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
351 return false, "not a number"; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
352 elseif n % 1 ~= 0 then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
353 return false, "not an integer"; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
354 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
355 if field.range_max and n > field.range_max then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
356 return false, "out of bounds"; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
357 elseif field.range_min and n < field.range_min then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
358 return false, "out of bounds"; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
359 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
360 return true, n; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
361 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
362 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
363 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
364 local function get_form_type(form) |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
365 if not st.is_stanza(form) then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
366 return nil, "not a stanza object"; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
367 elseif form.attr.xmlns ~= "jabber:x:data" or form.name ~= "x" then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
368 return nil, "not a dataform element"; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
369 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
370 for field in form:childtags("field") do |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
371 if field.attr.var == "FORM_TYPE" then |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
372 return field:get_child_text("value"); |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
373 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
374 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
375 return ""; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
376 end |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
377 |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
378 return { |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
379 new = new; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
380 from_stanza = from_stanza; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
381 get_type = get_form_type; |
e4c0b1d7fd6b
util.dataforms: Update from prosody trunk 5fb6563eee1e keeping from_stanza()
Kim Alvefur <zash@zash.se>
parents:
335
diff
changeset
|
382 }; |
123
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
383 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
384 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
385 --[=[ |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
386 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
387 Layout: |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
388 { |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
389 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
390 title = "MUC Configuration", |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
391 instructions = [[Use this form to configure options for this MUC room.]], |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
392 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
393 { name = "FORM_TYPE", type = "hidden", required = true }; |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
394 { name = "field-name", type = "field-type", required = false }; |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
395 } |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
396 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
397 |
8a079aa70b84
util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
398 --]=] |