Mercurial > prosody-modules
annotate mod_rest/jsonmap.lib.lua @ 3878:9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 04 Feb 2020 22:06:19 +0100 |
parents | 562b34050561 |
children | 44c2d36c40a4 |
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"; | |
6 | |
3878
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
7 -- Reused in many XEPs so declared up here |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
8 local dataform = { |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
9 "func", "jabber:x:data", "x", |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
10 function (s) |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
11 local fields = array(); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
12 local form = { |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
13 type = s.attr.type; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
14 title = s:get_child_text("title"); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
15 instructions = s:get_child_text("instructions"); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
16 fields = fields; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
17 }; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
18 for field in s:childtags("field") do |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
19 local i = { |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
20 var = field.attr.var; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
21 type = field.attr.type; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
22 label = field.attr.label; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
23 desc = field:get_child_text("desc"); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
24 required = field:get_child("required") and true or nil; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
25 value = field:get_child_text("value"); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
26 }; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
27 if field.attr.type == "jid-multi" or field.attr.type == "list-multi" or field.attr.type == "text-multi" then |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
28 local value = array(); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
29 for v in field:childtags("value") do |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
30 value:push(v:get_text()); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
31 end |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
32 if field.attr.type == "text-multi" then |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
33 i.value = value:concat("\n"); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
34 else |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
35 i.value = value; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
36 end |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
37 end |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
38 if field.attr.type == "list-single" or field.attr.type == "list-multi" then |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
39 local options = array(); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
40 for o in field:childtags("option") do |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
41 options:push({ label = o.attr.label, value = o:get_child_text("value") }); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
42 end |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
43 i.options = options; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
44 end |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
45 fields:push(i); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
46 end |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
47 return form; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
48 end; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
49 function (x) |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
50 -- TODO |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
51 end; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
52 }; |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
53 |
3813 | 54 local simple_types = { |
55 -- basic message | |
56 body = "text_tag", | |
57 subject = "text_tag", | |
58 thread = "text_tag", | |
59 | |
60 -- basic presence | |
61 show = "text_tag", | |
62 status = "text_tag", | |
63 priority = "text_tag", | |
64 | |
65 state = {"name", "http://jabber.org/protocol/chatstates"}, | |
66 nick = {"text_tag", "http://jabber.org/protocol/nick", "nick"}, | |
67 delay = {"attr", "urn:xmpp:delay", "delay", "stamp"}, | |
68 replace = {"attr", "urn:xmpp:message-correct:0", "replace", "id"}, | |
69 | |
70 -- XEP-0045 MUC | |
71 -- TODO history, password, ??? | |
72 join = {"bool_tag", "http://jabber.org/protocol/muc", "x"}, | |
73 | |
74 -- XEP-0071 | |
75 html = { | |
76 "func", "http://jabber.org/protocol/xhtml-im", "html", | |
77 function (s) --> json string | |
3856
8bdb1729529b
mod_rest: Make XHTML-IM mapping more convenient
Kim Alvefur <zash@zash.se>
parents:
3855
diff
changeset
|
78 return (tostring(s:get_child("body", "http://www.w3.org/1999/xhtml")):gsub(" xmlns='[^']*'","", 1)); |
3813 | 79 end; |
80 function (s) --> xml | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
81 if type(s) == "string" then |
3856
8bdb1729529b
mod_rest: Make XHTML-IM mapping more convenient
Kim Alvefur <zash@zash.se>
parents:
3855
diff
changeset
|
82 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
|
83 end |
3813 | 84 end; |
85 }; | |
86 | |
3855
0e1e900577c4
mod_rest: Improve some comments
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
87 -- XEP-0199: XMPP Ping |
3813 | 88 ping = {"bool_tag", "urn:xmpp:ping", "ping"}, |
89 | |
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
90 -- XEP-0092: Software Version |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
91 version = {"func", "jabber:iq:version", "query", |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
92 function (s) |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
93 return { |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
94 name = s:get_child_text("name"); |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
95 version = s:get_child_text("version"); |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
96 os = s:get_child_text("os"); |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
97 } |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
98 end, |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
99 function (s) |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
100 local v = st.stanza("query", { xmlns = "jabber:iq:version" }); |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
101 if type(s) == "table" then |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
102 v:text_tag("name", s.name); |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
103 v:text_tag("version", s.version); |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
104 if s.os then |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
105 v:text_tag("os", s.os); |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
106 end |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
107 end |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
108 return v; |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
109 end |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
110 }; |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
111 |
3813 | 112 -- XEP-0030 |
113 disco = { | |
114 "func", "http://jabber.org/protocol/disco#info", "query", | |
115 function (s) --> array of features | |
116 local identities, features = array(), array(); | |
117 for tag in s:childtags() do | |
118 if tag.name == "identity" and tag.attr.category and tag.attr.type then | |
119 identities:push({ category = tag.attr.category, type = tag.attr.type, name = tag.attr.name }); | |
120 elseif tag.name == "feature" and tag.attr.var then | |
121 features:push(tag.attr.var); | |
122 end | |
123 end | |
3822
f0a1d113dce4
mod_rest: Add support for mapping 'node' attr in disco#info
Kim Alvefur <zash@zash.se>
parents:
3819
diff
changeset
|
124 return { node = s.attr.node, identities = identities, features = features, }; |
3813 | 125 end; |
126 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
|
127 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
|
128 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", node = s.node }); |
3813 | 129 if s.identities then |
3848
1b9834500123
mod_rest: Fix iteration over disco#info identities
Kim Alvefur <zash@zash.se>
parents:
3823
diff
changeset
|
130 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
|
131 disco:tag("identity", { category = identity.category, type = identity.type, name = identity.name }):up(); |
3813 | 132 end |
133 end | |
134 if s.features then | |
3849
11c34e97fe1a
mod_rest: Fix iteration over disco#info features
Kim Alvefur <zash@zash.se>
parents:
3848
diff
changeset
|
135 for _, feature in ipairs(s.features) do |
3813 | 136 disco:tag("feature", { var = feature }):up(); |
137 end | |
138 end | |
3859
da3a0f055526
mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents:
3856
diff
changeset
|
139 return disco; |
da3a0f055526
mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents:
3856
diff
changeset
|
140 else |
3870 | 141 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", }); |
3813 | 142 end |
143 end; | |
144 }; | |
145 | |
146 items = { | |
147 "func", "http://jabber.org/protocol/disco#items", "query", | |
3875
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
148 function (s) --> array of features | map with node |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
149 if s.attr.node and s.tags[1] == nil then |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
150 return { node = s.attr. node }; |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
151 end |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
152 |
3813 | 153 local items = array(); |
154 for item in s:childtags("item") do | |
155 items:push({ jid = item.attr.jid, node = item.attr.node, name = item.attr.name }); | |
156 end | |
157 return items; | |
158 end; | |
159 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
|
160 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
|
161 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", node = s.node }); |
3813 | 162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 end |
3813 | 168 end |
3875
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
169 return disco; |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
170 else |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
171 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", }); |
3813 | 172 end |
173 end; | |
174 }; | |
175 | |
3877
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
176 -- XEP-0050: Ad-Hoc Commands |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
177 command = {"func", "http://jabber.org/protocol/commands", "command", |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
178 function (s) |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
179 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
|
180 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
|
181 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
|
182 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
|
183 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
|
184 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
192 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
|
193 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
|
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 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
|
196 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
|
197 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
|
198 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
|
199 }; |
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 |
3878
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
201 if form then |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
202 cmd.form = dataform[4](form); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
203 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
|
204 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
|
205 end; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
206 function (s) |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
207 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
|
208 local cmd = st.stanza("command", { |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
209 xmlns = "http://jabber.org/protocol/commands", |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
210 action = s.action, |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
211 node = s.node, |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
212 sessionid = s.sessionid, |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
213 status = s.status, |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
214 }); |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
215 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
|
216 elseif type(s) == "string" then -- assume node |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
217 return st.stanza("command", { xmlns = "http://jabber.org/protocol/commands", node = s }); |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
218 end |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
219 -- 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
|
220 end; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
221 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
222 |
3855
0e1e900577c4
mod_rest: Improve some comments
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
223 -- XEP-0066: Out of Band Data |
3813 | 224 oob_url = {"func", "jabber:iq:oob", "query", |
225 function (s) | |
226 return s:get_child_text("url"); | |
227 end; | |
228 function (s) | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
229 if type(s) == "string" then |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
230 return st.stanza("query", { xmlns = "jabber:iq:oob" }):text_tag("url", s); |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
231 end |
3813 | 232 end; |
233 }; | |
3823
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
234 |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
235 -- XEP-XXXX: User-defined Data Transfer |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
236 payload = {"func", "urn:xmpp:udt:0", "payload", |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
237 function (s) |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
238 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
|
239 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
|
240 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
|
241 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
|
242 return { |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
243 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
|
244 data = parsed; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
245 }; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
246 end; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
247 function (s) |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
248 if type(s) == "table" then |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
249 return st.stanza("payload", { xmlns = "urn:xmpp:udt:0", datatype = s.datatype }) |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
250 :tag("json", { xmlns = "urn:xmpp:json:0" }):text(json.encode(s.data)); |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
251 end; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
252 end |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
253 }; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
254 |
3813 | 255 }; |
256 | |
257 local implied_kinds = { | |
258 disco = "iq", | |
259 items = "iq", | |
260 ping = "iq", | |
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
261 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
|
262 command = "iq", |
3813 | 263 |
264 body = "message", | |
265 html = "message", | |
266 replace = "message", | |
267 state = "message", | |
268 subject = "message", | |
269 thread = "message", | |
270 | |
271 join = "presence", | |
272 priority = "presence", | |
273 show = "presence", | |
274 status = "presence", | |
275 } | |
276 | |
277 local kind_by_type = { | |
278 get = "iq", set = "iq", result = "iq", | |
279 normal = "message", chat = "message", headline = "message", groupchat = "message", | |
280 available = "presence", unavailable = "presence", | |
281 subscribe = "presence", unsubscribe = "presence", | |
282 subscribed = "presence", unsubscribed = "presence", | |
283 } | |
284 | |
285 local function st2json(s) | |
286 local t = { | |
287 kind = s.name, | |
288 type = s.attr.type, | |
289 to = s.attr.to, | |
290 from = s.attr.from, | |
291 id = s.attr.id, | |
292 }; | |
293 if s.name == "presence" and not s.attr.type then | |
294 t.type = "available"; | |
295 end | |
296 | |
297 if t.to then | |
298 t.to = jid.prep(t.to); | |
299 if not t.to then return nil, "invalid-jid-to"; end | |
300 end | |
301 if t.from then | |
302 t.from = jid.prep(t.from); | |
303 if not t.from then return nil, "invalid-jid-from"; end | |
304 end | |
305 | |
306 if t.type == "error" then | |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
307 local error = s:get_child("error"); |
3813 | 308 local err_typ, err_condition, err_text = s:get_error(); |
309 t.error = { | |
310 type = err_typ, | |
311 condition = err_condition, | |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
312 text = err_text, |
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
313 by = error.attr.by, |
3813 | 314 }; |
315 return t; | |
316 end | |
317 | |
318 for k, typ in pairs(simple_types) do | |
319 if typ == "text_tag" then | |
320 t[k] = s:get_child_text(k); | |
321 elseif typ[1] == "text_tag" then | |
322 t[k] = s:get_child_text(typ[3], typ[2]); | |
323 elseif typ[1] == "name" then | |
324 local child = s:get_child(nil, typ[2]); | |
325 if child then | |
326 t[k] = child.name; | |
327 end | |
328 elseif typ[1] == "attr" then | |
329 local child = s:get_child(typ[3], typ[2]) | |
330 if child then | |
331 t[k] = child.attr[typ[4]]; | |
332 end | |
333 elseif typ[1] == "bool_tag" then | |
334 if s:get_child(typ[3], typ[2]) then | |
335 t[k] = true; | |
336 end | |
337 elseif typ[1] == "func" then | |
338 local child = s:get_child(typ[3], typ[2] or k); | |
339 -- TODO handle err | |
340 if child then | |
341 t[k] = typ[4](child); | |
342 end | |
343 end | |
344 end | |
345 | |
346 return t; | |
347 end | |
348 | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
349 local function str(s) |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
350 if type(s) == "string" then |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
351 return s; |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
352 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
353 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
354 |
3813 | 355 local function json2st(t) |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
356 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
|
357 return nil, "invalid-json"; |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
358 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
359 local kind = str(t.kind) or kind_by_type[str(t.type)]; |
3813 | 360 if not kind then |
361 for k, implied in pairs(implied_kinds) do | |
362 if t[k] then | |
363 kind = implied; | |
364 break | |
365 end | |
366 end | |
367 end | |
368 | |
369 local s = st.stanza(kind or "message", { | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
370 type = t.type ~= "available" and str(t.type) or nil, |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
371 to = str(t.to) and jid.prep(t.to); |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
372 from = str(t.to) and jid.prep(t.from); |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
373 id = str(t.id), |
3813 | 374 }); |
375 | |
376 if t.to and not s.attr.to then | |
377 return nil, "invalid-jid-to"; | |
378 end | |
379 if t.from and not s.attr.from then | |
380 return nil, "invalid-jid-from"; | |
381 end | |
3819 | 382 if kind == "iq" and not s.attr.type then |
383 s.attr.type = "get"; | |
3818
a607c69d0804
mod_rest: Guess 'get' as default type for 'iq' stanzas in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3817
diff
changeset
|
384 end |
3813 | 385 |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
386 if type(t.error) == "table" then |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
387 return st.error_reply(st.reply(s), str(t.error.type), str(t.error.condition), str(t.error.text), str(t.error.by)); |
3813 | 388 elseif t.type == "error" then |
389 s:text_tag("error", t.body, { code = t.error_code and tostring(t.error_code) }); | |
390 return s; | |
391 end | |
392 | |
393 for k, v in pairs(t) do | |
394 local typ = simple_types[k]; | |
395 if typ then | |
396 if typ == "text_tag" then | |
397 s:text_tag(k, v); | |
398 elseif typ[1] == "text_tag" then | |
399 s:text_tag(typ[3] or k, v, typ[2] and { xmlns = typ[2] }); | |
400 elseif typ[1] == "name" then | |
401 s:tag(v, { xmlns = typ[2] }):up(); | |
402 elseif typ[1] == "attr" then | |
403 s:tag(typ[3] or k, { xmlns = typ[2], [ typ[4] or k ] = v }):up(); | |
404 elseif typ[1] == "bool_tag" then | |
405 s:tag(typ[3] or k, { xmlns = typ[2] }):up(); | |
406 elseif typ[1] == "func" then | |
407 s:add_child(typ[5](v)):up(); | |
408 end | |
409 end | |
410 end | |
411 | |
412 s:reset(); | |
413 | |
414 return s; | |
415 end | |
416 | |
417 return { | |
418 st2json = st2json; | |
419 json2st = json2st; | |
420 }; |