Diff

plugins/mod_smacks.lua @ 11977:9f7a6f7d13de

mod_smacks: Factor out formatting of 'h' value
author Kim Alvefur <zash@zash.se>
date Wed, 01 Dec 2021 16:20:40 +0100
parent 11976:10cdfb94f1cc
child 11978:628374809421
line wrap: on
line diff
--- a/plugins/mod_smacks.lua	Wed Dec 01 15:27:55 2021 +0100
+++ b/plugins/mod_smacks.lua	Wed Dec 01 16:20:40 2021 +0100
@@ -50,6 +50,8 @@
 
 local c2s_sessions = module:shared("/*/c2s/sessions");
 
+local function format_h(h) if h then return string.format("%d", h) end end
+
 local function init_session_cache(max_entries, evict_callback)
 	-- use per user limited cache for prosody >= 0.10
 	local stores = {};
@@ -253,7 +255,7 @@
 	if session.smacks and session.conn and session.handled_stanza_count then
 		(session.sends2s or session.send)(st.stanza("a", {
 			xmlns = session.smacks;
-			h = string.format("%d", session.handled_stanza_count);
+			h = format_h(session.handled_stanza_count);
 		}));
 	end
 end);
@@ -339,7 +341,7 @@
 	end
 	module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count);
 	-- Reply with <a>
-	(origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = string.format("%d", origin.handled_stanza_count) }));
+	(origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = format_h(origin.handled_stanza_count) }));
 	-- piggyback our own ack request if needed (see request_ack_if_needed() for explanation of last_requested_h)
 	request_ack_now_if_needed(origin, false, "piggybacked by handle_r", nil);
 	return true;
@@ -559,7 +561,7 @@
 		if old_session and session.username == old_session.username
 		and session.host == old_session.host
 		and old_session.h then
-			session.send(st.stanza("failed", { xmlns = xmlns_sm, h = string.format("%d", old_session.h) })
+			session.send(st.stanza("failed", { xmlns = xmlns_sm, h = format_h(old_session.h) })
 				:tag("item-not-found", { xmlns = xmlns_errors })
 			);
 		else
@@ -598,7 +600,7 @@
 		c2s_sessions[session.conn] = original_session;
 
 		original_session.send(st.stanza("resumed", { xmlns = xmlns_sm,
-			h = string.format("%d", original_session.handled_stanza_count), previd = id }));
+			h = format_h(original_session.handled_stanza_count), previd = id }));
 
 		-- Fake an <a> with the h of the <resume/> from the client
 		original_session:dispatch_stanza(st.stanza("a", { xmlns = xmlns_sm,