Software /
code /
prosody
Annotate
util/dataforms.lua @ 13267:7ae000fc8c07 0.12
mod_muc_mam: Improve wording of enable setting
Suggested by jstein in the chat
This option label is used by XMPP clients to explain what the option does.
a) The user should know where the data is archived.
b) The user needs a statement that can be enabled/disabled by the variable. A question would have the wrong logic here.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 15 Oct 2023 14:43:11 +0200 |
parent | 11881:2138fc13d53a |
child | 12975:d10957394a3c |
rev | line source |
---|---|
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
957
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2488
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2488
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5693
diff
changeset
|
4 -- |
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
957
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
957
diff
changeset
|
6 -- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
957
diff
changeset
|
7 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
957
diff
changeset
|
8 |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
9 local setmetatable = setmetatable; |
6668
f6f39c2f1b1f
util.dataforms: Remove unused import of pairs() (thanks luacheck)
Matthew Wild <mwild1@gmail.com>
parents:
6468
diff
changeset
|
10 local ipairs = ipairs; |
8863
64fa8d80c09f
util.dataforms: Remove string conversion to let util.stanza validate types
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
11 local type, next = type, next; |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
12 local tonumber = tonumber; |
11070
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
13 local tostring = tostring; |
954
72e4639c9310
util.dataforms: Fixes for hidden field type
Matthew Wild <mwild1@gmail.com>
parents:
953
diff
changeset
|
14 local t_concat = table.concat; |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
15 local st = require "util.stanza"; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
16 local jid_prep = require "util.jid".prep; |
11874
84f4c6957d62
util.dataforms: Add support for datetime field types via XEP-0122
Kim Alvefur <zash@zash.se>
parents:
11630
diff
changeset
|
17 local datetime = require "util.datetime"; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6672
diff
changeset
|
19 local _ENV = nil; |
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8255
diff
changeset
|
20 -- luacheck: std none |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 local xmlns_forms = 'jabber:x:data'; |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
23 local xmlns_validate = 'http://jabber.org/protocol/xdata-validate'; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 local form_t = {}; |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local form_mt = { __index = form_t }; |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6672
diff
changeset
|
28 local function new(layout) |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 return setmetatable(layout, form_mt); |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 end |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
2488
08bfd7c96531
util.dataforms: Add optional type parameters (defaults to 'form')
Matthew Wild <mwild1@gmail.com>
parents:
2219
diff
changeset
|
32 function form_t.form(layout, data, formtype) |
9087
9e45e7adcebf
util.dataforms: Set default value for form type argument
Kim Alvefur <zash@zash.se>
parents:
9084
diff
changeset
|
33 if not formtype then formtype = "form" end |
9e45e7adcebf
util.dataforms: Set default value for form type argument
Kim Alvefur <zash@zash.se>
parents:
9084
diff
changeset
|
34 local form = st.stanza("x", { xmlns = xmlns_forms, type = formtype }); |
9089
3a7a0b9f42f3
util.dataforms: Skip all fields for the 'cancel' form type
Kim Alvefur <zash@zash.se>
parents:
9088
diff
changeset
|
35 if formtype == "cancel" then |
3a7a0b9f42f3
util.dataforms: Skip all fields for the 'cancel' form type
Kim Alvefur <zash@zash.se>
parents:
9088
diff
changeset
|
36 return form; |
3a7a0b9f42f3
util.dataforms: Skip all fields for the 'cancel' form type
Kim Alvefur <zash@zash.se>
parents:
9088
diff
changeset
|
37 end |
9090
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
38 if formtype ~= "submit" then |
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
39 if layout.title then |
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
40 form:tag("title"):text(layout.title):up(); |
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
41 end |
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
42 if layout.instructions then |
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
43 form:tag("instructions"):text(layout.instructions):up(); |
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
44 end |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
45 end |
6672
d6a60e74f773
util.dataforms: Rename unused loop counter to '_' [luacheck]
Matthew Wild <mwild1@gmail.com>
parents:
6668
diff
changeset
|
46 for _, field in ipairs(layout) do |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
47 local field_type = field.type or "text-single"; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 -- Add field tag |
9242
68694c1bd960
util.dataforms: Allow field names to be different from the 'var' attribute
Kim Alvefur <zash@zash.se>
parents:
9121
diff
changeset
|
49 form:tag("field", { type = field_type, var = field.var or field.name, label = formtype ~= "submit" and field.label or nil }); |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
9090
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
51 if formtype ~= "submit" then |
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
52 if field.desc then |
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
53 form:text_tag("desc", field.desc); |
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
54 end |
8945
cbeb1a045eb6
util.dataforms: Add support for field descriptions in <desc>
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
55 end |
cbeb1a045eb6
util.dataforms: Add support for field descriptions in <desc>
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
56 |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
57 if formtype == "form" and field.datatype then |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
58 form:tag("validate", { xmlns = xmlns_validate, datatype = field.datatype }); |
11070
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
59 if field.range_min or field.range_max then |
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
60 form:tag("range", { |
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
61 min = field.range_min and tostring(field.range_min), |
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
62 max = field.range_max and tostring(field.range_max), |
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
63 }):up(); |
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
64 end |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
65 -- <basic/> assumed |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
66 form:up(); |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
67 end |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
68 |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
69 |
9094
05979ae1e38a
util.dataforms: Only allow overriding of options when passed via the :form method
Kim Alvefur <zash@zash.se>
parents:
9092
diff
changeset
|
70 local value = field.value; |
05979ae1e38a
util.dataforms: Only allow overriding of options when passed via the :form method
Kim Alvefur <zash@zash.se>
parents:
9092
diff
changeset
|
71 local options = field.options; |
05979ae1e38a
util.dataforms: Only allow overriding of options when passed via the :form method
Kim Alvefur <zash@zash.se>
parents:
9092
diff
changeset
|
72 |
8984
fa80b3231a02
util.dataforms: More robust handling of field values, especially booleans
Matthew Wild <mwild1@gmail.com>
parents:
8945
diff
changeset
|
73 if data and data[field.name] ~= nil then |
fa80b3231a02
util.dataforms: More robust handling of field values, especially booleans
Matthew Wild <mwild1@gmail.com>
parents:
8945
diff
changeset
|
74 value = data[field.name]; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5693
diff
changeset
|
75 |
9094
05979ae1e38a
util.dataforms: Only allow overriding of options when passed via the :form method
Kim Alvefur <zash@zash.se>
parents:
9092
diff
changeset
|
76 if formtype == "form" and type(value) == "table" |
05979ae1e38a
util.dataforms: Only allow overriding of options when passed via the :form method
Kim Alvefur <zash@zash.se>
parents:
9092
diff
changeset
|
77 and (field_type == "list-single" or field_type == "list-multi") then |
05979ae1e38a
util.dataforms: Only allow overriding of options when passed via the :form method
Kim Alvefur <zash@zash.se>
parents:
9092
diff
changeset
|
78 -- Allow passing dynamically generated options as values |
05979ae1e38a
util.dataforms: Only allow overriding of options when passed via the :form method
Kim Alvefur <zash@zash.se>
parents:
9092
diff
changeset
|
79 options, value = value, nil; |
05979ae1e38a
util.dataforms: Only allow overriding of options when passed via the :form method
Kim Alvefur <zash@zash.se>
parents:
9092
diff
changeset
|
80 end |
9091
519dea077d20
util.dataforms: Allow passing dynamically generated options as values (fixes traceback)
Kim Alvefur <zash@zash.se>
parents:
9090
diff
changeset
|
81 end |
519dea077d20
util.dataforms: Allow passing dynamically generated options as values (fixes traceback)
Kim Alvefur <zash@zash.se>
parents:
9090
diff
changeset
|
82 |
519dea077d20
util.dataforms: Allow passing dynamically generated options as values (fixes traceback)
Kim Alvefur <zash@zash.se>
parents:
9090
diff
changeset
|
83 if formtype == "form" and options then |
9084
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
84 local defaults = {}; |
9091
519dea077d20
util.dataforms: Allow passing dynamically generated options as values (fixes traceback)
Kim Alvefur <zash@zash.se>
parents:
9090
diff
changeset
|
85 for _, val in ipairs(options) do |
9084
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
86 if type(val) == "table" then |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
87 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up(); |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
88 if val.default then |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
89 defaults[#defaults+1] = val.value; |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
90 end |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
91 else |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
92 form:tag("option", { label= val }):tag("value"):text(val):up():up(); |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
93 end |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
94 end |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
95 if not value then |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
96 if field_type == "list-single" then |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
97 value = defaults[1]; |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
98 elseif field_type == "list-multi" then |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
99 value = defaults; |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
100 end |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
101 end |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
102 end |
572b6858db03
util.dataforms: Detach generation of options from values (fixes #1177)
Kim Alvefur <zash@zash.se>
parents:
9047
diff
changeset
|
103 |
8984
fa80b3231a02
util.dataforms: More robust handling of field values, especially booleans
Matthew Wild <mwild1@gmail.com>
parents:
8945
diff
changeset
|
104 if value ~= nil then |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
105 if type(value) == "number" then |
11878
bf6706057283
util.dataforms: Turn number values into timestamps for datetime fields
Kim Alvefur <zash@zash.se>
parents:
11877
diff
changeset
|
106 if field.datatype == "xs:dateTime" then |
bf6706057283
util.dataforms: Turn number values into timestamps for datetime fields
Kim Alvefur <zash@zash.se>
parents:
11877
diff
changeset
|
107 value = datetime.datetime(value); |
bf6706057283
util.dataforms: Turn number values into timestamps for datetime fields
Kim Alvefur <zash@zash.se>
parents:
11877
diff
changeset
|
108 elseif field_type == "boolean" then |
11877
593b141ba01c
util.dataforms: Coerce number values for boolean fields
Kim Alvefur <zash@zash.se>
parents:
11874
diff
changeset
|
109 value = value ~= 0; |
11880
d30c81b875f2
util.dataforms: Ensure larger integers are serialized as such
Kim Alvefur <zash@zash.se>
parents:
11878
diff
changeset
|
110 elseif field.datatype == "xs:double" or field.datatype == "xs:decimal" then |
d30c81b875f2
util.dataforms: Ensure larger integers are serialized as such
Kim Alvefur <zash@zash.se>
parents:
11878
diff
changeset
|
111 value = ("%f"):format(value); |
11877
593b141ba01c
util.dataforms: Coerce number values for boolean fields
Kim Alvefur <zash@zash.se>
parents:
11874
diff
changeset
|
112 else |
11880
d30c81b875f2
util.dataforms: Ensure larger integers are serialized as such
Kim Alvefur <zash@zash.se>
parents:
11878
diff
changeset
|
113 value = ("%d"):format(value); |
11877
593b141ba01c
util.dataforms: Coerce number values for boolean fields
Kim Alvefur <zash@zash.se>
parents:
11874
diff
changeset
|
114 end |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
115 end |
2061
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
116 -- Add value, depending on type |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
117 if field_type == "hidden" then |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
118 if type(value) == "table" then |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
119 -- Assume an XML snippet |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
120 form:tag("value") |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
121 :add_child(value) |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
122 :up(); |
1958
e2b0026143c4
util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents:
1945
diff
changeset
|
123 else |
8863
64fa8d80c09f
util.dataforms: Remove string conversion to let util.stanza validate types
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
124 form:tag("value"):text(value):up(); |
2061
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
125 end |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
126 elseif field_type == "boolean" then |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
127 form:tag("value"):text((value and "1") or "0"):up(); |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
128 elseif field_type == "fixed" then |
5551
e1e06f1465be
util.dataforms: Add support for generating type='fixed' fields
Florian Zeitz <florob@babelmonkeys.de>
parents:
4928
diff
changeset
|
129 form:tag("value"):text(value):up(); |
2061
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
130 elseif field_type == "jid-multi" then |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
131 for _, jid in ipairs(value) do |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
132 form:tag("value"):text(jid):up(); |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
133 end |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
134 elseif field_type == "jid-single" then |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
135 form:tag("value"):text(value):up(); |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
136 elseif field_type == "text-single" or field_type == "text-private" then |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
137 form:tag("value"):text(value):up(); |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
138 elseif field_type == "text-multi" then |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
139 -- Split into multiple <value> tags, one for each line |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
140 for line in value:gmatch("([^\r\n]+)\r?\n*") do |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
141 form:tag("value"):text(line):up(); |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
142 end |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
143 elseif field_type == "list-single" then |
9111
bc7473fb7ad3
util.dataforms: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
9094
diff
changeset
|
144 form:tag("value"):text(value):up(); |
3380
e74e80b454a1
util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents:
3379
diff
changeset
|
145 elseif field_type == "list-multi" then |
9111
bc7473fb7ad3
util.dataforms: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
9094
diff
changeset
|
146 for _, val in ipairs(value) do |
bc7473fb7ad3
util.dataforms: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
9094
diff
changeset
|
147 form:tag("value"):text(val):up(); |
7442
156c95bfed0b
util.dataforms: Allow separation of options from values in list fields
Kim Alvefur <zash@zash.se>
parents:
6981
diff
changeset
|
148 end |
1958
e2b0026143c4
util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents:
1945
diff
changeset
|
149 end |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 end |
6148
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
151 |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
152 local media = field.media; |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
153 if media then |
11881
2138fc13d53a
util.dataforms: Encode size attributes as integers in XEP-0221 media element
Kim Alvefur <zash@zash.se>
parents:
11880
diff
changeset
|
154 form:tag("media", { xmlns = "urn:xmpp:media-element", height = ("%d"):format(media.height), width = ("%d"):format(media.width) }); |
6148
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
155 for _, val in ipairs(media) do |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
156 form:tag("uri", { type = val.type }):text(val.uri):up() |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
157 end |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
158 form:up(); |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
159 end |
6149 | 160 |
9090
56c52cb4d44e
util.dataforms: Exclude descriptive text fields from forms of type 'submit'
Kim Alvefur <zash@zash.se>
parents:
9089
diff
changeset
|
161 if formtype == "form" and field.required then |
952
ef648f49e734
util.dataforms: Add support for <required/> fields
Matthew Wild <mwild1@gmail.com>
parents:
951
diff
changeset
|
162 form:tag("required"):up(); |
ef648f49e734
util.dataforms: Add support for <required/> fields
Matthew Wild <mwild1@gmail.com>
parents:
951
diff
changeset
|
163 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5693
diff
changeset
|
164 |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 -- Jump back up to list of fields |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 form:up(); |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 end |
851
b48c7ed3f7f8
util.dataforms: Return the form
Matthew Wild <mwild1@gmail.com>
parents:
845
diff
changeset
|
168 return form; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 end |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
171 local field_readers = {}; |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
172 local data_validators = {}; |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
173 |
9121
e5eb36ee07a2
util.dataforms: Allow passing the current values to be used in stead of omitted fields
Kim Alvefur <zash@zash.se>
parents:
9111
diff
changeset
|
174 function form_t.data(layout, stanza, current) |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
175 local data = {}; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
176 local errors = {}; |
6981
bcaa553de6e8
util.dataforms: Track which fields are included in a form
Kim Alvefur <zash@zash.se>
parents:
6830
diff
changeset
|
177 local present = {}; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
178 |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
179 for _, field in ipairs(layout) do |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
180 local tag; |
6467
77d00a895bbd
util/dataforms: Make sure we iterate over field tags
daurnimator <quae@daurnimator.com>
parents:
6148
diff
changeset
|
181 for field_tag in stanza:childtags("field") do |
9242
68694c1bd960
util.dataforms: Allow field names to be different from the 'var' attribute
Kim Alvefur <zash@zash.se>
parents:
9121
diff
changeset
|
182 if (field.var or field.name) == field_tag.attr.var then |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
183 tag = field_tag; |
2219
9e1c6b6a2ee4
util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents:
2070
diff
changeset
|
184 break; |
9e1c6b6a2ee4
util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents:
2070
diff
changeset
|
185 end |
9e1c6b6a2ee4
util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents:
2070
diff
changeset
|
186 end |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
187 |
4434
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
188 if not tag then |
9121
e5eb36ee07a2
util.dataforms: Allow passing the current values to be used in stead of omitted fields
Kim Alvefur <zash@zash.se>
parents:
9111
diff
changeset
|
189 if current and current[field.name] ~= nil then |
e5eb36ee07a2
util.dataforms: Allow passing the current values to be used in stead of omitted fields
Kim Alvefur <zash@zash.se>
parents:
9111
diff
changeset
|
190 data[field.name] = current[field.name]; |
e5eb36ee07a2
util.dataforms: Allow passing the current values to be used in stead of omitted fields
Kim Alvefur <zash@zash.se>
parents:
9111
diff
changeset
|
191 elseif field.required then |
4434
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
192 errors[field.name] = "Required value missing"; |
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
193 end |
9047
ab3488ee3ca5
util.dataforms: Ensure fields have names when collecting data (fixes traceback, thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
8984
diff
changeset
|
194 elseif field.name then |
6981
bcaa553de6e8
util.dataforms: Track which fields are included in a form
Kim Alvefur <zash@zash.se>
parents:
6830
diff
changeset
|
195 present[field.name] = true; |
4434
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
196 local reader = field_readers[field.type]; |
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
197 if reader then |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
198 local value, err = reader(tag, field.required); |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
199 local validator = field.datatype and data_validators[field.datatype]; |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
200 if value ~= nil and validator then |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
201 local valid, ret = validator(value, field); |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
202 if valid then |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
203 value = ret; |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
204 else |
9246
397e8e5a2f1f
util.dataforms: Improve default error message for failed datatype validation
Kim Alvefur <zash@zash.se>
parents:
9243
diff
changeset
|
205 value, err = nil, ret or ("Invalid value for data of type " .. field.datatype); |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
206 end |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
207 end |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
208 data[field.name], errors[field.name] = value, err; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
209 end |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
210 end |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
211 end |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
212 if next(errors) then |
6981
bcaa553de6e8
util.dataforms: Track which fields are included in a form
Kim Alvefur <zash@zash.se>
parents:
6830
diff
changeset
|
213 return data, errors, present; |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
214 end |
6981
bcaa553de6e8
util.dataforms: Track which fields are included in a form
Kim Alvefur <zash@zash.se>
parents:
6830
diff
changeset
|
215 return data, nil, present; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 end |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
218 local function simple_text(field_tag, required) |
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
219 local data = field_tag:get_child_text("value"); |
6830
e08f5d081a6c
util.dataforms: Fix interaction of required fields and empty string values (fixes #521)
Kim Alvefur <zash@zash.se>
parents:
6829
diff
changeset
|
220 -- XEP-0004 does not say if an empty string is acceptable for a required value |
e08f5d081a6c
util.dataforms: Fix interaction of required fields and empty string values (fixes #521)
Kim Alvefur <zash@zash.se>
parents:
6829
diff
changeset
|
221 -- so we will follow HTML5 which says that empty string means missing |
e08f5d081a6c
util.dataforms: Fix interaction of required fields and empty string values (fixes #521)
Kim Alvefur <zash@zash.se>
parents:
6829
diff
changeset
|
222 if required and (data == nil or data == "") then |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
223 return nil, "Required value missing"; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
224 end |
6830
e08f5d081a6c
util.dataforms: Fix interaction of required fields and empty string values (fixes #521)
Kim Alvefur <zash@zash.se>
parents:
6829
diff
changeset
|
225 return data; -- Return whatever get_child_text returned, even if empty string |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
226 end |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
227 |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
228 field_readers["text-single"] = simple_text; |
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
229 |
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
230 field_readers["text-private"] = simple_text; |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
231 |
1944
754eebd31538
util.dataforms: Support for jid-single field type especially for Florob :)
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
232 field_readers["jid-single"] = |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
233 function (field_tag, required) |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
234 local raw_data, err = simple_text(field_tag, required); |
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
235 if not raw_data then return raw_data, err; end |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
236 local data = jid_prep(raw_data); |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
237 if not data then |
4887
4dd61fe04db4
util.dataforms: Don't return invalid JIDs in jid-single.
Kim Alvefur <zash@zash.se>
parents:
4886
diff
changeset
|
238 return nil, "Invalid JID: " .. raw_data; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
239 end |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
240 return data; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
241 end |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
242 |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3380
diff
changeset
|
243 field_readers["jid-multi"] = |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
244 function (field_tag, required) |
2070
25dc4b9d06b1
util.dataforms: Support for jid-multi field type
Florian Zeitz
parents:
2061
diff
changeset
|
245 local result = {}; |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
246 local err = {}; |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
247 for value_tag in field_tag:childtags("value") do |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
248 local raw_value = value_tag:get_text(); |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
249 local value = jid_prep(raw_value); |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
250 result[#result+1] = value; |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
251 if raw_value and not value then |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
252 err[#err+1] = ("Invalid JID: " .. raw_value); |
2070
25dc4b9d06b1
util.dataforms: Support for jid-multi field type
Florian Zeitz
parents:
2061
diff
changeset
|
253 end |
25dc4b9d06b1
util.dataforms: Support for jid-multi field type
Florian Zeitz
parents:
2061
diff
changeset
|
254 end |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
255 if #result > 0 then |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
256 return result, (#err > 0 and t_concat(err, "\n") or nil); |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
257 elseif required then |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
258 return nil, "Required value missing"; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
259 end |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
260 end |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
261 |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
262 field_readers["list-multi"] = |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
263 function (field_tag, required) |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
264 local result = {}; |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
265 for value in field_tag:childtags("value") do |
4928
5211c11dd865
util.dataforms: Fix parsing of -multi fields
Florian Zeitz <florob@babelmonkeys.de>
parents:
4887
diff
changeset
|
266 result[#result+1] = value:get_text(); |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
267 end |
5693
ef490e9276df
util.dataforms: Return nil for empty list-mutli responses, to be consistent with other readers
Florian Zeitz <florob@babelmonkeys.de>
parents:
5551
diff
changeset
|
268 if #result > 0 then |
ef490e9276df
util.dataforms: Return nil for empty list-mutli responses, to be consistent with other readers
Florian Zeitz <florob@babelmonkeys.de>
parents:
5551
diff
changeset
|
269 return result; |
ef490e9276df
util.dataforms: Return nil for empty list-mutli responses, to be consistent with other readers
Florian Zeitz <florob@babelmonkeys.de>
parents:
5551
diff
changeset
|
270 elseif required then |
ef490e9276df
util.dataforms: Return nil for empty list-mutli responses, to be consistent with other readers
Florian Zeitz <florob@babelmonkeys.de>
parents:
5551
diff
changeset
|
271 return nil, "Required value missing"; |
ef490e9276df
util.dataforms: Return nil for empty list-mutli responses, to be consistent with other readers
Florian Zeitz <florob@babelmonkeys.de>
parents:
5551
diff
changeset
|
272 end |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
273 end |
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
274 |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
275 field_readers["text-multi"] = |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
276 function (field_tag, required) |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
277 local data, err = field_readers["list-multi"](field_tag, required); |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
278 if data then |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
279 data = t_concat(data, "\n"); |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
280 end |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
281 return data, err; |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
282 end |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
283 |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
284 field_readers["list-single"] = simple_text; |
1958
e2b0026143c4
util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents:
1945
diff
changeset
|
285 |
4928
5211c11dd865
util.dataforms: Fix parsing of -multi fields
Florian Zeitz <florob@babelmonkeys.de>
parents:
4887
diff
changeset
|
286 local boolean_values = { |
5211c11dd865
util.dataforms: Fix parsing of -multi fields
Florian Zeitz <florob@babelmonkeys.de>
parents:
4887
diff
changeset
|
287 ["1"] = true, ["true"] = true, |
5211c11dd865
util.dataforms: Fix parsing of -multi fields
Florian Zeitz <florob@babelmonkeys.de>
parents:
4887
diff
changeset
|
288 ["0"] = false, ["false"] = false, |
5211c11dd865
util.dataforms: Fix parsing of -multi fields
Florian Zeitz <florob@babelmonkeys.de>
parents:
4887
diff
changeset
|
289 }; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
290 |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3380
diff
changeset
|
291 field_readers["boolean"] = |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
292 function (field_tag, required) |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
293 local raw_value, err = simple_text(field_tag, required); |
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
294 if not raw_value then return raw_value, err; end |
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
295 local value = boolean_values[raw_value]; |
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
296 if value == nil then |
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
297 return nil, "Invalid boolean representation:" .. raw_value; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
298 end |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
299 return value; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
300 end |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
301 |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3380
diff
changeset
|
302 field_readers["hidden"] = |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
303 function (field_tag) |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
304 return field_tag:get_child_text("value"); |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
305 end |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
306 |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
307 data_validators["xs:integer"] = |
11070
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
308 function (data, field) |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
309 local n = tonumber(data); |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
310 if not n then |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
311 return false, "not a number"; |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
312 elseif n % 1 ~= 0 then |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
313 return false, "not an integer"; |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
314 end |
11070
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
315 if field.range_max and n > field.range_max then |
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
316 return false, "out of bounds"; |
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
317 elseif field.range_min and n < field.range_min then |
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
318 return false, "out of bounds"; |
f7f30a3464fe
util.dataforms: Add support for validating (integer) ranges
Kim Alvefur <zash@zash.se>
parents:
11026
diff
changeset
|
319 end |
9243
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
320 return true, n; |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
321 end |
a4c52e304e6f
util.dataforms: Add support for XEP-0122: Data Forms Validation
Kim Alvefur <zash@zash.se>
parents:
9242
diff
changeset
|
322 |
11630
855b065d5fd6
util.dataforms: Define a integer + "max" datatype
Kim Alvefur <zash@zash.se>
parents:
11070
diff
changeset
|
323 data_validators["pubsub:integer-or-max"] = |
855b065d5fd6
util.dataforms: Define a integer + "max" datatype
Kim Alvefur <zash@zash.se>
parents:
11070
diff
changeset
|
324 function (data, field) |
855b065d5fd6
util.dataforms: Define a integer + "max" datatype
Kim Alvefur <zash@zash.se>
parents:
11070
diff
changeset
|
325 if data == "max" then |
855b065d5fd6
util.dataforms: Define a integer + "max" datatype
Kim Alvefur <zash@zash.se>
parents:
11070
diff
changeset
|
326 return true, data; |
855b065d5fd6
util.dataforms: Define a integer + "max" datatype
Kim Alvefur <zash@zash.se>
parents:
11070
diff
changeset
|
327 else |
855b065d5fd6
util.dataforms: Define a integer + "max" datatype
Kim Alvefur <zash@zash.se>
parents:
11070
diff
changeset
|
328 return data_validators["xs:integer"](data, field); |
855b065d5fd6
util.dataforms: Define a integer + "max" datatype
Kim Alvefur <zash@zash.se>
parents:
11070
diff
changeset
|
329 end |
855b065d5fd6
util.dataforms: Define a integer + "max" datatype
Kim Alvefur <zash@zash.se>
parents:
11070
diff
changeset
|
330 end |
855b065d5fd6
util.dataforms: Define a integer + "max" datatype
Kim Alvefur <zash@zash.se>
parents:
11070
diff
changeset
|
331 |
11874
84f4c6957d62
util.dataforms: Add support for datetime field types via XEP-0122
Kim Alvefur <zash@zash.se>
parents:
11630
diff
changeset
|
332 data_validators["xs:dateTime"] = |
84f4c6957d62
util.dataforms: Add support for datetime field types via XEP-0122
Kim Alvefur <zash@zash.se>
parents:
11630
diff
changeset
|
333 function(data, field) -- luacheck: ignore 212/field |
84f4c6957d62
util.dataforms: Add support for datetime field types via XEP-0122
Kim Alvefur <zash@zash.se>
parents:
11630
diff
changeset
|
334 local n = datetime.parse(data); |
84f4c6957d62
util.dataforms: Add support for datetime field types via XEP-0122
Kim Alvefur <zash@zash.se>
parents:
11630
diff
changeset
|
335 if not n then return false, "invalid timestamp"; end |
84f4c6957d62
util.dataforms: Add support for datetime field types via XEP-0122
Kim Alvefur <zash@zash.se>
parents:
11630
diff
changeset
|
336 return true, n; |
84f4c6957d62
util.dataforms: Add support for datetime field types via XEP-0122
Kim Alvefur <zash@zash.se>
parents:
11630
diff
changeset
|
337 end |
84f4c6957d62
util.dataforms: Add support for datetime field types via XEP-0122
Kim Alvefur <zash@zash.se>
parents:
11630
diff
changeset
|
338 |
8864
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
339 |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
340 local function get_form_type(form) |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
341 if not st.is_stanza(form) then |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
342 return nil, "not a stanza object"; |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
343 elseif form.attr.xmlns ~= "jabber:x:data" or form.name ~= "x" then |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
344 return nil, "not a dataform element"; |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
345 end |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
346 for field in form:childtags("field") do |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
347 if field.attr.var == "FORM_TYPE" then |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
348 return field:get_child_text("value"); |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
349 end |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
350 end |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
351 return ""; |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
352 end |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
353 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6672
diff
changeset
|
354 return { |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6672
diff
changeset
|
355 new = new; |
8864
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
356 get_type = get_form_type; |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6672
diff
changeset
|
357 }; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
358 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
359 |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
360 --[=[ |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
361 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
362 Layout: |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
363 { |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
364 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
365 title = "MUC Configuration", |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
366 instructions = [[Use this form to configure options for this MUC room.]], |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
367 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
368 { name = "FORM_TYPE", type = "hidden", required = true }; |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
369 { name = "field-name", type = "field-type", required = false }; |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
370 } |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
371 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
372 |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
373 --]=] |