From c7a2eecd3c875e8225da13a83cfd37c390ee7082 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 27 Apr 2021 17:46:03 +0200 Subject: global-functions: introduce $SendEMail2, $SendNotification2 & $SendTelegram2 These accept just one array as argument. Adding new features is possible without breaking the API. These calls are the same for now: $SendNotification "Subject..." "Message..."; $SendNotification2 ({ subject="Subject..."; message="Message..." }); But the latter will bring more features in future. --- global-functions | 69 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 27 deletions(-) (limited to 'global-functions') diff --git a/global-functions b/global-functions index 855221e..b8a7dad 100644 --- a/global-functions +++ b/global-functions @@ -43,8 +43,11 @@ :global ScriptInstallUpdate; :global ScriptLock; :global SendEMail; +:global SendEMail2; :global SendNotification; +:global SendNotification2; :global SendTelegram; +:global SendTelegram2; :global SymbolByUnicodeName; :global SymbolForNotification; :global TimeIsSync; @@ -867,11 +870,16 @@ } } -# send notification via e-mail +# send notification via e-mail - expects at lease two string arguments :set SendEMail do={ - :local Subject [ :tostr $1 ]; - :local Message [ :tostr $2 ]; - :local Link [ :tostr $3 ]; + :global SendEMail2; + + $SendEMail2 ({ subject=$1; message=$2; link=$3 }); +} + +# send notification via e-mail - expects one array argument +:set SendEMail2 do={ + :local Notification $1; :global Identity; :global EmailGeneralTo; @@ -892,9 +900,9 @@ :local Signature [ / system note get note ]; :set ($EmailQueue->[ :len $EmailQueue ]) { to=$EmailGeneralTo; cc=$EmailGeneralCc; - subject=[ $QuotedPrintable ("[" . $Identity . "] " . $Subject) ]; - body=($Message . \ - [ $IfThenElse ([ :len $Link ] > 0) ("\n\n" . $Link) "" ] . \ + subject=[ $QuotedPrintable ("[" . $Identity . "] " . ($Notification->"subject")) ]; + body=(($Notification->"message") . \ + [ $IfThenElse ([ :len ($Notification->"link") ] > 0) ("\n\n" . ($Notification->"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 \ @@ -902,27 +910,34 @@ } } -# send notification via e-mail and telegram -# Note that attachment is ignored for telegram, silent is ignored for e-mail! +# send notification via e-mail and telegram - expects at lease two string arguments :set SendNotification do={ - :local Subject [ :tostr $1 ]; - :local Message [ :tostr $2 ]; - :local Link [ :tostr $3 ]; - :local Silent [ :tostr $4 ]; + :global SendNotification2; + + $SendNotification2 ({ subject=$1; message=$2; link=$3; silent=$4 }); +} - :global SendEMail; - :global SendTelegram; +# send notification via e-mail and telegram - expects one array argument +:set SendNotification2 do={ + :local Notification $1; - $SendEMail $Subject $Message $Link; - $SendTelegram $Subject $Message $Link $Silent; + :global SendEMail2; + :global SendTelegram2; + + $SendEMail2 $Notification; + $SendTelegram2 $Notification; } -# send notification via telegram +# send notification via telegram - expects at lease two string arguments :set SendTelegram do={ - :local Subject [ :tostr $1 ]; - :local Message [ :tostr $2 ]; - :local Link [ :tostr $3 ]; - :local Silent [ :tostr $4 ]; + :global SendTelegram2; + + $SendTelegram2 ({ subject=$1; message=$2; link=$3; silent=$4 }); +} + +# send notification via telegram - expects one array argument +:set SendTelegram2 do={ + :local Notification $1; :global Identity; :global TelegramChatId; @@ -975,8 +990,8 @@ } :local Truncated false; - :local LenLink [ :len $Link ]; - :local Text ("[" . $Identity . "] " . $Subject . "\n\n" . $Message); + :local LenLink [ :len ($Notification->"link") ]; + :local Text ("[" . $Identity . "] " . ($Notification->"subject") . "\n\n" . ($Notification->"message")); :local LenText [ :len $Text ]; :if ($LenText > (3968 - $LenLink)) do={ :set Text [ $EscapeMD ([ :pick $Text 0 (3840 - $LenLink) ] . "...") "body" ]; @@ -985,7 +1000,7 @@ :set Text [ $EscapeMD $Text "body" ]; } :if ($LenLink > 0) do={ - :set Text ($Text . "\n" . [ $SymbolForNotification "link" ] . [ $EscapeMD $Link "hint" ]); + :set Text ($Text . "\n" . [ $SymbolForNotification "link" ] . [ $EscapeMD ($Notification->"link") "hint" ]); } :if ($Truncated = true) do={ :set Text ($Text . "\n" . [ $SymbolForNotification "scissors" ] . \ @@ -1001,7 +1016,7 @@ } / tool fetch check-certificate=yes-without-crl output=none http-method=post \ ("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \ - http-data=("chat_id=" . $ChatId . "&disable_notification=" . $Silent . \ + http-data=("chat_id=" . $ChatId . "&disable_notification=" . ($Notification->"silent") . \ "&disable_web_page_preview=true&parse_mode=" . $ParseMode . "&text=" . $Text) as-value; } on-error={ $LogPrintExit2 info $0 ("Failed sending telegram notification! Queuing...") false; @@ -1013,7 +1028,7 @@ [ $EscapeMD ("This message was queued since " . [ / system clock get date ] . \ " " . [ / system clock get time ] . " and may be obsolete.") "hint" ]) ]); :set ($TelegramQueue->[ :len $TelegramQueue ]) { - chatid=$ChatId; parsemode=$ParseMode; text=$Text; silent=$Silent }; + chatid=$ChatId; parsemode=$ParseMode; text=$Text; silent=($Notification->"silent") }; :if ([ :len [ / system scheduler find where name="FlushTelegramQueue" ] ] = 0) do={ / system scheduler add name=FlushTelegramQueue interval=1m start-time=startup \ on-event=":global FlushTelegramQueue; \$FlushTelegramQueue;"; -- cgit v1.2.3-54-g00ecf From 76f32e3927d2e40f2b6cc28f93186bf84d222799 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 27 Apr 2021 20:37:33 +0200 Subject: global-functions: $ScriptInstallUpdate: use $SendNotification2 --- global-functions | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'global-functions') diff --git a/global-functions b/global-functions index b8a7dad..aa0ea07 100644 --- a/global-functions +++ b/global-functions @@ -678,7 +678,7 @@ :global LogPrintExit2; :global ParseKeyValueStore; :global ScriptInstallUpdate; - :global SendNotification; + :global SendNotification2; :global SymbolForNotification; :global ValidateSyntax; @@ -845,8 +845,8 @@ :set Link "https://git.eworm.de/cgit/routeros-scripts/about/#donate"; } - $SendNotification ([ $SymbolForNotification "pushpin" ] . "News and configuration changes") \ - $NotificationMessage $Link; + $SendNotification2 ({ subject=([ $SymbolForNotification "pushpin" ] . \ + "News and configuration changes"); message=$NotificationMessage; link=$Link }); :set SentConfigChangesNotification $ExpectedConfigVersion; } -- cgit v1.2.3-54-g00ecf From e97b3945363c8f322b7ff7f4bf65f13d887d373b Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 27 Apr 2021 21:17:45 +0200 Subject: global-functions: introduce $EitherOr --- global-functions | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'global-functions') diff --git a/global-functions b/global-functions index aa0ea07..48b58a6 100644 --- a/global-functions +++ b/global-functions @@ -24,6 +24,7 @@ :global DeviceInfo; :global DNSIsResolving; :global DownloadPackage; +:global EitherOr; :global EscapeForRegEx; :global FlushEmailQueue; :global FlushTelegramQueue; @@ -293,6 +294,16 @@ :return false; } +# return either first (if "true") or second +:set EitherOr do={ + :global IfThenElse; + + :if ([ :typeof $1 ] = "num") do={ + :return [ $IfThenElse ($1 != 0) $1 $2 ]; + } + :return [ $IfThenElse ([ :len [ :tostr $1 ] ] > 0) $1 $2 ]; +} + # escape for regular expression :set EscapeForRegEx do={ :local Input [ :tostr $1 ]; -- cgit v1.2.3-54-g00ecf From 42dcdae11eb5d3dcb25c07e2bb790b2c7851a785 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 27 Apr 2021 21:20:02 +0200 Subject: global-functions: $SendEMail2: support overriding to and cc --- global-functions | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'global-functions') diff --git a/global-functions b/global-functions index 48b58a6..e32a66e 100644 --- a/global-functions +++ b/global-functions @@ -894,14 +894,20 @@ :global Identity; :global EmailGeneralTo; + :global EmailGeneralToOverride; :global EmailGeneralCc; + :global EmailGeneralCcOverride; :global EmailQueue; + :global EitherOr; :global IfThenElse; :global LogPrintExit2; :global QuotedPrintable; - :if ([ :len $EmailGeneralTo ] = 0) do={ + :local To [ $EitherOr ($EmailGeneralToOverride->($Notification->"origin")) $EmailGeneralTo ]; + :local Cc [ $EitherOr ($EmailGeneralCcOverride->($Notification->"origin")) $EmailGeneralCc ]; + + :if ([ :len $To ] = 0) do={ :return false; } @@ -910,7 +916,7 @@ } :local Signature [ / system note get note ]; :set ($EmailQueue->[ :len $EmailQueue ]) { - to=$EmailGeneralTo; cc=$EmailGeneralCc; + to=$To; cc=$Cc; subject=[ $QuotedPrintable ("[" . $Identity . "] " . ($Notification->"subject")) ]; body=(($Notification->"message") . \ [ $IfThenElse ([ :len ($Notification->"link") ] > 0) ("\n\n" . ($Notification->"link")) "" ] . \ -- cgit v1.2.3-54-g00ecf From bd05ca1133635a595a976d150fd10e36d46705aa Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 27 Apr 2021 21:28:52 +0200 Subject: global-functions: $SendTelegram2: store token id in queue --- global-functions | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'global-functions') diff --git a/global-functions b/global-functions index e32a66e..fa0a34f 100644 --- a/global-functions +++ b/global-functions @@ -370,7 +370,6 @@ # flush telegram queue :set FlushTelegramQueue do={ :global TelegramQueue; - :global TelegramTokenId; :global LogPrintExit2; @@ -385,7 +384,7 @@ :if ([ :typeof $Message ] = "array" ) do={ :do { / tool fetch check-certificate=yes-without-crl output=none http-method=post \ - ("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \ + ("https://api.telegram.org/bot" . ($Message->"tokenid") . "/sendMessage") \ http-data=("chat_id=" . ($Message->"chatid") . \ "&disable_notification=" . ($Message->"silent") . \ "&disable_web_page_preview=true&parse_mode=" . ($Message->"parsemode") . \ @@ -1044,8 +1043,8 @@ :set Text ($Text . [ $UrlEncode ("\n" . [ $SymbolForNotification "alarm-clock" ] . \ [ $EscapeMD ("This message was queued since " . [ / system clock get date ] . \ " " . [ / system clock get time ] . " and may be obsolete.") "hint" ]) ]); - :set ($TelegramQueue->[ :len $TelegramQueue ]) { - chatid=$ChatId; parsemode=$ParseMode; text=$Text; silent=($Notification->"silent") }; + :set ($TelegramQueue->[ :len $TelegramQueue ]) { chatid=$ChatId; tokenid=$TelegramTokenId; + parsemode=$ParseMode; text=$Text; silent=($Notification->"silent") }; :if ([ :len [ / system scheduler find where name="FlushTelegramQueue" ] ] = 0) do={ / system scheduler add name=FlushTelegramQueue interval=1m start-time=startup \ on-event=":global FlushTelegramQueue; \$FlushTelegramQueue;"; -- cgit v1.2.3-54-g00ecf From 2d0ce176299c12de6898e17964ccfc4505b65b7c Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 27 Apr 2021 21:31:11 +0200 Subject: global-functions: $SendTelegram2: support overriding token id and chat id --- global-functions | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'global-functions') diff --git a/global-functions b/global-functions index fa0a34f..3535f38 100644 --- a/global-functions +++ b/global-functions @@ -961,9 +961,11 @@ :global TelegramFixedWidthFont; :global TelegramQueue; :global TelegramTokenId; + :global TelegramTokenIdOverride; :global CertificateAvailable; :global CharacterReplace; + :global EitherOr; :global IfThenElse; :global LogPrintExit2; :global SymbolForNotification; @@ -996,12 +998,10 @@ :return $Return; } - :local ChatId $TelegramChatId; - :if ([ :len $TelegramChatIdOverride ] > 0) do={ - :set ChatId $TelegramChatIdOverride; - } + :local ChatId [ $EitherOr ($TelegramChatIdOverride->($Notification->"origin")) $TelegramChatId ]; + :local TokenId [ $EitherOr ($TelegramTokenIdOverride->($Notification->"origin")) $TelegramTokenId ]; - :if ([ :len $TelegramTokenId ] = 0 || [ :len $ChatId ] = 0) do={ + :if ([ :len $TokenId ] = 0 || [ :len $ChatId ] = 0) do={ :return false; } @@ -1031,7 +1031,7 @@ $LogPrintExit2 warning $0 ("Downloading required certificate failed.") true; } / tool fetch check-certificate=yes-without-crl output=none http-method=post \ - ("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \ + ("https://api.telegram.org/bot" . $TokenId . "/sendMessage") \ http-data=("chat_id=" . $ChatId . "&disable_notification=" . ($Notification->"silent") . \ "&disable_web_page_preview=true&parse_mode=" . $ParseMode . "&text=" . $Text) as-value; } on-error={ @@ -1043,7 +1043,7 @@ :set Text ($Text . [ $UrlEncode ("\n" . [ $SymbolForNotification "alarm-clock" ] . \ [ $EscapeMD ("This message was queued since " . [ / system clock get date ] . \ " " . [ / system clock get time ] . " and may be obsolete.") "hint" ]) ]); - :set ($TelegramQueue->[ :len $TelegramQueue ]) { chatid=$ChatId; tokenid=$TelegramTokenId; + :set ($TelegramQueue->[ :len $TelegramQueue ]) { chatid=$ChatId; tokenid=$TokenId; parsemode=$ParseMode; text=$Text; silent=($Notification->"silent") }; :if ([ :len [ / system scheduler find where name="FlushTelegramQueue" ] ] = 0) do={ / system scheduler add name=FlushTelegramQueue interval=1m start-time=startup \ -- cgit v1.2.3-54-g00ecf From c64082388195fc81d0d43e8d360d64663c1d70a9 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 27 Apr 2021 21:38:37 +0200 Subject: global-functions: $ScriptInstallUpdate: pass origin to $SendNotification2 --- global-functions | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'global-functions') diff --git a/global-functions b/global-functions index 3535f38..ddddcb8 100644 --- a/global-functions +++ b/global-functions @@ -855,8 +855,9 @@ :set Link "https://git.eworm.de/cgit/routeros-scripts/about/#donate"; } - $SendNotification2 ({ subject=([ $SymbolForNotification "pushpin" ] . \ - "News and configuration changes"); message=$NotificationMessage; link=$Link }); + $SendNotification2 ({ origin=$0; \ + subject=([ $SymbolForNotification "pushpin" ] . "News and configuration changes"); \ + message=$NotificationMessage; link=$Link }); :set SentConfigChangesNotification $ExpectedConfigVersion; } -- cgit v1.2.3-54-g00ecf From 56b75237754eb065aecb932b3082ba0ca29c3f7c Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 28 Apr 2021 15:33:37 +0200 Subject: global-functions: notify about settings override --- global-config | 2 +- global-config-overlay | 2 +- global-config.changes | 1 + global-functions | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) (limited to 'global-functions') diff --git a/global-config b/global-config index 58c9617..9e69c18 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 47; +:global GlobalConfigVersion 48; # This is used for DNS and backup file. :global Domain "example.com"; diff --git a/global-config-overlay b/global-config-overlay index 850e6d8..316fde5 100644 --- a/global-config-overlay +++ b/global-config-overlay @@ -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'! # Comment or remove to disable news and change notifications. -:global GlobalConfigVersion 47; +:global GlobalConfigVersion 48; # Copy configuration from global-config here and modify it. diff --git a/global-config.changes b/global-config.changes index 2dc68f7..d3d1059 100644 --- a/global-config.changes +++ b/global-config.changes @@ -51,6 +51,7 @@ 45="We have a Telegram Group! Come along and say hello: https://t.me/routeros_scripts"; 46="Added configurable random delay in backup scripts to stretch execution and prevent resource congestion."; 47="Removed obsolete intermediate certificate 'Let's Encrypt Authority X3' from store."; + 48="Added support for overriding e-mail and Telegram settings for every script."; }; # Migration steps to be applied on script updates diff --git a/global-functions b/global-functions index ddddcb8..ff16076 100644 --- a/global-functions +++ b/global-functions @@ -8,7 +8,7 @@ # https://git.eworm.de/cgit/routeros-scripts/about/ # expected configuration version -:global ExpectedConfigVersion 47; +:global ExpectedConfigVersion 48; # global variables not to be changed by user :global GlobalFunctionsReady false; -- cgit v1.2.3-54-g00ecf