aboutsummaryrefslogtreecommitdiffstats
path: root/global-functions
diff options
context:
space:
mode:
Diffstat (limited to 'global-functions')
-rw-r--r--global-functions116
1 files changed, 74 insertions, 42 deletions
diff --git a/global-functions b/global-functions
index 855221e..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;
@@ -24,6 +24,7 @@
:global DeviceInfo;
:global DNSIsResolving;
:global DownloadPackage;
+:global EitherOr;
:global EscapeForRegEx;
:global FlushEmailQueue;
:global FlushTelegramQueue;
@@ -43,8 +44,11 @@
:global ScriptInstallUpdate;
:global ScriptLock;
:global SendEMail;
+:global SendEMail2;
:global SendNotification;
+:global SendNotification2;
:global SendTelegram;
+:global SendTelegram2;
:global SymbolByUnicodeName;
:global SymbolForNotification;
:global TimeIsSync;
@@ -290,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 ];
@@ -356,7 +370,6 @@
# flush telegram queue
:set FlushTelegramQueue do={
:global TelegramQueue;
- :global TelegramTokenId;
:global LogPrintExit2;
@@ -371,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") . \
@@ -675,7 +688,7 @@
:global LogPrintExit2;
:global ParseKeyValueStore;
:global ScriptInstallUpdate;
- :global SendNotification;
+ :global SendNotification2;
:global SymbolForNotification;
:global ValidateSyntax;
@@ -842,8 +855,9 @@
:set Link "https://git.eworm.de/cgit/routeros-scripts/about/#donate";
}
- $SendNotification ([ $SymbolForNotification "pushpin" ] . "News and configuration changes") \
- $NotificationMessage $Link;
+ $SendNotification2 ({ origin=$0; \
+ subject=([ $SymbolForNotification "pushpin" ] . "News and configuration changes"); \
+ message=$NotificationMessage; link=$Link });
:set SentConfigChangesNotification $ExpectedConfigVersion;
}
@@ -867,22 +881,33 @@
}
}
-# 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;
+ :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;
}
@@ -891,10 +916,10 @@
}
: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) "" ] . \
+ to=$To; cc=$Cc;
+ 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 +927,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 });
+}
+
+# send notification via e-mail and telegram - expects one array argument
+:set SendNotification2 do={
+ :local Notification $1;
- :global SendEMail;
- :global SendTelegram;
+ :global SendEMail2;
+ :global SendTelegram2;
- $SendEMail $Subject $Message $Link;
- $SendTelegram $Subject $Message $Link $Silent;
+ $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;
@@ -930,9 +962,11 @@
:global TelegramFixedWidthFont;
:global TelegramQueue;
:global TelegramTokenId;
+ :global TelegramTokenIdOverride;
:global CertificateAvailable;
:global CharacterReplace;
+ :global EitherOr;
:global IfThenElse;
:global LogPrintExit2;
:global SymbolForNotification;
@@ -965,18 +999,16 @@
: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;
}
: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 +1017,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" ] . \
@@ -1000,8 +1032,8 @@
$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") \
- http-data=("chat_id=" . $ChatId . "&disable_notification=" . $Silent . \
+ ("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={
$LogPrintExit2 info $0 ("Failed sending telegram notification! Queuing...") false;
@@ -1012,8 +1044,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=$Silent };
+ :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 \
on-event=":global FlushTelegramQueue; \$FlushTelegramQueue;";