# HG changeset patch
# User Matthew Wild <mwild1@gmail.com>
# Date 1739455234 0
# Node ID 2389b092f78145aae18d99f535a2f1c5ab79f04a
# Parent  40386b43de6fc60930339c71fa992a8c16522749# Parent  40c309f8b676e5c2910111a39718f640d9fd4623
Merge 13.0->trunk

diff -r 40386b43de6f -r 2389b092f781 plugins/mod_presence.lua
--- a/plugins/mod_presence.lua	Thu Feb 13 13:09:11 2025 +0000
+++ b/plugins/mod_presence.lua	Thu Feb 13 14:00:34 2025 +0000
@@ -54,11 +54,12 @@
 		if priority < -128 then priority = -128 end
 		if priority > 127 then priority = 127 end
 	else priority = 0; end
+
+	local node, host = origin.username, origin.host;
+	local roster = origin.roster;
 	if full_sessions[origin.full_jid] then -- if user is still connected
 		origin.send(stanza); -- reflect their presence back to them
 	end
-	local roster = origin.roster;
-	local node, host = origin.username, origin.host;
 	local user = bare_sessions[node.."@"..host];
 	for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
 		if res ~= origin and res.presence then -- to resource
@@ -72,6 +73,13 @@
 			core_post_stanza(origin, stanza, true);
 		end
 	end
+
+	-- It's possible that after the network activity above, the origin
+	-- has been disconnected (particularly if something happened while
+	-- sending the reflection). So we abort further presence processing
+	-- in that case.
+	if not origin.type then return; end
+
 	stanza.attr.to = nil;
 	if stanza.attr.type == nil and not origin.presence then -- initial presence
 		module:fire_event("presence/initial", { origin = origin, stanza = stanza } );