Annotate

util/dataforms.lua @ 370:75e7917761c0

Makefile, buildscripts/squish: Add Makefile and squish to the repo
author Matthew Wild <mwild1@gmail.com>
date Thu, 31 Dec 2015 21:12:00 +0000
parent 335:9e69ee8542d4
child 441:e4c0b1d7fd6b
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
335
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
27 function from_stanza(stanza)
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
28 local layout = {
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
29 title = stanza:get_child_text("title");
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
30 instructions = stanza:get_child_text("instructions");
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
31 };
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
32 for tag in stanza:childtags("field") do
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
33 local field = {
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
34 name = tag.attr.var;
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
35 label = tag.attr.label;
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
36 type = tag.attr.type;
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
37 required = tag:get_child("required") and true or nil;
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
38 value = tag:get_child_text("value");
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
39 };
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
40 layout[#layout+1] = field;
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
41 if field.type then
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
42 local value = {};
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
43 if field.type:match"list%-" then
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
44 for tag in tag:childtags("option") do
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
45 value[#value+1] = { label = tag.attr.label, value = tag:get_child_text("value") };
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
46 end
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
47 for tag in tag:childtags("value") do
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
48 value[#value+1] = { label = tag.attr.label, value = tag:get_text(), default = true };
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
49 end
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
50 elseif field.type:match"%-multi" then
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
51 for tag in tag:childtags("value") do
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
52 value[#value+1] = tag.attr.label and { label = tag.attr.label, value = tag:get_text() } or tag:get_text();
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
53 end
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
54 if field.type == "text-multi" then
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
55 field.value = t_concat(value, "\n");
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
56 else
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
57 field.value = value;
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
58 end
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
59 end
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
60 end
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
61 end
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
62 return new(layout);
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
63 end
9e69ee8542d4 util.dataforms: Add method for converting XML dataforms to tables
Kim Alvefur <zash@zash.se>
parents: 334
diff changeset
64
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 function form_t.form(layout, data, formtype)
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 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
67 if layout.title then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 form:tag("title"):text(layout.title):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 if layout.instructions then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 form:tag("instructions"):text(layout.instructions):up();
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 for n, field in ipairs(layout) do
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local field_type = field.type or "text-single";
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 -- Add field tag
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 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
77
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 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
79
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if value then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 -- Add value, depending on type
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 if field_type == "hidden" then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if type(value) == "table" then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 -- Assume an XML snippet
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 form:tag("value")
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 :add_child(value)
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 :up();
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 else
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 form:tag("value"):text(tostring(value)):up();
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 elseif field_type == "boolean" then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 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
93 elseif field_type == "fixed" then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 elseif field_type == "jid-multi" then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 for _, jid in ipairs(value) do
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 form:tag("value"):text(jid):up();
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 elseif field_type == "jid-single" then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 form:tag("value"):text(value):up();
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 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
102 form:tag("value"):text(value):up();
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 elseif field_type == "text-multi" then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 -- 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
105 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
106 form:tag("value"):text(line):up();
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 elseif field_type == "list-single" then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local has_default = false;
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
110 for _, val in ipairs(value) do
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
111 if type(val) == "table" then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
112 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
113 if val.default and (not has_default) then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
114 form:tag("value"):text(val.value):up();
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
115 has_default = true;
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 end
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
117 else
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
118 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
119 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 elseif field_type == "list-multi" then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 for _, val in ipairs(value) do
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 if type(val) == "table" then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 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
125 if val.default then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 form:tag("value"):text(val.value):up();
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 else
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 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
130 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 if field.required then
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 form:tag("required"):up();
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
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 -- Jump back up to list of fields
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 form:up();
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 return form;
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 local field_readers = {};
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 function form_t.data(layout, stanza)
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 local data = {};
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
149 local errors = {};
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
150
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
151 for _, field in ipairs(layout) do
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
152 local tag;
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
153 for field_tag in stanza:childtags() do
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 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
155 tag = field_tag;
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 break;
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 end
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
159
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
160 if not tag then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
161 if field.required then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
162 errors[field.name] = "Required value missing";
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
163 end
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
164 else
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
165 local reader = field_readers[field.type];
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
166 if reader then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
167 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
168 end
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 end
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
170 end
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
171 if next(errors) then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
172 return data, errors;
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 return data;
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
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
177 field_readers["text-single"] =
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
178 function (field_tag, required)
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
179 local data = field_tag:get_child_text("value");
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
180 if data and #data > 0 then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
181 return data
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
182 elseif required then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
183 return nil, "Required value missing";
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
187 field_readers["text-private"] =
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 field_readers["text-single"];
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 field_readers["jid-single"] =
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
191 function (field_tag, required)
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
192 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
193 local data = jid_prep(raw_data);
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
194 if data and #data > 0 then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
195 return data
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
196 elseif raw_data then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
197 return nil, "Invalid JID: " .. raw_data;
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
198 elseif required then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
199 return nil, "Required value missing";
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
200 end
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
201 end
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
203 field_readers["jid-multi"] =
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
204 function (field_tag, required)
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 local result = {};
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
206 local err = {};
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
207 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
208 local raw_value = value_tag:get_text();
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
209 local value = jid_prep(raw_value);
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
210 result[#result+1] = value;
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
211 if raw_value and not value then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
212 err[#err+1] = ("Invalid JID: " .. raw_value);
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 end
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
215 if #result > 0 then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
216 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
217 elseif required then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
218 return nil, "Required value missing";
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
219 end
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
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
222 field_readers["list-multi"] =
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
223 function (field_tag, required)
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 local result = {};
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
225 for value in field_tag:childtags("value") do
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
226 result[#result+1] = value:get_text();
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 end
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
228 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
229 end
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
230
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
231 field_readers["text-multi"] =
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
232 function (field_tag, required)
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
233 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
234 if data then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
235 data = t_concat(data, "\n");
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
236 end
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
237 return data, err;
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 field_readers["list-single"] =
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 field_readers["text-single"];
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
243 local boolean_values = {
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
244 ["1"] = true, ["true"] = true,
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
245 ["0"] = false, ["false"] = false,
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
246 };
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
247
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
248 field_readers["boolean"] =
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
249 function (field_tag, required)
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
250 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
251 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
252 if value ~= nil then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
253 return value;
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
254 elseif raw_value then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
255 return nil, "Invalid boolean representation";
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
256 elseif required then
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
257 return nil, "Required value missing";
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
261 field_readers["hidden"] =
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 function (field_tag)
334
34c52f3b21c4 util.dataforms: Update from Prosody repo
Kim Alvefur <zash@zash.se>
parents: 123
diff changeset
263 return field_tag:get_child_text("value");
123
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 end
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 return _M;
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 --[=[
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 Layout:
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 {
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 title = "MUC Configuration",
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 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
276
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 { name = "FORM_TYPE", type = "hidden", required = true };
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 { name = "field-name", type = "field-type", required = false };
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 }
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281
8a079aa70b84 util.dataforms, squishy: Add util.dataforms library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 --]=]