comparison mod_rest/jsonmap.lib.lua @ 3813:aa1ad69c7c10

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