Comparison

plugins/mod_smacks.lua @ 12471:a3b12eeedd4b

mod_smacks: Improve activation of smacks on outgoing s2s Using a timer was a hack to get around that stream features are not available at the right time and sendq stanzas were stored as strings so could not be counted properly. The later has now been fixed and the former is fixed by recording the relevant stream feature on the session so that the correct version of XEP-0198 can be activated once the connection has been authenticated and is ready to start.
author Kim Alvefur <zash@zash.se>
date Sun, 24 Apr 2022 16:17:32 +0200
parent 12272:fe0f5c47fda3
child 12504:c589874fe348
comparison
equal deleted inserted replaced
12470:80f3123053e2 12471:a3b12eeedd4b
1 -- XEP-0198: Stream Management for Prosody IM 1 -- XEP-0198: Stream Management for Prosody IM
2 -- 2 --
3 -- Copyright (C) 2010-2015 Matthew Wild 3 -- Copyright (C) 2010-2015 Matthew Wild
4 -- Copyright (C) 2010 Waqas Hussain 4 -- Copyright (C) 2010 Waqas Hussain
5 -- Copyright (C) 2012-2021 Kim Alvefur 5 -- Copyright (C) 2012-2022 Kim Alvefur
6 -- Copyright (C) 2012 Thijs Alkemade 6 -- Copyright (C) 2012 Thijs Alkemade
7 -- Copyright (C) 2014 Florian Zeitz 7 -- Copyright (C) 2014 Florian Zeitz
8 -- Copyright (C) 2016-2020 Thilo Molitor 8 -- Copyright (C) 2016-2020 Thilo Molitor
9 -- 9 --
10 -- This project is MIT/X11 licensed. Please see the 10 -- This project is MIT/X11 licensed. Please see the
11 -- COPYING file in the source package for more information. 11 -- COPYING file in the source package for more information.
12 -- 12 --
13 -- TODO unify sendq and smqueue
13 14
14 local tonumber = tonumber; 15 local tonumber = tonumber;
15 local tostring = tostring; 16 local tostring = tostring;
16 local os_time = os.time; 17 local os_time = os.time;
17 18
320 return true; 321 return true;
321 end 322 end
322 module:hook_tag(xmlns_sm2, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm2); end, 100); 323 module:hook_tag(xmlns_sm2, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm2); end, 100);
323 module:hook_tag(xmlns_sm3, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm3); end, 100); 324 module:hook_tag(xmlns_sm3, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm3); end, 100);
324 325
325 module:hook_tag("http://etherx.jabber.org/streams", "features", 326 module:hook_tag("http://etherx.jabber.org/streams", "features", function(session, stanza)
326 function (session, stanza) 327 if can_do_smacks(session) then
327 -- Needs to be done after flushing sendq since those aren't stored as 328 session.smacks_feature = stanza:get_child("sm", xmlns_sm3) or stanza:get_child("sm", xmlns_sm2);
328 -- stanzas and counting them is weird. 329 end
329 -- TODO unify sendq and smqueue 330 end);
330 timer.add_task(1e-6, function () 331
331 if can_do_smacks(session) then 332 module:hook("s2sout-established", function (event)
332 if stanza:get_child("sm", xmlns_sm3) then 333 local session = event.session;
333 session.sends2s(st.stanza("enable", sm3_attr)); 334 if not session.smacks_feature then return end
334 session.smacks = xmlns_sm3; 335
335 elseif stanza:get_child("sm", xmlns_sm2) then 336 session.smacks = session.smacks_feature.attr.xmlns;
336 session.sends2s(st.stanza("enable", sm2_attr)); 337 session.sends2s(st.stanza("enable", { xmlns = session.smacks }));
337 session.smacks = xmlns_sm2; 338 wrap_session_out(session, false);
338 else 339 end);
339 return;
340 end
341 wrap_session_out(session, false);
342 end
343 end);
344 end);
345 340
346 function handle_enabled(session, stanza, xmlns_sm) -- luacheck: ignore 212/stanza 341 function handle_enabled(session, stanza, xmlns_sm) -- luacheck: ignore 212/stanza
347 module:log("debug", "Enabling stream management"); 342 module:log("debug", "Enabling stream management");
348 session.smacks = xmlns_sm; 343 session.smacks = xmlns_sm;
349 344