mod_mam/mod_mam.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 28 Apr 2017 12:09:51 +0100
changeset 2715 ff1f7e61517f
parent 2666 2d5e0e2938d1
child 2728 9f17d0f28d77
permissions -rw-r--r--
mod_mam: Adjust event handler priorities to match guidelines in docs Avoids issues with mod_firewall dropped stanzas from being archived.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
635
30be50d2537f mod_mam: Update header
Kim Alvefur <zash@zash.se>
parents: 634
diff changeset
     1
-- XEP-0313: Message Archive Management for Prosody
2004
b5adfe72709b mod_mam: Improve error messages when failed to open storage
Kim Alvefur <zash@zash.se>
parents: 1961
diff changeset
     2
-- Copyright (C) 2011-2016 Kim Alvefur
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
--
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
-- This file is MIT/X11 licensed.
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
     6
local xmlns_mam0    = "urn:xmpp:mam:0";
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
     7
local xmlns_mam1    = "urn:xmpp:mam:1";
2520
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
     8
local xmlns_mam2    = "urn:xmpp:mam:2";
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
     9
local xmlns_delay   = "urn:xmpp:delay";
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    10
local xmlns_forward = "urn:xmpp:forward:0";
2520
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
    11
local xmlns_st_id   = "urn:xmpp:sid:0";
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    12
2297
144b74caa5ef mod_mam: Check that user exists (#731)
Kim Alvefur <zash@zash.se>
parents: 2274
diff changeset
    13
local um = require "core.usermanager";
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
local st = require "util.stanza";
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
    15
local rsm = module:require "rsm";
1399
7fb6b607afd6 mod_mam: Shuffle imports
Kim Alvefur <zash@zash.se>
parents: 1369
diff changeset
    16
local get_prefs = module:require"mamprefs".get;
7fb6b607afd6 mod_mam: Shuffle imports
Kim Alvefur <zash@zash.se>
parents: 1369
diff changeset
    17
local set_prefs = module:require"mamprefs".set;
7fb6b607afd6 mod_mam: Shuffle imports
Kim Alvefur <zash@zash.se>
parents: 1369
diff changeset
    18
local prefs_to_stanza = module:require"mamprefsxml".tostanza;
7fb6b607afd6 mod_mam: Shuffle imports
Kim Alvefur <zash@zash.se>
parents: 1369
diff changeset
    19
local prefs_from_stanza = module:require"mamprefsxml".fromstanza;
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
local jid_bare = require "util.jid".bare;
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
local jid_split = require "util.jid".split;
2520
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
    22
local jid_prepped_split = require "util.jid".prepped_split;
1484
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
    23
local dataform = require "util.dataforms".new;
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    24
local host = module.host;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    25
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    26
local rm_load_roster = require "core.rostermanager".load_roster;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    27
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
    28
local getmetatable = getmetatable;
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
    29
local function is_stanza(x)
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
    30
	return getmetatable(x) == st.stanza_mt;
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
    31
end
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
    32
672
8ae5317ba032 mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents: 671
diff changeset
    33
local tostring = tostring;
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
local time_now = os.time;
707
f987c7b79008 mod_mam: Fix typo
Kim Alvefur <zash@zash.se>
parents: 706
diff changeset
    35
local m_min = math.min;
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse;
706
5c2b96c4dde6 mod_mam: Enforce a max number of items returned, with a default.
Kim Alvefur <zash@zash.se>
parents: 705
diff changeset
    37
local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50);
2021
d44ac0756c46 mod_mam: Enable archiving by default, less of a privacy issue if messages expire
Kim Alvefur <zash@zash.se>
parents: 2020
diff changeset
    38
local global_default_policy = module:get_option("default_archive_policy", true);
1587
3ac2b835c7b3 mod_mam: Make sure default_archive_policy is a boolean or "roster" (thanks souliane)
Kim Alvefur <zash@zash.se>
parents: 1484
diff changeset
    39
if global_default_policy ~= "roster" then
3ac2b835c7b3 mod_mam: Make sure default_archive_policy is a boolean or "roster" (thanks souliane)
Kim Alvefur <zash@zash.se>
parents: 1484
diff changeset
    40
	global_default_policy = module:get_option_boolean("default_archive_policy", global_default_policy);
3ac2b835c7b3 mod_mam: Make sure default_archive_policy is a boolean or "roster" (thanks souliane)
Kim Alvefur <zash@zash.se>
parents: 1484
diff changeset
    41
end
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
    42
2514
d81882aa0e1e mod_mam: Make the store configurable to ease move to 0.10 version
Kim Alvefur <zash@zash.se>
parents: 2510
diff changeset
    43
