Software /
code /
prosody
Annotate
util/dataforms.lua @ 9002:1fcddb4a4d16
MUC: Return new occupant object from :save_occupant(), which can include useful info
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 09 Jul 2018 17:41:57 +0100 |
parent | 8984:fa80b3231a02 |
child | 9047:ab3488ee3ca5 |
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; |
954
72e4639c9310
util.dataforms: Fixes for hidden field type
Matthew Wild <mwild1@gmail.com>
parents:
953
diff
changeset
|
12 local t_concat = table.concat; |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
13 local st = require "util.stanza"; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
14 local jid_prep = require "util.jid".prep; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
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
|
16 local _ENV = nil; |
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8255
diff
changeset
|
17 -- luacheck: std none |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local xmlns_forms = 'jabber:x:data'; |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local form_t = {}; |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 local form_mt = { __index = form_t }; |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
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
|
24 local function new(layout) |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 return setmetatable(layout, form_mt); |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 end |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
2488
08bfd7c96531
util.dataforms: Add optional type parameters (defaults to 'form')
Matthew Wild <mwild1@gmail.com>
parents:
2219
diff
changeset
|
28 function form_t.form(layout, data, formtype) |
08bfd7c96531
util.dataforms: Add optional type parameters (defaults to 'form')
Matthew Wild <mwild1@gmail.com>
parents:
2219
diff
changeset
|
29 local form = st.stanza("x", { xmlns = xmlns_forms, type = formtype or "form" }); |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
30 if layout.title then |
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
31 form:tag("title"):text(layout.title):up(); |
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
32 end |
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
33 if layout.instructions then |
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
34 form:tag("instructions"):text(layout.instructions):up(); |
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
35 end |
6672
d6a60e74f773
util.dataforms: Rename unused loop counter to '_' [luacheck]
Matthew Wild <mwild1@gmail.com>
parents:
6668
diff
changeset
|
36 for _, field in ipairs(layout) do |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
37 local field_type = field.type or "text-single"; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 -- Add field tag |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
39 form:tag("field", { type = field_type, var = field.name, label = field.label }); |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
8945
cbeb1a045eb6
util.dataforms: Add support for field descriptions in <desc>
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
41 if field.desc then |
cbeb1a045eb6
util.dataforms: Add support for field descriptions in <desc>
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
42 form:text_tag("desc", field.desc); |
cbeb1a045eb6
util.dataforms: Add support for field descriptions in <desc>
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
43 end |
cbeb1a045eb6
util.dataforms: Add support for field descriptions in <desc>
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
44 |
8984
fa80b3231a02
util.dataforms: More robust handling of field values, especially booleans
Matthew Wild <mwild1@gmail.com>
parents:
8945
diff
changeset
|
45 local value; |
fa80b3231a02
util.dataforms: More robust handling of field values, especially booleans
Matthew Wild <mwild1@gmail.com>
parents:
8945
diff
changeset
|
46 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
|
47 value = data[field.name]; |
fa80b3231a02
util.dataforms: More robust handling of field values, especially booleans
Matthew Wild <mwild1@gmail.com>
parents:
8945
diff
changeset
|
48 else |
fa80b3231a02
util.dataforms: More robust handling of field values, especially booleans
Matthew Wild <mwild1@gmail.com>
parents:
8945
diff
changeset
|
49 value = field.value; |
fa80b3231a02
util.dataforms: More robust handling of field values, especially booleans
Matthew Wild <mwild1@gmail.com>
parents:
8945
diff
changeset
|
50 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5693
diff
changeset
|
51 |
8984
fa80b3231a02
util.dataforms: More robust handling of field values, especially booleans
Matthew Wild <mwild1@gmail.com>
parents:
8945
diff
changeset
|
52 if value ~= nil then |
2061
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
53 -- 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
|
54 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
|
55 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
|
56 -- Assume an XML snippet |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
57 form:tag("value") |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
58 :add_child(value) |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
59 :up(); |
1958
e2b0026143c4
util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents:
1945
diff
changeset
|
60 else |
8863
64fa8d80c09f
util.dataforms: Remove string conversion to let util.stanza validate types
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
61 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
|
62 end |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 end |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 -- 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
|
77 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
|
78 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
|
79 end |
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
80 elseif field_type == "list-single" then |
8255
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
81 if formtype ~= "result" then |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
82 local has_default = false; |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
83 for _, val in ipairs(field.options or value) do |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
84 if type(val) == "table" then |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
85 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up(); |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
86 if value == val.value or val.default and (not has_default) then |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
87 form:tag("value"):text(val.value):up(); |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
88 has_default = true; |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
89 end |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
90 else |
8863
64fa8d80c09f
util.dataforms: Remove string conversion to let util.stanza validate types
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
91 form:tag("option", { label= val }):tag("value"):text(val):up():up(); |
3379
17d4298b2a58
util.dataforms: Capability to set default for list-single
Florian Zeitz <florob@babelmonkeys.de>
parents:
2923
diff
changeset
|
92 end |
2061
e34fdca432a9
util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents:
1958
diff
changeset
|
93 end |
1958
e2b0026143c4
util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents:
1945
diff
changeset
|
94 end |
8255
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
95 if (field.options or formtype == "result") and value then |
7442
156c95bfed0b
util.dataforms: Allow separation of options from values in list fields
Kim Alvefur <zash@zash.se>
parents:
6981
diff
changeset
|
96 form:tag("value"):text(value):up(); |
156c95bfed0b
util.dataforms: Allow separation of options from values in list fields
Kim Alvefur <zash@zash.se>
parents:
6981
diff
changeset
|
97 end |
3380
e74e80b454a1
util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents:
3379
diff
changeset
|
98 elseif field_type == "list-multi" then |
8255
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
99 if formtype ~= "result" then |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
100 for _, val in ipairs(field.options or value) do |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
101 if type(val) == "table" then |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
102 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up(); |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
103 if not field.options and val.default then |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
104 form:tag("value"):text(val.value):up(); |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
105 end |
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
106 else |
8863
64fa8d80c09f
util.dataforms: Remove string conversion to let util.stanza validate types
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
107 form:tag("option", { label= val }):tag("value"):text(val):up():up(); |
3380
e74e80b454a1
util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents:
3379
diff
changeset
|
108 end |
e74e80b454a1
util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents:
3379
diff
changeset
|
109 end |
e74e80b454a1
util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents:
3379
diff
changeset
|
110 end |
8255
d70d4c1ac17a
util.dataforms: Don't include list options in result forms (fixes #983)
Matthew Wild <mwild1@gmail.com>
parents:
7446
diff
changeset
|
111 if (field.options or formtype == "result") and value then |
7442
156c95bfed0b
util.dataforms: Allow separation of options from values in list fields
Kim Alvefur <zash@zash.se>
parents:
6981
diff
changeset
|
112 for _, val in ipairs(value) do |
156c95bfed0b
util.dataforms: Allow separation of options from values in list fields
Kim Alvefur <zash@zash.se>
parents:
6981
diff
changeset
|
113 form:tag("value"):text(val):up(); |
156c95bfed0b
util.dataforms: Allow separation of options from values in list fields
Kim Alvefur <zash@zash.se>
parents:
6981
diff
changeset
|
114 end |
156c95bfed0b
util.dataforms: Allow separation of options from values in list fields
Kim Alvefur <zash@zash.se>
parents:
6981
diff
changeset
|
115 end |
1958
e2b0026143c4
util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents:
1945
diff
changeset
|
116 end |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end |
6148
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
118 |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
119 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
|
120 if media then |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
121 form:tag("media", { xmlns = "urn:xmpp:media-element", height = media.height, width = media.width }); |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
122 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
|
123 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
|
124 end |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
125 form:up(); |
7dcd6f124c93
util.dataforms: Add support for XEP-0221: Data Forms Media Element
Kim Alvefur <zash@zash.se>
parents:
5693
diff
changeset
|
126 end |
6149 | 127 |
952
ef648f49e734
util.dataforms: Add support for <required/> fields
Matthew Wild <mwild1@gmail.com>
parents:
951
diff
changeset
|
128 if field.required then |
ef648f49e734
util.dataforms: Add support for <required/> fields
Matthew Wild <mwild1@gmail.com>
parents:
951
diff
changeset
|
129 form:tag("required"):up(); |
ef648f49e734
util.dataforms: Add support for <required/> fields
Matthew Wild <mwild1@gmail.com>
parents:
951
diff
changeset
|
130 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5693
diff
changeset
|
131 |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 -- Jump back up to list of fields |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 form:up(); |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 end |
851
b48c7ed3f7f8
util.dataforms: Return the form
Matthew Wild <mwild1@gmail.com>
parents:
845
diff
changeset
|
135 return form; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 end |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
138 local field_readers = {}; |
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
139 |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 function form_t.data(layout, stanza) |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
141 local data = {}; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
142 local errors = {}; |
6981
bcaa553de6e8
util.dataforms: Track which fields are included in a form
Kim Alvefur <zash@zash.se>
parents:
6830
diff
changeset
|
143 local present = {}; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
144 |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
145 for _, field in ipairs(layout) do |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
146 local tag; |
6467
77d00a895bbd
util/dataforms: Make sure we iterate over field tags
daurnimator <quae@daurnimator.com>
parents:
6148
diff
changeset
|
147 for field_tag in stanza:childtags("field") do |
2219
9e1c6b6a2ee4
util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents:
2070
diff
changeset
|
148 if field.name == field_tag.attr.var then |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
149 tag = field_tag; |
2219
9e1c6b6a2ee4
util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents:
2070
diff
changeset
|
150 break; |
9e1c6b6a2ee4
util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents:
2070
diff
changeset
|
151 end |
9e1c6b6a2ee4
util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents:
2070
diff
changeset
|
152 end |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
153 |
4434
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
154 if not tag then |
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
155 if field.required then |
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
156 errors[field.name] = "Required value missing"; |
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
157 end |
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
158 else |
6981
bcaa553de6e8
util.dataforms: Track which fields are included in a form
Kim Alvefur <zash@zash.se>
parents:
6830
diff
changeset
|
159 present[field.name] = true; |
4434
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
160 local reader = field_readers[field.type]; |
51a7c85751b9
util.dataforms: Fix form verification
Florian Zeitz <florob@babelmonkeys.de>
parents:
4397
diff
changeset
|
161 if reader then |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
162 data[field.name], errors[field.name] = reader(tag, field.required); |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
163 end |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
164 end |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
165 end |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
166 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
|
167 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
|
168 end |
6981
bcaa553de6e8
util.dataforms: Track which fields are included in a form
Kim Alvefur <zash@zash.se>
parents:
6830
diff
changeset
|
169 return data, nil, present; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 end |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
172 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
|
173 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
|
174 -- 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
|
175 -- 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
|
176 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
|
177 return nil, "Required value missing"; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
178 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
|
179 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
|
180 end |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
181 |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
182 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
|
183 |
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
184 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
|
185 |
1944
754eebd31538
util.dataforms: Support for jid-single field type especially for Florob :)
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 return nil, "Invalid JID: " .. raw_data; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
193 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
|
194 return data; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
195 end |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
196 |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3380
diff
changeset
|
197 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
|
198 function (field_tag, required) |
2070
25dc4b9d06b1
util.dataforms: Support for jid-multi field type
Florian Zeitz
parents:
2061
diff
changeset
|
199 local result = {}; |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
200 local err = {}; |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
201 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
|
202 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
|
203 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
|
204 result[#result+1] = value; |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
205 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
|
206 err[#err+1] = ("Invalid JID: " .. raw_value); |
2070
25dc4b9d06b1
util.dataforms: Support for jid-multi field type
Florian Zeitz
parents:
2061
diff
changeset
|
207 end |
25dc4b9d06b1
util.dataforms: Support for jid-multi field type
Florian Zeitz
parents:
2061
diff
changeset
|
208 end |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
209 if #result > 0 then |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
210 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
|
211 elseif required then |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
212 return nil, "Required value missing"; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
213 end |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
214 end |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
215 |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
216 field_readers["list-multi"] = |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
217 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
|
218 local result = {}; |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 end |
955
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
227 end |
98ca5a753fee
util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents:
954
diff
changeset
|
228 |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
229 field_readers["text-multi"] = |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
230 function (field_tag, required) |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
231 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
|
232 if data then |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
233 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
|
234 end |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
235 return data, err; |
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
236 end |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
237 |
6829
22522443982f
util.dataforms: Refactor parsing to reuse simple text parser instead of duplicate code
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
238 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
|
239 |
4928
5211c11dd865
util.dataforms: Fix parsing of -multi fields
Florian Zeitz <florob@babelmonkeys.de>
parents:
4887
diff
changeset
|
240 local boolean_values = { |
5211c11dd865
util.dataforms: Fix parsing of -multi fields
Florian Zeitz <florob@babelmonkeys.de>
parents:
4887
diff
changeset
|
241 ["1"] = true, ["true"] = true, |
5211c11dd865
util.dataforms: Fix parsing of -multi fields
Florian Zeitz <florob@babelmonkeys.de>
parents:
4887
diff
changeset
|
242 ["0"] = false, ["false"] = false, |
5211c11dd865
util.dataforms: Fix parsing of -multi fields
Florian Zeitz <florob@babelmonkeys.de>
parents:
4887
diff
changeset
|
243 }; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
244 |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3380
diff
changeset
|
245 field_readers["boolean"] = |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 return nil, "Invalid boolean representation:" .. raw_value; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
252 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
|
253 return value; |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
254 end |
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
255 |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3380
diff
changeset
|
256 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
|
257 function (field_tag) |
4884
b8d852aea3ad
util.dataforms: Do field validation and normalization in field readers.
Kim Alvefur <zash@zash.se>
parents:
4435
diff
changeset
|
258 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
|
259 end |
4397
1378e3c79c34
util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
260 |
8864
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
261 |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
262 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
|
263 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
|
264 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
|
265 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
|
266 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
|
267 end |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
268 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
|
269 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
|
270 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
|
271 end |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
272 end |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
273 return ""; |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
274 end |
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
275 |
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
|
276 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
|
277 new = new; |
8864
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8863
diff
changeset
|
278 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
|
279 }; |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
282 --[=[ |
845
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
284 Layout: |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 { |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
286 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
287 title = "MUC Configuration", |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
288 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
|
289 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
290 { name = "FORM_TYPE", type = "hidden", required = true }; |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 { name = "field-name", type = "field-type", required = false }; |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
292 } |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 |
fc3dced9801e
util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
294 |
951
4b9207949735
util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents:
851
diff
changeset
|
295 --]=] |