mod_mam_adhoc/mod_mam_adhoc.lua
author Kim Alvefur <zash@zash.se>
Sun, 03 Mar 2024 11:23:40 +0100
changeset 5857 97c9b76867ca
parent 1147 27b41ee61601
permissions -rw-r--r--
mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel) Otherwise the global event handlers accumulate, one added each time logging is reoladed, and each invocation of the signal or event triggers one dump of each created ringbuffer.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
714
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
module:depends"adhoc";
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
local dataforms_new = require "util.dataforms".new;
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
local jid_split = require "util.jid".split;
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
local t_insert = table.insert;
1115
91d210b6106a mod_mam_adhoc: Use mod_mam/mamprefs.lib
Kim Alvefur <zash@zash.se>
parents: 739
diff changeset
     5
local prefs = module:require"mod_mam/mamprefs";
91d210b6106a mod_mam_adhoc: Use mod_mam/mamprefs.lib
Kim Alvefur <zash@zash.se>
parents: 739
diff changeset
     6
local set_prefs, get_prefs = prefs.set, prefs.get;
714
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
local mam_prefs_form = dataforms_new{
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
	title = "Archive preferences";
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
	--instructions = "";
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
	{
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
		name = "default",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
		label = "Default storage policy",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
		type = "list-single",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
		value = {
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
			{ value = "always", label = "Always" },
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
			{ value = "never", label = "Never", default = true},
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
			{ value = "roster", label = "Roster" },
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
		},
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
	};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
	{
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
		name = "always",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
		label = "Always store messages to/from",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
		type = "jid-multi"
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
	};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
	{
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
		name = "never",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
		label = "Never store messages to/from",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
		type = "jid-multi"
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
	};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
local host = module.host;
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
local default_attrs = {
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
	always = true, [true] = "always",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
	never = false, [false] = "never",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    38
	roster = "roster",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    39
}
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    40
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
local function mam_prefs_handler(self, data, state)
1147
27b41ee61601 mod_mam_adhoc: Use the new adhoc permission level for local users
Kim Alvefur <zash@zash.se>
parents: 1115
diff changeset
    42
	local username = jid_split(data.from);
714
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    43
	if state then -- the second return value
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    44
		if data.action == "cancel" then
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    45
			return { status = "canceled" };
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    46
		end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    47
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
		local fields = mam_prefs_form:data(data.form);
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    50
		local default, always, never = fields.default, fields.always, fields.never;
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    51
		local prefs = {};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    52
		if default then
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    53
			prefs[false] = default_attrs[default];
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    54
		end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    55
		if always then
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    56
			for i=1,#always do
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    57
				prefs[always[i]] = true;
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    58
			end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    59
		end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    60
		if never then
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    61
			for i=1,#never do
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    62
				prefs[never[i]] = false;
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    63
			end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    64
		end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    65
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    66
		set_prefs(username, prefs);
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    67
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    68
		return { status = "completed" }
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    69
	else -- No state, send the form.
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    70
		local prefs = get_prefs(username);
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    71
		local values = {
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    72
			default = {
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    73
				{ value = "always", label = "Always" };
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    74
				{ value = "never", label = "Never" };
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    75
				{ value = "roster", label = "Roster" };
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    76
			};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    77
			always = {};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    78
			never = {};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    79
		};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    80
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    81
		for jid, p in pairs(prefs) do
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    82
			if jid then
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    83
				t_insert(values[p and "always" or "never"], jid);
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    84
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    85
			elseif p == true then -- Yes, this is ugly.  FIXME later.
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    86
				values.default[1].default = true;
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    87
			elseif p == false then
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    88
				values.default[2].default = true;
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    89
			elseif p == "roster" then
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    90
				values.default[3].default = true;
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    91
			end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    92
		end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    93
		return { status = "executing", actions  = { "complete" }, form = { layout = mam_prefs_form, values = values } }, true;
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    94
	end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    95
end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    96
1147
27b41ee61601 mod_mam_adhoc: Use the new adhoc permission level for local users
Kim Alvefur <zash@zash.se>
parents: 1115
diff changeset
    97
module:provides("adhoc", module:require"adhoc".new("Archive settings", "urn:xmpp:mam#configure", mam_prefs_handler, "local_user"));