local archive_store = module:get_option_string("archive_store", "archive2");
2666
2d5e0e2938d1 mod_mam: Remove extraneous assert when opening the store
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2648
diff changeset
    44
local archive = module:open_store(archive_store, "archive");
2024
37b30f10fbba mod_mam: Bit of spacing between blocks of code
Kim Alvefur <zash@zash.se>
parents: 2023
diff changeset
    45
2028
6f4dcc723a60 mod_mam: Use the fallback driver if either the null driver or a driver that does not implement the archive api is returned
Kim Alvefur <zash@zash.se>
parents: 2027
diff changeset
    46
if archive.name == "null" or not archive.find then
2648
0f44d04d0d18 mod_mam: Ignore long log lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2520
diff changeset
    47
	-- luacheck: ignore 631
2028
6f4dcc723a60 mod_mam: Use the fallback driver if either the null driver or a driver that does not implement the archive api is returned
Kim Alvefur <zash@zash.se>
parents: 2027
diff changeset
    48
	if not archive.find then
6f4dcc723a60 mod_mam: Use the fallback driver if either the null driver or a driver that does not implement the archive api is returned
Kim Alvefur <zash@zash.se>
parents: 2027
diff changeset
    49
		module:log("debug", "Attempt to open archive storage returned a valid driver but it does not seem to implement the storage API");
6f4dcc723a60 mod_mam: Use the fallback driver if either the null driver or a driver that does not implement the archive api is returned
Kim Alvefur <zash@zash.se>
parents: 2027
diff changeset
    50
		module:log("debug", "mod_%s does not support archiving", archive._provided_by or archive.name and "storage_"..archive.name.."(?)" or "<unknown>");
6f4dcc723a60 mod_mam: Use the fallback driver if either the null driver or a driver that does not implement the archive api is returned
Kim Alvefur <zash@zash.se>
parents: 2027
diff changeset
    51
	else
6f4dcc723a60 mod_mam: Use the fallback driver if either the null driver or a driver that does not implement the archive api is returned
Kim Alvefur <zash@zash.se>
parents: 2027
diff changeset
    52
		module:log("debug", "Attempt to open archive storage returned null driver");
6f4dcc723a60 mod_mam: Use the fallback driver if either the null driver or a driver that does not implement the archive api is returned
Kim Alvefur <zash@zash.se>
parents: 2027
diff changeset
    53
	end
2034
66156e4d5274 mod_mam: This was meant to be a debug message
Kim Alvefur <zash@zash.se>
parents: 2029
diff changeset
    54
	module:log("debug", "See https://prosody.im/doc/storage and https://prosody.im/doc/archiving for more information");
2027
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents: 2026
diff changeset
    55
	module:log("info", "Using in-memory fallback archive driver");
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents: 2026
diff changeset
    56
	archive = module:require "fallback_archive";
1185
30b681898c2d mod_mam: Log error message if unable to open archive storage
Kim Alvefur <zash@zash.se>
parents: 1152
diff changeset
    57
end
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    58
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
    59
local cleanup;
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
    60
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    61
-- Handle prefs.
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    62
local function handle_prefs(event)
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    63
	local origin, stanza = event.origin, event.stanza;
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    64
	local xmlns_mam = stanza.tags[1].attr.xmlns;
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    65
	local user = origin.username;
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    66
	if stanza.attr.type == "get" then
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    67
		local prefs = prefs_to_stanza(get_prefs(user), xmlns_mam);
1135
0d6ab5e4bc30 mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
Kim Alvefur <zash@zash.se>
parents: 1116
diff changeset
    68
		local reply = st.reply(stanza):add_child(prefs);
1688
d20cfc5ba827 mod_mam: Always return true when a stanza event has been handled
Kim Alvefur <zash@zash.se>
parents: 1682
diff changeset
    69
		origin.send(reply);
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    70
	else -- type == "set"
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    71
		local new_prefs = stanza:get_child("prefs", xmlns_mam);
1135
0d6ab5e4bc30 mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
Kim Alvefur <zash@zash.se>
parents: 1116
diff changeset
    72
		local prefs = prefs_from_stanza(new_prefs);
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    73
		local ok, err = set_prefs(user, prefs);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    74
		if not ok then
1688
d20cfc5ba827 mod_mam: Always return true when a stanza event has been handled
Kim Alvefur <zash@zash.se>
parents: 1682
diff changeset
    75
			origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err)));
d20cfc5ba827 mod_mam: Always return true when a stanza event has been handled
Kim Alvefur <zash@zash.se>
parents: 1682
diff changeset
    76
		else
d20cfc5ba827 mod_mam: Always return true when a stanza event has been handled
Kim Alvefur <zash@zash.se>
parents: 1682
diff changeset
    77
			origin.send(st.reply(stanza));
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
    78
		end
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    79
	end
