Comparison

mod_mam/mamprefsxml.lib.lua @ 1135:0d6ab5e4bc30

mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
author Kim Alvefur <zash@zash.se>
date Sat, 10 Aug 2013 20:38:25 +0200
child 1514:18349533c44d
comparison
equal deleted inserted replaced
1134:0664f8b783fd 1135:0d6ab5e4bc30
1 -- XEP-0313: Message Archive Management for Prosody
2 -- Copyright (C) 2011-2013 Kim Alvefur
3 --
4 -- This file is MIT/X11 licensed.
5
6 local st = require"util.stanza";
7 local xmlns_mam = "urn:xmpp:mam:tmp";
8
9 local global_default_policy = module:get_option("default_archive_policy", false);
10
11 local default_attrs = {
12 always = true, [true] = "always",
13 never = false, [false] = "never",
14 roster = "roster",
15 }
16
17 local function tostanza(prefs)
18 local default = prefs[false];
19 default = default ~= nil and default_attrs[default] or global_default_policy;
20 local prefstanza = st.stanza("prefs", { xmlns = xmlns_mam, default = default });
21 local always = st.stanza("always");
22 local never = st.stanza("never");
23 for jid, choice in pairs(prefs) do
24 if jid then
25 (choice and always or never):tag("jid"):text(jid):up();
26 end
27 end
28 prefstanza:add_child(always):add_child(never);
29 return prefstanza;
30 end
31 local function fromstanza(prefstanza)
32 local prefs = {};
33 local default = prefstanza.attr.default;
34 if default then
35 prefs[false] = default_attrs[default];
36 end
37
38 local always = prefstanza:get_child("always");
39 if always then
40 for rule in always:childtags("jid") do
41 local jid = rule:get_text();
42 prefs[jid] = true;
43 end
44 end
45
46 local never = prefstanza:get_child("never");
47 if never then
48 for rule in never:childtags("jid") do
49 local jid = rule:get_text();
50 prefs[jid] = false;
51 end
52 end
53
54 return prefs;
55 end
56
57 return {
58 tostanza = tostanza;
59 fromstanza = fromstanza;
60 }