Diff

mod_csi_battery_saver/mod_csi_battery_saver.lua @ 2741:69248dcd7cff

mod_csi_battery_saver: Fix interaction with smacks hibernation This should fix a bug that caused the module to be deactivated on smacks resume. Also update the readme accordingly,
author tmolitor <thilo@eightysoft.de>
date Thu, 17 Aug 2017 21:21:23 +0200
parent 2737:f43c77c69a8a
child 2742:2e30bb3a10d5
line wrap: on
line diff
--- a/mod_csi_battery_saver/mod_csi_battery_saver.lua	Tue Aug 15 21:14:55 2017 +0200
+++ b/mod_csi_battery_saver/mod_csi_battery_saver.lua	Thu Aug 17 21:21:23 2017 +0200
@@ -9,7 +9,7 @@
 local jid = require "util.jid";
 local new_queue = require "util.queue".new;
 local datetime = require "util.datetime";
-local clone = require "util.stanza".clone;
+local st = require "util.stanza";
 
 local xmlns_delay = "urn:xmpp:delay";
 
@@ -57,7 +57,7 @@
 	end
 	local push = q.push;
 	function q:push(item)
-		local ok = push(self, clone(item));
+		local ok = push(self, item);
 		if not ok then
 			q:flush();
 			output(item, self);
@@ -95,7 +95,7 @@
 
 local function is_important(stanza, session)
 	local st_name = stanza and stanza.name or nil;
-	if not st_name then return false; end
+	if not st_name then return true; end	-- nonzas are always important
 	if st_name == "presence" then
 		-- TODO check for MUC status codes?
 		return false;
@@ -148,6 +148,7 @@
 module:hook("csi-client-inactive", function (event)
 	local session = event.origin;
 	if session.pump then
+		session.log("debug", "mod_csi_battery_saver(%s): Client is inactive, buffering unimportant stanzas", id);
 		session.pump:pause();
 	else
 		session.log("debug", "mod_csi_battery_saver(%s): Client is inactive the first time, initializing module for this session", id);
@@ -156,19 +157,18 @@
 		session.pump = pump;
 		session._pump_orig_send = session.send;
 		function session.send(stanza)
-			session.log("debug", "mod_csi_battery_saver(%s): Got stanza: <%s>", id, tostring(stanza.name));
+			session.log("debug", "mod_csi_battery_saver(%s): Got stanza: <%s>", id, tostring(stanza.name or stanza));
 			local important = is_important(stanza, session);
 			-- add delay stamp to unimportant (buffered) stanzas that can/need be stamped
 			if not important and is_stamp_needed(stanza, session) then stanza = add_stamp(stanza, session); end
 			pump:push(stanza);
 			if important then
-				session.log("debug", "mod_csi_battery_saver(%s): Encountered important stanza, flushing buffer: <%s>", id, tostring(stanza.name));
+				session.log("debug", "mod_csi_battery_saver(%s): Encountered important stanza, flushing buffer: <%s>", id, tostring(stanza.name or stanza));
 				pump:flush();
 			end
 			return true;
 		end
 	end
-	session.log("debug", "mod_csi_battery_saver(%s): Client is inactive, buffering unimportant stanzas", id);
 end);
 
 module:hook("csi-client-active", function (event)
@@ -179,17 +179,28 @@
 	end
 end);
 
+-- clean up this session
+local function remove_pump(session)
+	if session.pump then
+		session.log("debug", "mod_csi_battery_saver(%s): Flushing buffer and restoring to original session.send()", id);
+		session.pump:flush();
+		session.send = session._pump_orig_send;
+		session.pump = nil;
+		session._pump_orig_send = nil;
+	end
+end
+
+-- clean up this session on hibernation start
+module:hook("smacks-hibernation-start", function (event)
+	remove_pump(event.origin);
+end);
+
 function module.unload()
 	module:log("info", "%s: Unloading module, flushing all buffers", id);
 	local host_sessions = prosody.hosts[module.host].sessions;
 	for _, user in pairs(host_sessions) do
 		for _, session in pairs(user.sessions) do
-			if session.pump then
-				session.pump:flush();
-				session.send = session._pump_orig_send;
-				session.pump = nil;
-				session._pump_orig_send = nil;
-			end
+			remove_pump(session);
 		end
 	end
 end