Software /
code /
prosody-modules
Annotate
mod_rest/jsonmap.lib.lua @ 4733:755dd83e9214
mod_rest: Add JSON mapping of XEP-0313 archive result container
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 27 Oct 2021 20:54:54 +0200 |
parent | 4732:607cac9b9393 |
child | 4742:b7df2c61a144 |
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; |
607cac9b9393
mod_rest: Add recursive properties of XEP-0297 forwarded container
Kim Alvefur <zash@zash.se>
parents:
4731
diff
changeset
|
21 schema.properties.message.properties.forwarded.properties.message.properties = schema.properties.message.properties; |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
22 schema._common = nil; |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
23 end |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
24 end |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
25 |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
26 -- 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
|
27 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
|
28 field_mappings = { |
3813 | 29 -- XEP-0071 |
30 html = { | |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
31 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
|
32 st2json = function (s) --> json string |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
33 return (tostring(s:get_child("body", "http://www.w3.org/1999/xhtml")):gsub(" xmlns='[^']*'", "", 1)); |
3813 | 34 end; |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
35 json2st = function (s) --> xml |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
36 if type(s) == "string" then |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
37 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
|
38 end |
3813 | 39 end; |
40 }; | |
41 | |
42 -- XEP-0030 | |
43 disco = { | |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
44 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
|
45 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
|
46 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
|
47 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
|
48 end |
3953
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
49 local identities, features, extensions = array(), array(), {}; |
3813 | 50 for tag in s:childtags() do |
51 if tag.name == "identity" and tag.attr.category and tag.attr.type then | |
52 identities:push({ category = tag.attr.category, type = tag.attr.type, name = tag.attr.name }); | |
53 elseif tag.name == "feature" and tag.attr.var then | |
54 features:push(tag.attr.var); | |
55 end | |
56 end | |
3953
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 if jform then |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
61 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
|
62 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
|
63 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
64 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
65 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
|
66 return { node = s.attr.node, identities = identities, features = features, extensions = extensions }; |
3813 | 67 end; |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
68 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
|
69 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
|
70 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", node = s.node }); |
3813 | 71 if s.identities then |
3848
1b9834500123
mod_rest: Fix iteration over disco#info identities
Kim Alvefur <zash@zash.se>
parents:
3823
diff
changeset
|
72 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
|
73 disco:tag("identity", { category = identity.category, type = identity.type, name = identity.name }):up(); |
3813 | 74 end |
75 end | |
76 if s.features then | |
3849
11c34e97fe1a
mod_rest: Fix iteration over disco#info features
Kim Alvefur <zash@zash.se>
parents:
3848
diff
changeset
|
77 for _, feature in ipairs(s.features) do |
3813 | 78 disco:tag("feature", { var = feature }):up(); |
79 end | |
80 end | |
3953
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 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
|
85 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
86 end |
3859
da3a0f055526
mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents:
3856
diff
changeset
|
87 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
|
88 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
|
89 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
|
90 else |
3870 | 91 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", }); |
3813 | 92 end |
93 end; | |
94 }; | |
95 | |
96 items = { | |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
97 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
|
98 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
|
99 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
|
100 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
|
101 end |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
102 |
3813 | 103 local items = array(); |
104 for item in s:childtags("item") do | |
105 items:push({ jid = item.attr.jid, node = item.attr.node, name = item.attr.name }); | |
106 end | |
107 return items; | |
108 end; | |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
109 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
|
110 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
|
111 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", node = s.node }); |
3813 | 112 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 end |
3813 | 118 end |
3875
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 else |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
123 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", }); |
3813 | 124 end |
125 end; | |
126 }; | |
127 | |
3877
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
128 -- XEP-0050: Ad-Hoc Commands |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
144 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
|
145 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
|
146 end |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 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
|
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 end |
3878
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 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
|
157 end; |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
158 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
|
159 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
|
160 local cmd = st.stanza("command", { |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
161 xmlns = "http://jabber.org/protocol/commands", |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
162 action = s.action, |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
163 node = s.node, |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
164 sessionid = s.sessionid, |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
165 status = s.status, |
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
166 }); |
3881
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
167 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
|
168 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
|
169 do |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
170 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
|
171 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
|
172 end |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
173 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
|
174 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
|
175 end |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
176 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
|
177 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
|
178 end |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
179 end |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
180 cmd:up(); |
5d7df207dc2b
mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents:
3880
diff
changeset
|
181 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
|
182 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
|
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.form then |
3922
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 end |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
193 -- 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
|
194 end; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
195 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
196 |
3855
0e1e900577c4
mod_rest: Improve some comments
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
197 -- 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
|
198 -- 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
|
199 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
|
200 -- 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
|
201 st2json = function (s) |
3813 | 202 return s:get_child_text("url"); |
203 end; | |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
204 json2st = function (s) |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
205 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
|
206 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
|
207 end |
3813 | 208 end; |
209 }; | |
3823
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
210 |
3907
d5ecb9b9cb3b
mod_rest: Update with namespace and name of XEP-0432
Kim Alvefur <zash@zash.se>
parents:
3906
diff
changeset
|
211 -- 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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 return { |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
219 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
|
220 data = parsed; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
221 }; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
222 end; |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
223 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
|
224 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
|
225 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
|
226 :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
|
227 end; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
228 end |
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 |
3889
59765d1bb6dc
mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents:
3888
diff
changeset
|
231 -- 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
|
232 dataform = { |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
233 -- 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
|
234 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
|
235 st2json = function (s) |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
236 local fields = array(); |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
237 local form = { |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
238 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
|
239 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
|
240 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
|
241 fields = fields; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
242 }; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
243 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
|
244 local i = { |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 }; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
252 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
|
253 local value = array(); |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
254 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
|
255 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
|
256 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
257 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
|
258 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
|
259 else |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
260 i.value = value; |
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 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
263 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
|
264 local options = array(); |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
265 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
|
266 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
|
267 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
268 i.options = options; |
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 fields:push(i); |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
271 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
272 return form; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
273 end; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
274 json2st = function (x) |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
275 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
|
276 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
|
277 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
|
278 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
|
279 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
280 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
|
281 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
|
282 end |
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.fields) == "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 _, 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
|
285 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
|
286 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
|
287 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
|
288 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
|
289 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
290 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
|
291 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
|
292 end |
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.value) == "string" then |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
294 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
|
295 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
|
296 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
|
297 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
|
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 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
|
301 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
|
302 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
|
303 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
|
304 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
|
305 form:up(); |
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 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
309 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
310 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
311 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
312 return form; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
313 end |
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 }; |
3889
59765d1bb6dc
mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents:
3888
diff
changeset
|
316 |
3922
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
317 -- 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
|
318 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
|
319 st2json = function (s) |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
320 local r = {}; |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
321 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
|
322 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
|
323 local values = array(); |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
324 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
|
325 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
|
326 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
327 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
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
338 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
339 end |
2c6d5734ae04
mod_rest: Add JSON mapping of XEP-0128: Service Discovery Extensions
Kim Alvefur <zash@zash.se>
parents:
3932
diff
changeset
|
340 return r; |
3889
59765d1bb6dc
mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents:
3888
diff
changeset
|
341 end, |
3922
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
342 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
|
343 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
|
344 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
|
345 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
|
346 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
|
347 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
|
348 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
|
349 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
|
350 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
|
351 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
352 end |
3932
8b34222216f4
mod_rest: Fix encoding of simple dataforms
Kim Alvefur <zash@zash.se>
parents:
3923
diff
changeset
|
353 form:up(); |
3922
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
354 end |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
355 return form; |
ea59c9455f93
mod_rest: Move dataforms into structure for more logical code order
Kim Alvefur <zash@zash.se>
parents:
3912
diff
changeset
|
356 end |
3889
59765d1bb6dc
mod_rest: Support mapping XEP-0004 Data Forms directly
Kim Alvefur <zash@zash.se>
parents:
3888
diff
changeset
|
357 }; |
3923
3c3d216c6f6d
mod_rest: Add JSON mapping of XEP-0039: Statistics Gathering
Kim Alvefur <zash@zash.se>
parents:
3922
diff
changeset
|
358 |
3813 | 359 }; |
360 | |
4309
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
361 local byxmlname = {}; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
362 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
|
363 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
|
364 replace.properties[k] = nil |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
365 end |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
366 |
4309
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
367 if type(spec) == "table" then |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
368 spec.key = k; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
369 if spec.xmlns and spec.tagname then |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
370 byxmlname["{" .. spec.xmlns .. "}" .. spec.tagname] = spec; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
371 elseif spec.type == "name" then |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
372 byxmlname["{" .. spec.xmlns .. "}"] = spec; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
373 end |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
374 elseif type(spec) == "string" then |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
375 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
|
376 end |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
377 end |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
378 |
3813 | 379 local implied_kinds = { |
380 disco = "iq", | |
381 items = "iq", | |
382 ping = "iq", | |
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
383 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
|
384 command = "iq", |
3813 | 385 |
386 body = "message", | |
387 html = "message", | |
388 replace = "message", | |
389 state = "message", | |
390 subject = "message", | |
391 thread = "message", | |
392 | |
393 join = "presence", | |
394 priority = "presence", | |
395 show = "presence", | |
396 status = "presence", | |
397 } | |
398 | |
4021
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
399 local implied_types = { |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
400 command = "set", |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
401 } |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
402 |
3813 | 403 local kind_by_type = { |
404 get = "iq", set = "iq", result = "iq", | |
405 normal = "message", chat = "message", headline = "message", groupchat = "message", | |
406 available = "presence", unavailable = "presence", | |
407 subscribe = "presence", unsubscribe = "presence", | |
408 subscribed = "presence", unsubscribed = "presence", | |
409 } | |
410 | |
411 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
|
412 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
|
413 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
|
414 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
|
415 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
|
416 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
|
417 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
|
418 end |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
419 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
|
420 |
4525
b68b801ddc50
mod_rest: Restore 'kind' property in JSON-mapped objects
Kim Alvefur <zash@zash.se>
parents:
4519
diff
changeset
|
421 t.kind = s.name; |
b68b801ddc50
mod_rest: Restore 'kind' property in JSON-mapped objects
Kim Alvefur <zash@zash.se>
parents:
4519
diff
changeset
|
422 |
3813 | 423 if s.name == "presence" and not s.attr.type then |
424 t.type = "available"; | |
425 end | |
426 | |
427 if t.to then | |
428 t.to = jid.prep(t.to); | |
429 if not t.to then return nil, "invalid-jid-to"; end | |
430 end | |
431 if t.from then | |
432 t.from = jid.prep(t.from); | |
433 if not t.from then return nil, "invalid-jid-from"; end | |
434 end | |
435 | |
436 if t.type == "error" then | |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
437 local error = s:get_child("error"); |
3813 | 438 local err_typ, err_condition, err_text = s:get_error(); |
439 t.error = { | |
440 type = err_typ, | |
441 condition = err_condition, | |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
442 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
|
443 by = error and error.attr.by or nil, |
3813 | 444 }; |
445 return t; | |
446 end | |
447 | |
4372
78de3c7acf58
mod_rest: Fix json-mapping stanzas with text or whitespace between tags
Kim Alvefur <zash@zash.se>
parents:
4309
diff
changeset
|
448 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
|
449 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
|
450 local mapping = byxmlname[prefix .. tag.name]; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
451 if not mapping then |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
452 mapping = byxmlname[prefix]; |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
453 end |
e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
Kim Alvefur <zash@zash.se>
parents:
4251
diff
changeset
|
454 |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
455 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
|
456 t[mapping.key] = mapping.st2json(tag); |
3813 | 457 end |
458 end | |
459 | |
460 return t; | |
461 end | |
462 | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
463 local function str(s) |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
464 if type(s) == "string" then |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
465 return s; |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
466 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
467 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
468 |
3813 | 469 local function json2st(t) |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
470 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
|
471 return nil, "invalid-json"; |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
472 end |
4021
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
473 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
|
474 if t_type == nil then |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
475 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
|
476 if t[k] then |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
477 t_type = implied; |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
478 end |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
479 end |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
480 end |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
481 local kind = str(t.kind) or kind_by_type[t_type]; |
3813 | 482 if not kind then |
483 for k, implied in pairs(implied_kinds) do | |
484 if t[k] then | |
485 kind = implied; | |
486 break | |
487 end | |
488 end | |
489 end | |
490 | |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
491 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
|
492 t_type = nil; |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
493 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
|
494 t_type = "get"; |
4021
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
495 end |
1925d63eec6b
mod_rest/jsonmap: Derive stanza @type from certain payloads
Kim Alvefur <zash@zash.se>
parents:
3953
diff
changeset
|
496 |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
497 local s = map.unparse(schema.properties[kind or "message"], t); |
3813 | 498 |
4518
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
499 s.attr.type = t_type; |
073f5397c1d2
mod_rest: Replace most mappings by using util.datamapper
Kim Alvefur <zash@zash.se>
parents:
4501
diff
changeset
|
500 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
|
501 s.attr.from = str(t.to) and jid.prep(t.from); |
3813 | 502 |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
503 if type(t.error) == "table" then |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
504 return st.error_reply(st.reply(s), str(t.error.type), str(t.error.condition), str(t.error.text), str(t.error.by)); |
3813 | 505 elseif t.type == "error" then |
506 s:text_tag("error", t.body, { code = t.error_code and tostring(t.error_code) }); | |
507 return s; | |
508 end | |
509 | |
510 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
|
511 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
|
512 if mapping and mapping.type == "func" and mapping.json2st then |
3896
987b203bb091
mod_rest: Restructure JSON / Stanza mapping definitions
Kim Alvefur <zash@zash.se>
parents:
3895
diff
changeset
|
513 s:add_child(mapping.json2st(v)):up(); |
3813 | 514 end |
515 end | |
516 | |
517 s:reset(); | |
518 | |
519 return s; | |
520 end | |
521 | |
522 return { | |
523 st2json = st2json; | |
524 json2st = json2st; | |
525 }; |