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