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
11 -- luacheck: ignore 122/prosody
13 local global_default_policy
= module
:get_option_string("default_archive_policy", true);
14 if global_default_policy
~= "roster" then
15 global_default_policy
= module
:get_option_boolean("default_archive_policy", global_default_policy
);
17 local smart_enable
= module
:get_option_boolean("mam_smart_enable", false);
20 -- luacheck: ignore 211/prefs_format
21 local prefs_format
= {
23 -- default ::= true | false | "roster"
24 -- true = always, false = never, nil = global default
25 ["romeo@montague.net"] = true, -- always
26 ["montague@montague.net"] = false, -- newer
30 local sessions
= prosody
.hosts
[module
.host
].sessions
;
31 local archive_store
= module
:get_option_string("archive_store", "archive");
32 local prefs
= module
:open_store(archive_store
.. "_prefs");
34 local function get_prefs(user
, explicit
)
35 local user_sessions
= sessions
[user
];
36 local user_prefs
= user_sessions
and user_sessions
.archive_prefs
37 if not user_prefs
then
39 user_prefs
= prefs
:get(user
);
40 if not user_prefs
then
42 if smart_enable
and explicit
then
43 -- a mam-capable client was involved in this action, set defaults
44 user_prefs
= { [false] = global_default_policy
};
45 prefs
:set(user
, user_prefs
);
49 -- cache settings if they originate from user action
50 user_sessions
.archive_prefs
= user_prefs
;
52 if not user_prefs
then
54 -- not yet enabled, either explicitly or "smart"
55 user_prefs
= { [false] = false };
57 -- no explicit settings, return defaults
58 user_prefs
= { [false] = global_default_policy
};
65 local function set_prefs(user
, user_prefs
)
66 local user_sessions
= sessions
[user
];
68 user_sessions
.archive_prefs
= user_prefs
;
70 return prefs
:set(user
, user_prefs
);