view mod_smacks_offline/mod_smacks_offline.lua @ 2712:d89ab70808f6

mod_cloud_notify: fix bug when multiple resources are used This bug was triggered under the rare circumstances that a message arrived and one resource was smacks hibernated while the other one(s) were offline. Then only the hibernated resource but not the offline one(s) (or the other way round) got notified.
author tmolitor <thilo@eightysoft.de>
date Mon, 08 May 2017 18:24:29 +0200
parents b912cb8e0b3c
children a3693e0d26b8
line wrap: on
line source

local t_insert = table.insert;

local mod_smacks = module:depends"smacks"

local function store_unacked_stanzas(session)
	local queue = session.outgoing_stanza_queue;
	local replacement_queue = {};
	session.outgoing_stanza_queue = replacement_queue;

	for _, stanza in ipairs(queue) do
		if stanza.name == "message" and stanza.attr.xmlns == nil and
				( stanza.attr.type == "chat" or ( stanza.attr.type or "normal" ) == "normal" ) then
			module:fire_event("message/offline/handle", { origin = session, stanza = stanza } )
		else
			t_insert(replacement_queue, stanza);
		end
	end
end

local handle_unacked_stanzas = mod_smacks.handle_unacked_stanzas;

local host_sessions = prosody.hosts[module.host].sessions;
mod_smacks.handle_unacked_stanzas = function (session)
	if session.username then
		local sessions = host_sessions[session.username].sessions;
		if next(sessions) == session.resource and next(sessions, session.resource) == nil then
			store_unacked_stanzas(session)
		end
	end
	return handle_unacked_stanzas(session);
end

function module.unload()
	mod_smacks.handle_unacked_stanzas = handle_unacked_stanzas;
end