Mercurial > prosody-modules
annotate mod_mam/rsm.lib.lua @ 815:b16b291d68c3
mod_host_guard: prevent possible traceback during host manual activation/deactivation.
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Wed, 12 Sep 2012 19:15:45 +0000 |
parents | 57206f8f7b09 |
children | 479df3e0b012 |
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; |
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) |
703 | 64 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
|
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 end |
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 return { parse = parse, generate = generate, get = get }; |