1688
d20cfc5ba827 mod_mam: Always return true when a stanza event has been handled
Kim Alvefur <zash@zash.se>
parents: 1682
diff changeset
    80
	return true;
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    81
end
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    82
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    83
module:hook("iq/self/"..xmlns_mam0..":prefs", handle_prefs);
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    84
module:hook("iq/self/"..xmlns_mam1..":prefs", handle_prefs);
2520
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
    85
module:hook("iq/self/"..xmlns_mam2..":prefs", handle_prefs);
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    86
1484
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
    87
local query_form = dataform {
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    88
	{ name = "FORM_TYPE"; type = "hidden"; value = xmlns_mam0; };
1484
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
    89
	{ name = "with"; type = "jid-single"; };
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
    90
	{ name = "start"; type = "text-single" };
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
    91
	{ name = "end"; type = "text-single"; };
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
    92
};
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
    93
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
    94
-- Serve form
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    95
local function handle_get_form(event)
1484
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
    96
	local origin, stanza = event.origin, event.stanza;
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    97
	local xmlns_mam = stanza.tags[1].attr.xmlns;
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
    98
	query_form[1].value = xmlns_mam;
2509
1398f3f76c0f mod_mam: Add missing wrapping <query> element when returning the query form
Kim Alvefur <zash@zash.se>
parents: 2297
diff changeset
    99
	origin.send(st.reply(stanza):query(xmlns_mam):add_child(query_form:form()));
1688
d20cfc5ba827 mod_mam: Always return true when a stanza event has been handled
Kim Alvefur <zash@zash.se>
parents: 1682
diff changeset
   100
	return true;
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   101
end
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   102
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   103
module:hook("iq-get/self/"..xmlns_mam0..":query", handle_get_form);
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   104
module:hook("iq-get/self/"..xmlns_mam1..":query", handle_get_form);
2520
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   105
module:hook("iq-get/self/"..xmlns_mam2..":query", handle_get_form);
1484
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   106
1325
b21236b6b8d8 Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents: 1324
diff changeset
   107
-- Handle archive queries
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   108
local function handle_mam_query(event)
1324
853a382c9bd6 mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents: 1187
diff changeset
   109
	local origin, stanza = event.origin, event.stanza;
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   110
	local xmlns_mam = stanza.tags[1].attr.xmlns;
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   111
	local query = stanza.tags[1];
1112
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   112
	local qid = query.attr.queryid;
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   113
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   114
	if cleanup then cleanup[origin.username] = true; end
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   115
1112
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   116
	-- Search query parameters
1484
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   117
	local qwith, qstart, qend;
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   118
	local form = query:get_child("x", "jabber:x:data");
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   119
	if form then
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   120
		local err;
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   121
		query_form[1].value = xmlns_mam;
1484
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   122
		form, err = query_form:data(form);
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   123
		if err then
1702
55bc42c1d8c5 mod_mam: Semicolons!
Kim Alvefur <zash@zash.se>
parents: 1692
diff changeset
   124
			origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err))));
1688
d20cfc5ba827 mod_mam: Always return true when a stanza event has been handled
Kim Alvefur <zash@zash.se>
parents: 1682
diff changeset
   125
			return true;
1484
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   126
		end
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   127
		qwith, qstart, qend = form["with"], form["start"], form["end"];
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   128
		qwith = qwith and jid_bare(qwith); -- dataforms does jidprep
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   129
	end
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   130
1112
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   131
	if qstart or qend then -- Validate timestamps
1702
55bc42c1d8c5 mod_mam: Semicolons!
Kim Alvefur <zash@zash.se>
parents: 1692
diff changeset
   132
		local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend));
1112
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   133
		if (qstart and not vstart) or (qend and not vend) then
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   134
			origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp"))
1702
55bc42c1d8c5 mod_mam: Semicolons!
Kim Alvefur <zash@zash.se>
parents: 1692
diff changeset
   135
			return true;
1112
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   136
		end
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   137
		qstart, qend = vstart, vend;
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   138
	end
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   139
1484
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   140
	module:log("debug", "Archive query, id %s with %s from %s until %s)",
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   141
		tostring(qid), qwith or "anyone", qstart or "the dawn of time", qend or "now");
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   142
1112
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   143
	-- RSM stuff
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   144
	local qset = rsm.get(query);
