Comparison

plugins/mod_smacks.lua @ 11977:9f7a6f7d13de

mod_smacks: Factor out formatting of 'h' value
author Kim Alvefur <zash@zash.se>
date Wed, 01 Dec 2021 16:20:40 +0100
parent 11976:10cdfb94f1cc
child 11978:628374809421
comparison
equal deleted inserted replaced
11976:10cdfb94f1cc 11977:9f7a6f7d13de
47 47
48 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");
49 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");
50 50
51 local c2s_sessions = module:shared("/*/c2s/sessions"); 51 local c2s_sessions = module:shared("/*/c2s/sessions");
52
53 local function format_h(h) if h then return string.format("%d", h) end end
52 54
53 local function init_session_cache(max_entries, evict_callback) 55 local function init_session_cache(max_entries, evict_callback)
54 -- use per user limited cache for prosody >= 0.10 56 -- use per user limited cache for prosody >= 0.10
55 local stores = {}; 57 local stores = {};
56 return { 58 return {
251 end 253 end
252 -- send out last ack as per revision 1.5.2 of XEP-0198 254 -- send out last ack as per revision 1.5.2 of XEP-0198
253 if session.smacks and session.conn and session.handled_stanza_count then 255 if session.smacks and session.conn and session.handled_stanza_count then
254 (session.sends2s or session.send)(st.stanza("a", { 256 (session.sends2s or session.send)(st.stanza("a", {
255 xmlns = session.smacks; 257 xmlns = session.smacks;
256 h = string.format("%d", session.handled_stanza_count); 258 h = format_h(session.handled_stanza_count);
257 })); 259 }));
258 end 260 end
259 end); 261 end);
260 262
261 local function wrap_session_in(session, resume) 263 local function wrap_session_in(session, resume)
337 module:log("debug", "Received ack request from non-smack-enabled session"); 339 module:log("debug", "Received ack request from non-smack-enabled session");
338 return; 340 return;
339 end 341 end
340 module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count); 342 module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count);
341 -- Reply with <a> 343 -- Reply with <a>
342 (origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = string.format("%d", origin.handled_stanza_count) })); 344 (origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = format_h(origin.handled_stanza_count) }));
343 -- piggyback our own ack request if needed (see request_ack_if_needed() for explanation of last_requested_h) 345 -- piggyback our own ack request if needed (see request_ack_if_needed() for explanation of last_requested_h)
344 request_ack_now_if_needed(origin, false, "piggybacked by handle_r", nil); 346 request_ack_now_if_needed(origin, false, "piggybacked by handle_r", nil);
345 return true; 347 return true;
346 end 348 end
347 module:hook_tag(xmlns_sm2, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm2); end); 349 module:hook_tag(xmlns_sm2, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm2); end);
557 session.log("debug", "Tried to resume non-existent session with id %s", id); 559 session.log("debug", "Tried to resume non-existent session with id %s", id);
558 local old_session = old_session_registry.get(session.username, id); 560 local old_session = old_session_registry.get(session.username, id);
559 if old_session and session.username == old_session.username 561 if old_session and session.username == old_session.username
560 and session.host == old_session.host 562 and session.host == old_session.host
561 and old_session.h then 563 and old_session.h then
562 session.send(st.stanza("failed", { xmlns = xmlns_sm, h = string.format("%d", old_session.h) }) 564 session.send(st.stanza("failed", { xmlns = xmlns_sm, h = format_h(old_session.h) })
563 :tag("item-not-found", { xmlns = xmlns_errors }) 565 :tag("item-not-found", { xmlns = xmlns_errors })
564 ); 566 );
565 else 567 else
566 session.send(st.stanza("failed", { xmlns = xmlns_sm }) 568 session.send(st.stanza("failed", { xmlns = xmlns_sm })
567 :tag("item-not-found", { xmlns = xmlns_errors }) 569 :tag("item-not-found", { xmlns = xmlns_errors })
596 original_session.stream:set_session(original_session); 598 original_session.stream:set_session(original_session);
597 -- Similar for connlisteners 599 -- Similar for connlisteners
598 c2s_sessions[session.conn] = original_session; 600 c2s_sessions[session.conn] = original_session;
599 601
600 original_session.send(st.stanza("resumed", { xmlns = xmlns_sm, 602 original_session.send(st.stanza("resumed", { xmlns = xmlns_sm,
601 h = string.format("%d", original_session.handled_stanza_count), previd = id })); 603 h = format_h(original_session.handled_stanza_count), previd = id }));
602 604
603 -- Fake an <a> with the h of the <resume/> from the client 605 -- Fake an <a> with the h of the <resume/> from the client
604 original_session:dispatch_stanza(st.stanza("a", { xmlns = xmlns_sm, 606 original_session:dispatch_stanza(st.stanza("a", { xmlns = xmlns_sm,
605 h = stanza.attr.h })); 607 h = stanza.attr.h }));
606 608