annotate mod_rest/jsonmap.lib.lua @ 3886:b64b08b7bf8e

mod_rest: Ignore already handled top-level stanza attr fields Prevents the check in 1ec45dbc7db5 from returning an error for these fields that aren't handled in that loop.
author Kim Alvefur <zash@zash.se>
date Fri, 07 Feb 2020 20:53:57 +0100
parents 1ec45dbc7db5
children 04ea96a0488d
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";
3823
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
3 local json = require "util.json";
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local st = require "util.stanza";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local xml = require "util.xml";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
3878
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
7 -- Reused in many XEPs so declared up here
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
8 local dataform = {
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
9 "func", "jabber:x:data", "x",
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
10 function (s)
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
11 local fields = array();
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
12 local form = {
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
13 type = s.attr.type;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
14 title = s:get_child_text("title");
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
15 instructions = s:get_child_text("instructions");
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
16 fields = fields;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
17 };
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
18 for field in s:childtags("field") do
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
19 local i = {
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
20 var = field.attr.var;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
21 type = field.attr.type;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
22 label = field.attr.label;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
23 desc = field:get_child_text("desc");
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
24 required = field:get_child("required") and true or nil;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
25 value = field:get_child_text("value");
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
26 };
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
27 if field.attr.type == "jid-multi" or field.attr.type == "list-multi" or field.attr.type == "text-multi" then
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
28 local value = array();
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
29 for v in field:childtags("value") do
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
30 value:push(v:get_text());
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
31 end
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
32 if field.attr.type == "text-multi" then
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
33 i.value = value:concat("\n");
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
34 else
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
35 i.value = value;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
36 end
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
37 end
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
38 if field.attr.type == "list-single" or field.attr.type == "list-multi" then
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
39 local options = array();
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
40 for o in field:childtags("option") do
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
41 options:push({ label = o.attr.label, value = o:get_child_text("value") });
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
42 end
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
43 i.options = options;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
44 end
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
45 fields:push(i);
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
46 end
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
47 return form;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
48 end;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
49 function (x)
3880
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
50 if type(x) == "table" and x ~= json.null then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
51 local form = st.stanza("x", { xmlns = "jabber:x:data", type = x.type });
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
52 if x.title then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
53 form:text_tag("title", x.title);
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
54 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
55 if x.instructions then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
56 form:text_tag("instructions", x.instructions);
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
57 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
58 if type(x.fields) == "table" then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
59 for _, f in ipairs(x.fields) do
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
60 if type(f) == "table" then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
61 form:tag("field", { var = f.var, type = f.type, label = f.label });
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
62 if f.desc then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
63 form:text_tag("desc", f.desc);
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
64 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
65 if f.required == true then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
66 form:tag("required"):up();
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
67 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
68 if type(f.value) == "string" then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
69 form:text_tag("value", f.value);
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
70 elseif type(f.value) == "table" then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
71 for _, v in ipairs(f.value) do
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
72 form:text_tag("value", v);
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
73 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
74 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
75 if type(f.options) == "table" then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
76 for _, o in ipairs(f.value) do
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
77 if type(o) == "table" then
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
78 form:tag("option", { label = o.label });
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
79 form:text_tag("value", o.value);
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
80 form:up();
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
81 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
82 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
83 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
84 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
85 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
86 end
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
87 return form;
44c2d36c40a4 mod_rest: Add JSON to XML mapping of dataforms
Kim Alvefur <zash@zash.se>
parents: 3878
diff changeset
88 end
3878
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
89 end;
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
90 };
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
91
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 local simple_types = {
3886
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
93 -- top level stanza attributes
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
94 -- needed here to mark them as known fields
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
95 kind = "attr",
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
96 type = "attr",
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
97 to = "attr",
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
98 from = "attr",
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
99 id = "attr",
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
100
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 -- basic message
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 body = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 subject = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 thread = "text_tag",
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 -- basic presence
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 show = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 status = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 priority = "text_tag",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 state = {"name", "http://jabber.org/protocol/chatstates"},
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 nick = {"text_tag", "http://jabber.org/protocol/nick", "nick"},
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 delay = {"attr", "urn:xmpp:delay", "delay", "stamp"},
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 replace = {"attr", "urn:xmpp:message-correct:0", "replace", "id"},
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 -- XEP-0045 MUC
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 -- TODO history, password, ???
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 join = {"bool_tag", "http://jabber.org/protocol/muc", "x"},
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 -- XEP-0071
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 html = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 "func", "http://jabber.org/protocol/xhtml-im", "html",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 function (s) --> json string
3856
8bdb1729529b mod_rest: Make XHTML-IM mapping more convenient
Kim Alvefur <zash@zash.se>
parents: 3855
diff changeset
124 return (tostring(s:get_child("body", "http://www.w3.org/1999/xhtml")):gsub(" xmlns='[^']*'","", 1));
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 function (s) --> xml
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
127 if type(s) == "string" then
3856
8bdb1729529b mod_rest: Make XHTML-IM mapping more convenient
Kim Alvefur <zash@zash.se>
parents: 3855
diff changeset
128 return assert(xml.parse([[<x:html xmlns:x='http://jabber.org/protocol/xhtml-im' xmlns='http://www.w3.org/1999/xhtml'>]]..s..[[</x:html>]]));
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
129 end
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 end;
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
3855
0e1e900577c4 mod_rest: Improve some comments
Kim Alvefur <zash@zash.se>
parents: 3854
diff changeset
133 -- XEP-0199: XMPP Ping
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 ping = {"bool_tag", "urn:xmpp:ping", "ping"},
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135
3854
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
136 -- XEP-0092: Software Version
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
137 version = {"func", "jabber:iq:version", "query",
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
138 function (s)
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
139 return {
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
140 name = s:get_child_text("name");
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
141 version = s:get_child_text("version");
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
142 os = s:get_child_text("os");
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
143 }
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
144 end,
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
145 function (s)
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
146 local v = st.stanza("query", { xmlns = "jabber:iq:version" });
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
147 if type(s) == "table" then
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
148 v:text_tag("name", s.name);
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
149 v:text_tag("version", s.version);
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
150 if s.os then
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
151 v:text_tag("os", s.os);
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
152 end
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
153 end
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
154 return v;
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
155 end
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
156 };
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
157
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 -- XEP-0030
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 disco = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 "func", "http://jabber.org/protocol/disco#info", "query",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 function (s) --> array of features
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 local identities, features = array(), array();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 for tag in s:childtags() do
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 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
165 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
166 elseif tag.name == "feature" and tag.attr.var then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 features:push(tag.attr.var);
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 end
3822
f0a1d113dce4 mod_rest: Add support for mapping 'node' attr in disco#info
Kim Alvefur <zash@zash.se>
parents: 3819
diff changeset
170 return { node = s.attr.node, identities = identities, features = features, };
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 end;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 function (s)
3860
9752a6f1b9f3 mod_rest: Avoid treating special json.null value as any other table
Kim Alvefur <zash@zash.se>
parents: 3859
diff changeset
173 if type(s) == "table" and s ~= json.null then
3859
da3a0f055526 mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents: 3856
diff changeset
174 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", node = s.node });
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 if s.identities then
3848
1b9834500123 mod_rest: Fix iteration over disco#info identities
Kim Alvefur <zash@zash.se>
parents: 3823
diff changeset
176 for _, identity in ipairs(s.identities) do
3850
8d13b9c9ba75 mod_rest: Fix disco#info identities data mapping
Kim Alvefur <zash@zash.se>
parents: 3849
diff changeset
177 disco:tag("identity", { category = identity.category, type = identity.type, name = identity.name }):up();
3813
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 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 if s.features then
3849
11c34e97fe1a mod_rest: Fix iteration over disco#info features
Kim Alvefur <zash@zash.se>
parents: 3848
diff changeset
181 for _, feature in ipairs(s.features) do
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 disco:tag("feature", { var = feature }):up();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 end
3859
da3a0f055526 mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents: 3856
diff changeset
185 return disco;
da3a0f055526 mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents: 3856
diff changeset
186 else
3870
3261a82884bb mod_rest: Fix missing return
Kim Alvefur <zash@zash.se>
parents: 3860
diff changeset
187 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", });
3813
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 };
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 items = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 "func", "http://jabber.org/protocol/disco#items", "query",
3875
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
194 function (s) --> array of features | map with node
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
195 if s.attr.node and s.tags[1] == nil then
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
196 return { node = s.attr. node };
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
197 end
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
198
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 local items = array();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 for item in s:childtags("item") do
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 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
202 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 return items;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 end;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 function (s)
3860
9752a6f1b9f3 mod_rest: Avoid treating special json.null value as any other table
Kim Alvefur <zash@zash.se>
parents: 3859
diff changeset
206 if type(s) == "table" and s ~= json.null then
3875
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
207 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", node = s.node });
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 for _, item in ipairs(s) do
3852
66f96b98d219 mod_rest: Allow returning an array of JID strings as disco#items
Kim Alvefur <zash@zash.se>
parents: 3851
diff changeset
209 if type(item) == "string" then
66f96b98d219 mod_rest: Allow returning an array of JID strings as disco#items
Kim Alvefur <zash@zash.se>
parents: 3851
diff changeset
210 disco:tag("item", { jid = item });
66f96b98d219 mod_rest: Allow returning an array of JID strings as disco#items
Kim Alvefur <zash@zash.se>
parents: 3851
diff changeset
211 elseif type(item) == "table" then
66f96b98d219 mod_rest: Allow returning an array of JID strings as disco#items
Kim Alvefur <zash@zash.se>
parents: 3851
diff changeset
212 disco:tag("item", { jid = item.jid, node = item.node, name = item.name });
66f96b98d219 mod_rest: Allow returning an array of JID strings as disco#items
Kim Alvefur <zash@zash.se>
parents: 3851
diff changeset
213 end
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 end
3875
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
215 return disco;
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
216 else
93f71ab6cb00 mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents: 3871
diff changeset
217 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", });
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 end;
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
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
222 -- XEP-0050: Ad-Hoc Commands
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
223 command = {"func", "http://jabber.org/protocol/commands", "command",
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
224 function (s)
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
225 local cmd = {
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
226 action = s.attr.action,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
227 node = s.attr.node,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
228 sessionid = s.attr.sessionid,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
229 status = s.attr.status,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
230 };
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
231 local actions = s:get_child("actions");
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
232 local note = s:get_child("note");
3878
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
233 local form = s:get_child("x", "jabber:x:data");
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
234 if actions then
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
235 cmd.actions = {
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
236 execute = actions.attr.execute,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
237 };
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
238 for action in actions:childtags() do
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
239 cmd.actions[action.name] = true
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
240 end
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
241 elseif note then
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
242 cmd.note = {
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
243 type = note.attr.type;
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
244 text = note:get_text();
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
245 };
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
246 end
3878
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
247 if form then
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
248 cmd.form = dataform[4](form);
9a3dfe0bf9fd mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents: 3877
diff changeset
249 end
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
250 return cmd;
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
251 end;
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
252 function (s)
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
253 if type(s) == "table" and s ~= json.null then
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
254 local cmd = st.stanza("command", {
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
255 xmlns = "http://jabber.org/protocol/commands",
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
256 action = s.action,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
257 node = s.node,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
258 sessionid = s.sessionid,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
259 status = s.status,
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
260 });
3881
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
261 if type(s.actions) == "table" then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
262 cmd:tag("actions", { execute = s.actions.execute });
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
263 do
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
264 if s.actions.next == true then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
265 cmd:tag("next"):up();
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
266 end
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
267 if s.actions.prev == true then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
268 cmd:tag("prev"):up();
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
269 end
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
270 if s.actions.complete == true then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
271 cmd:tag("complete"):up();
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
272 end
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
273 end
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
274 cmd:up();
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
275 elseif type(s.note) == "table" then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
276 cmd:text_tag("note", s.note.text, { type = s.note.type });
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
277 end
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
278 if s.form then
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
279 cmd:add_child(dataform[5](s.form));
5d7df207dc2b mod_rest: Add final pieces of XEP-0050 (actions, note, form)
Kim Alvefur <zash@zash.se>
parents: 3880
diff changeset
280 end
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
281 return cmd;
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
282 elseif type(s) == "string" then -- assume node
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
283 return st.stanza("command", { xmlns = "http://jabber.org/protocol/commands", node = s });
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
284 end
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
285 -- else .. missing required attribute
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
286 end;
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
287 };
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
288
3855
0e1e900577c4 mod_rest: Improve some comments
Kim Alvefur <zash@zash.se>
parents: 3854
diff changeset
289 -- XEP-0066: Out of Band Data
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 oob_url = {"func", "jabber:iq:oob", "query",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 function (s)
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 return s:get_child_text("url");
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 end;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 function (s)
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
295 if type(s) == "string" then
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
296 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
297 end
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 end;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299 };
3823
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
300
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
301 -- XEP-XXXX: User-defined Data Transfer
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
302 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: 3822
diff changeset
303 function (s)
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
304 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: 3822
diff changeset
305 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: 3822
diff changeset
306 local parsed, err = json.decode(rawjson);
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
307 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: 3822
diff changeset
308 return {
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
309 datatype = s.attr.datatype;
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
310 data = parsed;
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
311 };
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
312 end;
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
313 function (s)
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
314 if type(s) == "table" then
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
315 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: 3822
diff changeset
316 :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: 3822
diff changeset
317 end;
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
318 end
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
319 };
31b1797a78e1 mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents: 3822
diff changeset
320
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 };
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 local implied_kinds = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 disco = "iq",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 items = "iq",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 ping = "iq",
3854
25c34c9f755c mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents: 3852
diff changeset
327 version = "iq",
3877
562b34050561 mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents: 3875
diff changeset
328 command = "iq",
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 body = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 html = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 replace = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 state = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 subject = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 thread = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 join = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 priority = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 show = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 status = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 }
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 local kind_by_type = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 get = "iq", set = "iq", result = "iq",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 normal = "message", chat = "message", headline = "message", groupchat = "message",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346 available = "presence", unavailable = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 subscribe = "presence", unsubscribe = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 subscribed = "presence", unsubscribed = "presence",
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 }
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351 local function st2json(s)
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 local t = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353 kind = s.name,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 type = s.attr.type,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 to = s.attr.to,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 from = s.attr.from,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 id = s.attr.id,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 };
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 if s.name == "presence" and not s.attr.type then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 t.type = "available";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
362
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 if t.to then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 t.to = jid.prep(t.to);
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365 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
366 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367 if t.from then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368 t.from = jid.prep(t.from);
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369 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
370 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 if t.type == "error" then
3871
e5d08bb58155 mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents: 3870
diff changeset
373 local error = s:get_child("error");
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374 local err_typ, err_condition, err_text = s:get_error();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375 t.error = {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
376 type = err_typ,
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
377 condition = err_condition,
3871
e5d08bb58155 mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents: 3870
diff changeset
378 text = err_text,
e5d08bb58155 mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents: 3870
diff changeset
379 by = error.attr.by,
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
380 };
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
381 return t;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
382 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 for k, typ in pairs(simple_types) do
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385 if typ == "text_tag" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 t[k] = s:get_child_text(k);
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 elseif typ[1] == "text_tag" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388 t[k] = s:get_child_text(typ[3], typ[2]);
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389 elseif typ[1] == "name" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 local child = s:get_child(nil, typ[2]);
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 if child then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
392 t[k] = child.name;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 elseif typ[1] == "attr" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 local child = s:get_child(typ[3], typ[2])
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396 if child then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 t[k] = child.attr[typ[4]];
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399 elseif typ[1] == "bool_tag" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 if s:get_child(typ[3], typ[2]) then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 t[k] = true;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
402 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 elseif typ[1] == "func" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
404 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
405 -- TODO handle err
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
406 if child then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 t[k] = typ[4](child);
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
408 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
409 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
410 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
411
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
412 return t;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
413 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
414
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
415 local function str(s)
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
416 if type(s) == "string" then
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
417 return s;
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
418 end
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
419 end
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
420
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
421 local function json2st(t)
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
422 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
423 return nil, "invalid-json";
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
424 end
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
425 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
426 if not kind then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
427 for k, implied in pairs(implied_kinds) do
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
428 if t[k] then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
429 kind = implied;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
430 break
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
433 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
434
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435 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
436 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
437 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
438 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
439 id = str(t.id),
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
440 });
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
441
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
442 if t.to and not s.attr.to then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
443 return nil, "invalid-jid-to";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
444 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
445 if t.from and not s.attr.from then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
446 return nil, "invalid-jid-from";
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
447 end
3819
1bab6f67eb5f mod_rest: Fix previous commit
Kim Alvefur <zash@zash.se>
parents: 3818
diff changeset
448 if kind == "iq" and not s.attr.type then
1bab6f67eb5f mod_rest: Fix previous commit
Kim Alvefur <zash@zash.se>
parents: 3818
diff changeset
449 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
450 end
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
451
3817
937f8c463be6 mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents: 3813
diff changeset
452 if type(t.error) == "table" then
3871
e5d08bb58155 mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents: 3870
diff changeset
453 return st.error_reply(st.reply(s), str(t.error.type), str(t.error.condition), str(t.error.text), str(t.error.by));
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
454 elseif t.type == "error" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
455 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
456 return s;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
457 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
458
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
459 for k, v in pairs(t) do
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
460 local typ = simple_types[k];
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
461 if typ then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
462 if typ == "text_tag" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
463 s:text_tag(k, v);
3886
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
464 elseif typ == "attr" then -- luacheck: ignore 542
b64b08b7bf8e mod_rest: Ignore already handled top-level stanza attr fields
Kim Alvefur <zash@zash.se>
parents: 3885
diff changeset
465 -- handled already
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
466 elseif typ[1] == "text_tag" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
467 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
468 elseif typ[1] == "name" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
469 s:tag(v, { xmlns = typ[2] }):up();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
470 elseif typ[1] == "attr" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
471 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
472 elseif typ[1] == "bool_tag" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
473 s:tag(typ[3] or k, { xmlns = typ[2] }):up();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
474 elseif typ[1] == "func" then
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
475 s:add_child(typ[5](v)):up();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
476 end
3885
1ec45dbc7db5 mod_rest: Return an error for unknown fields in JSON input
Kim Alvefur <zash@zash.se>
parents: 3881
diff changeset
477 else
1ec45dbc7db5 mod_rest: Return an error for unknown fields in JSON input
Kim Alvefur <zash@zash.se>
parents: 3881
diff changeset
478 return nil, "unknown-field";
3813
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
479 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
480 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
481
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
482 s:reset();
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
483
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
484 return s;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
485 end
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
486
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
487 return {
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
488 st2json = st2json;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
489 json2st = json2st;
aa1ad69c7c10 mod_rest: Add JSON support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
490 };