1112
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   145
	local qmax = m_min(qset and qset.max or default_max_items, max_max_items);
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   146
	local reverse = qset and qset.before or false;
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   147
	local before, after = qset and qset.before, qset and qset.after;
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   148
	if type(before) ~= "string" then before = nil; end
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   149
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   150
	-- Load all the data!
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   151
	local data, err = archive:find(origin.username, {
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   152
		start = qstart; ["end"] = qend; -- Time range
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   153
		with = qwith;
1689
6b2122630b92 mod_mam: Support the mandatory 'complete' attribute by requesting one extra item from storage
Kim Alvefur <zash@zash.se>
parents: 1688
diff changeset
   154
		limit = qmax + 1;
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   155
		before = before; after = after;
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   156
		reverse = reverse;
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   157
		total = true;
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   158
	});
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   159
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   160
	if not data then
1688
d20cfc5ba827 mod_mam: Always return true when a stanza event has been handled
Kim Alvefur <zash@zash.se>
parents: 1682
diff changeset
   161
		origin.send(st.error_reply(stanza, "cancel", "internal-server-error", err));
d20cfc5ba827 mod_mam: Always return true when a stanza event has been handled
Kim Alvefur <zash@zash.se>
parents: 1682
diff changeset
   162
		return true;
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   163
	end
2023
7198c129657c mod_mam: Always convert 'total' to a number
Kim Alvefur <zash@zash.se>
parents: 2022
diff changeset
   164
	local total = tonumber(err);
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   165
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   166
	if xmlns_mam == xmlns_mam0 then
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   167
		origin.send(st.reply(stanza));
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   168
	end
1403
6b3db167374a mod_mam: Mirror to and from attributes from iq on result messages
Kim Alvefur <zash@zash.se>
parents: 1400
diff changeset
   169
	local msg_reply_attr = { to = stanza.attr.from, from = stanza.attr.to };
6b3db167374a mod_mam: Mirror to and from attributes from iq on result messages
Kim Alvefur <zash@zash.se>
parents: 1400
diff changeset
   170
1692
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   171
	local results = {};
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   172
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   173
	-- Wrap it in stuff and deliver
1691
838150167871 mod_mam: Move variable into loop scope
Kim Alvefur <zash@zash.se>
parents: 1690
diff changeset
   174
	local first, last;
1689
6b2122630b92 mod_mam: Support the mandatory 'complete' attribute by requesting one extra item from storage
Kim Alvefur <zash@zash.se>
parents: 1688
diff changeset
   175
	local count = 0;
6b2122630b92 mod_mam: Support the mandatory 'complete' attribute by requesting one extra item from storage
Kim Alvefur <zash@zash.se>
parents: 1688
diff changeset
   176
	local complete = "true";
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   177
	for id, item, when in data do
1689
6b2122630b92 mod_mam: Support the mandatory 'complete' attribute by requesting one extra item from storage
Kim Alvefur <zash@zash.se>
parents: 1688
diff changeset
   178
		count = count + 1;
6b2122630b92 mod_mam: Support the mandatory 'complete' attribute by requesting one extra item from storage
Kim Alvefur <zash@zash.se>
parents: 1688
diff changeset
   179
		if count > qmax then
6b2122630b92 mod_mam: Support the mandatory 'complete' attribute by requesting one extra item from storage
Kim Alvefur <zash@zash.se>
parents: 1688
diff changeset
   180
			complete = nil;
6b2122630b92 mod_mam: Support the mandatory 'complete' attribute by requesting one extra item from storage
Kim Alvefur <zash@zash.se>
parents: 1688
diff changeset
   181
			break;
6b2122630b92 mod_mam: Support the mandatory 'complete' attribute by requesting one extra item from storage
Kim Alvefur <zash@zash.se>
parents: 1688
diff changeset
   182
		end
1691
838150167871 mod_mam: Move variable into loop scope
Kim Alvefur <zash@zash.se>
parents: 1690
diff changeset
   183
		local fwd_st = st.message(msg_reply_attr)
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   184
			:tag("result", { xmlns = xmlns_mam, queryid = qid, id = id })
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   185
				:tag("forwarded", { xmlns = xmlns_forward })
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   186
					:tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up();
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   187
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   188
		if not is_stanza(item) then
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   189
			item = st.deserialize(item);
1112
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   190
		end
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   191
		item.attr.xmlns = "jabber:client";
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   192
		fwd_st:add_child(item);
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
   193
1325
b21236b6b8d8 Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents: 1324
diff changeset
   194
		if not first then first = id; end
b21236b6b8d8 Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents: 1324
diff changeset
   195
		last = id;
1116
2345a30dd8b4 mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents: 1114
diff changeset
   196
1692
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   197
		if reverse then
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   198
			results[count] = fwd_st;
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   199
		else
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   200
			origin.send(fwd_st);
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   201
		end
1112
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   202
	end
2024
37b30f10fbba mod_mam: Bit of spacing between blocks of code
Kim Alvefur <zash@zash.se>
parents: 2023
diff changeset
   203
