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