Diff

plugins/mod_mam/mod_mam.lua @ 8235:7d9a2c200736

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 14 Sep 2017 02:48:34 +0200
parent 8231:97a094fdf101
child 8250:9ea5ea53744b
line wrap: on
line diff
--- a/plugins/mod_mam/mod_mam.lua	Wed Sep 13 18:46:39 2017 +0200
+++ b/plugins/mod_mam/mod_mam.lua	Thu Sep 14 02:48:34 2017 +0200
@@ -56,6 +56,13 @@
 
 local cleanup;
 
+local function schedule_cleanup(username)
+	if cleanup and not cleanup[username] then
+		table.insert(cleanup, username);
+		cleanup[username] = true;
+	end
+end
+
 -- Handle prefs.
 module:hook("iq/self/"..xmlns_mam..":prefs", function(event)
 	local origin, stanza = event.origin, event.stanza;
@@ -97,7 +104,7 @@
 	local query = stanza.tags[1];
 	local qid = query.attr.queryid;
 
-	if cleanup then cleanup[origin.username] = true; end
+	schedule_cleanup(origin.username);
 
 	-- Search query parameters
 	local qwith, qstart, qend;
@@ -304,7 +311,7 @@
 			local id = ok;
 			clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up();
 			event.stanza = clone_for_other_handlers;
-			if cleanup then cleanup[store_user] = true; end
+			schedule_cleanup(store_user);
 			module:fire_event("archive-message-added", { origin = origin, stanza = clone_for_storage, for_user = store_user, id = id });
 		end
 	else
@@ -326,7 +333,9 @@
 
 local cleanup_after = module:get_option_string("archive_expires_after", "1w");
 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60);
-if cleanup_after ~= "never" then
+if not archive.delete then
+	module:log("debug", "Selected storage driver does not support deletion, archives will not expire");
+elseif cleanup_after ~= "never" then
 	local day = 86400;
 	local multipliers = { d = day, w = day * 7, m = 31 * day, y = 365.2425 * day };
 	local n, m = cleanup_after:lower():match("(%d+)%s*([dwmy]?)");
@@ -353,13 +362,13 @@
 	pcall(function ()
 		-- If this works, then we schedule cleanup for all known users on startup
 		for user in um.users(module.host) do
-			cleanup[user] = true;
+			schedule_cleanup(user);
 		end
 	end);
 
 	-- At odd intervals, delete old messages for one user
 	module:add_timer(math.random(10, 60), function()
-		local user = next(cleanup);
+		local user = table.remove(cleanup, 1);
 		if user then
 			module:log("debug", "Removing old messages for user %q", user);
 			local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; })