annotate mod_mam/rsm.lib.lua @ 3877:562b34050561

mod_rest: Add basic support for XEP-0050: Ad-Hoc commands (no forms) This allows calling simple commands like the one provided by mod_uptime
author Kim Alvefur <zash@zash.se>
date Tue, 04 Feb 2020 21:49:14 +0100
parents 57ee26681561
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local stanza = require"util.stanza".stanza;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local tostring, tonumber = tostring, tonumber;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local type = type;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local pairs = pairs;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local xmlns_rsm = 'http://jabber.org/protocol/rsm';
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
1401
1a5ae0dee157 mod_mam/rsm.lib: Pointless optimization
Kim Alvefur <zash@zash.se>
parents: 1102
diff changeset
8 local element_parsers = {};
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 do
1401
1a5ae0dee157 mod_mam/rsm.lib: Pointless optimization
Kim Alvefur <zash@zash.se>
parents: 1102
diff changeset
11 local parsers = element_parsers;
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local function xs_int(st)
702
d94ee0848b27 mod_mam: Fix issue with get_text() and clean some unneeded variables.
Kim Alvefur <zash@zash.se>
parents: 701
diff changeset
13 return tonumber((st:get_text()));
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local function xs_string(st)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 return st:get_text();
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
1401
1a5ae0dee157 mod_mam/rsm.lib: Pointless optimization
Kim Alvefur <zash@zash.se>
parents: 1102
diff changeset
19 parsers.after = xs_string;
1a5ae0dee157 mod_mam/rsm.lib: Pointless optimization
Kim Alvefur <zash@zash.se>
parents: 1102
diff changeset
20 parsers.before = function(st)
1102
bfde5e5318d7 mod_mam/rsm.lib: Fix parsing of empty before tag
Kim Alvefur <zash@zash.se>
parents: 1101
diff changeset
21 local text = st:get_text();
bfde5e5318d7 mod_mam/rsm.lib: Fix parsing of empty before tag
Kim Alvefur <zash@zash.se>
parents: 1101
diff changeset
22 return text == "" or text;
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end;
1401
1a5ae0dee157 mod_mam/rsm.lib: Pointless optimization
Kim Alvefur <zash@zash.se>
parents: 1102
diff changeset
24 parsers.max = xs_int;
1a5ae0dee157 mod_mam/rsm.lib: Pointless optimization
Kim Alvefur <zash@zash.se>
parents: 1102
diff changeset
25 parsers.index = xs_int;
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
1401
1a5ae0dee157 mod_mam/rsm.lib: Pointless optimization
Kim Alvefur <zash@zash.se>
parents: 1102
diff changeset
27 parsers.first = function(st)
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 return { index = tonumber(st.attr.index); st:get_text() };
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end;
1401
1a5ae0dee157 mod_mam/rsm.lib: Pointless optimization
Kim Alvefur <zash@zash.se>
parents: 1102
diff changeset
30 parsers.last = xs_string;
1a5ae0dee157 mod_mam/rsm.lib: Pointless optimization
Kim Alvefur <zash@zash.se>
parents: 1102
diff changeset
31 parsers.count = xs_int;
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local element_generators = setmetatable({
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 first = function(st, data)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 if type(data) == "table" then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 st:tag("first", { index = data.index }):text(data[1]):up();
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 else
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 st:tag("first"):text(tostring(data)):up();
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end;
1101
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
42 before = function(st, data)
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
43 if data == true then
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
44 st:tag("before"):up();
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
45 else
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
46 st:tag("before"):text(tostring(data)):up();
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
47 end
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
48 end
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 }, {
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 __index = function(_, name)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 return function(st, data)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 st:tag(name):text(tostring(data)):up();
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 });
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
1402
57ee26681561 mod_mam/rsm.lib: Rename argument for clarity
Kim Alvefur <zash@zash.se>
parents: 1401
diff changeset
58 local function parse(set)
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 local rs = {};
1402
57ee26681561 mod_mam/rsm.lib: Rename argument for clarity
Kim Alvefur <zash@zash.se>
parents: 1401
diff changeset
60 for tag in set:childtags() do
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 local name = tag.name;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local parser = name and element_parsers[name];
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if parser then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 rs[name] = parser(tag);
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 return rs;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 local function generate(t)
703
7e065836c3f6 mod_mam/rsm.lib: Fix typo
Kim Alvefur <zash@zash.se>
parents: 702
diff changeset
71 local st = stanza("set", { xmlns = xmlns_rsm });
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 for k,v in pairs(t) do
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 if element_parsers[k] then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 element_generators[k](st, v);
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 return st;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 local function get(st)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 local set = st:get_child("set", xmlns_rsm);
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 if set and #set.tags > 0 then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 return parse(set);
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 return { parse = parse, generate = generate, get = get };