# HG changeset patch # User Kim Alvefur # Date 1645282330 -3600 # Node ID c720f331327c4f4fcbbbc4e54d73d4a55261458d # Parent 6d97b42bb1b46310c6a1b0dfb33add361d64e695 plugins.smacks: Restructure events to avoid double hooks after (failed) resumption diff -r 6d97b42bb1b4 -r c720f331327c plugins/smacks.lua --- 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