Mercurial > prosody-modules
annotate mod_rest/jsonmap.lib.lua @ 3880:44c2d36c40a4
mod_rest: Add JSON to XML mapping of dataforms
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 04 Feb 2020 22:34:19 +0100 |
parents | 9a3dfe0bf9fd |
children | 5d7df207dc2b |
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 = { |
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 | 92 local simple_types = { |
93 -- basic message | |
94 body = "text_tag", | |
95 subject = "text_tag", | |
96 thread = "text_tag", | |
97 | |
98 -- basic presence | |
99 show = "text_tag", | |
100 status = "text_tag", | |
101 priority = "text_tag", | |
102 | |
103 state = {"name", "http://jabber.org/protocol/chatstates"}, | |
104 nick = {"text_tag", "http://jabber.org/protocol/nick", "nick"}, | |
105 delay = {"attr", "urn:xmpp:delay", "delay", "stamp"}, | |
106 replace = {"attr", "urn:xmpp:message-correct:0", "replace", "id"}, | |
107 | |
108 -- XEP-0045 MUC | |
109 -- TODO history, password, ??? | |
110 join = {"bool_tag", "http://jabber.org/protocol/muc", "x"}, | |
111 | |
112 -- XEP-0071 | |
113 html = { | |
114 "func", "http://jabber.org/protocol/xhtml-im", "html", | |
115 function (s) --> json string | |
3856
8bdb1729529b
mod_rest: Make XHTML-IM mapping more convenient
Kim Alvefur <zash@zash.se>
parents:
3855
diff
changeset
|
116 return (tostring(s:get_child("body", "http://www.w3.org/1999/xhtml")):gsub(" xmlns='[^']*'","", 1)); |
3813 | 117 end; |
118 function (s) --> xml | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
119 if type(s) == "string" then |
3856
8bdb1729529b
mod_rest: Make XHTML-IM mapping more convenient
Kim Alvefur <zash@zash.se>
parents:
3855
diff
changeset
|
120 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
|
121 end |
3813 | 122 end; |
123 }; | |
124 | |
3855
0e1e900577c4
mod_rest: Improve some comments
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
125 -- XEP-0199: XMPP Ping |
3813 | 126 ping = {"bool_tag", "urn:xmpp:ping", "ping"}, |
127 | |
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
128 -- XEP-0092: Software Version |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
129 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
|
130 function (s) |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
131 return { |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 } |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
136 end, |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
137 function (s) |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
138 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
|
139 if type(s) == "table" then |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
140 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
|
141 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
|
142 if s.os then |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
143 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
|
144 end |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
145 end |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
146 return v; |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
147 end |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
148 }; |
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
149 |
3813 | 150 -- XEP-0030 |
151 disco = { | |
152 "func", "http://jabber.org/protocol/disco#info", "query", | |
153 function (s) --> array of features | |
154 local identities, features = array(), array(); | |
155 for tag in s:childtags() do | |
156 if tag.name == "identity" and tag.attr.category and tag.attr.type then | |
157 identities:push({ category = tag.attr.category, type = tag.attr.type, name = tag.attr.name }); | |
158 elseif tag.name == "feature" and tag.attr.var then | |
159 features:push(tag.attr.var); | |
160 end | |
161 end | |
3822
f0a1d113dce4
mod_rest: Add support for mapping 'node' attr in disco#info
Kim Alvefur <zash@zash.se>
parents:
3819
diff
changeset
|
162 return { node = s.attr.node, identities = identities, features = features, }; |
3813 | 163 end; |
164 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
|
165 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
|
166 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", node = s.node }); |
3813 | 167 if s.identities then |
3848
1b9834500123
mod_rest: Fix iteration over disco#info identities
Kim Alvefur <zash@zash.se>
parents:
3823
diff
changeset
|
168 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
|
169 disco:tag("identity", { category = identity.category, type = identity.type, name = identity.name }):up(); |
3813 | 170 end |
171 end | |
172 if s.features then | |
3849
11c34e97fe1a
mod_rest: Fix iteration over disco#info features
Kim Alvefur <zash@zash.se>
parents:
3848
diff
changeset
|
173 for _, feature in ipairs(s.features) do |
3813 | 174 disco:tag("feature", { var = feature }):up(); |
175 end | |
176 end | |
3859
da3a0f055526
mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents:
3856
diff
changeset
|
177 return disco; |
da3a0f055526
mod_rest: Fix handling of 'node' attribute in disco#info
Kim Alvefur <zash@zash.se>
parents:
3856
diff
changeset
|
178 else |
3870 | 179 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info", }); |
3813 | 180 end |
181 end; | |
182 }; | |
183 | |
184 items = { | |
185 "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
|
186 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
|
187 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
|
188 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
|
189 end |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
190 |
3813 | 191 local items = array(); |
192 for item in s:childtags("item") do | |
193 items:push({ jid = item.attr.jid, node = item.attr.node, name = item.attr.name }); | |
194 end | |
195 return items; | |
196 end; | |
197 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
|
198 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
|
199 local disco = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", node = s.node }); |
3813 | 200 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 end |
3813 | 206 end |
3875
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
207 return disco; |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
208 else |
93f71ab6cb00
mod_rest: Support passing 'node' attr in disco#items queries
Kim Alvefur <zash@zash.se>
parents:
3871
diff
changeset
|
209 return st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#items", }); |
3813 | 210 end |
211 end; | |
212 }; | |
213 | |
3877
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
214 -- 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
230 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
|
231 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
|
232 end |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
233 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
|
234 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
|
235 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
|
236 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
|
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 end |
3878
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
239 if form then |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
240 cmd.form = dataform[4](form); |
9a3dfe0bf9fd
mod_rest: Add JSON mapping for dataform (XEP-0004)
Kim Alvefur <zash@zash.se>
parents:
3877
diff
changeset
|
241 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
|
242 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
|
243 end; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 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
|
252 }); |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
253 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
|
254 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
|
255 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
|
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 -- 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
|
258 end; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
259 }; |
562b34050561
mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms)
Kim Alvefur <zash@zash.se>
parents:
3875
diff
changeset
|
260 |
3855
0e1e900577c4
mod_rest: Improve some comments
Kim Alvefur <zash@zash.se>
parents:
3854
diff
changeset
|
261 -- XEP-0066: Out of Band Data |
3813 | 262 oob_url = {"func", "jabber:iq:oob", "query", |
263 function (s) | |
264 return s:get_child_text("url"); | |
265 end; | |
266 function (s) | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
267 if type(s) == "string" then |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
268 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
|
269 end |
3813 | 270 end; |
271 }; | |
3823
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
272 |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
273 -- 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
|
274 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
|
275 function (s) |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
276 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
|
277 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
|
278 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
|
279 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
|
280 return { |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
281 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
|
282 data = parsed; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
283 }; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
284 end; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
285 function (s) |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
286 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
|
287 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
|
288 :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
|
289 end; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
290 end |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
291 }; |
31b1797a78e1
mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
Kim Alvefur <zash@zash.se>
parents:
3822
diff
changeset
|
292 |
3813 | 293 }; |
294 | |
295 local implied_kinds = { | |
296 disco = "iq", | |
297 items = "iq", | |
298 ping = "iq", | |
3854
25c34c9f755c
mod_rest: Add mapping of XEP-0092: Software Version
Kim Alvefur <zash@zash.se>
parents:
3852
diff
changeset
|
299 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
|
300 command = "iq", |
3813 | 301 |
302 body = "message", | |
303 html = "message", | |
304 replace = "message", | |
305 state = "message", | |
306 subject = "message", | |
307 thread = "message", | |
308 | |
309 join = "presence", | |
310 priority = "presence", | |
311 show = "presence", | |
312 status = "presence", | |
313 } | |
314 | |
315 local kind_by_type = { | |
316 get = "iq", set = "iq", result = "iq", | |
317 normal = "message", chat = "message", headline = "message", groupchat = "message", | |
318 available = "presence", unavailable = "presence", | |
319 subscribe = "presence", unsubscribe = "presence", | |
320 subscribed = "presence", unsubscribed = "presence", | |
321 } | |
322 | |
323 local function st2json(s) | |
324 local t = { | |
325 kind = s.name, | |
326 type = s.attr.type, | |
327 to = s.attr.to, | |
328 from = s.attr.from, | |
329 id = s.attr.id, | |
330 }; | |
331 if s.name == "presence" and not s.attr.type then | |
332 t.type = "available"; | |
333 end | |
334 | |
335 if t.to then | |
336 t.to = jid.prep(t.to); | |
337 if not t.to then return nil, "invalid-jid-to"; end | |
338 end | |
339 if t.from then | |
340 t.from = jid.prep(t.from); | |
341 if not t.from then return nil, "invalid-jid-from"; end | |
342 end | |
343 | |
344 if t.type == "error" then | |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
345 local error = s:get_child("error"); |
3813 | 346 local err_typ, err_condition, err_text = s:get_error(); |
347 t.error = { | |
348 type = err_typ, | |
349 condition = err_condition, | |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
350 text = err_text, |
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
351 by = error.attr.by, |
3813 | 352 }; |
353 return t; | |
354 end | |
355 | |
356 for k, typ in pairs(simple_types) do | |
357 if typ == "text_tag" then | |
358 t[k] = s:get_child_text(k); | |
359 elseif typ[1] == "text_tag" then | |
360 t[k] = s:get_child_text(typ[3], typ[2]); | |
361 elseif typ[1] == "name" then | |
362 local child = s:get_child(nil, typ[2]); | |
363 if child then | |
364 t[k] = child.name; | |
365 end | |
366 elseif typ[1] == "attr" then | |
367 local child = s:get_child(typ[3], typ[2]) | |
368 if child then | |
369 t[k] = child.attr[typ[4]]; | |
370 end | |
371 elseif typ[1] == "bool_tag" then | |
372 if s:get_child(typ[3], typ[2]) then | |
373 t[k] = true; | |
374 end | |
375 elseif typ[1] == "func" then | |
376 local child = s:get_child(typ[3], typ[2] or k); | |
377 -- TODO handle err | |
378 if child then | |
379 t[k] = typ[4](child); | |
380 end | |
381 end | |
382 end | |
383 | |
384 return t; | |
385 end | |
386 | |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
387 local function str(s) |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
388 if type(s) == "string" then |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
389 return s; |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
390 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
391 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
392 |
3813 | 393 local function json2st(t) |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
394 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
|
395 return nil, "invalid-json"; |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
396 end |
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
397 local kind = str(t.kind) or kind_by_type[str(t.type)]; |
3813 | 398 if not kind then |
399 for k, implied in pairs(implied_kinds) do | |
400 if t[k] then | |
401 kind = implied; | |
402 break | |
403 end | |
404 end | |
405 end | |
406 | |
407 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
|
408 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
|
409 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
|
410 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
|
411 id = str(t.id), |
3813 | 412 }); |
413 | |
414 if t.to and not s.attr.to then | |
415 return nil, "invalid-jid-to"; | |
416 end | |
417 if t.from and not s.attr.from then | |
418 return nil, "invalid-jid-from"; | |
419 end | |
3819 | 420 if kind == "iq" and not s.attr.type then |
421 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
|
422 end |
3813 | 423 |
3817
937f8c463be6
mod_rest: Stricter type checks in JSON mapping
Kim Alvefur <zash@zash.se>
parents:
3813
diff
changeset
|
424 if type(t.error) == "table" then |
3871
e5d08bb58155
mod_rest: Map the error@by attribute
Kim Alvefur <zash@zash.se>
parents:
3870
diff
changeset
|
425 return st.error_reply(st.reply(s), str(t.error.type), str(t.error.condition), str(t.error.text), str(t.error.by)); |
3813 | 426 elseif t.type == "error" then |
427 s:text_tag("error", t.body, { code = t.error_code and tostring(t.error_code) }); | |
428 return s; | |
429 end | |
430 | |
431 for k, v in pairs(t) do | |
432 local typ = simple_types[k]; | |
433 if typ then | |
434 if typ == "text_tag" then | |
435 s:text_tag(k, v); | |
436 elseif typ[1] == "text_tag" then | |
437 s:text_tag(typ[3] or k, v, typ[2] and { xmlns = typ[2] }); | |
438 elseif typ[1] == "name" then | |
439 s:tag(v, { xmlns = typ[2] }):up(); | |
440 elseif typ[1] == "attr" then | |
441 s:tag(typ[3] or k, { xmlns = typ[2], [ typ[4] or k ] = v }):up(); | |
442 elseif typ[1] == "bool_tag" then | |
443 s:tag(typ[3] or k, { xmlns = typ[2] }):up(); | |
444 elseif typ[1] == "func" then | |
445 s:add_child(typ[5](v)):up(); | |
446 end | |
447 end | |
448 end | |
449 | |
450 s:reset(); | |
451 | |
452 return s; | |
453 end | |
454 | |
455 return { | |
456 st2json = st2json; | |
457 json2st = json2st; | |
458 }; |