Annotate

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