plugins.smacks: Restructure events to avoid double hooks after (failed) resumption

Sat, 19 Feb 2022 15:52:10 +0100

author
Kim Alvefur <zash@zash.se>
date
Sat, 19 Feb 2022 15:52:10 +0100
changeset 449
c720f331327c
parent 448
6d97b42bb1b4
child 450
e72deac76e0e

plugins.smacks: Restructure events to avoid double hooks after (failed) resumption

plugins/smacks.lua file | annotate | diff | comparison | revisions
--- 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

mercurial