Comparison

util/dataforms.lua @ 334:34c52f3b21c4

util.dataforms: Update from Prosody repo
author Kim Alvefur <zash@zash.se>
date Fri, 15 Feb 2013 20:40:56 +0100
parent 123:8a079aa70b84
child 335:9e69ee8542d4
comparison
equal deleted inserted replaced
333:36e9d5a2ae79 334:34c52f3b21c4
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
66 for line in value:gmatch("([^\r\n]+)\r?\n*") do 67 for line in value:gmatch("([^\r\n]+)\r?\n*") do
67 form:tag("value"):text(line):up(); 68 form:tag("value"):text(line):up();
68 end 69 end
69 elseif field_type == "list-single" then 70 elseif field_type == "list-single" then
70 local has_default = false; 71 local has_default = false;
71 if type(value) == "string" then 72 for _, val in ipairs(value) do
72 form:tag("value"):text(value):up(); 73 if type(val) == "table" then
73 else 74 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
74 for _, val in ipairs(value) do 75 if val.default and (not has_default) then
75 if type(val) == "table" then 76 form:tag("value"):text(val.value):up();
76 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up(); 77 has_default = true;
77 if val.default and (not has_default) then
78 form:tag("value"):text(val.value):up();
79 has_default = true;
80 end
81 else
82 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
83 end 78 end
79 else
80 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
84 end 81 end
85 end 82 end
86 elseif field_type == "list-multi" then 83 elseif field_type == "list-multi" then
87 for _, val in ipairs(value) do 84 for _, val in ipairs(value) do
88 if type(val) == "table" then 85 if type(val) == "table" then
109 106
110 local field_readers = {}; 107 local field_readers = {};
111 108
112 function form_t.data(layout, stanza) 109 function form_t.data(layout, stanza)
113 local data = {}; 110 local data = {};
114 111 local errors = {};
115 for field_tag in stanza:childtags() do 112
116 local field_type; 113 for _, field in ipairs(layout) do
117 for n, field in ipairs(layout) do 114 local tag;
115 for field_tag in stanza:childtags() do
118 if field.name == field_tag.attr.var then 116 if field.name == field_tag.attr.var then
119 field_type = field.type; 117 tag = field_tag;
120 break; 118 break;
121 end 119 end
122 end 120 end
123 121
124 local reader = field_readers[field_type]; 122 if not tag then
125 if reader then 123 if field.required then
126 data[field_tag.attr.var] = reader(field_tag); 124 errors[field.name] = "Required value missing";
127 end 125 end
128 126 else
127 local reader = field_readers[field.type];
128 if reader then
129 data[field.name], errors[field.name] = reader(tag, field.required);
130 end
131 end
132 end
133 if next(errors) then
134 return data, errors;
129 end 135 end
130 return data; 136 return data;
131 end 137 end
132 138
133 field_readers["text-single"] = 139 field_readers["text-single"] =
134 function (field_tag) 140 function (field_tag, required)
135 local value = field_tag:child_with_name("value"); 141 local data = field_tag:get_child_text("value");
136 if value then 142 if data and #data > 0 then
137 return value[1]; 143 return data
138 end 144 elseif required then
139 end 145 return nil, "Required value missing";
140 146 end
141 field_readers["text-private"] = 147 end
148
149 field_readers["text-private"] =
142 field_readers["text-single"]; 150 field_readers["text-single"];
143 151
144 field_readers["jid-single"] = 152 field_readers["jid-single"] =
145 field_readers["text-single"]; 153 function (field_tag, required)
146 154 local raw_data = field_tag:get_child_text("value")
147 field_readers["jid-multi"] = 155 local data = jid_prep(raw_data);
148 function (field_tag) 156 if data and #data > 0 then
157 return data
158 elseif raw_data then
159 return nil, "Invalid JID: " .. raw_data;
160 elseif required then
161 return nil, "Required value missing";
162 end
163 end
164
165 field_readers["jid-multi"] =
166 function (field_tag, required)
149 local result = {}; 167 local result = {};
150 for value_tag in field_tag:childtags() do 168 local err = {};
151 if value_tag.name == "value" then 169 for value_tag in field_tag:childtags("value") do
152 result[#result+1] = value_tag[1]; 170 local raw_value = value_tag:get_text();
153 end 171 local value = jid_prep(raw_value);
154 end 172 result[#result+1] = value;
155 return result; 173 if raw_value and not value then
156 end 174 err[#err+1] = ("Invalid JID: " .. raw_value);
157 175 end
158 field_readers["text-multi"] = 176 end
159 function (field_tag) 177 if #result > 0 then
178 return result, (#err > 0 and t_concat(err, "\n") or nil);
179 elseif required then
180 return nil, "Required value missing";
181 end
182 end
183
184 field_readers["list-multi"] =
185 function (field_tag, required)
160 local result = {}; 186 local result = {};
161 for value_tag in field_tag:childtags() do 187 for value in field_tag:childtags("value") do
162 if value_tag.name == "value" then 188 result[#result+1] = value:get_text();
163 result[#result+1] = value_tag[1]; 189 end
164 end 190 return result, (required and #result == 0 and "Required value missing" or nil);
165 end 191 end
166 return t_concat(result, "\n"); 192
193 field_readers["text-multi"] =
194 function (field_tag, required)
195 local data, err = field_readers["list-multi"](field_tag, required);
196 if data then
197 data = t_concat(data, "\n");
198 end
199 return data, err;
167 end 200 end
168 201
169 field_readers["list-single"] = 202 field_readers["list-single"] =
170 field_readers["text-single"]; 203 field_readers["text-single"];
171 204
172 field_readers["list-multi"] = 205 local boolean_values = {
206 ["1"] = true, ["true"] = true,
207 ["0"] = false, ["false"] = false,
208 };
209
210 field_readers["boolean"] =
211 function (field_tag, required)
212 local raw_value = field_tag:get_child_text("value");
213 local value = boolean_values[raw_value ~= nil and raw_value];
214 if value ~= nil then
215 return value;
216 elseif raw_value then
217 return nil, "Invalid boolean representation";
218 elseif required then
219 return nil, "Required value missing";
220 end
221 end
222
223 field_readers["hidden"] =
173 function (field_tag) 224 function (field_tag)
174 local result = {}; 225 return field_tag:get_child_text("value");
175 for value_tag in field_tag:childtags() do 226 end
176 if value_tag.name == "value" then 227
177 result[#result+1] = value_tag[1];
178 end
179 end
180 return result;
181 end
182
183 field_readers["boolean"] =
184 function (field_tag)
185 local value = field_tag:child_with_name("value");
186 if value then
187 if value[1] == "1" or value[1] == "true" then
188 return true;
189 else
190 return false;
191 end
192 end
193 end
194
195 field_readers["hidden"] =
196 function (field_tag)
197 local value = field_tag:child_with_name("value");
198 if value then
199 return value[1];
200 end
201 end
202
203 return _M; 228 return _M;
204 229
205 230
206 --[=[ 231 --[=[
207 232