annotate mod_rest/jsonmap.lib.lua @ 3818:a607c69d0804

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