Software /
code /
prosody-modules
Annotate
mod_mam/rsm.lib.lua @ 1101:479df3e0b012
mod_mam/rsm.lib: Fix serialization of before = true
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 08 Jul 2013 12:19:53 +0200 |
parent | 704:57206f8f7b09 |
child | 1102:bfde5e5318d7 |
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) |
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; |
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 |
cc5805f83583
mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 local function parse(stanza) |
cc5805f83583
mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 local rs = {}; |
cc5805f83583
mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 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
|
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 | 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 }; |