1692
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   204
	if reverse then
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   205
		for i = #results, 1, -1 do
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   206
			origin.send(results[i]);
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   207
		end
2025
5fb917b86838 mod_mam: Merge identical conditional blocks
Kim Alvefur <zash@zash.se>
parents: 2024
diff changeset
   208
		first, last = last, first;
1692
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   209
	end
cd87a2eba8f2 mod_mam: Reverse the order of reversed queries back to chronological order
Kim Alvefur <zash@zash.se>
parents: 1691
diff changeset
   210
1112
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   211
	-- That's all folks!
1dc07833355e mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents: 1111
diff changeset
   212
	module:log("debug", "Archive query %s completed", tostring(qid));
705
c9d0ba39a33b mod_mam: Move RSM pointer to last message into a MAM-namespaced child
Kim Alvefur <zash@zash.se>
parents: 702
diff changeset
   213
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   214
	local fin;
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   215
	if xmlns_mam == xmlns_mam0 then
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   216
		fin = st.message(msg_reply_attr);
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   217
	else
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   218
		fin = st.reply(stanza);
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   219
	end
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   220
	do
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   221
		fin:tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete })
1484
53a3a19d6093 mod_mam: Update to version 0.3 of XEP-0313
Kim Alvefur <zash@zash.se>
parents: 1403
diff changeset
   222
			:add_child(rsm.generate {
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   223
				first = first, last = last, count = total })
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   224
	end
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   225
	origin.send(fin);
1688
d20cfc5ba827 mod_mam: Always return true when a stanza event has been handled
Kim Alvefur <zash@zash.se>
parents: 1682
diff changeset
   226
	return true;
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   227
end
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   228
module:hook("iq-set/self/"..xmlns_mam0..":query", handle_mam_query);
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   229
module:hook("iq-set/self/"..xmlns_mam1..":query", handle_mam_query);
2520
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   230
module:hook("iq-set/self/"..xmlns_mam2..":query", handle_mam_query);
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   231
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   232
local function has_in_roster(user, who)
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   233
	local roster = rm_load_roster(user, host);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   234
	module:log("debug", "%s has %s in roster? %s", user, who, roster[who] and "yes" or "no");
798
2b8ceb4d1a73 mod_mam: remove useless check
Kim Alvefur <zash@zash.se>
parents: 751
diff changeset
   235
	return roster[who];
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   236
end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   237
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   238
local function shall_store(user, who)
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   239
	-- TODO Cache this?
