# HG changeset patch # User Kim Alvefur # Date 1531435649 -7200 # Node ID b718092e442fa5f24818764f2d1644b2dc6bfa34 # Parent 9e5616a49d595c38e60ad5474fef277b05f98b56 mod_presence_cache: Forget cached presence on s2s close This should remove stale entries from unclean shutdowns and similar. diff -r 9e5616a49d59 -r b718092e442f mod_presence_cache/mod_presence_cache.lua --- 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);