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