annotate mod_mam/rsm.lib.lua @ 1113:f1f0c87cffdd

mod_mam: Neater reply building
author Kim Alvefur <zash@zash.se>
date Fri, 12 Jul 2013 23:18:30 +0200
parents bfde5e5318d7
children 1a5ae0dee157
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
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local element_parsers;
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
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 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
12 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
13 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local function xs_string(st)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 return st:get_text();
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 element_parsers = {
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 after = xs_string;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 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;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 max = xs_int;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 index = xs_int;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 first = function(st)
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;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 last = xs_string;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 count = xs_int;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 }
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 local element_generators = setmetatable({
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 first = function(st, data)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 if type(data) == "table" then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 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
39 else
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 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
41 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end;
1101
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
43 before = function(st, data)
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
44 if data == true then
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
45 st:tag("before"):up();
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
46 else
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
47 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
48 end
479df3e0b012 mod_mam/rsm.lib: Fix serialization of before = true
Kim Alvefur <zash@zash.se>
parents: 704
diff changeset
49 end
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 }, {
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 __index = function(_, name)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 return function(st, data)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 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
54 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end;
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
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 local function parse(stanza)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 local rs = {};
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 for tag in stanza:childtags() do
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local name = tag.name;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 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
64 if parser then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 rs[name] = parser(tag);
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 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 return rs;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 local function generate(t)
703
7e065836c3f6 mod_mam/rsm.lib: Fix typo
Kim Alvefur <zash@zash.se>
parents: 702
diff changeset
72 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
73 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
74 if element_parsers[k] then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 element_generators[k](st, v);
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 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 return st;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 local function get(st)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 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
83 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
84 return parse(set);
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 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 return { parse = parse, generate = generate, get = get };