Comparison

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
comparison
equal deleted inserted replaced
8221:4989a625419a 8235:7d9a2c200736
53 end 53 end
54 54
55 local use_total = true; 55 local use_total = true;
56 56
57 local cleanup; 57 local cleanup;
58
59 local function schedule_cleanup(username)
60 if cleanup and not cleanup[username] then
61 table.insert(cleanup, username);
62 cleanup[username] = true;
63 end
64 end
58 65
59 -- Handle prefs. 66 -- Handle prefs.
60 module:hook("iq/self/"..xmlns_mam..":prefs", function(event) 67 module:hook("iq/self/"..xmlns_mam..":prefs", function(event)
61 local origin, stanza = event.origin, event.stanza; 68 local origin, stanza = event.origin, event.stanza;
62 local user = origin.username; 69 local user = origin.username;
95 module:hook("iq-set/self/"..xmlns_mam..":query", function(event) 102 module:hook("iq-set/self/"..xmlns_mam..":query", function(event)
96 local origin, stanza = event.origin, event.stanza; 103 local origin, stanza = event.origin, event.stanza;
97 local query = stanza.tags[1]; 104 local query = stanza.tags[1];
98 local qid = query.attr.queryid; 105 local qid = query.attr.queryid;
99 106
100 if cleanup then cleanup[origin.username] = true; end 107 schedule_cleanup(origin.username);
101 108
102 -- Search query parameters 109 -- Search query parameters
103 local qwith, qstart, qend; 110 local qwith, qstart, qend;
104 local form = query:get_child("x", "jabber:x:data"); 111 local form = query:get_child("x", "jabber:x:data");
105 if form then 112 if form then
302 if ok then 309 if ok then
303 local clone_for_other_handlers = st.clone(stanza); 310 local clone_for_other_handlers = st.clone(stanza);
304 local id = ok; 311 local id = ok;
305 clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up(); 312 clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up();
306 event.stanza = clone_for_other_handlers; 313 event.stanza = clone_for_other_handlers;
307 if cleanup then cleanup[store_user] = true; end 314 schedule_cleanup(store_user);
308 module:fire_event("archive-message-added", { origin = origin, stanza = clone_for_storage, for_user = store_user, id = id }); 315 module:fire_event("archive-message-added", { origin = origin, stanza = clone_for_storage, for_user = store_user, id = id });
309 end 316 end
310 else 317 else
311 log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag()); 318 log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag());
312 end 319 end
324 module:hook("pre-message/bare", strip_stanza_id_after_other_events, -1); 331 module:hook("pre-message/bare", strip_stanza_id_after_other_events, -1);
325 module:hook("pre-message/full", strip_stanza_id_after_other_events, -1); 332 module:hook("pre-message/full", strip_stanza_id_after_other_events, -1);
326 333
327 local cleanup_after = module:get_option_string("archive_expires_after", "1w"); 334 local cleanup_after = module:get_option_string("archive_expires_after", "1w");
328 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60); 335 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60);
329 if cleanup_after ~= "never" then 336 if not archive.delete then
337 module:log("debug", "Selected storage driver does not support deletion, archives will not expire");
338 elseif cleanup_after ~= "never" then
330 local day = 86400; 339 local day = 86400;
331 local multipliers = { d = day, w = day * 7, m = 31 * day, y = 365.2425 * day }; 340 local multipliers = { d = day, w = day * 7, m = 31 * day, y = 365.2425 * day };
332 local n, m = cleanup_after:lower():match("(%d+)%s*([dwmy]?)"); 341 local n, m = cleanup_after:lower():match("(%d+)%s*([dwmy]?)");
333 if not n then 342 if not n then
334 module:log("error", "Could not parse archive_expires_after string %q", cleanup_after); 343 module:log("error", "Could not parse archive_expires_after string %q", cleanup_after);
351 -- Iterating over users is not supported by all authentication modules 360 -- Iterating over users is not supported by all authentication modules
352 -- Catch and ignore error if not supported 361 -- Catch and ignore error if not supported
353 pcall(function () 362 pcall(function ()
354 -- If this works, then we schedule cleanup for all known users on startup 363 -- If this works, then we schedule cleanup for all known users on startup
355 for user in um.users(module.host) do 364 for user in um.users(module.host) do
356 cleanup[user] = true; 365 schedule_cleanup(user);
357 end 366 end
358 end); 367 end);
359 368
360 -- At odd intervals, delete old messages for one user 369 -- At odd intervals, delete old messages for one user
361 module:add_timer(math.random(10, 60), function() 370 module:add_timer(math.random(10, 60), function()
362 local user = next(cleanup); 371 local user = table.remove(cleanup, 1);
363 if user then 372 if user then
364 module:log("debug", "Removing old messages for user %q", user); 373 module:log("debug", "Removing old messages for user %q", user);
365 local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; }) 374 local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; })
366 if not ok then 375 if not ok then
367 module:log("warn", "Could not expire archives for user %s: %s", user, err); 376 module:log("warn", "Could not expire archives for user %s: %s", user, err);