Comparison

mod_cloud_notify/mod_cloud_notify.lua @ 4968:487f1eb829cf

mod_cloud_notify: Compat for prosody 0.12
author tmolitor <thilo@eightysoft.de>
date Sat, 02 Jul 2022 02:29:03 +0200
parent 4827:fe5303da99cb
child 4980:da151f9af861
comparison
equal deleted inserted replaced
4967:1e8381f0d0a8 4968:487f1eb829cf
9 local jid = require"util.jid"; 9 local jid = require"util.jid";
10 local dataform = require"util.dataforms".new; 10 local dataform = require"util.dataforms".new;
11 local hashes = require"util.hashes"; 11 local hashes = require"util.hashes";
12 local random = require"util.random"; 12 local random = require"util.random";
13 local cache = require"util.cache"; 13 local cache = require"util.cache";
14 local watchdog = require "util.watchdog";
14 15
15 local xmlns_push = "urn:xmpp:push:0"; 16 local xmlns_push = "urn:xmpp:push:0";
16 17
17 -- configuration 18 -- configuration
18 local include_body = module:get_option_boolean("push_notification_with_body", false); 19 local include_body = module:get_option_boolean("push_notification_with_body", false);
19 local include_sender = module:get_option_boolean("push_notification_with_sender", false); 20 local include_sender = module:get_option_boolean("push_notification_with_sender", false);
20 local max_push_errors = module:get_option_number("push_max_errors", 16); 21 local max_push_errors = module:get_option_number("push_max_errors", 16);
21 local max_push_devices = module:get_option_number("push_max_devices", 5); 22 local max_push_devices = module:get_option_number("push_max_devices", 5);
22 local dummy_body = module:get_option_string("push_notification_important_body", "New Message!"); 23 local dummy_body = module:get_option_string("push_notification_important_body", "New Message!");
24 local extended_hibernation_timeout = module:get_option_number("push_max_hibernation_timeout", 72*24*3600); -- use same timeout like ejabberd
23 25
24 local host_sessions = prosody.hosts[module.host].sessions; 26 local host_sessions = prosody.hosts[module.host].sessions;
25 local push_errors = module:shared("push_errors"); 27 local push_errors = module:shared("push_errors");
26 local id2node = {}; 28 local id2node = {};
27 local id2identifier = {}; 29 local id2identifier = {};
96 for _, session in pairs(host_sessions[node].sessions) do 98 for _, session in pairs(host_sessions[node].sessions) do
97 if session.push_identifier == push_identifier then 99 if session.push_identifier == push_identifier then
98 session.push_identifier = nil; 100 session.push_identifier = nil;
99 session.push_settings = nil; 101 session.push_settings = nil;
100 session.first_hibernated_push = nil; 102 session.first_hibernated_push = nil;
103 -- check for prosody 0.12 mod_smacks
104 if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
105 -- restore old smacks watchdog
106 session.hibernating_watchdog:cancel();
107 session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
108 end
101 end 109 end
102 end 110 end
103 end 111 end
104 -- save changed global config 112 -- save changed global config
105 changed = true; 113 changed = true;
228 origin.log("info", "Push notifications disabled (%s)", tostring(key)); 236 origin.log("info", "Push notifications disabled (%s)", tostring(key));
229 if origin.push_identifier == key then 237 if origin.push_identifier == key then
230 origin.push_identifier = nil; 238 origin.push_identifier = nil;
231 origin.push_settings = nil; 239 origin.push_settings = nil;
232 origin.first_hibernated_push = nil; 240 origin.first_hibernated_push = nil;
241 -- check for prosody 0.12 mod_smacks
242 if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
243 -- restore old smacks watchdog
244 session.hibernating_watchdog:cancel();
245 session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
246 end
233 end 247 end
234 user_push_services[key] = nil; 248 user_push_services[key] = nil;
235 push_errors[key] = nil; 249 push_errors[key] = nil;
236 for stanza_id, identifier in pairs(id2identifier) do 250 for stanza_id, identifier in pairs(id2identifier) do
237 if identifier == key then 251 if identifier == key then
444 -- if the message was important (e.g. had a last-message-body field) OR if we treat all pushes equally, 458 -- if the message was important (e.g. had a last-message-body field) OR if we treat all pushes equally,
445 -- then record the time of first push in the session for the smack module which will extend its hibernation 459 -- then record the time of first push in the session for the smack module which will extend its hibernation
446 -- timeout based on the value of session.first_hibernated_push 460 -- timeout based on the value of session.first_hibernated_push
447 if not dummy_body or (dummy_body and is_important(stanza)) then 461 if not dummy_body or (dummy_body and is_important(stanza)) then
448 session.first_hibernated_push = os_time(); 462 session.first_hibernated_push = os_time();
463 -- check for prosody 0.12 mod_smacks
464 if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
465 -- restore old smacks watchdog (--> the start of our original timeout will be delayed until first push)
466 session.hibernating_watchdog:cancel();
467 session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
468 end
449 end 469 end
450 end 470 end
451 session.log("debug", "Cloud handle_notify_request() > 0, not notifying for other %s queued stanzas of type %s", queue_type, stanza_type); 471 session.log("debug", "Cloud handle_notify_request() > 0, not notifying for other %s queued stanzas of type %s", queue_type, stanza_type);
452 notified[stanza_type] = true 472 notified[stanza_type] = true
453 end 473 end
492 -- smacks hibernation is started 512 -- smacks hibernation is started
493 local function hibernate_session(event) 513 local function hibernate_session(event)
494 local session = event.origin; 514 local session = event.origin;
495 local queue = event.queue; 515 local queue = event.queue;
496 session.first_hibernated_push = nil; 516 session.first_hibernated_push = nil;
517 if session.hibernating_watchdog then -- check for prosody 0.12 mod_smacks
518 -- save old watchdog callback and timeout
519 session.original_smacks_callback = session.hibernating_watchdog.callback;
520 session.original_smacks_timeout = session.hibernating_watchdog.timeout;
521 -- cancel old watchdog and create a new watchdog with extended timeout
522 session.hibernating_watchdog:cancel();
523 session.hibernating_watchdog = watchdog.new(extended_hibernation_timeout, function()
524 session.log("debug", "Push-extended smacks watchdog triggered");
525 if session.original_smacks_callback then
526 session.log("debug", "Calling original smacks watchdog handler");
527 session.original_smacks_callback();
528 end
529 end);
530 end
497 -- process unacked stanzas 531 -- process unacked stanzas
498 process_stanza_queue(queue, session, "smacks"); 532 process_stanza_queue(queue, session, "smacks");
499 end 533 end
500 534
501 -- smacks hibernation is ended 535 -- smacks hibernation is ended
505 if session.awaiting_push_timer then 539 if session.awaiting_push_timer then
506 session.awaiting_push_timer:stop(); 540 session.awaiting_push_timer:stop();
507 session.awaiting_push_timer = nil; 541 session.awaiting_push_timer = nil;
508 end 542 end
509 session.first_hibernated_push = nil; 543 session.first_hibernated_push = nil;
544 -- the extended smacks watchdog will be canceled by the smacks module, no need to anything here
510 end 545 end
511 end 546 end
512 547
513 -- smacks ack is delayed 548 -- smacks ack is delayed
514 local function ack_delayed(event) 549 local function ack_delayed(event)
592 -- cleanup some settings, reloading this module can cause process_smacks_stanza() to stop working otherwise 627 -- cleanup some settings, reloading this module can cause process_smacks_stanza() to stop working otherwise
593 for user, _ in pairs(host_sessions) do 628 for user, _ in pairs(host_sessions) do
594 for _, session in pairs(host_sessions[user].sessions) do 629 for _, session in pairs(host_sessions[user].sessions) do
595 if session.awaiting_push_timer then session.awaiting_push_timer:stop(); end 630 if session.awaiting_push_timer then session.awaiting_push_timer:stop(); end
596 session.awaiting_push_timer = nil; 631 session.awaiting_push_timer = nil;
632 session.push_queue = nil;
597 session.first_hibernated_push = nil; 633 session.first_hibernated_push = nil;
598 session.push_queue = nil; 634 -- check for prosody 0.12 mod_smacks
635 if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
636 -- restore old smacks watchdog
637 session.hibernating_watchdog:cancel();
638 session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
639 end
599 end 640 end
600 end 641 end
601 module:log("info", "Module unloaded"); 642 module:log("info", "Module unloaded");
602 end 643 end