changeset 3183:b718092e442f

mod_presence_cache: Forget cached presence on s2s close This should remove stale entries from unclean shutdowns and similar.
author Kim Alvefur <zash@zash.se>
date Fri, 13 Jul 2018 00:47:29 +0200
parents 9e5616a49d59
children c25853bc387b
files mod_presence_cache/mod_presence_cache.lua
diffstat 1 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_presence_cache/mod_presence_cache.lua	Thu Jul 12 23:47:26 2018 +0200
+++ b/mod_presence_cache/mod_presence_cache.lua	Fri Jul 13 00:47:29 2018 +0200
@@ -6,6 +6,7 @@
 local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed;
 local jid_split = require"util.jid".split;
 local jid_bare = require"util.jid".bare;
+local jid_host = require"util.jid".host;
 local st = require"util.stanza";
 local datetime = require"util.datetime";
 local cache = require "util.cache";
@@ -98,3 +99,27 @@
 end
 
 module:hook("pre-presence/bare", answer_probe_from_cache, 10);
+
+local function clear_cache_from_s2s(remote, reason)
+	if not remote then return end
+	if reason and reason:find("timeout") then return end -- Ignore connections closed for being idle
+
+	module:log("debug", "Dropping cached presence from host %s", remote);
+
+	for bare, cached in pairs(bare_cache) do
+		if jid_host(bare) == remote then
+			for jid in pairs(cached) do
+				presence_cache:set(jid, nil);
+			end
+			bare_cache[bare] = nil;
+		end
+	end
+end
+
+module:hook("s2sin-destroyed", function (event)
+	return clear_cache_from_s2s(event.session.from_host, event.reason);
+end);
+
+module:hook("s2sout-destroyed", function (event)
+	return clear_cache_from_s2s(event.session.to_host, event.reason);
+end);