1 local stanza
= require
"util.stanza".stanza
;
2 local tostring, tonumber = tostring, tonumber;
6 local xmlns_rsm
= 'http://jabber.org/protocol/rsm';
8 local element_parsers
= {};
11 local parsers
= element_parsers
;
12 local function xs_int(st
)
13 return tonumber((st
:get_text()));
15 local function xs_string(st
)
19 parsers
.after
= xs_string
;
20 parsers
.before
= function(st
)
21 local text
= st
:get_text();
22 return text
== "" or text
;
25 parsers
.index
= xs_int
;
27 parsers
.first
= function(st
)
28 return { index
= tonumber(st
.attr
.index
); st
:get_text() };
30 parsers
.last
= xs_string
;
31 parsers
.count
= xs_int
;
34 local element_generators
= setmetatable({
35 first
= function(st
, data
)
36 if type(data
) == "table" then
37 st
:tag("first", { index
= data
.index
}):text(data
[1]):up();
39 st
:tag("first"):text(tostring(data
)):up();
42 before
= function(st
, data
)
44 st
:tag("before"):up();
46 st
:tag("before"):text(tostring(data
)):up();
50 __index
= function(_
, name
)
51 return function(st
, data
)
52 st
:tag(name
):text(tostring(data
)):up();
58 local function parse(set
)
60 for tag in set
:childtags() do
61 local name
= tag.name
;
62 local parser
= name
and element_parsers
[name
];
64 rs
[name
] = parser(tag);
70 local function generate(t
)
71 local st
= stanza("set", { xmlns
= xmlns_rsm
});
72 for k
,v
in pairs(t
) do
73 if element_parsers
[k
] then
74 element_generators
[k
](st
, v
);
80 local function get(st
)
81 local set
= st
:get_child("set", xmlns_rsm
);
82 if set
and #set
.tags
> 0 then
87 return { parse
= parse
, generate
= generate
, get
= get
};