Software /
code /
prosody
Annotate
util/rsm.lua @ 7837:a17bddf62a28
util.rsm: Move out from mod_mam directory
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 26 Nov 2016 21:50:06 +0100 |
parent | 7836:plugins/mod_mam/rsm.lib.lua@30fac9154fd4 |
child | 7852:e594010e1ba0 |
rev | line source |
---|---|
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local stanza = require"util.stanza".stanza; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local tostring, tonumber = tostring, tonumber; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local type = type; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local pairs = pairs; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local xmlns_rsm = 'http://jabber.org/protocol/rsm'; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local element_parsers = {}; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 do |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local parsers = element_parsers; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local function xs_int(st) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 return tonumber((st:get_text())); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local function xs_string(st) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 return st:get_text(); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 parsers.after = xs_string; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 parsers.before = function(st) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local text = st:get_text(); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 return text == "" or text; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 end; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 parsers.max = xs_int; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 parsers.index = xs_int; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 parsers.first = function(st) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 return { index = tonumber(st.attr.index); st:get_text() }; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 end; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 parsers.last = xs_string; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 parsers.count = xs_int; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 local element_generators = setmetatable({ |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 first = function(st, data) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 if type(data) == "table" then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 st:tag("first", { index = data.index }):text(data[1]):up(); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 else |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 st:tag("first"):text(tostring(data)):up(); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 end; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 before = function(st, data) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 if data == true then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 st:tag("before"):up(); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 else |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 st:tag("before"):text(tostring(data)):up(); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 }, { |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 __index = function(_, name) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 return function(st, data) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 st:tag(name):text(tostring(data)):up(); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 end; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 }); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 local function parse(set) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 local rs = {}; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 for tag in set:childtags() do |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 local name = tag.name; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 local parser = name and element_parsers[name]; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 if parser then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 rs[name] = parser(tag); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 return rs; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 local function generate(t) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 local st = stanza("set", { xmlns = xmlns_rsm }); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 for k,v in pairs(t) do |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 if element_parsers[k] then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 element_generators[k](st, v); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 return st; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 local function get(st) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 local set = st:get_child("set", xmlns_rsm); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 if set and #set.tags > 0 then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 return parse(set); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 return { parse = parse, generate = generate, get = get }; |