Mercurial > prosody-modules
annotate mod_rest/jsonmap.lib.lua @ 3854:25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 25 Jan 2020 00:40:38 +0100 |
parents | 66f96b98d219 |
children | 0e1e900577c4 |
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 | |
7 local simple_types = { | |
8 -- basic message | |
9 body = "text_tag", | |
10 subject = "text_tag", | |
11 thread = "text_tag", | |
12 | |
13 -- basic presence | |
14 show = "text_tag", | |
15 status = "text_tag", | |
16 priority = "text_tag", | |
17 | |
18 state = {"name", "http://jabber.org/protocol/chatstates"}, | |
19 nick = {"text_tag", "http://jabber.org/protocol/nick", "nick"}, | |
20 delay = {"attr", "urn:xmpp:delay", "delay", "stamp"}, | |
21 replace = {"attr", "urn:xmpp:message-correct:0", "replace", "id"}, | |
22 | |
23 -- XEP-0045 MUC | |
24 -- TODO history, password, ??? | |
25 join = {"bool_tag", "http://jabber.org/protocol/muc", "x"}, | |
26 | |
27 -- XEP-0071 | |
28 -- FIXME xmlns is awkward | |
29 html = { | |
30 "func", "http://jabber.org/protocol/xhtml-im", "html", | |
31 function (s) --> json string | |
32 return tostring(s:get_child("body", "http://www.w3.org/1999/xhtml")); | |
33 end; | |
34 function (s) --> xml | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
35 if type(s) == "string" then |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
36 return xml.parse([[<html xmlns='http://jabber.org/protocol/xhtml-im'>]]..s..[[</html>]]); |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
37 end |
3813 | 38 end; |
39 }; | |
40 | |
41 -- XEP-0199 | |
42 ping = {"bool_tag", "urn:xmpp:ping", "ping"}, | |
43 | |
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
44 -- XEP-0092: Software Version |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
45 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
|
46 function (s) |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
47 return { |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
48 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
|
49 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
|
50 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
|
51 } |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
52 end, |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
53 function (s) |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
54 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
|
55 if type(s) == "table" then |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
56 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
|
57 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
|
58 if s.os then |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
59 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
|
60 end |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
61 end |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
62 return v; |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
63 end |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
64 }; |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
65 |
3813 | 66 -- XEP-0030 |
67 disco = { | |
68 "func", "http://jabber.org/protocol/disco#info", "query", | |
69 function (s) --> array of features | |
70 local identities, features = array(), array(); | |
71 for tag in s:childtags() do | |
72 if tag.name == "identity" and tag.attr.category and tag.attr.type then | |
73 identities:push({ category = tag.attr.category, type = tag.attr.type, name = tag.attr.name }); | |
74 elseif tag.name == "feature" and tag.attr.var then | |
75 features:push(tag.attr.var); | |
76 end | |
77 end | |
3822
f0a1d113dce4
mod_rest: Add support for mapping 'node' attr in disco#info
Kim Alvefur <zash@zash.se>
parents:
3819
diff
changeset
|
78 return { node = s.attr.node, identities = identities, features = features, }; |
3813 | 79 end; |
80 function (s) | |
81 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" }); | |
82 if type(s) == "table" then | |
3822
f0a1d113dce4
mod_rest: Add support for mapping 'node' attr in disco#info
Kim Alvefur <zash@zash.se>
parents:
3819
diff
changeset
|
83 disco.attr.node = tostring(s.node); |
3813 | 84 if s.identities then |
3848
1b9834500123
mod_rest: Fix iteration over disco#info identities
Kim Alvefur <zash@zash.se>
parents:
3823
diff
changeset
|
85 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
|
86 disco:tag("identity", { category = identity.category, type = identity.type, name = identity.name }):up(); |
3813 | 87 end |
88 end | |
89 if s.features then | |
3849
11c34e97fe1a
mod_rest: Fix iteration over disco#info features
Kim Alvefur <zash@zash.se>
parents:
3848
diff
changeset
|
90 for _, feature in ipairs(s.features) do |
3813 | 91 disco:tag("feature", { var = feature }):up(); |
92 end | |
93 end | |
94 end | |
95 return disco; | |
96 end; | |
97 }; | |
98 | |
99 items = { | |
100 "func", "http://jabber.org/protocol/disco#items", "query", | |
101 function (s) --> array of features | |
102 local items = array(); | |
103 for item in s:childtags("item") do | |
104 items:push({ jid = item.attr.jid, node = item.attr.node, name = item.attr.name }); | |
105 end | |
106 return items; | |
107 end; | |
108 function (s) | |
109 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items" }); | |
110 if type(s) == "table" then | |
111 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
|
112 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
|
113 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
|
114 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
|
115 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
|
116 end |
3813 | 117 end |
118 end | |
119 return disco; | |
120 end; | |
121 }; | |
122 | |
123 oob_url = {"func", "jabber:iq:oob", "query", | |
124 function (s) | |
125 return s:get_child_text("url"); | |
126 end; | |
127 function (s) | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
128 if type(s) == "string" then |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
129 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
|
130 end |
3813 | 131 end; |
132 }; | |
3823
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
133 |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
134 -- 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
|
135 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
|
136 function (s) |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
137 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
|
138 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
|
139 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
|
140 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
|
141 return { |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
142 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
|
143 data = parsed; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
144 }; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
145 end; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
146 function (s) |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
147 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
|
148 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
|
149 :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
|
150 end; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
151 end |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
152 }; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
153 |
3813 | 154 }; |
155 | |
156 local implied_kinds = { | |
157 disco = "iq", | |
158 items = "iq", | |
159 ping = "iq", | |
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
160 version = "iq", |
3813 | 161 |
162 body = "message", | |
163 html = "message", | |
164 replace = "message", | |
165 state = "message", | |
166 subject = "message", | |
167 thread = "message", | |
168 | |
169 join = "presence", | |
170 priority = "presence", | |
171 show = "presence", | |
172 status = "presence", | |
173 } | |
174 | |
175 local kind_by_type = { | |
176 get = "iq", set = "iq", result = "iq", | |
177 normal = "message", chat = "message", headline = "message", groupchat = "message", | |
178 available = "presence", unavailable = "presence", | |
179 subscribe = "presence", unsubscribe = "presence", | |
180 subscribed = "presence", unsubscribed = "presence", | |
181 } | |
182 | |
183 local function st2json(s) | |
184 local t = { | |
185 kind = s.name, | |
186 type = s.attr.type, | |
187 to = s.attr.to, | |
188 from = s.attr.from, | |
189 id = s.attr.id, | |
190 }; | |
191 if s.name == "presence" and not s.attr.type then | |
192 t.type = "available"; | |
193 end | |
194 | |
195 if t.to then | |
196 t.to = jid.prep(t.to); | |
197 if not t.to then return nil, "invalid-jid-to"; end | |
198 end | |
199 if t.from then | |
200 t.from = jid.prep(t.from); | |
201 if not t.from then return nil, "invalid-jid-from"; end | |
202 end | |
203 | |
204 if t.type == "error" then | |
205 local err_typ, err_condition, err_text = s:get_error(); | |
206 t.error = { | |
207 type = err_typ, | |
208 condition = err_condition, | |
209 text = err_text | |
210 }; | |
211 return t; | |
212 end | |
213 | |
214 for k, typ in pairs(simple_types) do | |
215 if typ == "text_tag" then | |
216 t[k] = s:get_child_text(k); | |
217 elseif typ[1] == "text_tag" then | |
218 t[k] = s:get_child_text(typ[3], typ[2]); | |
219 elseif typ[1] == "name" then | |
220 local child = s:get_child(nil, typ[2]); | |
221 if child then | |
222 t[k] = child.name; | |
223 end | |
224 elseif typ[1] == "attr" then | |
225 local child = s:get_child(typ[3], typ[2]) | |
226 if child then | |
227 t[k] = child.attr[typ[4]]; | |
228 end | |
229 elseif typ[1] == "bool_tag" then | |
230 if s:get_child(typ[3], typ[2]) then | |
231 t[k] = true; | |
232 end | |
233 elseif typ[1] == "func" then | |
234 local child = s:get_child(typ[3], typ[2] or k); | |
235 -- TODO handle err | |
236 if child then | |
237 t[k] = typ[4](child); | |
238 end | |
239 end | |
240 end | |
241 | |
242 return t; | |
243 end | |
244 | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
245 local function str(s) |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
246 if type(s) == "string" then |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
247 return s; |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
248 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
249 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
250 |
3813 | 251 local function json2st(t) |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
252 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
|
253 return nil, "invalid-json"; |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
254 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
255 local kind = str(t.kind) or kind_by_type[str(t.type)]; |
3813 | 256 if not kind then |
257 for k, implied in pairs(implied_kinds) do | |
258 if t[k] then | |
259 kind = implied; | |
260 break | |
261 end | |
262 end | |
263 end | |
264 | |
265 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
|
266 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
|
267 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
|
268 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
|
269 id = str(t.id), |
3813 | 270 }); |
271 | |
272 if t.to and not s.attr.to then | |
273 return nil, "invalid-jid-to"; | |
274 end | |
275 if t.from and not s.attr.from then | |
276 return nil, "invalid-jid-from"; | |
277 end | |
3819 | 278 if kind == "iq" and not s.attr.type then |
279 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
|
280 end |
3813 | 281 |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
282 if type(t.error) == "table" then |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
283 return st.error_reply(st.reply(s), str(t.error.type), str(t.error.condition), str(t.error.text)); |
3813 | 284 elseif t.type == "error" then |
285 s:text_tag("error", t.body, { code = t.error_code and tostring(t.error_code) }); | |
286 return s; | |
287 end | |
288 | |
289 for k, v in pairs(t) do | |
290 local typ = simple_types[k]; | |
291 if typ then | |
292 if typ == "text_tag" then | |
293 s:text_tag(k, v); | |
294 elseif typ[1] == "text_tag" then | |
295 s:text_tag(typ[3] or k, v, typ[2] and { xmlns = typ[2] }); | |
296 elseif typ[1] == "name" then | |
297 s:tag(v, { xmlns = typ[2] }):up(); | |
298 elseif typ[1] == "attr" then | |
299 s:tag(typ[3] or k, { xmlns = typ[2], [ typ[4] or k ] = v }):up(); | |
300 elseif typ[1] == "bool_tag" then | |
301 s:tag(typ[3] or k, { xmlns = typ[2] }):up(); | |
302 elseif typ[1] == "func" then | |
303 s:add_child(typ[5](v)):up(); | |
304 end | |
305 end | |
306 end | |
307 | |
308 s:reset(); | |
309 | |
310 return s; | |
311 end | |
312 | |
313 return { | |
314 st2json = st2json; | |
315 json2st = json2st; | |
316 }; |