mod_storage_memory: Add support for clearing a user's data when they log out

Tue, 14 Mar 2017 17:46:51 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 14 Mar 2017 17:46:51 +0000
changeset 2620
8b8cab2eb7fc
parent 2619
1e4bbff0a6fd
child 2621
c4c51e43b788

mod_storage_memory: Add support for clearing a user's data when they log out

mod_storage_memory/mod_storage_memory.lua file | annotate | diff | comparison | revisions
--- a/mod_storage_memory/mod_storage_memory.lua	Tue Mar 14 12:59:41 2017 +0000
+++ b/mod_storage_memory/mod_storage_memory.lua	Tue Mar 14 17:46:51 2017 +0000
@@ -1,3 +1,5 @@
+local auto_purge_enabled = module:get_option_boolean("storage_memory_temporary", false);
+local auto_purge_stores = module:get_option_set("storage_memory_temporary_stores", {});
 
 local memory = setmetatable({}, {
 	__index = function(t, k)
@@ -171,4 +173,25 @@
 	return nil, "unsupported-store";
 end
 
+if auto_purge_enabled then
+	module:hook("resource-unbind", function (event)
+		local user_bare_jid = event.session.username.."@"..event.session.host;
+		if not prosody.bare_sessions[user_bare_jid] then -- User went offline
+			module:log("debug", "Clearing store for offline user %s", user_bare_jid);
+			local f, s, v;
+			if auto_purge_stores:empty() then
+				f, s, v = pairs(memory);
+			else
+				f, s, v = auto_purge_stores:items();
+			end
+
+			for store_name in f, s, v do
+				if memory[store_name] then
+					memory[store_name][event.session.username] = nil;
+				end
+			end
+		end
+	end);
+end
+
 module:provides("storage", driver);

mercurial