mod_mam_adhoc/mod_mam_adhoc.lua
author Kim Alvefur <zash@zash.se>
Sat, 13 Jul 2013 17:30:56 +0200
changeset 1115 91d210b6106a
parent 739 6c7b6a0dcacf
child 1147 27b41ee61601
permissions -rw-r--r--
mod_mam_adhoc: Use mod_mam/mamprefs.lib
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)
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    42
	local username, hostname = jid_split(data.from);
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
		if not username or not hostname or hostname ~= module.host then
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
			return { status = "error", error = { type = "cancel",
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    50
				condition = "forbidden", message = "Invalid user or hostname." } };
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    51
		end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    52
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    53
		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
    54
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    55
		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
    56
		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
    57
		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
    58
			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
    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 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
    61
			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
    62
				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
    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
		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
    66
			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
    67
				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
    68
			end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    69
		end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    70
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    71
		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
    72
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    73
		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
    74
	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
    75
		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
    76
		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
    77
			default = {
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    78
				{ 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
    79
				{ 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
    80
				{ 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
    81
			};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    82
			always = {};
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    83
			never = {};
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
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    86
		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
    87
			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
    88
				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
    89
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    90
			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
    91
				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
    92
			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
    93
				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
    94
			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
    95
				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
    96
			end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    97
		end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    98
		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
    99
	end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   100
end
5879368d9ff7 mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   101
739
6c7b6a0dcacf mod_mam_adhoc: Use ad-hoc command node suggested by XEP-0313
Kim Alvefur <zash@zash.se>
parents: 714
diff changeset
   102
module:provides("adhoc", module:require"adhoc".new("Archive settings", "urn:xmpp:mam#configure", mam_prefs_handler));