Comparison

util/dataforms.lua @ 4397:1378e3c79c34

util.dataforms: Add field verification logic
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 11 Aug 2011 23:58:26 +0200
parent 3540:bc139431830b
child 4434:51a7c85751b9
comparison
equal deleted inserted replaced
4396:03b59a511671 4397:1378e3c79c34
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local setmetatable = setmetatable; 9 local setmetatable = setmetatable;
10 local pairs, ipairs = pairs, ipairs; 10 local pairs, ipairs = pairs, ipairs;
11 local tostring, type = tostring, type; 11 local tostring, type, next = tostring, type, next;
12 local t_concat = table.concat; 12 local t_concat = table.concat;
13 local st = require "util.stanza"; 13 local st = require "util.stanza";
14 local jid_prep = require "util.jid".prep;
14 15
15 module "dataforms" 16 module "dataforms"
16 17
17 local xmlns_forms = 'jabber:x:data'; 18 local xmlns_forms = 'jabber:x:data';
18 19
102 end 103 end
103 return form; 104 return form;
104 end 105 end
105 106
106 local field_readers = {}; 107 local field_readers = {};
108 local field_verifiers = {};
107 109
108 function form_t.data(layout, stanza) 110 function form_t.data(layout, stanza)
109 local data = {}; 111 local data = {};
110 112 local errors = {};
111 for field_tag in stanza:childtags() do 113
112 local field_type; 114 for _, field in ipairs(layout) do
113 for n, field in ipairs(layout) do 115 local tag;
116 for field_tag in stanza:childtags() do
114 if field.name == field_tag.attr.var then 117 if field.name == field_tag.attr.var then
115 field_type = field.type; 118 tag = field_tag;
116 break; 119 break;
117 end 120 end
118 end 121 end
119 122
120 local reader = field_readers[field_type]; 123 local reader = field_readers[field.type];
124 local verifier = field.verifier or field_verifiers[field.type];
121 if reader then 125 if reader then
122 data[field_tag.attr.var] = reader(field_tag); 126 data[field.name] = reader(tag);
123 end 127 if verifier then
124 128 errors[field.name] = verifier(data[field.name], tag, field.required);
129 end
130 end
131 end
132 if next(errors) then
133 return data, errors;
125 end 134 end
126 return data; 135 return data;
127 end 136 end
128 137
129 field_readers["text-single"] = 138 field_readers["text-single"] =
132 if value then 141 if value then
133 return value[1]; 142 return value[1];
134 end 143 end
135 end 144 end
136 145
146 field_verifiers["text-single"] =
147 function (data, field_tag, required)
148 if ((not data) or (#data == 0)) and required then
149 return "Required value missing";
150 end
151 end
152
137 field_readers["text-private"] = 153 field_readers["text-private"] =
138 field_readers["text-single"]; 154 field_readers["text-single"];
139 155
156 field_verifiers["text-private"] =
157 field_verifiers["text-single"];
158
140 field_readers["jid-single"] = 159 field_readers["jid-single"] =
141 field_readers["text-single"]; 160 field_readers["text-single"];
161
162 field_verifiers["jid-single"] =
163 function (data, field_tag, required)
164 if #data == 0 and required then
165 return "Required value missing";
166 end
167 if not jid_prep(data) then
168 return "Invalid JID";
169 end
170 end
142 171
143 field_readers["jid-multi"] = 172 field_readers["jid-multi"] =
144 function (field_tag) 173 function (field_tag)
145 local result = {}; 174 local result = {};
146 for value_tag in field_tag:childtags() do 175 for value_tag in field_tag:childtags() do
149 end 178 end
150 end 179 end
151 return result; 180 return result;
152 end 181 end
153 182
183 field_verifiers["jid-multi"] =
184 function (data, field_tag, required)
185 if #data == 0 and required then
186 return "Required value missing";
187 end
188
189 for _, jid in ipairs(data) do
190 if not jid_prep(jid) then
191 return "Invalid JID";
192 end
193 end
194 end
195
154 field_readers["text-multi"] = 196 field_readers["text-multi"] =
155 function (field_tag) 197 function (field_tag)
156 local result = {}; 198 local result = {};
157 for value_tag in field_tag:childtags() do 199 for value_tag in field_tag:childtags() do
158 if value_tag.name == "value" then 200 if value_tag.name == "value" then
160 end 202 end
161 end 203 end
162 return t_concat(result, "\n"); 204 return t_concat(result, "\n");
163 end 205 end
164 206
207 field_verifiers["text-multi"] =
208 field_verifiers["text-single"];
209
165 field_readers["list-single"] = 210 field_readers["list-single"] =
166 field_readers["text-single"]; 211 field_readers["text-single"];
212
213 field_verifiers["list-single"] =
214 field_verifiers["text-single"];
167 215
168 field_readers["list-multi"] = 216 field_readers["list-multi"] =
169 function (field_tag) 217 function (field_tag)
170 local result = {}; 218 local result = {};
171 for value_tag in field_tag:childtags() do 219 for value_tag in field_tag:childtags() do
174 end 222 end
175 end 223 end
176 return result; 224 return result;
177 end 225 end
178 226
227 field_verifiers["list-multi"] =
228 function (data, field_tag, required)
229 if #data == 0 and required then
230 return "Required value missing";
231 end
232 end
233
179 field_readers["boolean"] = 234 field_readers["boolean"] =
180 function (field_tag) 235 function (field_tag)
181 local value = field_tag:child_with_name("value"); 236 local value = field_tag:child_with_name("value");
182 if value then 237 if value then
183 if value[1] == "1" or value[1] == "true" then 238 if value[1] == "1" or value[1] == "true" then
186 return false; 241 return false;
187 end 242 end
188 end 243 end
189 end 244 end
190 245
246 field_verifiers["boolean"] =
247 function (data, field_tag, required)
248 data = field_readers["text-single"](field_tag);
249 if #data == 0 and required then
250 return "Required value missing";
251 end
252 if data ~= "1" and data ~= "true" and data ~= "0" and data ~= "false" then
253 return "Invalid boolean representation";
254 end
255 end
256
191 field_readers["hidden"] = 257 field_readers["hidden"] =
192 function (field_tag) 258 function (field_tag)
193 local value = field_tag:child_with_name("value"); 259 local value = field_tag:child_with_name("value");
194 if value then 260 if value then
195 return value[1]; 261 return value[1];
196 end 262 end
263 end
264
265 field_verifiers["hidden"] =
266 function (data, field_tag, required)
267 return nil;
197 end 268 end
198 269
199 return _M; 270 return _M;
200 271
201 272