Mercurial > prosody-modules
annotate mod_rest/jsonmap.lib.lua @ 5027:8b9ab351dd78
mod_sasl2_sm: Use a stanza method that actually exists
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 29 Aug 2022 17:41:16 +0100 |
parents | b171ddf1bc3e |
children | 048e339706ba |
rev | line source |
---|---|
3813 | 1 local array = require "util.array"; |
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 | 4 local st = require "util.stanza"; |
5 local xml = require "util.xml"; | |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
6 local map = require "util.datamapper"; |
3813 | 7 |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
8 local schema do |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
9 local f = assert(module:load_resource("res/schema-xmpp.json")); |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
10 schema = json.decode(f:read("*a")) |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
11 f:close(); |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
12 -- Copy common properties to all stanza kinds |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
13 if schema._common then |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
14 for key, prop in pairs(schema._common) do |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
15 for _, copyto in pairs(schema.properties) do |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
16 copyto.properties[key] = prop; |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
17 end |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
18 end |
4733
755dd83e9214
mod_rest: Add JSON mapping of XEP-0313 archive result container
Kim Alvefur <zash@zash.se>
parents:
4732
diff
changeset
|
19 schema.properties.message.properties.archive.properties.forward = schema.properties.message.properties.forwarded; |
4732
607cac9b9393
mod_rest: Add recursive properties of XEP-0297 forwarded container
Kim Alvefur <zash@zash.se>
parents:
4731
diff
changeset
|
20 schema.properties.message.properties.forwarded.properties.delay = schema._common.delay; |
4841
f69c5a443156
mod_rest: Fix nested message stanzas in XEP-0297 containers
Kim Alvefur <zash@zash.se>
parents:
4803
diff
changeset
|
21 schema.properties.message.properties.forwarded.properties.message = schema.properties.message; |
4742
b7df2c61a144
mod_rest: Add support for mapping of XEP-0313 query iqs
Kim Alvefur <zash@zash.se>
parents:
4733
diff
changeset
|
22 schema.properties.iq.properties.archive.properties.form = schema._common.dataform; |
b7df2c61a144
mod_rest: Add support for mapping of XEP-0313 query iqs
Kim Alvefur <zash@zash.se>
parents:
4733
diff
changeset
|
23 schema.properties.iq.properties.archive.properties.page = schema._common.rsm; |
4747
566e54a07f54
mod_rest: Map the XEP-0313 <fin> element to make paging work
Kim Alvefur <zash@zash.se>
parents:
4746
diff
changeset
|
24 schema.properties.iq.properties.result.properties.page = schema._common.rsm; |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
25 schema._common = nil; |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
26 end |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
27 end |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
28 |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
29 -- Some mappings that are still hard to do in a nice way with util.datamapper |
3922
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
30 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
|
31 field_mappings = { |
3813 | 32 -- XEP-0071 |
33 html = { | |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
34 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
|
35 st2json = function (s) --> json string |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
36 return (tostring(s:get_child("body", "http://www.w3.org/1999/xhtml")):gsub(" xmlns='[^']*'", "", 1)); |
3813 | 37 end; |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
38 json2st = function (s) --> xml |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
39 if type(s) == "string" then |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
40 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
|
41 end |
3813 | 42 end; |
43 }; | |
44 | |
45 -- XEP-0030 | |
46 disco = { | |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
47 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
|
48 st2json = function (s) --> array of features |
4473
3b50a9a75fb6
mod_rest: Roundtrip disco and items when string or boolean is used
Kim Alvefur <zash@zash.se>
parents:
4372
diff
changeset
|
49 if s.tags[1] == nil then |
3b50a9a75fb6
mod_rest: Roundtrip disco and items when string or boolean is used
Kim Alvefur <zash@zash.se>
parents:
4372
diff
changeset
|
50 return s.attr.node or true; |
3b50a9a75fb6
mod_rest: Roundtrip disco and items when string or boolean is used
Kim Alvefur <zash@zash.se>
parents:
4372
diff
changeset
|
51 end |
3953
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
52 local identities, features, extensions = array(), array(), {}; |
4928
77b7e1322281
mod_rest: Add some notes about custom disco#info mapping code
Kim Alvefur <zash@zash.se>
parents:
4917
diff
changeset
|
53 |
77b7e1322281
mod_rest: Add some notes about custom disco#info mapping code
Kim Alvefur <zash@zash.se>
parents:
4917
diff
changeset
|
54 -- features and identities could be done with util.datamapper |
3813 | 55 for tag in s:childtags() do |
56 if tag.name == "identity" and tag.attr.category and tag.attr.type then | |
57 identities:push({ category = tag.attr.category, type = tag.attr.type, name = tag.attr.name }); | |
58 elseif tag.name == "feature" and tag.attr.var then | |
59 features:push(tag.attr.var); | |
60 end | |
61 end | |
4928
77b7e1322281
mod_rest: Add some notes about custom disco#info mapping code
Kim Alvefur <zash@zash.se>
parents:
4917
diff
changeset
|
62 |
77b7e1322281
mod_rest: Add some notes about custom disco#info mapping code
Kim Alvefur <zash@zash.se>
parents:
4917
diff
changeset
|
63 -- Especially this would be hard to do with util.datamapper |
3953
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
64 for form in s:childtags("x", "jabber:x:data") do |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
65 local jform = field_mappings.formdata.st2json(form); |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
66 local form_type = jform["FORM_TYPE"]; |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
67 if jform then |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
68 jform["FORM_TYPE"] = nil; |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
69 extensions[form_type] = jform; |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
70 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
71 end |
4928
77b7e1322281
mod_rest: Add some notes about custom disco#info mapping code
Kim Alvefur <zash@zash.se>
parents:
4917
diff
changeset
|
72 |
3953
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
73 if next(extensions) == nil then extensions = nil; end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
74 return { node = s.attr.node, identities = identities, features = features, extensions = extensions }; |
3813 | 75 end; |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
76 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
|
77 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
|
78 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", node = s.node }); |
3813 | 79 if s.identities then |
3848
1b9834500123
mod_rest: Fix iteration over disco#info identities
Kim Alvefur <zash@zash.se>
parents:
3823
diff
changeset
|
80 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
|
81 disco:tag("identity", { category = identity.category, type = identity.type, name = identity.name }):up(); |
3813 | 82 end |
83 end | |
84 if s.features then | |
3849
11c34e97fe1a
mod_rest: Fix iteration over disco#info features
Kim Alvefur <zash@zash.se>
parents:
3848
diff
changeset
|
85 for _, feature in ipairs(s.features) do |
3813 | 86 disco:tag("feature", { var = feature }):up(); |
87 end | |
88 end | |
3953
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
89 if s.extensions then |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
90 for form_type, extension in pairs(s.extensions) do |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
91 extension["FORM_TYPE"] = form_type; |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
92 disco:add_child(field_mappings.formdata.json2st(extension)); |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
93 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
94 end |
3859
da3a0f055526
mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents:
3856
diff
changeset
|
95 return disco; |
4473
3b50a9a75fb6
mod_rest: Roundtrip disco and items when string or boolean is used
Kim Alvefur <zash@zash.se>
parents:
4372
diff
changeset
|
96 elseif type(s) == "string" then |
3b50a9a75fb6
mod_rest: Roundtrip disco and items when string or boolean is used
Kim Alvefur <zash@zash.se>
parents:
4372
diff
changeset
|
97 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", node = s }); |
3859
da3a0f055526
mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents:
3856
diff
changeset
|
98 else |
3870 | 99 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", }); |
3813 | 100 end |
101 end; | |
102 }; | |
103 | |
104 items = { | |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
105 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
|
106 st2json = function (s) --> array of features | map with node |
4473
3b50a9a75fb6
mod_rest: Roundtrip disco and items when string or boolean is used
Kim Alvefur <zash@zash.se>
parents:
4372
diff
changeset
|
107 if s.tags[1] == nil then |
3b50a9a75fb6
mod_rest: Roundtrip disco and items when string or boolean is used
Kim Alvefur <zash@zash.se>
parents:
4372
diff
changeset
|
108 return s.attr.node or true; |
3875
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
109 end |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
110 |
3813 | 111 local items = array(); |
112 for item in s:childtags("item") do | |
113 items:push({ jid = item.attr.jid, node = item.attr.node, name = item.attr.name }); | |
114 end | |
115 return items; | |
116 end; | |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
117 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
|
118 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
|
119 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", node = s.node }); |
3813 | 120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 end |
3813 | 126 end |
3875
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
127 return disco; |
4473
3b50a9a75fb6
mod_rest: Roundtrip disco and items when string or boolean is used
Kim Alvefur <zash@zash.se>
parents:
4372
diff
changeset
|
128 elseif type(s) == "string" then |
3b50a9a75fb6
mod_rest: Roundtrip disco and items when string or boolean is used
Kim Alvefur <zash@zash.se>
parents:
4372
diff
changeset
|
129 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", node = s }); |
3875
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
130 else |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
131 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", }); |
3813 | 132 end |
133 end; | |
134 }; | |
135 | |
3877
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
136 -- XEP-0050: Ad-Hoc Commands |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
152 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
|
153 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
|
154 end |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
155 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
|
156 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
|
157 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
|
158 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
|
159 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
160 end |
3878
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
161 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
|
162 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
|
163 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
|
164 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
|
165 end; |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
166 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
|
167 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
|
168 local cmd = st.stanza("command", { |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
169 xmlns = "http://jabber.org/protocol/commands", |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
170 action = s.action, |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
171 node = s.node, |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
172 sessionid = s.sessionid, |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
173 status = s.status, |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
174 }); |
3881
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
175 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
|
176 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
|
177 do |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
178 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
|
179 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
|
180 end |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
181 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
|
182 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
|
183 end |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
184 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
|
185 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
|
186 end |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
187 end |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
188 cmd:up(); |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
189 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
|
190 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
|
191 end |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
192 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
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 end |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
201 -- 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
|
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 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
204 |
3855
0e1e900577c4
mod_rest: Improve some comments
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
205 -- XEP-0066: Out of Band Data |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
206 -- TODO Replace by oob.url in datamapper schema |
4501
42f43f1383db
mod_rest: Fix tag name in parsing of OOB payloads
Kim Alvefur <zash@zash.se>
parents:
4500
diff
changeset
|
207 oob_url = { type = "func", xmlns = "jabber:x:oob", tagname = "x", |
4499
8e644bf36627
mod_rest: Change OOB namespace to the one used in messages
Kim Alvefur <zash@zash.se>
parents:
4473
diff
changeset
|
208 -- XXX namespace depends on whether it's in an iq or message stanza |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
209 st2json = function (s) |
3813 | 210 return s:get_child_text("url"); |
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 |
4500
34c0f760f34a
mod_rest: Fix the OOB tag name which also differs in messages
Kim Alvefur <zash@zash.se>
parents:
4499
diff
changeset
|
214 return st.stanza("x", { xmlns = "jabber:x:oob" }):text_tag("url", s); |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
215 end |
3813 | 216 end; |
217 }; | |
3823
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
218 |
3889
59765d1bb6dc
mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents:
3888
diff
changeset
|
219 -- 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
|
220 dataform = { |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
221 -- 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
|
222 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
|
223 st2json = function (s) |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
224 local fields = array(); |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
225 local form = { |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
226 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
|
227 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
|
228 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
|
229 fields = fields; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
230 }; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
231 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
|
232 local i = { |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
233 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
|
234 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
|
235 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
|
236 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
|
237 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
|
238 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
|
239 }; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
240 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
|
241 local value = array(); |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
242 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
|
243 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
|
244 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
245 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
|
246 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
|
247 else |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
248 i.value = value; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
249 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
250 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
251 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
|
252 local options = array(); |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
253 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
|
254 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
|
255 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
256 i.options = options; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
257 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
258 fields:push(i); |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
259 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
260 return form; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
261 end; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
262 json2st = function (x) |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
263 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
|
264 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
|
265 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
|
266 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
|
267 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
268 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
|
269 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
|
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 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
|
272 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
|
273 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
|
274 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
|
275 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
|
276 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
|
277 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
278 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
|
279 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
|
280 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
281 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
|
282 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
|
283 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
|
284 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
|
285 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
|
286 end |
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 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
|
289 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
|
290 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
|
291 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
|
292 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
|
293 form:up(); |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
294 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
295 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
296 end |
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 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
299 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
300 return form; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
301 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
302 end; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
303 }; |
3889
59765d1bb6dc
mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents:
3888
diff
changeset
|
304 |
3922
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
305 -- 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
|
306 formdata = { type = "func", xmlns = "jabber:x:data", tagname = "", |
3953
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
307 st2json = function (s) |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
308 local r = {}; |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
309 for field in s:childtags("field") do |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
310 if field.attr.var then |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
311 local values = array(); |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
312 for value in field:childtags("value") do |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
313 values:push(value:get_text()); |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
314 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
315 if field.attr.type == "list-single" or field.attr.type == "list-multi" then |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
316 r[field.attr.var] = values; |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
317 elseif field.attr.type == "text-multi" then |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
318 r[field.attr.var] = values:concat("\n"); |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
319 elseif field.attr.type == "boolean" then |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
320 r[field.attr.var] = values[1] == "1" or values[1] == "true"; |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
321 elseif field.attr.type then |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
322 r[field.attr.var] = values[1] or json.null; |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
323 else -- type is optional, no way to know if multiple or single value is expected |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
324 r[field.attr.var] = values; |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
325 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
326 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
327 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
328 return r; |
3889
59765d1bb6dc
mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents:
3888
diff
changeset
|
329 end, |
3922
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 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
|
338 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
|
339 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
340 end |
3932
8b34222216f4
mod_rest: Fix encoding of simple dataforms
Kim Alvefur <zash@zash.se>
parents:
3923
diff
changeset
|
341 form:up(); |
3922
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 return form; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
344 end |
3889
59765d1bb6dc
mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents:
3888
diff
changeset
|
345 }; |
3923
3c3d216c6f6d
mod_rest: Add JSON mapping of XEP-0039: Statistics Gathering
Kim Alvefur <zash@zash.se>
parents:
3922
diff
changeset
|
346 |
3813 | 347 }; |
348 | |
4309
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
349 local byxmlname = {}; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
350 for k, spec in pairs(field_mappings) do |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
351 for _, replace in pairs(schema.properties) do |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
352 replace.properties[k] = nil |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
353 end |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
354 |
4309
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
355 if type(spec) == "table" then |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
356 spec.key = k; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
357 if spec.xmlns and spec.tagname then |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
358 byxmlname["{" .. spec.xmlns .. "}" .. spec.tagname] = spec; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
359 elseif spec.type == "name" then |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
360 byxmlname["{" .. spec.xmlns .. "}"] = spec; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
361 end |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
362 elseif type(spec) == "string" then |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
363 byxmlname["{jabber:client}" .. k] = {key = k; type = spec}; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
364 end |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
365 end |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
366 |
3813 | 367 local implied_kinds = { |
368 disco = "iq", | |
369 items = "iq", | |
370 ping = "iq", | |
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
371 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
|
372 command = "iq", |
4743
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
373 archive = "iq", |
3813 | 374 |
375 body = "message", | |
376 html = "message", | |
377 replace = "message", | |
378 state = "message", | |
379 subject = "message", | |
380 thread = "message", | |
381 | |
382 join = "presence", | |
383 priority = "presence", | |
384 show = "presence", | |
385 status = "presence", | |
386 } | |
387 | |
4021
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
388 local implied_types = { |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
389 command = "set", |
4746
88f5e12c8351
mod_rest: Imply type=set for archive queries
Kim Alvefur <zash@zash.se>
parents:
4744
diff
changeset
|
390 archive = "set", |
4021
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
391 } |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
392 |
3813 | 393 local kind_by_type = { |
394 get = "iq", set = "iq", result = "iq", | |
395 normal = "message", chat = "message", headline = "message", groupchat = "message", | |
396 available = "presence", unavailable = "presence", | |
397 subscribe = "presence", unsubscribe = "presence", | |
398 subscribed = "presence", unsubscribed = "presence", | |
399 } | |
400 | |
401 local function st2json(s) | |
4731
d71beacaec3b
mod_rest: Apply JSON mapping to items in <xmpp> container (e.g. MAM results etc)
Kim Alvefur <zash@zash.se>
parents:
4525
diff
changeset
|
402 if s.name == "xmpp" then |
d71beacaec3b
mod_rest: Apply JSON mapping to items in <xmpp> container (e.g. MAM results etc)
Kim Alvefur <zash@zash.se>
parents:
4525
diff
changeset
|
403 local result = array(); |
d71beacaec3b
mod_rest: Apply JSON mapping to items in <xmpp> container (e.g. MAM results etc)
Kim Alvefur <zash@zash.se>
parents:
4525
diff
changeset
|
404 for child in s:childtags() do |
d71beacaec3b
mod_rest: Apply JSON mapping to items in <xmpp> container (e.g. MAM results etc)
Kim Alvefur <zash@zash.se>
parents:
4525
diff
changeset
|
405 result:push(st2json(child)); |
d71beacaec3b
mod_rest: Apply JSON mapping to items in <xmpp> container (e.g. MAM results etc)
Kim Alvefur <zash@zash.se>
parents:
4525
diff
changeset
|
406 end |
d71beacaec3b
mod_rest: Apply JSON mapping to items in <xmpp> container (e.g. MAM results etc)
Kim Alvefur <zash@zash.se>
parents:
4525
diff
changeset
|
407 return { xmpp = result }; |
d71beacaec3b
mod_rest: Apply JSON mapping to items in <xmpp> container (e.g. MAM results etc)
Kim Alvefur <zash@zash.se>
parents:
4525
diff
changeset
|
408 end |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
409 local t = map.parse(schema.properties[s.name], s); |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
410 |
4525
b68b801ddc50
mod_rest: Restore 'kind' property in JSON-mapped objects
Kim Alvefur <zash@zash.se>
parents:
4519
diff
changeset
|
411 t.kind = s.name; |
b68b801ddc50
mod_rest: Restore 'kind' property in JSON-mapped objects
Kim Alvefur <zash@zash.se>
parents:
4519
diff
changeset
|
412 |
3813 | 413 if s.name == "presence" and not s.attr.type then |
414 t.type = "available"; | |
415 end | |
416 | |
417 if t.to then | |
418 t.to = jid.prep(t.to); | |
419 if not t.to then return nil, "invalid-jid-to"; end | |
420 end | |
421 if t.from then | |
422 t.from = jid.prep(t.from); | |
423 if not t.from then return nil, "invalid-jid-from"; end | |
424 end | |
425 | |
426 if t.type == "error" then | |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
427 local error = s:get_child("error"); |
3813 | 428 local err_typ, err_condition, err_text = s:get_error(); |
429 t.error = { | |
430 type = err_typ, | |
431 condition = err_condition, | |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
432 text = err_text, |
4251
d33b480befcb
mod_rest: Fix attempt at indexing nil if an error stanza is missing <error>
Kim Alvefur <zash@zash.se>
parents:
4035
diff
changeset
|
433 by = error and error.attr.by or nil, |
3813 | 434 }; |
435 return t; | |
436 end | |
437 | |
4917
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
438 if type(t.payload) == "table" then |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
439 if type(t.payload.data) == "string" then |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
440 local data, err = json.decode(t.payload.data); |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
441 if err then |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
442 return nil, err; |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
443 else |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
444 t.payload.data = data; |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
445 end |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
446 else |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
447 return nil, "invalid payload.data"; |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
448 end |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
449 end |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
450 |
4372
78de3c7acf58
mod_rest: Fix json-mapping stanzas with text or whitespace between tags
Kim Alvefur <zash@zash.se>
parents:
4309
diff
changeset
|
451 for _, tag in ipairs(s.tags) do |
4309
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
452 local prefix = "{" .. (tag.attr.xmlns or "jabber:client") .. "}"; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
453 local mapping = byxmlname[prefix .. tag.name]; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
454 if not mapping then |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
455 mapping = byxmlname[prefix]; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
456 end |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
457 |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
458 if mapping and mapping.type == "func" and mapping.st2json then |
4309
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
459 t[mapping.key] = mapping.st2json(tag); |
3813 | 460 end |
461 end | |
462 | |
463 return t; | |
464 end | |
465 | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
466 local function str(s) |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
467 if type(s) == "string" then |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
468 return s; |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
469 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
470 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
471 |
3813 | 472 local function json2st(t) |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
473 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
|
474 return nil, "invalid-json"; |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
475 end |
4021
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
476 local t_type = str(t.type); |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
477 if t_type == nil then |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
478 for k, implied in pairs(implied_types) do |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
479 if t[k] then |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
480 t_type = implied; |
4744
f478855f4565
mod_rest: Stop search when an implied type is determined
Kim Alvefur <zash@zash.se>
parents:
4743
diff
changeset
|
481 break; |
4021
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
482 end |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
483 end |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
484 end |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
485 local kind = str(t.kind) or kind_by_type[t_type]; |
3813 | 486 if not kind then |
487 for k, implied in pairs(implied_kinds) do | |
488 if t[k] then | |
489 kind = implied; | |
490 break | |
491 end | |
492 end | |
493 end | |
494 | |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
495 if kind == "presence" and t_type == "available" then |
4021
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
496 t_type = nil; |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
497 elseif kind == "iq" and not t_type then |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
498 t_type = "get"; |
4021
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
499 end |
4803
f74c7c518bb2
mod_rest: Handle unknown 'kind' values
Kim Alvefur <zash@zash.se>
parents:
4747
diff
changeset
|
500 if not schema.properties[kind or "message"] then |
f74c7c518bb2
mod_rest: Handle unknown 'kind' values
Kim Alvefur <zash@zash.se>
parents:
4747
diff
changeset
|
501 return nil, "unknown-kind"; |
f74c7c518bb2
mod_rest: Handle unknown 'kind' values
Kim Alvefur <zash@zash.se>
parents:
4747
diff
changeset
|
502 end |
4021
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
503 |
4743
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
504 -- XEP-0313 conveninece mapping |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
505 if kind == "iq" and t_type == "set" and type(t.archive) == "table" and not t.archive.form then |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
506 local archive = t.archive; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
507 if archive["with"] or archive["start"] or archive["end"] or archive["before-id"] or archive["after-id"] |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
508 or archive["ids"] then |
4948
b171ddf1bc3e
mod_rest: Treat archive.ids as comma-separated to work in query string
Kim Alvefur <zash@zash.se>
parents:
4936
diff
changeset
|
509 if type(archive["ids"]) == "string" then |
b171ddf1bc3e
mod_rest: Treat archive.ids as comma-separated to work in query string
Kim Alvefur <zash@zash.se>
parents:
4936
diff
changeset
|
510 local ids = {}; |
b171ddf1bc3e
mod_rest: Treat archive.ids as comma-separated to work in query string
Kim Alvefur <zash@zash.se>
parents:
4936
diff
changeset
|
511 for id in archive["ids"]:gmatch("[^,]+") do |
b171ddf1bc3e
mod_rest: Treat archive.ids as comma-separated to work in query string
Kim Alvefur <zash@zash.se>
parents:
4936
diff
changeset
|
512 table.insert(ids, id); |
b171ddf1bc3e
mod_rest: Treat archive.ids as comma-separated to work in query string
Kim Alvefur <zash@zash.se>
parents:
4936
diff
changeset
|
513 end |
b171ddf1bc3e
mod_rest: Treat archive.ids as comma-separated to work in query string
Kim Alvefur <zash@zash.se>
parents:
4936
diff
changeset
|
514 archive["ids"] = ids; |
b171ddf1bc3e
mod_rest: Treat archive.ids as comma-separated to work in query string
Kim Alvefur <zash@zash.se>
parents:
4936
diff
changeset
|
515 end |
4743
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
516 archive.form = { |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
517 type = "submit"; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
518 fields = { |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
519 { var = "FORM_TYPE"; values = { "urn:xmpp:mam:2" } }; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
520 { var = "with"; values = { archive["with"] } }; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
521 { var = "start"; values = { archive["start"] } }; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
522 { var = "end"; values = { archive["end"] } }; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
523 { var = "before-id"; values = { archive["before-id"] } }; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
524 { var = "after-id"; values = { archive["after-id"] } }; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
525 { var = "ids"; values = archive["ids"] }; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
526 }; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
527 }; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
528 archive["with"] = nil; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
529 archive["start"] = nil; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
530 archive["end"] = nil; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
531 archive["before-id"] = nil; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
532 archive["after-id"] = nil; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
533 archive["ids"] = nil; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
534 end |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
535 |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
536 if archive["after"] or archive["before"] or archive["max"] then |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
537 archive.page = { after = archive["after"]; before = archive["before"]; max = tonumber(archive["max"]) } |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
538 archive["after"] = nil; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
539 archive["before"] = nil; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
540 archive["max"] = nil; |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
541 end |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
542 end |
0a501df823fd
mod_rest: Add some convenience mapping to make MAM queries easier
Kim Alvefur <zash@zash.se>
parents:
4742
diff
changeset
|
543 |
4917
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
544 if type(t.payload) == "table" then |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
545 t.payload.data = json.encode(t.payload.data); |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
546 end |
3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
Kim Alvefur <zash@zash.se>
parents:
4916
diff
changeset
|
547 |
4936
a85efae90e21
mod_rest: Expand mapping of XEP-0045 join stanza
Kim Alvefur <zash@zash.se>
parents:
4928
diff
changeset
|
548 if kind == "presence" and t.join == true and t.muc == nil then |
a85efae90e21
mod_rest: Expand mapping of XEP-0045 join stanza
Kim Alvefur <zash@zash.se>
parents:
4928
diff
changeset
|
549 -- COMPAT Older boolean 'join' property used with XEP-0045 |
a85efae90e21
mod_rest: Expand mapping of XEP-0045 join stanza
Kim Alvefur <zash@zash.se>
parents:
4928
diff
changeset
|
550 t.muc = {}; |
a85efae90e21
mod_rest: Expand mapping of XEP-0045 join stanza
Kim Alvefur <zash@zash.se>
parents:
4928
diff
changeset
|
551 end |
a85efae90e21
mod_rest: Expand mapping of XEP-0045 join stanza
Kim Alvefur <zash@zash.se>
parents:
4928
diff
changeset
|
552 |
4841
f69c5a443156
mod_rest: Fix nested message stanzas in XEP-0297 containers
Kim Alvefur <zash@zash.se>
parents:
4803
diff
changeset
|
553 local s = map.unparse(schema, { [kind or "message"] = t }).tags[1]; |
3813 | 554 |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
555 s.attr.type = t_type; |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
556 s.attr.to = str(t.to) and jid.prep(t.to); |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
557 s.attr.from = str(t.to) and jid.prep(t.from); |
3813 | 558 |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
559 if type(t.error) == "table" then |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
560 return st.error_reply(st.reply(s), str(t.error.type), str(t.error.condition), str(t.error.text), str(t.error.by)); |
3813 | 561 elseif t.type == "error" then |
562 s:text_tag("error", t.body, { code = t.error_code and tostring(t.error_code) }); | |
563 return s; | |
564 end | |
565 | |
566 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
|
567 local mapping = field_mappings[k]; |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
568 if mapping and mapping.type == "func" and mapping.json2st then |
4916
1d231fb827d3
mod_rest/jsonmap: Fix indentation
Kim Alvefur <zash@zash.se>
parents:
4841
diff
changeset
|
569 s:add_child(mapping.json2st(v)):up(); |
1d231fb827d3
mod_rest/jsonmap: Fix indentation
Kim Alvefur <zash@zash.se>
parents:
4841
diff
changeset
|
570 end |
3813 | 571 end |
572 | |
573 s:reset(); | |
574 | |
575 return s; | |
576 end | |
577 | |
578 return { | |
579 st2json = st2json; | |
580 json2st = json2st; | |
581 }; |