annotate mod_mam/rsm.lib.lua @ 701:cc5805f83583

mod_mam: Implement support for Result Set Management in queries.
author Kim Alvefur <zash@zash.se>
date Fri, 08 Jun 2012 03:13:31 +0200
parents
children d94ee0848b27
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)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 return tonumber(st:get_text());
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)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 return st:get_text() or true;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 max = xs_int;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 index = xs_int;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 first = function(st)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 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
28 end;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 last = xs_string;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 count = xs_int;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 }
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;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 }, {
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 __index = function(_, name)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return function(st, data)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 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
46 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 end;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 });
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
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 local function parse(stanza)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 local rs = {};
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 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
54 local name = tag.name;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 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
56 if parser then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 rs[name] = parser(tag);
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 return rs;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 local function generate(t)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 local st = stanza("rsm", { xmlns = xmlns_rsm });
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 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
66 if element_parsers[k] then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 element_generators[k](st, v);
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 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 return st;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 local function get(st)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 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
75 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
76 return parse(set);
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 else
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 module:log("debug", "RSM parse failed, %s", tostring(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 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 return { parse = parse, generate = generate, get = get };