Annotate

util/dataforms.lua @ 4432:cbc3224ed3c2

util.ip: Fix in IP range calculation for 172.16.0.0/12
author Matthew Wild <mwild1@gmail.com>
date Wed, 30 Nov 2011 17:20:16 +0000
parent 4397:1378e3c79c34
child 4434:51a7c85751b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 957
diff changeset
4 --
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;
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
10 local pairs, ipairs = pairs, ipairs;
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
11 local tostring, type, next = tostring, 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
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 module "dataforms"
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local xmlns_forms = 'jabber:x:data';
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local form_t = {};
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local form_mt = { __index = form_t };
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 function new(layout)
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return setmetatable(layout, form_mt);
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
2488
08bfd7c96531 util.dataforms: Add optional type parameters (defaults to 'form')
Matthew Wild <mwild1@gmail.com>
parents: 2219
diff changeset
27 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
28 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
29 if layout.title then
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
30 form:tag("title"):text(layout.title):up();
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
31 end
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
32 if layout.instructions then
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
33 form:tag("instructions"):text(layout.instructions):up();
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
34 end
845
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 for n, field in ipairs(layout) do
951
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
36 local field_type = field.type or "text-single";
845
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 -- Add field tag
951
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
38 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
39
1945
adfd7f3720f5 util.dataforms: Small fix to allow generating forms without specifying any input data
Matthew Wild <mwild1@gmail.com>
parents: 1944
diff changeset
40 local value = (data and data[field.name]) or field.value;
845
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
2061
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
42 if value then
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
43 -- 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
44 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
45 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
46 -- Assume an XML snippet
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
47 form:tag("value")
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
48 :add_child(value)
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
49 :up();
1958
e2b0026143c4 util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents: 1945
diff changeset
50 else
2061
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
51 form:tag("value"):text(tostring(value)):up();
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
52 end
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
53 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
54 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
55 elseif field_type == "fixed" then
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
56
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
57 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
58 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
59 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
60 end
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
61 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
62 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
63 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
64 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
65 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
66 -- 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
67 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
68 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
69 end
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
70 elseif field_type == "list-single" then
3379
17d4298b2a58 util.dataforms: Capability to set default for list-single
Florian Zeitz <florob@babelmonkeys.de>
parents: 2923
diff changeset
71 local has_default = false;
2061
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
72 for _, val in ipairs(value) do
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
73 if type(val) == "table" then
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
74 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
3379
17d4298b2a58 util.dataforms: Capability to set default for list-single
Florian Zeitz <florob@babelmonkeys.de>
parents: 2923
diff changeset
75 if val.default and (not has_default) then
17d4298b2a58 util.dataforms: Capability to set default for list-single
Florian Zeitz <florob@babelmonkeys.de>
parents: 2923
diff changeset
76 form:tag("value"):text(val.value):up();
17d4298b2a58 util.dataforms: Capability to set default for list-single
Florian Zeitz <florob@babelmonkeys.de>
parents: 2923
diff changeset
77 has_default = true;
17d4298b2a58 util.dataforms: Capability to set default for list-single
Florian Zeitz <florob@babelmonkeys.de>
parents: 2923
diff changeset
78 end
2061
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
79 else
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
80 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
e34fdca432a9 util.dataforms: Only add value to rendered form if supplied in the data
Florian Zeitz
parents: 1958
diff changeset
81 end
1958
e2b0026143c4 util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents: 1945
diff changeset
82 end
3380
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
83 elseif field_type == "list-multi" then
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
84 for _, val in ipairs(value) do
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
85 if type(val) == "table" then
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
86 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
87 if val.default then
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
88 form:tag("value"):text(val.value):up();
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
89 end
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
90 else
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
91 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
92 end
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
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
845
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96
952
ef648f49e734 util.dataforms: Add support for <required/> fields
Matthew Wild <mwild1@gmail.com>
parents: 951
diff changeset
97 if field.required then
ef648f49e734 util.dataforms: Add support for <required/> fields
Matthew Wild <mwild1@gmail.com>
parents: 951
diff changeset
98 form:tag("required"):up();
ef648f49e734 util.dataforms: Add support for <required/> fields
Matthew Wild <mwild1@gmail.com>
parents: 951
diff changeset
99 end
ef648f49e734 util.dataforms: Add support for <required/> fields
Matthew Wild <mwild1@gmail.com>
parents: 951
diff changeset
100
845
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 -- Jump back up to list of fields
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 form:up();
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
851
b48c7ed3f7f8 util.dataforms: Return the form
Matthew Wild <mwild1@gmail.com>
parents: 845
diff changeset
104 return form;
845
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 end
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
107 local field_readers = {};
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
108 local field_verifiers = {};
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
109
845
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 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
111 local data = {};
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
112 local errors = {};
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
113
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
114 for _, field in ipairs(layout) do
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
115 local tag;
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
116 for field_tag in stanza:childtags() do
2219
9e1c6b6a2ee4 util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents: 2070
diff changeset
117 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
118 tag = field_tag;
2219
9e1c6b6a2ee4 util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents: 2070
diff changeset
119 break;
9e1c6b6a2ee4 util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents: 2070
diff changeset
120 end
9e1c6b6a2ee4 util.dataforms: Don't require type when parsing form XML
Florian Zeitz
parents: 2070
diff changeset
121 end
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
122
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
123 local reader = field_readers[field.type];
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
124 local verifier = field.verifier or field_verifiers[field.type];
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
125 if reader then
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
126 data[field.name] = reader(tag);
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
127 if verifier then
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
128 errors[field.name] = verifier(data[field.name], tag, field.required);
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
129 end
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
130 end
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
131 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
132 if next(errors) then
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
133 return data, errors;
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
134 end
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
135 return data;
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
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3380
diff changeset
138 field_readers["text-single"] =
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
139 function (field_tag)
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
140 local value = field_tag:child_with_name("value");
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
141 if value then
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
142 return value[1];
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
143 end
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
144 end
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
145
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
146 field_verifiers["text-single"] =
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
147 function (data, field_tag, required)
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
148 if ((not data) or (#data == 0)) and required then
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
149 return "Required value missing";
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
150 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
151 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
152
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3380
diff changeset
153 field_readers["text-private"] =
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
154 field_readers["text-single"];
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
155
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
156 field_verifiers["text-private"] =
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
157 field_verifiers["text-single"];
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
158
1944
754eebd31538 util.dataforms: Support for jid-single field type especially for Florob :)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
159 field_readers["jid-single"] =
754eebd31538 util.dataforms: Support for jid-single field type especially for Florob :)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
160 field_readers["text-single"];
754eebd31538 util.dataforms: Support for jid-single field type especially for Florob :)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
161
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
162 field_verifiers["jid-single"] =
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
163 function (data, field_tag, required)
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
164 if #data == 0 and required then
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
165 return "Required value missing";
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
166 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
167 if not jid_prep(data) then
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
168 return "Invalid JID";
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
169 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
170 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
171
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3380
diff changeset
172 field_readers["jid-multi"] =
2070
25dc4b9d06b1 util.dataforms: Support for jid-multi field type
Florian Zeitz
parents: 2061
diff changeset
173 function (field_tag)
25dc4b9d06b1 util.dataforms: Support for jid-multi field type
Florian Zeitz
parents: 2061
diff changeset
174 local result = {};
25dc4b9d06b1 util.dataforms: Support for jid-multi field type
Florian Zeitz
parents: 2061
diff changeset
175 for value_tag in field_tag:childtags() do
25dc4b9d06b1 util.dataforms: Support for jid-multi field type
Florian Zeitz
parents: 2061
diff changeset
176 if value_tag.name == "value" then
25dc4b9d06b1 util.dataforms: Support for jid-multi field type
Florian Zeitz
parents: 2061
diff changeset
177 result[#result+1] = value_tag[1];
25dc4b9d06b1 util.dataforms: Support for jid-multi field type
Florian Zeitz
parents: 2061
diff changeset
178 end
25dc4b9d06b1 util.dataforms: Support for jid-multi field type
Florian Zeitz
parents: 2061
diff changeset
179 end
25dc4b9d06b1 util.dataforms: Support for jid-multi field type
Florian Zeitz
parents: 2061
diff changeset
180 return result;
25dc4b9d06b1 util.dataforms: Support for jid-multi field type
Florian Zeitz
parents: 2061
diff changeset
181 end
25dc4b9d06b1 util.dataforms: Support for jid-multi field type
Florian Zeitz
parents: 2061
diff changeset
182
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
183 field_verifiers["jid-multi"] =
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
184 function (data, field_tag, required)
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
185 if #data == 0 and required then
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
186 return "Required value missing";
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
187 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
188
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
189 for _, jid in ipairs(data) do
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
190 if not jid_prep(jid) then
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
191 return "Invalid JID";
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
192 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
193 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
194 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
195
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3380
diff changeset
196 field_readers["text-multi"] =
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
197 function (field_tag)
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
198 local result = {};
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
199 for value_tag in field_tag:childtags() do
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
200 if value_tag.name == "value" then
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
201 result[#result+1] = value_tag[1];
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
202 end
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
203 end
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
204 return t_concat(result, "\n");
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
205 end
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
206
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
207 field_verifiers["text-multi"] =
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
208 field_verifiers["text-single"];
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
209
1958
e2b0026143c4 util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents: 1945
diff changeset
210 field_readers["list-single"] =
e2b0026143c4 util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents: 1945
diff changeset
211 field_readers["text-single"];
e2b0026143c4 util.dataforms: Incorporate slightly modified patch for list-single type from Florob
Matthew Wild <mwild1@gmail.com>
parents: 1945
diff changeset
212
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
213 field_verifiers["list-single"] =
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
214 field_verifiers["text-single"];
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
215
3380
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
216 field_readers["list-multi"] =
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
217 function (field_tag)
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
218 local result = {};
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
219 for value_tag in field_tag:childtags() do
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
220 if value_tag.name == "value" then
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
221 result[#result+1] = value_tag[1];
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
222 end
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
223 end
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
224 return result;
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
225 end
e74e80b454a1 util.dataforms: Add list-multi support
Florian Zeitz <florob@babelmonkeys.de>
parents: 3379
diff changeset
226
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
227 field_verifiers["list-multi"] =
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
228 function (data, field_tag, required)
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
229 if #data == 0 and required then
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
230 return "Required value missing";
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
231 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
232 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
233
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3380
diff changeset
234 field_readers["boolean"] =
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
235 function (field_tag)
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
236 local value = field_tag:child_with_name("value");
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
237 if value then
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
238 if value[1] == "1" or value[1] == "true" then
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
239 return true;
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
240 else
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
241 return false;
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
242 end
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3380
diff changeset
243 end
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
244 end
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
245
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
246 field_verifiers["boolean"] =
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
247 function (data, field_tag, required)
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
248 data = field_readers["text-single"](field_tag);
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
249 if #data == 0 and required then
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
250 return "Required value missing";
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
251 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
252 if data ~= "1" and data ~= "true" and data ~= "0" and data ~= "false" then
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
253 return "Invalid boolean representation";
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 end
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
256
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3380
diff changeset
257 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
258 function (field_tag)
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
259 local value = field_tag:child_with_name("value");
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
260 if value then
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
261 return value[1];
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
262 end
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
263 end
4397
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
264
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
265 field_verifiers["hidden"] =
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
266 function (data, field_tag, required)
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
267 return nil;
1378e3c79c34 util.dataforms: Add field verification logic
Florian Zeitz <florob@babelmonkeys.de>
parents: 3540
diff changeset
268 end
955
98ca5a753fee util.dataforms: Support for retriving the field value data from forms
Matthew Wild <mwild1@gmail.com>
parents: 954
diff changeset
269
951
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
270 return _M;
845
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272
951
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
273 --[=[
845
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 Layout:
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 {
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 title = "MUC Configuration",
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 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
280
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 { name = "FORM_TYPE", type = "hidden", required = true };
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 { name = "field-name", type = "field-type", required = false };
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
fc3dced9801e util.dataforms: First commit, incomplete
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285
951
4b9207949735 util.dataforms: Fixed to actually work, mostly
Matthew Wild <mwild1@gmail.com>
parents: 851
diff changeset
286 --]=]