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