diff options
author | Kim Alvefur <zash@zash.se> | 2017-01-23 19:34:14 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2017-01-23 19:34:14 +0100 |
commit | db88e0f7b51eeaed68b9b50502b6ba82c53633a1 (patch) | |
tree | ea895b1073f2ff2de7a08fc01f12797ab58c7125 /util | |
parent | 26f2953106794bbdd23db98f69fb6e90a4b8e460 (diff) | |
parent | ffde63fe687f5ffecf5fdec28358d1774a8a7169 (diff) | |
download | prosody-db88e0f7b51eeaed68b9b50502b6ba82c53633a1.tar.gz prosody-db88e0f7b51eeaed68b9b50502b6ba82c53633a1.zip |
Merge 0.10->trunk
Diffstat (limited to 'util')
-rw-r--r-- | util/rsm.lua | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/util/rsm.lua b/util/rsm.lua new file mode 100644 index 00000000..40a78fb5 --- /dev/null +++ b/util/rsm.lua @@ -0,0 +1,98 @@ +-- Prosody IM +-- Copyright (C) 2008-2017 Matthew Wild +-- Copyright (C) 2008-2017 Waqas Hussain +-- Copyright (C) 2011-2017 Kim Alvefur +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- +-- XEP-0313: Message Archive Management for Prosody +-- + +local stanza = require"util.stanza".stanza; +local tostring, tonumber = tostring, tonumber; +local type = type; +local pairs = pairs; + +local xmlns_rsm = 'http://jabber.org/protocol/rsm'; + +local element_parsers = {}; + +do + local parsers = element_parsers; + local function xs_int(st) + return tonumber((st:get_text())); + end + local function xs_string(st) + return st:get_text(); + end + + parsers.after = xs_string; + parsers.before = function(st) + local text = st:get_text(); + return text == "" or text; + end; + parsers.max = xs_int; + parsers.index = xs_int; + + parsers.first = function(st) + return { index = tonumber(st.attr.index); st:get_text() }; + end; + parsers.last = xs_string; + parsers.count = xs_int; +end + +local element_generators = setmetatable({ + first = function(st, data) + if type(data) == "table" then + st:tag("first", { index = data.index }):text(data[1]):up(); + else + st:tag("first"):text(tostring(data)):up(); + end + end; + before = function(st, data) + if data == true then + st:tag("before"):up(); + else + st:tag("before"):text(tostring(data)):up(); + end + end +}, { + __index = function(_, name) + return function(st, data) + st:tag(name):text(tostring(data)):up(); + end + end; +}); + + +local function parse(set) + local rs = {}; + for tag in set:childtags() do + local name = tag.name; + local parser = name and element_parsers[name]; + if parser then + rs[name] = parser(tag); + end + end + return rs; +end + +local function generate(t) + local st = stanza("set", { xmlns = xmlns_rsm }); + for k,v in pairs(t) do + if element_parsers[k] then + element_generators[k](st, v); + end + end + return st; +end + +local function get(st) + local set = st:get_child("set", xmlns_rsm); + if set and #set.tags > 0 then + return parse(set); + end +end + +return { parse = parse, generate = generate, get = get }; |