Mercurial > prosody-modules
annotate mod_captcha_registration/util/dataforms.lua @ 2076:d2ad556dcfb7
mod_firewall: Remove unused imports [luacheck]
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 11 Mar 2016 18:47:39 +0100 |
parents | 985bfc6e8cad |
children |
rev | line source |
---|---|
1373
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
1 -- Prosody IM |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
4 -- |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
6 -- COPYING file in the source package for more information. |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
7 -- |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
8 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
9 local setmetatable = setmetatable; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
10 local pairs, ipairs = pairs, ipairs; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
11 local tostring, type, next = tostring, type, next; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
12 local t_concat = table.concat; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
13 local st = require "util.stanza"; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
14 local jid_prep = require "util.jid".prep; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
15 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
16 module "dataforms" |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
17 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
18 local xmlns_forms = 'jabber:x:data'; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
19 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
20 local form_t = {}; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
21 local form_mt = { __index = form_t }; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
22 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
23 function new(layout) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
24 return setmetatable(layout, form_mt); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
25 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
26 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
27 function form_t.form(layout, data, formtype) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
28 local form = st.stanza("x", { xmlns = xmlns_forms, type = formtype or "form" }); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
29 if layout.title then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
30 form:tag("title"):text(layout.title):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
31 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
32 if layout.instructions then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
33 form:tag("instructions"):text(layout.instructions):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
34 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
35 for n, field in ipairs(layout) do |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
36 local field_type = field.type or "text-single"; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
37 -- Add field tag |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
38 form:tag("field", { type = field_type, var = field.name, label = field.label }); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
39 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
40 local value = (data and data[field.name]) or field.value; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
41 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
42 if value then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
43 -- Add value, depending on type |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
44 if field_type == "hidden" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
45 if type(value) == "table" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
46 -- Assume an XML snippet |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
47 form:tag("value") |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
48 :add_child(value) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
49 :up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
50 else |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
51 form:tag("value"):text(tostring(value)):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
52 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
53 elseif field_type == "boolean" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
54 form:tag("value"):text((value and "1") or "0"):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
55 elseif field_type == "fixed" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
56 form:tag("value"):text(value):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
57 elseif field_type == "jid-multi" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
58 for _, jid in ipairs(value) do |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
59 form:tag("value"):text(jid):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
60 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
61 elseif field_type == "jid-single" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
62 form:tag("value"):text(value):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
63 elseif field_type == "text-single" or field_type == "text-private" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
64 form:tag("value"):text(value):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
65 elseif field_type == "text-multi" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
66 -- Split into multiple <value> tags, one for each line |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
67 for line in value:gmatch("([^\r\n]+)\r?\n*") do |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
68 form:tag("value"):text(line):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
69 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
70 elseif field_type == "list-single" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
71 local has_default = false; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
72 for _, val in ipairs(value) do |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
73 if type(val) == "table" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
74 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
75 if val.default and (not has_default) then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
76 form:tag("value"):text(val.value):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
77 has_default = true; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
78 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
79 else |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
80 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
81 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
82 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
83 elseif field_type == "list-multi" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
84 for _, val in ipairs(value) do |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
85 if type(val) == "table" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
86 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
87 if val.default then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
88 form:tag("value"):text(val.value):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
89 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
90 else |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
91 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
92 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
93 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
94 elseif field_type == "media" then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
95 form:tag("media", { xmlns = "urn:xmpp:media-element" }); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
96 for _, val in ipairs(value) do |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
97 form:tag("uri", { type = val.type }):text(val.uri):up() |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
98 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
99 form:up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
100 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
101 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
102 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
103 if field.required then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
104 form:tag("required"):up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
105 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
106 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
107 -- Jump back up to list of fields |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
108 form:up(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
109 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
110 return form; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
111 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
112 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
113 local field_readers = {}; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
114 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
115 function form_t.data(layout, stanza) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
116 local data = {}; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
117 local errors = {}; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
118 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
119 for _, field in ipairs(layout) do |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
120 local tag; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
121 for field_tag in stanza:childtags() do |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
122 if field.name == field_tag.attr.var then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
123 tag = field_tag; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
124 break; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
125 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
126 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
127 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
128 if not tag then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
129 if field.required then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
130 errors[field.name] = "Required value missing"; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
131 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
132 else |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
133 local reader = field_readers[field.type]; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
134 if reader then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
135 data[field.name], errors[field.name] = reader(tag, field.required); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
136 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
137 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
138 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
139 if next(errors) then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
140 return data, errors; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
141 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
142 return data; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
143 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
144 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
145 field_readers["text-single"] = |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
146 function (field_tag, required) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
147 local data = field_tag:get_child_text("value"); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
148 if data and #data > 0 then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
149 return data |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
150 elseif required then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
151 return nil, "Required value missing"; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
152 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
153 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
154 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
155 field_readers["text-private"] = |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
156 field_readers["text-single"]; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
157 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
158 field_readers["jid-single"] = |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
159 function (field_tag, required) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
160 local raw_data = field_tag:get_child_text("value") |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
161 local data = jid_prep(raw_data); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
162 if data and #data > 0 then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
163 return data |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
164 elseif raw_data then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
165 return nil, "Invalid JID: " .. raw_data; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
166 elseif required then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
167 return nil, "Required value missing"; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
168 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
169 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
170 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
171 field_readers["jid-multi"] = |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
172 function (field_tag, required) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
173 local result = {}; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
174 local err = {}; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
175 for value_tag in field_tag:childtags("value") do |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
176 local raw_value = value_tag:get_text(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
177 local value = jid_prep(raw_value); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
178 result[#result+1] = value; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
179 if raw_value and not value then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
180 err[#err+1] = ("Invalid JID: " .. raw_value); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
181 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
182 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
183 if #result > 0 then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
184 return result, (#err > 0 and t_concat(err, "\n") or nil); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
185 elseif required then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
186 return nil, "Required value missing"; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
187 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
188 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
189 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
190 field_readers["list-multi"] = |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
191 function (field_tag, required) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
192 local result = {}; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
193 for value in field_tag:childtags("value") do |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
194 result[#result+1] = value:get_text(); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
195 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
196 if #result > 0 then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
197 return result; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
198 elseif required then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
199 return nil, "Required value missing"; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
200 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
201 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
202 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
203 field_readers["text-multi"] = |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
204 function (field_tag, required) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
205 local data, err = field_readers["list-multi"](field_tag, required); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
206 if data then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
207 data = t_concat(data, "\n"); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
208 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
209 return data, err; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
210 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
211 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
212 field_readers["list-single"] = |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
213 field_readers["text-single"]; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
214 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
215 local boolean_values = { |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
216 ["1"] = true, ["true"] = true, |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
217 ["0"] = false, ["false"] = false, |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
218 }; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
219 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
220 field_readers["boolean"] = |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
221 function (field_tag, required) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
222 local raw_value = field_tag:get_child_text("value"); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
223 local value = boolean_values[raw_value ~= nil and raw_value]; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
224 if value ~= nil then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
225 return value; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
226 elseif raw_value then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
227 return nil, "Invalid boolean representation"; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
228 elseif required then |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
229 return nil, "Required value missing"; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
230 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
231 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
232 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
233 field_readers["hidden"] = |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
234 function (field_tag) |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
235 return field_tag:get_child_text("value"); |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
236 end |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
237 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
238 field_readers["media"] = field_readers["text-single"] |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
239 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
240 return _M; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
241 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
242 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
243 --[=[ |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
244 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
245 Layout: |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
246 { |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
247 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
248 title = "MUC Configuration", |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
249 instructions = [[Use this form to configure options for this MUC room.]], |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
250 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
251 { name = "FORM_TYPE", type = "hidden", required = true }; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
252 { name = "field-name", type = "field-type", required = false }; |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
253 } |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
254 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
255 |
985bfc6e8cad
mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff
changeset
|
256 --]=] |