annotate mod_rest/jsonmap.lib.lua @ 3922:ea59c9455f93

mod_rest: Move dataforms into structure for more logical code order Needs that `local x; x = ...` trick to make the mapping table be in scope for all the inner functions.
author Kim Alvefur <zash@zash.se>
date Fri, 28 Feb 2020 22:24:49 +0100
parents 1df4900bbd29
children 3c3d216c6f6d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local array = require "util.array";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local jid = require "util.jid";
3823
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
3 local json = require "util.json";
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local st = require "util.stanza";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local xml = require "util.xml";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
3922
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
7 local field_mappings; -- in scope for "func" mappings
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
8 field_mappings = {
3886
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
9 -- top level stanza attributes
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
10 -- needed here to mark them as known fields
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
11 kind = "attr",
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
12 type = "attr",
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
13 to = "attr",
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
14 from = "attr",
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
15 id = "attr",
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
16
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 -- basic message
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 body = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 subject = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 thread = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 -- basic presence
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 show = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 status = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 priority = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
27 state = { type = "name", xmlns = "http://jabber.org/protocol/chatstates" },
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
28 nick = { type = "text_tag", xmlns = "http://jabber.org/protocol/nick", tagname = "nick" },
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
29 delay = { type = "attr", xmlns = "urn:xmpp:delay", tagname = "delay", attr = "stamp" },
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
30 replace = { type = "attr", xmlns = "urn:xmpp:message-correct:0", tagname = "replace", attr = "id" },
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 -- XEP-0045 MUC
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 -- TODO history, password, ???
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
34 join = { type = "bool_tag", xmlns = "http://jabber.org/protocol/muc", tagname = "x" },
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 -- XEP-0071
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 html = {
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
38 type = "func", xmlns = "http://jabber.org/protocol/xhtml-im", tagname = "html",
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
39 st2json = function (s) --> json string
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
40 return (tostring(s:get_child("body", "http://www.w3.org/1999/xhtml")):gsub(" xmlns='[^']*'", "", 1));
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end;
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
42 json2st = function (s) --> xml
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
43 if type(s) == "string" then
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
44 return assert(xml.parse("<x:html xmlns:x='http://jabber.org/protocol/xhtml-im' xmlns='http://www.w3.org/1999/xhtml'>" .. s .. "</x:html>"));
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
45 end
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 };
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
3855
0e1e900577c4 mod_rest: Improve some comments
Kim Alvefur <zash@zash.se>
parents: 3854
diff changeset
49 -- XEP-0199: XMPP Ping
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
50 ping = { type = "bool_tag", xmlns = "urn:xmpp:ping", tagname = "ping" },
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51
3854
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
52 -- XEP-0092: Software Version
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
53 version = { type = "func", xmlns = "jabber:iq:version", tagname = "query",
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
54 st2json = function (s)
3854
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
55 return {
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
56 name = s:get_child_text("name");
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
57 version = s:get_child_text("version");
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
58 os = s:get_child_text("os");
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
59 }
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
60 end,
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
61 json2st = function (s)
3854
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
62 local v = st.stanza("query", { xmlns = "jabber:iq:version" });
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
63 if type(s) == "table" then
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
64 v:text_tag("name", s.name);
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
65 v:text_tag("version", s.version);
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
66 if s.os then
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
67 v:text_tag("os", s.os);
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
68 end
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
69 end
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
70 return v;
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
71 end
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
72 };
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
73
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 -- XEP-0030
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 disco = {
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
76 type = "func", xmlns = "http://jabber.org/protocol/disco#info", tagname = "query",
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
77 st2json = function (s) --> array of features
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 local identities, features = array(), array();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 for tag in s:childtags() do
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 if tag.name == "identity" and tag.attr.category and tag.attr.type then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 identities:push({ category = tag.attr.category, type = tag.attr.type, name = tag.attr.name });
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 elseif tag.name == "feature" and tag.attr.var then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 features:push(tag.attr.var);
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 end
3822
f0a1d113dce4 mod_rest: Add support for mapping 'node' attr in disco#info
Kim Alvefur <zash@zash.se>
parents: 3819
diff changeset
86 return { node = s.attr.node, identities = identities, features = features, };
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 end;
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
88 json2st = function (s)
3860
9752a6f1b9f3 mod_rest: Avoid treating special json.null value as any other table
Kim Alvefur <zash@zash.se>
parents: 3859
diff changeset
89 if type(s) == "table" and s ~= json.null then
3859
da3a0f055526 mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents: 3856
diff changeset
90 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", node = s.node });
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 if s.identities then
3848
1b9834500123 mod_rest: Fix iteration over disco#info identities
Kim Alvefur <zash@zash.se>
parents: 3823
diff changeset
92 for _, identity in ipairs(s.identities) do
3850
8d13b9c9ba75 mod_rest: Fix disco#info identities data mapping
Kim Alvefur <zash@zash.se>
parents: 3849
diff changeset
93 disco:tag("identity", { category = identity.category, type = identity.type, name = identity.name }):up();
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 if s.features then
3849
11c34e97fe1a mod_rest: Fix iteration over disco#info features
Kim Alvefur <zash@zash.se>
parents: 3848
diff changeset
97 for _, feature in ipairs(s.features) do
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 disco:tag("feature", { var = feature }):up();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 end
3859
da3a0f055526 mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents: 3856
diff changeset
101 return disco;
da3a0f055526 mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents: 3856
diff changeset
102 else
3870
3261a82884bb mod_rest: Fix missing return
Kim Alvefur <zash@zash.se>
parents: 3860
diff changeset
103 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", });
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 end;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 };
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 items = {
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
109 type = "func", xmlns = "http://jabber.org/protocol/disco#items", tagname = "query",
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
110 st2json = function (s) --> array of features | map with node
3875
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
111 if s.attr.node and s.tags[1] == nil then
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
112 return { node = s.attr.node };
3875
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
113 end
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
114
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 local items = array();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 for item in s:childtags("item") do
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 items:push({ jid = item.attr.jid, node = item.attr.node, name = item.attr.name });
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 return items;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 end;
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
121 json2st = function (s)
3860
9752a6f1b9f3 mod_rest: Avoid treating special json.null value as any other table
Kim Alvefur <zash@zash.se>
parents: 3859
diff changeset
122 if type(s) == "table" and s ~= json.null then
3875
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
123 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", node = s.node });
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 for _, item in ipairs(s) do
3852
66f96b98d219 mod_rest: Allow returning an array of JID strings as disco#items
Kim Alvefur <zash@zash.se>
parents: 3851
diff changeset
125 if type(item) == "string" then
66f96b98d219 mod_rest: Allow returning an array of JID strings as disco#items
Kim Alvefur <zash@zash.se>
parents: 3851
diff changeset
126 disco:tag("item", { jid = item });
66f96b98d219 mod_rest: Allow returning an array of JID strings as disco#items
Kim Alvefur <zash@zash.se>
parents: 3851
diff changeset
127 elseif type(item) == "table" then
66f96b98d219 mod_rest: Allow returning an array of JID strings as disco#items
Kim Alvefur <zash@zash.se>
parents: 3851
diff changeset
128 disco:tag("item", { jid = item.jid, node = item.node, name = item.name });
66f96b98d219 mod_rest: Allow returning an array of JID strings as disco#items
Kim Alvefur <zash@zash.se>
parents: 3851
diff changeset
129 end
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 end
3875
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
131 return disco;
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
132 else
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
133 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", });
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 end;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 };
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
138 -- XEP-0050: Ad-Hoc Commands
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
139 command = { type = "func", xmlns = "http://jabber.org/protocol/commands", tagname = "command",
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
140 st2json = function (s)
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
141 local cmd = {
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
142 action = s.attr.action,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
143 node = s.attr.node,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
144 sessionid = s.attr.sessionid,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
145 status = s.attr.status,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
146 };
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
147 local actions = s:get_child("actions");
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
148 local note = s:get_child("note");
3878
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
149 local form = s:get_child("x", "jabber:x:data");
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
150 if actions then
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
151 cmd.actions = {
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
152 execute = actions.attr.execute,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
153 };
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
154 for action in actions:childtags() do
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
155 cmd.actions[action.name] = true
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
156 end
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
157 elseif note then
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
158 cmd.note = {
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
159 type = note.attr.type;
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
160 text = note:get_text();
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
161 };
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
162 end
3878
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
163 if form then
3922
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
164 cmd.form = field_mappings.dataform.st2json(form);
3878
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
165 end
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
166 return cmd;
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
167 end;
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
168 json2st = function (s)
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
169 if type(s) == "table" and s ~= json.null then
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
170 local cmd = st.stanza("command", {
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
171 xmlns = "http://jabber.org/protocol/commands",
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
172 action = s.action,
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
173 node = s.node,
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
174 sessionid = s.sessionid,
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
175 status = s.status,
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
176 });
3881
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
177 if type(s.actions) == "table" then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
178 cmd:tag("actions", { execute = s.actions.execute });
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
179 do
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
180 if s.actions.next == true then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
181 cmd:tag("next"):up();
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
182 end
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
183 if s.actions.prev == true then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
184 cmd:tag("prev"):up();
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
185 end
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
186 if s.actions.complete == true then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
187 cmd:tag("complete"):up();
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
188 end
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
189 end
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
190 cmd:up();
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
191 elseif type(s.note) == "table" then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
192 cmd:text_tag("note", s.note.text, { type = s.note.type });
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
193 end
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
194 if s.form then
3922
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
195 cmd:add_child(field_mappings.dataform.json2st(s.form));
3888
04ea96a0488d mod_rest: Allow passing form data in a more compact format
Kim Alvefur <zash@zash.se>
parents: 3886
diff changeset
196 elseif s.data then
3922
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
197 cmd:add_child(field_mappings.formdata.json2st(s.data));
3881
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
198 end
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
199 return cmd;
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
200 elseif type(s) == "string" then -- assume node
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
201 return st.stanza("command", { xmlns = "http://jabber.org/protocol/commands", node = s });
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
202 end
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
203 -- else .. missing required attribute
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
204 end;
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
205 };
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
206
3855
0e1e900577c4 mod_rest: Improve some comments
Kim Alvefur <zash@zash.se>
parents: 3854
diff changeset
207 -- XEP-0066: Out of Band Data
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
208 oob_url = { type = "func", xmlns = "jabber:iq:oob", tagname = "query",
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
209 st2json = function (s)
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 return s:get_child_text("url");
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 end;
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
212 json2st = function (s)
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
213 if type(s) == "string" then
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
214 return st.stanza("query", { xmlns = "jabber:iq:oob" }):text_tag("url", s);
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
215 end
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 end;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 };
3823
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
218
3907
d5ecb9b9cb3b mod_rest: Update with namespace and name of XEP-0432
Kim Alvefur <zash@zash.se>
parents: 3906
diff changeset
219 -- XEP-0432: Simple JSON Messaging
d5ecb9b9cb3b mod_rest: Update with namespace and name of XEP-0432
Kim Alvefur <zash@zash.se>
parents: 3906
diff changeset
220 payload = { type = "func", xmlns = "urn:xmpp:json-msg:0", tagname = "payload",
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
221 st2json = function (s)
3823
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
222 local rawjson = s:get_child_text("json", "urn:xmpp:json:0");
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
223 if not rawjson then return nil, "missing-json-payload"; end
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
224 local parsed, err = json.decode(rawjson);
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
225 if not parsed then return nil, err; end
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
226 return {
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
227 datatype = s.attr.datatype;
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
228 data = parsed;
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
229 };
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
230 end;
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
231 json2st = function (s)
3823
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
232 if type(s) == "table" then
3907
d5ecb9b9cb3b mod_rest: Update with namespace and name of XEP-0432
Kim Alvefur <zash@zash.se>
parents: 3906
diff changeset
233 return st.stanza("payload", { xmlns = "urn:xmpp:json-msg:0", datatype = s.datatype })
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
234 :tag("json", { xmlns = "urn:xmpp:json:0" }):text(json.encode(s.data));
3823
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
235 end;
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
236 end
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
237 };
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
238
3889
59765d1bb6dc mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents: 3888
diff changeset
239 -- XEP-0004: Data Forms
3922
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
240 dataform = {
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
241 -- Generic and complete dataforms mapping
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
242 type = "func", xmlns = "jabber:x:data", tagname = "x",
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
243 st2json = function (s)
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
244 local fields = array();
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
245 local form = {
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
246 type = s.attr.type;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
247 title = s:get_child_text("title");
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
248 instructions = s:get_child_text("instructions");
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
249 fields = fields;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
250 };
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
251 for field in s:childtags("field") do
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
252 local i = {
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
253 var = field.attr.var;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
254 type = field.attr.type;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
255 label = field.attr.label;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
256 desc = field:get_child_text("desc");
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
257 required = field:get_child("required") and true or nil;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
258 value = field:get_child_text("value");
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
259 };
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
260 if field.attr.type == "jid-multi" or field.attr.type == "list-multi" or field.attr.type == "text-multi" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
261 local value = array();
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
262 for v in field:childtags("value") do
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
263 value:push(v:get_text());
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
264 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
265 if field.attr.type == "text-multi" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
266 i.value = value:concat("\n");
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
267 else
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
268 i.value = value;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
269 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
270 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
271 if field.attr.type == "list-single" or field.attr.type == "list-multi" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
272 local options = array();
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
273 for o in field:childtags("option") do
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
274 options:push({ label = o.attr.label, value = o:get_child_text("value") });
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
275 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
276 i.options = options;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
277 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
278 fields:push(i);
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
279 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
280 return form;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
281 end;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
282 json2st = function (x)
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
283 if type(x) == "table" and x ~= json.null then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
284 local form = st.stanza("x", { xmlns = "jabber:x:data", type = x.type });
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
285 if x.title then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
286 form:text_tag("title", x.title);
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
287 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
288 if x.instructions then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
289 form:text_tag("instructions", x.instructions);
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
290 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
291 if type(x.fields) == "table" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
292 for _, f in ipairs(x.fields) do
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
293 if type(f) == "table" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
294 form:tag("field", { var = f.var, type = f.type, label = f.label });
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
295 if f.desc then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
296 form:text_tag("desc", f.desc);
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
297 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
298 if f.required == true then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
299 form:tag("required"):up();
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
300 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
301 if type(f.value) == "string" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
302 form:text_tag("value", f.value);
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
303 elseif type(f.value) == "table" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
304 for _, v in ipairs(f.value) do
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
305 form:text_tag("value", v);
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
306 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
307 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
308 if type(f.options) == "table" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
309 for _, o in ipairs(f.value) do
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
310 if type(o) == "table" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
311 form:tag("option", { label = o.label });
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
312 form:text_tag("value", o.value);
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
313 form:up();
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
314 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
315 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
316 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
317 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
318 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
319 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
320 return form;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
321 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
322 end;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
323 };
3889
59765d1bb6dc mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents: 3888
diff changeset
324
3922
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
325 -- Simpler mapping of dataform from JSON map
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
326 formdata = { type = "func", xmlns = "jabber:x:data", tagname = "",
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
327 st2json = function ()
3889
59765d1bb6dc mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents: 3888
diff changeset
328 -- Tricky to do in a generic way without each form layout
59765d1bb6dc mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents: 3888
diff changeset
329 -- In the future, some well-known layouts might be understood
59765d1bb6dc mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents: 3888
diff changeset
330 return nil, "not-implemented";
59765d1bb6dc mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents: 3888
diff changeset
331 end,
3922
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
332 json2st = function (s, t)
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
333 local form = st.stanza("x", { xmlns = "jabber:x:data", type = t });
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
334 for k, v in pairs(s) do
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
335 form:tag("field", { var = k });
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
336 if type(v) == "string" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
337 form:text_tag("value", v);
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
338 elseif type(v) == "table" then
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
339 for _, v_ in ipairs(v) do
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
340 form:text_tag("value", v_);
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
341 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
342 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
343 end
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
344 return form;
ea59c9455f93 mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents: 3912
diff changeset
345 end
3889
59765d1bb6dc mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents: 3888
diff changeset
346 };
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 };
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 local implied_kinds = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 disco = "iq",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351 items = "iq",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 ping = "iq",
3854
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
353 version = "iq",
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
354 command = "iq",
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 body = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 html = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 replace = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 state = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 subject = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 thread = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
362
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 join = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 priority = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365 show = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366 status = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367 }
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369 local kind_by_type = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370 get = "iq", set = "iq", result = "iq",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371 normal = "message", chat = "message", headline = "message", groupchat = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 available = "presence", unavailable = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
373 subscribe = "presence", unsubscribe = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374 subscribed = "presence", unsubscribed = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375 }
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
376
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
377 local function st2json(s)
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
378 local t = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
379 kind = s.name,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
380 type = s.attr.type,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
381 to = s.attr.to,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
382 from = s.attr.from,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383 id = s.attr.id,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 };
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385 if s.name == "presence" and not s.attr.type then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 t.type = "available";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389 if t.to then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 t.to = jid.prep(t.to);
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 if not t.to then return nil, "invalid-jid-to"; end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
392 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393 if t.from then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 t.from = jid.prep(t.from);
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 if not t.from then return nil, "invalid-jid-from"; end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398 if t.type == "error" then
3871
e5d08bb58155 mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents: 3870
diff changeset
399 local error = s:get_child("error");
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 local err_typ, err_condition, err_text = s:get_error();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 t.error = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
402 type = err_typ,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 condition = err_condition,
3871
e5d08bb58155 mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents: 3870
diff changeset
404 text = err_text,
e5d08bb58155 mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents: 3870
diff changeset
405 by = error.attr.by,
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
406 };
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 return t;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
408 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
409
3895
25a3ad36ef3e mod_rest: Rename loop variable for improved clarity
Kim Alvefur <zash@zash.se>
parents: 3894
diff changeset
410 for k, mapping in pairs(field_mappings) do
25a3ad36ef3e mod_rest: Rename loop variable for improved clarity
Kim Alvefur <zash@zash.se>
parents: 3894
diff changeset
411 if mapping == "text_tag" then
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
412 t[k] = s:get_child_text(k);
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
413 elseif mapping.type == "text_tag" then
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
414 t[k] = s:get_child_text(mapping.tagname, mapping.xmlns);
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
415 elseif mapping.type == "name" then
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
416 local child = s:get_child(nil, mapping.xmlns);
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
417 if child then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
418 t[k] = child.name;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
419 end
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
420 elseif mapping.type == "attr" then
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
421 local child = s:get_child(mapping.tagname, mapping.xmlns);
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
422 if child then
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
423 t[k] = child.attr[mapping.attr];
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
424 end
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
425 elseif mapping.type == "bool_tag" then
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
426 if s:get_child(mapping.tagname, mapping.xmlns) then
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
427 t[k] = true;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
428 end
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
429 elseif mapping.type == "func" and mapping.st2json then
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
430 local child = s:get_child(mapping.tagname, mapping.xmlns or k);
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 -- TODO handle err
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432 if child then
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
433 t[k] = mapping.st2json(child);
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
434 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
437
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
438 return t;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
439 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
440
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
441 local function str(s)
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
442 if type(s) == "string" then
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
443 return s;
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
444 end
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
445 end
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
446
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
447 local function json2st(t)
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
448 if type(t) ~= "table" or not str(next(t)) then
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
449 return nil, "invalid-json";
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
450 end
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
451 local kind = str(t.kind) or kind_by_type[str(t.type)];
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
452 if not kind then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
453 for k, implied in pairs(implied_kinds) do
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
454 if t[k] then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
455 kind = implied;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
456 break
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
457 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
458 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
459 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
460
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
461 local s = st.stanza(kind or "message", {
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
462 type = t.type ~= "available" and str(t.type) or nil,
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
463 to = str(t.to) and jid.prep(t.to);
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
464 from = str(t.to) and jid.prep(t.from);
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
465 id = str(t.id),
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
466 });
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
467
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
468 if t.to and not s.attr.to then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
469 return nil, "invalid-jid-to";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
470 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
471 if t.from and not s.attr.from then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
472 return nil, "invalid-jid-from";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
473 end
3819
1bab6f67eb5f mod_rest: Fix previous commit
Kim Alvefur <zash@zash.se>
parents: 3818
diff changeset
474 if kind == "iq" and not s.attr.type then
1bab6f67eb5f mod_rest: Fix previous commit
Kim Alvefur <zash@zash.se>
parents: 3818
diff changeset
475 s.attr.type = "get";
3818
a607c69d0804 mod_rest: Guess 'get' as default type for 'iq' stanzas in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3817
diff changeset
476 end
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
477
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
478 if type(t.error) == "table" then
3871
e5d08bb58155 mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents: 3870
diff changeset
479 return st.error_reply(st.reply(s), str(t.error.type), str(t.error.condition), str(t.error.text), str(t.error.by));
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
480 elseif t.type == "error" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
481 s:text_tag("error", t.body, { code = t.error_code and tostring(t.error_code) });
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
482 return s;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
483 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
484
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
485 for k, v in pairs(t) do
3895
25a3ad36ef3e mod_rest: Rename loop variable for improved clarity
Kim Alvefur <zash@zash.se>
parents: 3894
diff changeset
486 local mapping = field_mappings[k];
25a3ad36ef3e mod_rest: Rename loop variable for improved clarity
Kim Alvefur <zash@zash.se>
parents: 3894
diff changeset
487 if mapping then
25a3ad36ef3e mod_rest: Rename loop variable for improved clarity
Kim Alvefur <zash@zash.se>
parents: 3894
diff changeset
488 if mapping == "text_tag" then
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
489 s:text_tag(k, v);
3895
25a3ad36ef3e mod_rest: Rename loop variable for improved clarity
Kim Alvefur <zash@zash.se>
parents: 3894
diff changeset
490 elseif mapping == "attr" then -- luacheck: ignore 542
3886
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
491 -- handled already
3896
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
492 elseif mapping.type == "text_tag" then
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
493 s:text_tag(mapping.tagname or k, v, mapping.xmlns and { xmlns = mapping.xmlns });
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
494 elseif mapping.type == "name" then
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
495 s:tag(v, { xmlns = mapping.xmlns }):up();
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
496 elseif mapping.type == "attr" then
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
497 s:tag(mapping.tagname or k, { xmlns = mapping.xmlns, [mapping.attr or k] = v }):up();
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
498 elseif mapping.type == "bool_tag" then
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
499 s:tag(mapping.tagname or k, { xmlns = mapping.xmlns }):up();
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
500 elseif mapping.type == "func" and mapping.json2st then
987b203bb091 mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents: 3895
diff changeset
501 s:add_child(mapping.json2st(v)):up();
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
502 end
3885
1ec45dbc7db5 mod_rest: Return an error for unknown fields in JSON input
Kim Alvefur <zash@zash.se>
parents: 3881
diff changeset
503 else
1ec45dbc7db5 mod_rest: Return an error for unknown fields in JSON input
Kim Alvefur <zash@zash.se>
parents: 3881
diff changeset
504 return nil, "unknown-field";
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
505 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
506 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
507
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
508 s:reset();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
509
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
510 return s;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
511 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
512
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
513 return {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
514 st2json = st2json;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
515 json2st = json2st;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
516 };