mod_csi_simple: Consider messages encrypted payload as important (fixes part of ...
[prosody.git] / plugins / mod_mam / mamprefs.lib.lua
blobdd82b626914dafb818c1ab3d6281f87efc2b88d9
1 -- Prosody IM
2 -- Copyright (C) 2008-2017 Matthew Wild
3 -- Copyright (C) 2008-2017 Waqas Hussain
4 -- Copyright (C) 2011-2017 Kim Alvefur
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
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);
16 end
17 local smart_enable = module:get_option_boolean("mam_smart_enable", false);
20 -- luacheck: ignore 211/prefs_format
21 local prefs_format = {
22 [false] = "roster",
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
28 end
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
38 -- prefs not cached
39 user_prefs = prefs:get(user);
40 if not user_prefs then
41 -- prefs not set
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);
46 end
47 end
48 if user_sessions then
49 -- cache settings if they originate from user action
50 user_sessions.archive_prefs = user_prefs;
51 end
52 if not user_prefs then
53 if smart_enable then
54 -- not yet enabled, either explicitly or "smart"
55 user_prefs = { [false] = false };
56 else
57 -- no explicit settings, return defaults
58 user_prefs = { [false] = global_default_policy };
59 end
60 end
61 end
62 return user_prefs;
63 end
65 local function set_prefs(user, user_prefs)
66 local user_sessions = sessions[user];
67 if user_sessions then
68 user_sessions.archive_prefs = user_prefs;
69 end
70 return prefs:set(user, user_prefs);
71 end
73 return {
74 get = get_prefs,
75 set = set_prefs,