changeset 1702:725ebd494000

mod_smacks_offline: Monkeypatch mod_smacks to send unacked messages to offline storage IFF there was only one session
author Kim Alvefur <zash@zash.se>
date Wed, 06 May 2015 00:55:00 +0200
parents 9b429fc9e8a0
children a53385a78a64
files mod_smacks_offline/mod_smacks_offline.lua
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_smacks_offline/mod_smacks_offline.lua	Wed May 06 00:55:00 2015 +0200
@@ -0,0 +1,33 @@
+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)
+	local sessions = host_sessions[session.username].sessions;
+	if next(sessions) == session.resource and next(sessions, session.resource) == nil then
+		store_unacked_stanzas(session)
+	end
+	return handle_unacked_stanzas(session);
+end
+
+function module.unload()
+	mod_smacks.handle_unacked_stanzas = handle_unacked_stanzas;
+end