From 28db4732998da95a33a6f7698038b20a0be13473 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 16 Feb 2021 15:20:01 +0100 Subject: global-functions: send (and re-send) e-mails from queue --- global-config | 2 +- global-config-overlay | 2 +- global-config.changes | 1 + global-functions | 66 +++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 59 insertions(+), 12 deletions(-) diff --git a/global-config b/global-config index b8bdd44..a3e515c 100644 --- a/global-config +++ b/global-config @@ -8,7 +8,7 @@ # Make sure all configuration properties are up to date and this # value is in sync with value in script 'global-functions'! -:global GlobalConfigVersion 42; +:global GlobalConfigVersion 43; # This is used for DNS and backup file. :global Domain "example.com"; diff --git a/global-config-overlay b/global-config-overlay index 8925b4c..e8d7913 100644 --- a/global-config-overlay +++ b/global-config-overlay @@ -9,7 +9,7 @@ # Make sure all configuration properties are up to date and this # value is in sync with value in script 'global-functions'! # Comment or remove to disable change notifications. -:global GlobalConfigVersion 42; +:global GlobalConfigVersion 43; # Copy configuration from global-config here and modify it. diff --git a/global-config.changes b/global-config.changes index 4ad3fee..a27b43a 100644 --- a/global-config.changes +++ b/global-config.changes @@ -46,6 +46,7 @@ 40="Made the certificate renewal time configurable."; 41="Implemented migration mechanism for script updates."; 42="Made severity in terminal output colorful, with opt-out."; + 43="Added queue for e-mail notifications to resend later on error."; }; # Migration steps to be applied on script updates diff --git a/global-functions b/global-functions index 6434307..889dcea 100644 --- a/global-functions +++ b/global-functions @@ -8,7 +8,7 @@ # https://git.eworm.de/cgit/routeros-scripts/about/ # expected configuration version -:global ExpectedConfigVersion 42; +:global ExpectedConfigVersion 43; # global variables not to be changed by user :global GlobalFunctionsReady false; @@ -24,6 +24,7 @@ :global DeviceInfo; :global DNSIsResolving; :global DownloadPackage; +:global FlushEmailQueue; :global FlushTelegramQueue; :global GetMacVendor; :global GetRandom20CharHex; @@ -286,6 +287,47 @@ :return false; } +# flush e-mail queue +:set FlushEmailQueue do={ + :global EmailQueue; + + :global LogPrintExit; + + :local AllDone true; + :local QueueLen [ :len $EmailQueue ]; + + :if ([ :len [ / system scheduler find where name="FlushEmailQueue" ] ] > 0 && $QueueLen = 0) do={ + $LogPrintExit warning ("Flushing E-Mail messages from scheduler, but queue is empty.") false; + } + + / system scheduler set interval=1m [ find where name="FlushEmailQueue" interval=1s ]; + + :foreach Id,Message in=$EmailQueue do={ + :if ([ :typeof $Message ] = "array" ) do={ + / tool e-mail send to=($Message->"to") cc=($Message->"cc") \ + subject=($Message->"subject") body=($Message->"body"); + :local Wait true; + :do { + :delay 1s; + :local Status [ / tool e-mail get last-status ]; + :if ($Status = "succeeded") do={ + :set ($EmailQueue->$Id); + :set Wait false; + } + :if ($Status = "failed") do={ + :set AllDone false; + :set Wait false; + } + } while ($Wait = true); + } + } + + :if ($AllDone = true && $QueueLen = [ :len $EmailQueue ]) do={ + / system scheduler remove [ find where name="FlushEmailQueue" ]; + :set EmailQueue; + } +} + # flush telegram queue :set FlushTelegramQueue do={ :global TelegramQueue; @@ -777,6 +819,7 @@ :global Identity; :global EmailGeneralTo; :global EmailGeneralCc; + :global EmailQueue; :global LogPrintExit; :global IfThenElse; @@ -785,15 +828,18 @@ :return false; } - :do { - :local Signature [ / system note get note ]; - / tool e-mail send to=$EmailGeneralTo cc=$EmailGeneralCc \ - subject=("[" . $Identity . "] " . $Subject) \ - body=($Message . \ - [ $IfThenElse ([ :len $Link ] > 0) ("\n\n" . $Link) "" ] . \ - [ $IfThenElse ([ :len $Signature ] > 0) ("\n-- \n" . $Signature) "" ]); - } on-error={ - $LogPrintExit warning ("Failed sending notification mail!") false; + :if ([ :typeof $EmailQueue ] = "nothing") do={ + :set EmailQueue [ :toarray "" ]; + } + :local Signature [ / system note get note ]; + :set ($EmailQueue->[ :len $EmailQueue ]) { + to=$EmailGeneralTo; cc=$EmailGeneralCc; subject=("[" . $Identity . "] " . $Subject); + body=($Message . \ + [ $IfThenElse ([ :len $Link ] > 0) ("\n\n" . $Link) "" ] . \ + [ $IfThenElse ([ :len $Signature ] > 0) ("\n-- \n" . $Signature) "" ]) }; + :if ([ :len [ / system scheduler find where name="FlushEmailQueue" ] ] = 0) do={ + / system scheduler add name=FlushEmailQueue interval=1s start-time=startup \ + on-event=":global FlushEmailQueue; \$FlushEmailQueue;"; } } -- cgit v1.2.3-70-g09d2