Software / code / prosody
Comparison
plugins/mod_smacks.lua @ 11935:4d0d10fabb82
mod_smacks: Clean up compat code etc
Unstoppable stoppable timer compat not needed since 26f54b462601 ca 0.11.0
module:hook_stanza was renamed in 2012
No idea what was going on with the indentation and such
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 24 Nov 2021 21:27:49 +0100 |
| parent | 11934:65cdb1b21db3 |
| child | 11936:3f49c35607ca |
comparison
equal
deleted
inserted
replaced
| 11934:65cdb1b21db3 | 11935:4d0d10fabb82 |
|---|---|
| 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 | 13 |
| 14 local st = require "util.stanza"; | 14 local st = require "util.stanza"; |
| 15 local dep = require "util.dependencies"; | 15 local cache = require "util.cache"; |
| 16 local cache = dep.softreq("util.cache"); -- only available in prosody 0.10+ | |
| 17 local uuid_generate = require "util.uuid".generate; | 16 local uuid_generate = require "util.uuid".generate; |
| 18 local jid = require "util.jid"; | 17 local jid = require "util.jid"; |
| 19 | 18 |
| 20 local t_remove = table.remove; | 19 local t_remove = table.remove; |
| 21 local math_min = math.min; | 20 local math_min = math.min; |
| 42 local max_inactive_unacked_stanzas = module:get_option_number("smacks_max_inactive_unacked_stanzas", 256); | 41 local max_inactive_unacked_stanzas = module:get_option_number("smacks_max_inactive_unacked_stanzas", 256); |
| 43 local delayed_ack_timeout = module:get_option_number("smacks_max_ack_delay", 30); | 42 local delayed_ack_timeout = module:get_option_number("smacks_max_ack_delay", 30); |
| 44 local max_hibernated_sessions = module:get_option_number("smacks_max_hibernated_sessions", 10); | 43 local max_hibernated_sessions = module:get_option_number("smacks_max_hibernated_sessions", 10); |
| 45 local max_old_sessions = module:get_option_number("smacks_max_old_sessions", 10); | 44 local max_old_sessions = module:get_option_number("smacks_max_old_sessions", 10); |
| 46 local core_process_stanza = prosody.core_process_stanza; | 45 local core_process_stanza = prosody.core_process_stanza; |
| 47 local sessionmanager = require"core.sessionmanager"; | 46 local sessionmanager = require "core.sessionmanager"; |
| 48 | 47 |
| 49 assert(max_hibernated_sessions > 0, "smacks_max_hibernated_sessions must be greater than 0"); | 48 assert(max_hibernated_sessions > 0, "smacks_max_hibernated_sessions must be greater than 0"); |
| 50 assert(max_old_sessions > 0, "smacks_max_old_sessions must be greater than 0"); | 49 assert(max_old_sessions > 0, "smacks_max_old_sessions must be greater than 0"); |
| 51 | 50 |
| 52 local c2s_sessions = module:shared("/*/c2s/sessions"); | 51 local c2s_sessions = module:shared("/*/c2s/sessions"); |
| 90 end; | 89 end; |
| 91 }; | 90 }; |
| 92 end | 91 end |
| 93 local old_session_registry = init_session_cache(max_old_sessions, nil); | 92 local old_session_registry = init_session_cache(max_old_sessions, nil); |
| 94 local session_registry = init_session_cache(max_hibernated_sessions, function(resumption_token, session) | 93 local session_registry = init_session_cache(max_hibernated_sessions, function(resumption_token, session) |
| 95 if session.destroyed then return true; end -- destroyed session can always be removed from cache | 94 if session.destroyed then return true; end -- destroyed session can always be removed from cache |
| 96 session.log("warn", "User has too much hibernated sessions, removing oldest session (token: %s)", resumption_token); | 95 session.log("warn", "User has too much hibernated sessions, removing oldest session (token: %s)", resumption_token); |
| 97 -- store old session's h values on force delete | 96 -- store old session's h values on force delete |
| 98 -- save only actual h value and username/host (for security) | 97 -- save only actual h value and username/host (for security) |
| 99 old_session_registry.set(session.username, resumption_token, { | 98 old_session_registry.set(session.username, resumption_token, { |
| 100 h = session.handled_stanza_count, | 99 h = session.handled_stanza_count, |
| 101 username = session.username, | 100 username = session.username, |
| 102 host = session.host | 101 host = session.host |
| 103 }); | 102 }); |
| 104 return true; -- allow session to be removed from full cache to make room for new one | 103 return true; -- allow session to be removed from full cache to make room for new one |
| 105 end); | 104 end); |
| 106 | 105 |
| 107 local function stoppable_timer(delay, callback) | 106 local function ack_delayed(session, stanza) |
| 108 local stopped = false; | |
| 109 local timer = module:add_timer(delay, function (t) | |
| 110 if stopped then return; end | |
| 111 return callback(t); | |
| 112 end); | |
| 113 if timer and timer.stop then return timer; end -- new prosody api includes stop() function | |
| 114 return { | |
| 115 stop = function(self) stopped = true end; | |
| 116 timer; | |
| 117 }; | |
| 118 end | |
| 119 | |
| 120 local function delayed_ack_function(session, stanza) | |
| 121 -- fire event only if configured to do so and our session is not already hibernated or destroyed | 107 -- fire event only if configured to do so and our session is not already hibernated or destroyed |
| 122 if delayed_ack_timeout > 0 and session.awaiting_ack | 108 if delayed_ack_timeout > 0 and session.awaiting_ack |
| 123 and not session.hibernating and not session.destroyed then | 109 and not session.hibernating and not session.destroyed then |
| 124 session.log("debug", "Firing event 'smacks-ack-delayed', queue = %d", | 110 session.log("debug", "Firing event 'smacks-ack-delayed', queue = %d", |
| 125 session.outgoing_stanza_queue and #session.outgoing_stanza_queue or 0); | 111 session.outgoing_stanza_queue and #session.outgoing_stanza_queue or 0); |
| 160 end); | 146 end); |
| 161 | 147 |
| 162 local function request_ack_if_needed(session, force, reason, stanza) | 148 local function request_ack_if_needed(session, force, reason, stanza) |
| 163 local queue = session.outgoing_stanza_queue; | 149 local queue = session.outgoing_stanza_queue; |
| 164 local expected_h = session.last_acknowledged_stanza + #queue; | 150 local expected_h = session.last_acknowledged_stanza + #queue; |
| 165 -- session.log("debug", "*** SMACKS(1) ***: awaiting_ack=%s, hibernating=%s", tostring(session.awaiting_ack), tostring(session.hibernating)); | |
| 166 local max_unacked = max_unacked_stanzas; | 151 local max_unacked = max_unacked_stanzas; |
| 167 if session.state == "inactive" then | 152 if session.state == "inactive" then |
| 168 max_unacked = max_inactive_unacked_stanzas; | 153 max_unacked = max_inactive_unacked_stanzas; |
| 169 end | 154 end |
| 170 if session.awaiting_ack == nil and not session.hibernating then | 155 if session.awaiting_ack == nil and not session.hibernating then |
| 171 -- this check of last_requested_h prevents ack-loops if missbehaving clients report wrong | 156 -- this check of last_requested_h prevents ack-loops if missbehaving clients report wrong |
| 172 -- stanza counts. it is set when an <r> is really sent (e.g. inside timer), preventing any | 157 -- stanza counts. it is set when an <r> is really sent (e.g. inside timer), preventing any |
| 173 -- further requests until a higher h-value would be expected. | 158 -- further requests until a higher h-value would be expected. |
| 174 -- session.log("debug", "*** SMACKS(2) ***: #queue=%s, max_unacked_stanzas=%s, expected_h=%s, last_requested_h=%s", tostring(#queue), tostring(max_unacked_stanzas), tostring(expected_h), tostring(session.last_requested_h)); | |
| 175 if (#queue > max_unacked and expected_h ~= session.last_requested_h) or force then | 159 if (#queue > max_unacked and expected_h ~= session.last_requested_h) or force then |
| 176 session.log("debug", "Queuing <r> (in a moment) from %s - #queue=%d", reason, #queue); | 160 session.log("debug", "Queuing <r> (in a moment) from %s - #queue=%d", reason, #queue); |
| 177 session.awaiting_ack = false; | 161 session.awaiting_ack = false; |
| 178 session.awaiting_ack_timer = stoppable_timer(1e-06, function () | 162 session.awaiting_ack_timer = module:add_timer(1e-06, function () |
| 179 -- session.log("debug", "*** SMACKS(3) ***: awaiting_ack=%s, hibernating=%s", tostring(session.awaiting_ack), tostring(session.hibernating)); | 163 -- session.log("debug", "*** SMACKS(3) ***: awaiting_ack=%s, hibernating=%s", tostring(session.awaiting_ack), tostring(session.hibernating)); |
| 180 -- only request ack if needed and our session is not already hibernated or destroyed | 164 -- only request ack if needed and our session is not already hibernated or destroyed |
| 181 if not session.awaiting_ack and not session.hibernating and not session.destroyed then | 165 if not session.awaiting_ack and not session.hibernating and not session.destroyed then |
| 182 session.log("debug", "Sending <r> (inside timer, before send) from %s - #queue=%d", reason, #queue); | 166 session.log("debug", "Sending <r> (inside timer, before send) from %s - #queue=%d", reason, #queue); |
| 183 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks })) | 167 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks })) |
| 185 session.awaiting_ack = true; | 169 session.awaiting_ack = true; |
| 186 -- expected_h could be lower than this expression e.g. more stanzas added to the queue meanwhile) | 170 -- expected_h could be lower than this expression e.g. more stanzas added to the queue meanwhile) |
| 187 session.last_requested_h = session.last_acknowledged_stanza + #queue; | 171 session.last_requested_h = session.last_acknowledged_stanza + #queue; |
| 188 session.log("debug", "Sending <r> (inside timer, after send) from %s - #queue=%d", reason, #queue); | 172 session.log("debug", "Sending <r> (inside timer, after send) from %s - #queue=%d", reason, #queue); |
| 189 if not session.delayed_ack_timer then | 173 if not session.delayed_ack_timer then |
| 190 session.delayed_ack_timer = stoppable_timer(delayed_ack_timeout, function() | 174 session.delayed_ack_timer = module:add_timer(delayed_ack_timeout, function() |
| 191 delayed_ack_function(session, nil); -- we don't know if this is the only new stanza in the queue | 175 ack_delayed(session, nil); -- we don't know if this is the only new stanza in the queue |
| 192 end); | 176 end); |
| 193 end | 177 end |
| 194 end | 178 end |
| 195 end); | 179 end); |
| 196 end | 180 end |
| 199 -- Trigger "smacks-ack-delayed"-event if we added new (ackable) stanzas to the outgoing queue | 183 -- Trigger "smacks-ack-delayed"-event if we added new (ackable) stanzas to the outgoing queue |
| 200 -- and there isn't already a timer for this event running. | 184 -- and there isn't already a timer for this event running. |
| 201 -- If we wouldn't do this, stanzas added to the queue after the first "smacks-ack-delayed"-event | 185 -- If we wouldn't do this, stanzas added to the queue after the first "smacks-ack-delayed"-event |
| 202 -- would not trigger this event (again). | 186 -- would not trigger this event (again). |
| 203 if #queue > max_unacked and session.awaiting_ack and session.delayed_ack_timer == nil then | 187 if #queue > max_unacked and session.awaiting_ack and session.delayed_ack_timer == nil then |
| 204 session.log("debug", "Calling delayed_ack_function directly (still waiting for ack)"); | 188 session.log("debug", "Calling ack_delayed directly (still waiting for ack)"); |
| 205 delayed_ack_function(session, stanza); -- this is the only new stanza in the queue --> provide it to other modules | 189 ack_delayed(session, stanza); -- this is the only new stanza in the queue --> provide it to other modules |
| 206 end | 190 end |
| 207 end | 191 end |
| 208 | 192 |
| 209 local function outgoing_stanza_filter(stanza, session) | 193 local function outgoing_stanza_filter(stanza, session) |
| 210 -- XXX: Normally you wouldn't have to check the xmlns for a stanza as it's | 194 -- XXX: Normally you wouldn't have to check the xmlns for a stanza as it's |
| 307 session.resumption_token = resume_token; | 291 session.resumption_token = resume_token; |
| 308 end | 292 end |
| 309 (session.sends2s or session.send)(st.stanza("enabled", { xmlns = xmlns_sm, id = resume_token, resume = resume, max = tostring(resume_timeout) })); | 293 (session.sends2s or session.send)(st.stanza("enabled", { xmlns = xmlns_sm, id = resume_token, resume = resume, max = tostring(resume_timeout) })); |
| 310 return true; | 294 return true; |
| 311 end | 295 end |
| 312 module:hook_stanza(xmlns_sm2, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm2); end, 100); | 296 module:hook_tag(xmlns_sm2, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm2); end, 100); |
| 313 module:hook_stanza(xmlns_sm3, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm3); end, 100); | 297 module:hook_tag(xmlns_sm3, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm3); end, 100); |
| 314 | 298 |
| 315 module:hook_stanza("http://etherx.jabber.org/streams", "features", | 299 module:hook_tag("http://etherx.jabber.org/streams", "features", |
| 316 function (session, stanza) | 300 function (session, stanza) |
| 317 stoppable_timer(1e-6, function () | 301 module:add_timer(1e-6, function () |
| 318 if can_do_smacks(session) then | 302 if can_do_smacks(session) then |
| 319 if stanza:get_child("sm", xmlns_sm3) then | 303 if stanza:get_child("sm", xmlns_sm3) then |
| 320 session.sends2s(st.stanza("enable", sm3_attr)); | 304 session.sends2s(st.stanza("enable", sm3_attr)); |
| 321 session.smacks = xmlns_sm3; | 305 session.smacks = xmlns_sm3; |
| 322 elseif stanza:get_child("sm", xmlns_sm2) then | 306 elseif stanza:get_child("sm", xmlns_sm2) then |
| 328 wrap_session_out(session, false); | 312 wrap_session_out(session, false); |
| 329 end | 313 end |
| 330 end); | 314 end); |
| 331 end); | 315 end); |
| 332 | 316 |
| 333 function handle_enabled(session, stanza, xmlns_sm) | 317 function handle_enabled(session, stanza, xmlns_sm) -- luacheck: ignore 212/stanza |
| 334 module:log("debug", "Enabling stream management"); | 318 module:log("debug", "Enabling stream management"); |
| 335 session.smacks = xmlns_sm; | 319 session.smacks = xmlns_sm; |
| 336 | 320 |
| 337 wrap_session_in(session, false); | 321 wrap_session_in(session, false); |
| 338 | 322 |
| 339 -- FIXME Resume? | 323 -- FIXME Resume? |
| 340 | 324 |
| 341 return true; | 325 return true; |
| 342 end | 326 end |
| 343 module:hook_stanza(xmlns_sm2, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm2); end, 100); | 327 module:hook_tag(xmlns_sm2, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm2); end, 100); |
| 344 module:hook_stanza(xmlns_sm3, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm3); end, 100); | 328 module:hook_tag(xmlns_sm3, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm3); end, 100); |
| 345 | 329 |
| 346 function handle_r(origin, stanza, xmlns_sm) | 330 function handle_r(origin, stanza, xmlns_sm) -- luacheck: ignore 212/stanza |
| 347 if not origin.smacks then | 331 if not origin.smacks then |
| 348 module:log("debug", "Received ack request from non-smack-enabled session"); | 332 module:log("debug", "Received ack request from non-smack-enabled session"); |
| 349 return; | 333 return; |
| 350 end | 334 end |
| 351 module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count); | 335 module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count); |
| 356 if #origin.outgoing_stanza_queue > 0 and expected_h ~= origin.last_requested_h then | 340 if #origin.outgoing_stanza_queue > 0 and expected_h ~= origin.last_requested_h then |
| 357 request_ack_if_needed(origin, true, "piggybacked by handle_r", nil); | 341 request_ack_if_needed(origin, true, "piggybacked by handle_r", nil); |
| 358 end | 342 end |
| 359 return true; | 343 return true; |
| 360 end | 344 end |
| 361 module:hook_stanza(xmlns_sm2, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm2); end); | 345 module:hook_tag(xmlns_sm2, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm2); end); |
| 362 module:hook_stanza(xmlns_sm3, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm3); end); | 346 module:hook_tag(xmlns_sm3, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm3); end); |
| 363 | 347 |
| 364 function handle_a(origin, stanza) | 348 function handle_a(origin, stanza) |
| 365 if not origin.smacks then return; end | 349 if not origin.smacks then return; end |
| 366 origin.awaiting_ack = nil; | 350 origin.awaiting_ack = nil; |
| 367 if origin.awaiting_ack_timer then | 351 if origin.awaiting_ack_timer then |
| 385 handled_stanza_count, #queue); | 369 handled_stanza_count, #queue); |
| 386 origin.log("debug", "Client h: %d, our h: %d", tonumber(stanza.attr.h), origin.last_acknowledged_stanza); | 370 origin.log("debug", "Client h: %d, our h: %d", tonumber(stanza.attr.h), origin.last_acknowledged_stanza); |
| 387 for i=1,#queue do | 371 for i=1,#queue do |
| 388 origin.log("debug", "Q item %d: %s", i, tostring(queue[i])); | 372 origin.log("debug", "Q item %d: %s", i, tostring(queue[i])); |
| 389 end | 373 end |
| 390 origin:close{ condition = "undefined-condition"; text = "Client acknowledged more stanzas than sent by server"; }; | 374 origin:close{ condition = "undefined-condition"; text = "Client acknowledged more stanzas than sent by server"; }; |
| 391 return; | 375 return; |
| 392 end | 376 end |
| 393 | 377 |
| 394 for i=1,math_min(handled_stanza_count,#queue) do | 378 for _=1,math_min(handled_stanza_count,#queue) do |
| 395 local handled_stanza = t_remove(origin.outgoing_stanza_queue, 1); | 379 local handled_stanza = t_remove(origin.outgoing_stanza_queue, 1); |
| 396 module:fire_event("delivery/success", { session = origin, stanza = handled_stanza }); | 380 module:fire_event("delivery/success", { session = origin, stanza = handled_stanza }); |
| 397 end | 381 end |
| 398 | 382 |
| 399 origin.log("debug", "#queue = %d", #queue); | 383 origin.log("debug", "#queue = %d", #queue); |
| 400 origin.last_acknowledged_stanza = origin.last_acknowledged_stanza + handled_stanza_count; | 384 origin.last_acknowledged_stanza = origin.last_acknowledged_stanza + handled_stanza_count; |
| 401 request_ack_if_needed(origin, false, "handle_a", nil) | 385 request_ack_if_needed(origin, false, "handle_a", nil) |
| 402 return true; | 386 return true; |
| 403 end | 387 end |
| 404 module:hook_stanza(xmlns_sm2, "a", handle_a); | 388 module:hook_tag(xmlns_sm2, "a", handle_a); |
| 405 module:hook_stanza(xmlns_sm3, "a", handle_a); | 389 module:hook_tag(xmlns_sm3, "a", handle_a); |
| 406 | 390 |
| 407 --TODO: Optimise... incoming stanzas should be handled by a per-session | 391 --TODO: Optimise... incoming stanzas should be handled by a per-session |
| 408 -- function that has a counter as an upvalue (no table indexing for increments, | 392 -- function that has a counter as an upvalue (no table indexing for increments, |
| 409 -- and won't slow non-198 sessions). We can also then remove the .handled flag | 393 -- and won't slow non-198 sessions). We can also then remove the .handled flag |
| 410 -- on stanzas | 394 -- on stanzas |
| 448 if stanza.name == "message" and stanza.attr.xmlns == nil and | 432 if stanza.name == "message" and stanza.attr.xmlns == nil and |
| 449 ( stanza.attr.type == "chat" or ( stanza.attr.type or "normal" ) == "normal" ) then | 433 ( stanza.attr.type == "chat" or ( stanza.attr.type or "normal" ) == "normal" ) then |
| 450 -- don't store messages in offline store if they are mam results | 434 -- don't store messages in offline store if they are mam results |
| 451 local mam_result = stanza:get_child("result", xmlns_mam2); | 435 local mam_result = stanza:get_child("result", xmlns_mam2); |
| 452 if mam_result ~= nil then | 436 if mam_result ~= nil then |
| 453 return true; -- stanza already "handled", don't send an error and don't add it to offline storage | 437 return true; -- stanza already "handled", don't send an error and don't add it to offline storage |
| 454 end | 438 end |
| 455 -- do nothing here for normal messages and don't send out "message delivery errors", | 439 -- do nothing here for normal messages and don't send out "message delivery errors", |
| 456 -- because messages are already in MAM at this point (no need to frighten users) | 440 -- because messages are already in MAM at this point (no need to frighten users) |
| 457 local stanza_id = get_stanza_id(stanza, jid.bare(session.full_jid)); | 441 local stanza_id = get_stanza_id(stanza, jid.bare(session.full_jid)); |
| 458 if session.mam_requested and stanza_id ~= nil then | 442 if session.mam_requested and stanza_id ~= nil then |
| 459 session.log("debug", "mod_smacks delivery/failure returning true for mam-handled stanza: mam-archive-id=%s", tostring(stanza_id)); | 443 session.log("debug", "mod_smacks delivery/failure returning true for mam-handled stanza: mam-archive-id=%s", tostring(stanza_id)); |
| 460 return true; -- stanza handled, don't send an error | 444 return true; -- stanza handled, don't send an error |
| 461 end | 445 end |
| 462 -- store message in offline store, if this client does not use mam *and* was the last client online | 446 -- store message in offline store, if this client does not use mam *and* was the last client online |
| 463 local sessions = prosody.hosts[module.host].sessions[session.username] and | 447 local sessions = prosody.hosts[module.host].sessions[session.username] and |
| 464 prosody.hosts[module.host].sessions[session.username].sessions or nil; | 448 prosody.hosts[module.host].sessions[session.username].sessions or nil; |
| 465 if sessions and next(sessions) == session.resource and next(sessions, session.resource) == nil then | 449 if sessions and next(sessions) == session.resource and next(sessions, session.resource) == nil then |
| 466 local ok = module:fire_event("message/offline/handle", { origin = session, username = session.username, stanza = stanza }); | 450 local ok = module:fire_event("message/offline/handle", { origin = session, username = session.username, stanza = stanza }); |
| 467 session.log("debug", "mod_smacks delivery/failuere returning %s for offline-handled stanza", tostring(ok)); | 451 session.log("debug", "mod_smacks delivery/failuere returning %s for offline-handled stanza", tostring(ok)); |
| 468 return ok; -- if stanza was handled, don't send an error | 452 return ok; -- if stanza was handled, don't send an error |
| 469 end | 453 end |
| 470 end | 454 end |
| 471 end | 455 end |
| 472 end); | 456 end); |
| 473 | 457 |
| 474 module:hook("pre-resource-unbind", function (event) | 458 module:hook("pre-resource-unbind", function (event) |
| 475 local session, err = event.session, event.error; | 459 local session = event.session; |
| 476 if session.smacks then | 460 if session.smacks then |
| 477 if not session.resumption_token then | 461 if not session.resumption_token then |
| 478 local queue = session.outgoing_stanza_queue; | 462 local queue = session.outgoing_stanza_queue; |
| 479 if #queue > 0 then | 463 if #queue > 0 then |
| 480 session.log("debug", "Destroying session with %d unacked stanzas", #queue); | 464 session.log("debug", "Destroying session with %d unacked stanzas", #queue); |
| 490 session.log("debug", "mod_smacks hibernation timeout reached..."); | 474 session.log("debug", "mod_smacks hibernation timeout reached..."); |
| 491 -- We need to check the current resumption token for this resource | 475 -- We need to check the current resumption token for this resource |
| 492 -- matches the smacks session this timer is for in case it changed | 476 -- matches the smacks session this timer is for in case it changed |
| 493 -- (for example, the client may have bound a new resource and | 477 -- (for example, the client may have bound a new resource and |
| 494 -- started a new smacks session, or not be using smacks) | 478 -- started a new smacks session, or not be using smacks) |
| 495 local curr_session = full_sessions[session.full_jid]; | 479 local curr_session = prosody.full_sessions[session.full_jid]; |
| 496 if session.destroyed then | 480 if session.destroyed then |
| 497 session.log("debug", "The session has already been destroyed"); | 481 session.log("debug", "The session has already been destroyed"); |
| 498 elseif curr_session and curr_session.resumption_token == resumption_token | 482 elseif curr_session and curr_session.resumption_token == resumption_token |
| 499 -- Check the hibernate time still matches what we think it is, | 483 -- Check the hibernate time still matches what we think it is, |
| 500 -- otherwise the session resumed and re-hibernated. | 484 -- otherwise the session resumed and re-hibernated. |
| 507 if session.push_identifier ~= nil and not session.first_hibernated_push then | 491 if session.push_identifier ~= nil and not session.first_hibernated_push then |
| 508 session.log("debug", "No push happened since hibernation started, hibernating session for up to %d extra seconds", resume_timeout); | 492 session.log("debug", "No push happened since hibernation started, hibernating session for up to %d extra seconds", resume_timeout); |
| 509 return resume_timeout; | 493 return resume_timeout; |
| 510 end | 494 end |
| 511 if session.push_identifier ~= nil and current_time-timeout_start < resume_timeout then | 495 if session.push_identifier ~= nil and current_time-timeout_start < resume_timeout then |
| 512 session.log("debug", "A push happened since hibernation started, hibernating session for up to %d extra seconds", resume_timeout-(current_time-timeout_start)); | 496 session.log("debug", "A push happened since hibernation started, hibernating session for up to %d extra seconds", |
| 513 return resume_timeout-(current_time-timeout_start); -- time left to wait | 497 resume_timeout - (current_time - timeout_start)); |
| 498 return resume_timeout-(current_time-timeout_start); -- time left to wait | |
| 514 end | 499 end |
| 515 session.log("debug", "Destroying session for hibernating too long"); | 500 session.log("debug", "Destroying session for hibernating too long"); |
| 516 session_registry.set(session.username, session.resumption_token, nil); | 501 session_registry.set(session.username, session.resumption_token, nil); |
| 517 -- save only actual h value and username/host (for security) | 502 -- save only actual h value and username/host (for security) |
| 518 old_session_registry.set(session.username, session.resumption_token, { | 503 old_session_registry.set(session.username, session.resumption_token, { |
| 624 session.log("debug", "resending all unacked stanzas that are still queued after resume, #queue = %d", #queue); | 609 session.log("debug", "resending all unacked stanzas that are still queued after resume, #queue = %d", #queue); |
| 625 for i=1,#queue do | 610 for i=1,#queue do |
| 626 session.send(queue[i]); | 611 session.send(queue[i]); |
| 627 end | 612 end |
| 628 session.log("debug", "all stanzas resent, now disabling send() in this migrated session, #queue = %d", #queue); | 613 session.log("debug", "all stanzas resent, now disabling send() in this migrated session, #queue = %d", #queue); |
| 629 function session.send(stanza) | 614 function session.send(stanza) -- luacheck: ignore 432 |
| 630 migrated_session_log("error", "Tried to send stanza on old session migrated by smacks resume (maybe there is a bug?): %s", tostring(stanza)); | 615 migrated_session_log("error", "Tried to send stanza on old session migrated by smacks resume (maybe there is a bug?): %s", tostring(stanza)); |
| 631 return false; | 616 return false; |
| 632 end | 617 end |
| 633 module:fire_event("smacks-hibernation-end", {origin = session, resumed = original_session, queue = queue}); | 618 module:fire_event("smacks-hibernation-end", {origin = session, resumed = original_session, queue = queue}); |
| 634 request_ack_if_needed(original_session, true, "handle_resume", nil); | 619 request_ack_if_needed(original_session, true, "handle_resume", nil); |
| 639 session.send(st.stanza("failed", { xmlns = xmlns_sm }) | 624 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
| 640 :tag("not-authorized", { xmlns = xmlns_errors })); | 625 :tag("not-authorized", { xmlns = xmlns_errors })); |
| 641 end | 626 end |
| 642 return true; | 627 return true; |
| 643 end | 628 end |
| 644 module:hook_stanza(xmlns_sm2, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm2); end); | 629 module:hook_tag(xmlns_sm2, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm2); end); |
| 645 module:hook_stanza(xmlns_sm3, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm3); end); | 630 module:hook_tag(xmlns_sm3, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm3); end); |
| 646 | 631 |
| 647 module:hook("csi-client-active", function (event) | 632 module:hook("csi-client-active", function (event) |
| 648 if event.origin.smacks then | 633 if event.origin.smacks then |
| 649 request_ack_if_needed(event.origin, true, "csi-active", nil); | 634 request_ack_if_needed(event.origin, true, "csi-active", nil); |
| 650 end | 635 end |
| 676 end | 661 end |
| 677 session.log("debug", "Sending <r> (read timeout)"); | 662 session.log("debug", "Sending <r> (read timeout)"); |
| 678 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks })); | 663 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks })); |
| 679 session.awaiting_ack = true; | 664 session.awaiting_ack = true; |
| 680 if not session.delayed_ack_timer then | 665 if not session.delayed_ack_timer then |
| 681 session.delayed_ack_timer = stoppable_timer(delayed_ack_timeout, function() | 666 session.delayed_ack_timer = module:add_timer(delayed_ack_timeout, function() |
| 682 delayed_ack_function(session, nil); | 667 ack_delayed(session, nil); |
| 683 end); | 668 end); |
| 684 end | 669 end |
| 685 return true; | 670 return true; |
| 686 end | 671 end |
| 687 end | 672 end |