2297
144b74caa5ef mod_mam: Check that user exists (#731)
Kim Alvefur <zash@zash.se>
parents: 2274
diff changeset
   240
	if not um.user_exists(user, host) then
144b74caa5ef mod_mam: Check that user exists (#731)
Kim Alvefur <zash@zash.se>
parents: 2274
diff changeset
   241
		return false;
144b74caa5ef mod_mam: Check that user exists (#731)
Kim Alvefur <zash@zash.se>
parents: 2274
diff changeset
   242
	end
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   243
	local prefs = get_prefs(user);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   244
	local rule = prefs[who];
1702
55bc42c1d8c5 mod_mam: Semicolons!
Kim Alvefur <zash@zash.se>
parents: 1692
diff changeset
   245
	module:log("debug", "%s's rule for %s is %s", user, who, tostring(rule));
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   246
	if rule ~= nil then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   247
		return rule;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   248
	end
1792
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   249
	-- Below could be done by a metatable
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   250
	local default = prefs[false];
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   251
	module:log("debug", "%s's default rule is %s", user, tostring(default));
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   252
	if default == nil then
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   253
		default = global_default_policy;
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   254
		module:log("debug", "Using global default rule, %s", tostring(default));
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   255
	end
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   256
	if default == "roster" then
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   257
		return has_in_roster(user, who);
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   258
	end
d2b82b90c413 mod_mam: Unindent 'else' block
Kim Alvefur <zash@zash.se>
parents: 1777
diff changeset
   259
	return default;
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   260
end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   261
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   262
-- Handle messages
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   263
local function message_handler(event, c2s)
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   264
	local origin, stanza = event.origin, event.stanza;
2274
9c99200afd17 mod_mam: Log messages from local clients on the clients session
Kim Alvefur <zash@zash.se>
parents: 2053
diff changeset
   265
	local log = c2s and origin.log or module._log;
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   266
	local orig_type = stanza.attr.type or "normal";
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   267
	local orig_from = stanza.attr.from;
1149
d055c44a7f61 mod_mam: Clean up and explain the code that determines who's archive we put the message in
Kim Alvefur <zash@zash.se>
parents: 1135
diff changeset
   268
	local orig_to = stanza.attr.to or orig_from;
d055c44a7f61 mod_mam: Clean up and explain the code that determines who's archive we put the message in
Kim Alvefur <zash@zash.se>
parents: 1135
diff changeset
   269
	-- Stanza without 'to' are treated as if it was to their own bare jid
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   270
2520
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   271
	-- Whos storage do we put it in?
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   272
	local store_user = c2s and origin.username or jid_split(orig_to);
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   273
	-- And who are they chatting with?
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   274
	local with = jid_bare(c2s and orig_to or orig_from);
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   275
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   276
	-- Filter out <stanza-id> that claim to be from us
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   277
	stanza:maptags(function (tag)
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   278
		if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id then
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   279
			local by_user, by_host, res = jid_prepped_split(tag.attr.by);
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   280
			if not res and by_host == module.host and by_user == store_user then
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   281
				return nil;
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   282
			end
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   283
		end
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   284
		return tag;
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   285
	end);
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   286
1794
4c2146f5bf39 mod_mam: Store chat messages and normal messages with a body
Kim Alvefur <zash@zash.se>
parents: 1792
diff changeset
   287
	-- We store chat messages or normal messages that have a body
1961
ca33cca2e028 mod_mam: Clarify condition presendence
Kim Alvefur <zash@zash.se>
parents: 1794
diff changeset
   288
	if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then
2274
9c99200afd17 mod_mam: Log messages from local clients on the clients session
Kim Alvefur <zash@zash.se>
parents: 2053
diff changeset
   289
		log("debug", "Not archiving stanza: %s (type)", stanza:top_tag());
1794
4c2146f5bf39 mod_mam: Store chat messages and normal messages with a body
Kim Alvefur <zash@zash.se>
parents: 1792
diff changeset
   290
		return;
4c2146f5bf39 mod_mam: Store chat messages and normal messages with a body
Kim Alvefur <zash@zash.se>
parents: 1792
diff changeset
   291
	end
2516
bc7539ccbde1 mod_mam: Normalize whitespace
Kim Alvefur <zash@zash.se>
parents: 2515
diff changeset
   292
1150
296820f18ba6 mod_mam: Add support for XEP-0334: Message Processing Hints
Kim Alvefur <zash@zash.se>
parents: 1149
diff changeset
   293
	-- or if hints suggest we shouldn't
2515
34a6ba5dfe4f mod_mam: Bring hints processing in line with 0.10 version
Kim Alvefur <zash@zash.se>
parents: 2514
diff changeset
   294
	if not stanza:get_child("store", "urn:xmpp:hints") then -- No hint telling us we should store
34a6ba5dfe4f mod_mam: Bring hints processing in line with 0.10 version
Kim Alvefur <zash@zash.se>
parents: 2514
diff changeset
   295
		if stanza:get_child("no-permanent-store", "urn:xmpp:hints")
34a6ba5dfe4f mod_mam: Bring hints processing in line with 0.10 version
Kim Alvefur <zash@zash.se>
parents: 2514
diff changeset
   296
			or stanza:get_child("no-store", "urn:xmpp:hints") then -- Hint telling us we should NOT store
34a6ba5dfe4f mod_mam: Bring hints processing in line with 0.10 version
Kim Alvefur <zash@zash.se>
parents: 2514
diff changeset
   297
			log("debug", "Not archiving stanza: %s (hint)", stanza:top_tag());
34a6ba5dfe4f mod_mam: Bring hints processing in line with 0.10 version
Kim Alvefur <zash@zash.se>
parents: 2514
diff changeset
   298
			return;
34a6ba5dfe4f mod_mam: Bring hints processing in line with 0.10 version
Kim Alvefur <zash@zash.se>
parents: 2514
diff changeset
   299
		end
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   300
	end
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   301
1149
d055c44a7f61 mod_mam: Clean up and explain the code that determines who's archive we put the message in
Kim Alvefur <zash@zash.se>
parents: 1135
diff changeset
   302
	-- Check with the users preferences
d055c44a7f61 mod_mam: Clean up and explain the code that determines who's archive we put the message in
Kim Alvefur <zash@zash.se>
parents: 1135
diff changeset
   303
	if shall_store(store_user, with) then
2274
9c99200afd17 mod_mam: Log messages from local clients on the clients session
Kim Alvefur <zash@zash.se>
parents: 2053
diff changeset
   304
		log("debug", "Archiving stanza: %s", stanza:top_tag());
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   305
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   306
		-- And stash it
2519
4cb549622862 mod_mam: Fix to expect archive id as first return value from archive:append
Kim Alvefur <zash@zash.se>
parents: 2517
diff changeset
   307
		local ok = archive:append(store_user, nil, stanza, time_now(), with);
1750
5734a6199938 mod_mam: Fire event on successful storage of message
Kim Alvefur <zash@zash.se>
parents: 1741
diff changeset
   308
		if ok then
2519
4cb549622862 mod_mam: Fix to expect archive id as first return value from archive:append
Kim Alvefur <zash@zash.se>
parents: 2517
diff changeset
   309
			local id = ok;
2520
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   310
			stanza:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up();
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   311
			if cleanup then cleanup[store_user] = true; end
1750
5734a6199938 mod_mam: Fire event on successful storage of message
Kim Alvefur <zash@zash.se>
parents: 1741
diff changeset
   312
			module:fire_event("archive-message-added", { origin = origin, stanza = stanza, for_user = store_user, id = id });
5734a6199938 mod_mam: Fire event on successful storage of message
Kim Alvefur <zash@zash.se>
parents: 1741
diff changeset
   313
		end
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   314
	else
2274
9c99200afd17 mod_mam: Log messages from local clients on the clients session
Kim Alvefur <zash@zash.se>
parents: 2053
diff changeset
   315
		log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag());
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   316
	end
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   317
end
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   318
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   319
local function c2s_message_handler(event)
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   320
	return message_handler(event, true);
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   321
end
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   322
2520
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   323
local function strip_stanza_id(event)
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   324
	local strip_by = jid_bare(event.origin.full_jid);
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   325
	event.stanza:maptags(function(tag)
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   326
		if not ( tag.attr.xmlns == xmlns_st_id and tag.attr.by == strip_by ) then
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   327
			return tag;
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   328
		end
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   329
	end);
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   330
end
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   331
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   332
module:hook("pre-message/bare", strip_stanza_id, 0.01);
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   333
module:hook("pre-message/full", strip_stanza_id, 0.01);
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   334
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   335
local cleanup_after = module:get_option_string("archive_expires_after", "1w");
2026
77b9c7e5fd63 mod_mam: Allow interval between archive cleanup to be changed
Kim Alvefur <zash@zash.se>
parents: 2025
diff changeset
   336
local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60);
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   337
if cleanup_after ~= "never" then
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   338
	local day = 86400;
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   339
	local multipliers = { d = day, w = day * 7, m = 31 * day, y = 365.2425 * day };
2053
50c188cf0ae3 mod_mam: Include 'w' (week) in pattern for "archive_expires_after" option (fixes #627)
Kim Alvefur <zash@zash.se>
parents: 2046
diff changeset
   340
	local n, m = cleanup_after:lower():match("(%d+)%s*([dwmy]?)");
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   341
	if not n then
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   342
		module:log("error", "Could not parse archive_expires_after string %q", cleanup_after);
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   343
		return false;
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   344
	end
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   345
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   346
	cleanup_after = tonumber(n) * ( multipliers[m] or 1 );
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   347
2046
752eee154891 mod_mam: Add some debug logging
Kim Alvefur <zash@zash.se>
parents: 2043
diff changeset
   348
	module:log("debug", "archive_expires_after = %d -- in seconds", cleanup_after);
752eee154891 mod_mam: Add some debug logging
Kim Alvefur <zash@zash.se>
parents: 2043
diff changeset
   349
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   350
	if not archive.delete then
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   351
		module:log("error", "archive_expires_after set but mod_%s does not support deleting", archive._provided_by);
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   352
		return false;
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   353
	end
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   354
2517
66a61f48e376 mod_mam: Add comments
Kim Alvefur <zash@zash.se>
parents: 2516
diff changeset
   355
	-- Set of known users to do message expiry for
66a61f48e376 mod_mam: Add comments
Kim Alvefur <zash@zash.se>
parents: 2516
diff changeset
   356
	-- Populated either below or when new messages are added
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   357
	cleanup = {};
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   358
2517
66a61f48e376 mod_mam: Add comments
Kim Alvefur <zash@zash.se>
parents: 2516
diff changeset
   359
	-- Iterating over users is not supported by all authentication modules
66a61f48e376 mod_mam: Add comments
Kim Alvefur <zash@zash.se>
parents: 2516
diff changeset
   360
	-- Catch and ignore error if not supported
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   361
	pcall(function ()
2517
66a61f48e376 mod_mam: Add comments
Kim Alvefur <zash@zash.se>
parents: 2516
diff changeset
   362
		-- If this works, then we schedule cleanup for all known users on startup
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   363
		for user in um.users(module.host) do
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   364
			cleanup[user] = true;
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   365
		end
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   366
	end);
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   367
2517
66a61f48e376 mod_mam: Add comments
Kim Alvefur <zash@zash.se>
parents: 2516
diff changeset
   368
	-- At odd intervals, delete old messages for one user
2043
464edd03099a mod_mam: Add a bit of randomness to cleanup intervals to keep some time between multiple instances
Kim Alvefur <zash@zash.se>
parents: 2037
diff changeset
   369
	module:add_timer(math.random(10, 60), function()
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   370
		local user = next(cleanup);
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   371
		if user then
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   372
			module:log("debug", "Removing old messages for user %q", user);
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   373
			local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; })
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   374
			if not ok then
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   375
				module:log("warn", "Could not expire archives for user %s: %s", user, err);
2046
752eee154891 mod_mam: Add some debug logging
Kim Alvefur <zash@zash.se>
parents: 2043
diff changeset
   376
			else
752eee154891 mod_mam: Add some debug logging
Kim Alvefur <zash@zash.se>
parents: 2043
diff changeset
   377
				-- :affected() is a recent addition for eg SQLite3 in LuaDBI
752eee154891 mod_mam: Add some debug logging
Kim Alvefur <zash@zash.se>
parents: 2043
diff changeset
   378
				pcall(function(stmt)
752eee154891 mod_mam: Add some debug logging
Kim Alvefur <zash@zash.se>
parents: 2043
diff changeset
   379
					module:log("debug", "Removed %d messages", stmt:affected());
752eee154891 mod_mam: Add some debug logging
Kim Alvefur <zash@zash.se>
parents: 2043
diff changeset
   380
				end, err);
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   381
			end
2029
ae98bb884110 mod_mam: Fix Top Level Error from string[table] = nil instead of table[string] = nil
Kim Alvefur <zash@zash.se>
parents: 2028
diff changeset
   382
			cleanup[user] = nil;
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   383
		end
2043
464edd03099a mod_mam: Add a bit of randomness to cleanup intervals to keep some time between multiple instances
Kim Alvefur <zash@zash.se>
parents: 2037
diff changeset
   384
		return math.random(cleanup_interval, cleanup_interval * 2);
2020
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   385
	end);
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   386
end
279885fd9728 mod_mam: Add support for trimming old archived messages
Kim Alvefur <zash@zash.se>
parents: 2005
diff changeset
   387
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   388
-- Stanzas sent by local clients
2715
ff1f7e61517f mod_mam: Adjust event handler priorities to match guidelines in docs
Matthew Wild <mwild1@gmail.com>
parents: 2666
diff changeset
   389
