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