2 -- Copyright (C) 2008-2017 Matthew Wild
3 -- Copyright (C) 2008-2017 Waqas Hussain
4 -- Copyright (C) 2011-2017 Kim Alvefur
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
9 -- XEP-0313: Message Archive Management for Prosody
12 local stanza
= require
"util.stanza".stanza
;
13 local tostring, tonumber = tostring, tonumber;
17 local xmlns_rsm
= 'http://jabber.org/protocol/rsm';
19 local element_parsers
= {};
22 local parsers
= element_parsers
;
23 local function xs_int(st
)
24 return tonumber((st
:get_text()));
26 local function xs_string(st
)
30 parsers
.after
= xs_string
;
31 parsers
.before
= function(st
)
32 local text
= st
:get_text();
33 return text
== "" or text
;
36 parsers
.index
= xs_int
;
38 parsers
.first
= function(st
)
39 return { index
= tonumber(st
.attr
.index
); st
:get_text() };
41 parsers
.last
= xs_string
;
42 parsers
.count
= xs_int
;
45 local element_generators
= setmetatable({
46 first
= function(st
, data
)
47 if type(data
) == "table" then
48 st
:tag("first", { index
= data
.index
}):text(data
[1]):up();
50 st
:tag("first"):text(tostring(data
)):up();
53 before
= function(st
, data
)
55 st
:tag("before"):up();
57 st
:tag("before"):text(tostring(data
)):up();
61 __index
= function(_
, name
)
62 return function(st
, data
)
63 st
:tag(name
):text(tostring(data
)):up();
69 local function parse(set
)
71 for tag in set
:childtags() do
72 local name
= tag.name
;
73 local parser
= name
and element_parsers
[name
];
75 rs
[name
] = parser(tag);
81 local function generate(t
)
82 local st
= stanza("set", { xmlns
= xmlns_rsm
});
83 for k
,v
in pairs(t
) do
84 if element_parsers
[k
] then
85 element_generators
[k
](st
, v
);
91 local function get(st
)
92 local set
= st
:get_child("set", xmlns_rsm
);
93 if set
and #set
.tags
> 0 then
98 return { parse
= parse
, generate
= generate
, get
= get
};