module:hook("pre-message/bare", c2s_message_handler, 0);
ff1f7e61517f mod_mam: Adjust event handler priorities to match guidelines in docs
Matthew Wild <mwild1@gmail.com>
parents: 2666
diff changeset
   390
module:hook("pre-message/full", c2s_message_handler, 0);
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   391
-- Stanszas to local clients
2715
ff1f7e61517f mod_mam: Adjust event handler priorities to match guidelines in docs
Matthew Wild <mwild1@gmail.com>
parents: 2666
diff changeset
   392
module:hook("message/bare", message_handler, 0);
ff1f7e61517f mod_mam: Adjust event handler priorities to match guidelines in docs
Matthew Wild <mwild1@gmail.com>
parents: 2666
diff changeset
   393
module:hook("message/full", message_handler, 0);
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   394
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   395
module:add_feature(xmlns_mam0); -- COMPAT with XEP-0313 v 0.1
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
   396
1682
9ee56cc1be2c mod_mam: Advertise feature in disco#info for account as per XEP-0313 >= 0.2
Kim Alvefur <zash@zash.se>
parents: 1587
diff changeset
   397
module:hook("account-disco-info", function(event)
2510
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   398
	(event.reply or event.stanza):tag("feature", {var=xmlns_mam0}):up();
5941aac79f06 mod_mam: Add support for XEP-0313 v0.5
Kim Alvefur <zash@zash.se>
parents: 2509
diff changeset
   399
	(event.reply or event.stanza):tag("feature", {var=xmlns_mam1}):up();
2520
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   400
	(event.reply or event.stanza):tag("feature", {var=xmlns_mam2}):up();
248054199d0f mod_mam: Add support for XEP-0313 v0.6
Kim Alvefur <zash@zash.se>
parents: 2519
diff changeset
   401
	(event.reply or event.stanza):tag("feature", {var=xmlns_st_id}):up();
1682
9ee56cc1be2c mod_mam: Advertise feature in disco#info for account as per XEP-0313 >= 0.2
Kim Alvefur <zash@zash.se>
parents: 1587
diff changeset
   402
end);
9ee56cc1be2c mod_mam: Advertise feature in disco#info for account as per XEP-0313 >= 0.2
Kim Alvefur <zash@zash.se>
parents: 1587
diff changeset
   403