Diff

plugins/smacks.lua @ 449:c720f331327c

plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
author Kim Alvefur <zash@zash.se>
date Sat, 19 Feb 2022 15:52:10 +0100
parent 448:6d97b42bb1b4
child 450:e72deac76e0e
line wrap: on
line diff
--- a/plugins/smacks.lua	Sat Feb 19 15:43:47 2022 +0100
+++ b/plugins/smacks.lua	Sat Feb 19 15:52:10 2022 +0100
@@ -15,7 +15,7 @@
 
 	-- Catch incoming stanzas
 	local function incoming_stanza(stanza)
-		if stanza.attr.xmlns == "jabber:client" or not stanza.attr.xmlns then
+		if stream.smacks and (stanza.attr.xmlns == "jabber:client" or not stanza.attr.xmlns) then
 			handled_stanza_count = handled_stanza_count + 1;
 			stream:debug("Increasing handled stanzas to %d for %s", handled_stanza_count, stanza:top_tag());
 		end
@@ -24,7 +24,7 @@
 	-- Catch outgoing stanzas
 	local function outgoing_stanza(stanza)
 		-- NOTE: This will not behave nice if stanzas are serialized before this point
-		if stanza.name and not stanza.attr.xmlns then
+		if stream.smacks and (stanza.name and not stanza.attr.xmlns) then
 			-- serialize stanzas in order to bypass this on resumption
 			outgoing_queue[#outgoing_queue+1] = tostring(stanza);
 			last_stanza_time = now();
@@ -64,7 +64,6 @@
 	-- Graceful shutdown
 	local function on_close()
 		stream.resumption_token = nil;
-		stream:unhook("disconnected", on_disconnect);
 	end
 
 	local function handle_sm_command(stanza)
@@ -88,8 +87,6 @@
 
 			if stanza.attr.id then
 				stream.resumption_token = stanza.attr.id;
-				stream:hook("closed", on_close, 100);
-				stream:hook("disconnected", on_disconnect, 100);
 			end
 		elseif stanza.name == "resumed" then
 			stream.pre_smacks_features = nil;
@@ -128,15 +125,11 @@
 	end
 
 	local function on_bind_success()
-		if not stream.smacks then
+		if stream.stream_management_supported and not stream.smacks then
 			--stream:unhook("bind-success", on_bind_success);
 			stream:debug("smacks: sending enable");
 			stream:send(verse.stanza("enable", { xmlns = xmlns_sm, resume = "true" }));
 			stream.smacks = true;
-
-			-- Catch stanzas
-			stream:hook("stanza", incoming_stanza);
-			stream:hook("outgoing", outgoing_stanza);
 		end
 	end
 
@@ -150,12 +143,20 @@
 					h = tostring(handled_stanza_count), previd = stream.resumption_token }));
 				return true;
 			else
-				stream:hook("bind-success", on_bind_success, 1);
 			end
 		end
 	end
 
 	stream:hook("stream-features", on_features, 250);
 	stream:hook("stream/"..xmlns_sm, handle_sm_command);
+	stream:hook("bind-success", on_bind_success, 1);
+
+	-- Catch stanzas
+	stream:hook("stanza", incoming_stanza);
+	stream:hook("outgoing", outgoing_stanza);
+
+	stream:hook("closed", on_close, 100);
+	stream:hook("disconnected", on_disconnect, 100);
+
 	--stream:hook("ready", on_stream_ready, 500);
 end