sms-forward: move code into function

This commit is contained in:
Christian Hesse 2024-03-04 13:48:01 +01:00
parent 721b6c783b
commit 6b1c6a7119

View file

@ -9,10 +9,12 @@
# forward SMS to e-mail # forward SMS to e-mail
# https://git.eworm.de/cgit/routeros-scripts/about/doc/sms-forward.md # https://git.eworm.de/cgit/routeros-scripts/about/doc/sms-forward.md
:local 0 [ :jobname ];
:global GlobalFunctionsReady; :global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } :while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:local Main do={
:local ScriptName [ :tostr $1 ];
:global Identity; :global Identity;
:global SmsForwardHooks; :global SmsForwardHooks;
@ -25,10 +27,10 @@
:global ValidateSyntax; :global ValidateSyntax;
:global WaitFullyConnected; :global WaitFullyConnected;
$ScriptLock $0; $ScriptLock $ScriptName;
:if ([ /tool/sms/get receive-enabled ] = false) do={ :if ([ /tool/sms/get receive-enabled ] = false) do={
$LogPrintOnce warning $0 ("Receiving of SMS is not enabled.") true; $LogPrintOnce warning $ScriptName ("Receiving of SMS is not enabled.") true;
} }
$WaitFullyConnected; $WaitFullyConnected;
@ -36,7 +38,7 @@ $WaitFullyConnected;
:local Settings [ /tool/sms/get ]; :local Settings [ /tool/sms/get ];
:if ([ /interface/lte/get ($Settings->"port") running ] != true) do={ :if ([ /interface/lte/get ($Settings->"port") running ] != true) do={
$LogPrintExit2 info $0 ("The LTE interface is not in running state, skipping.") true; $LogPrintExit2 info $ScriptName ("The LTE interface is not in running state, skipping.") true;
} }
# forward SMS in a loop # forward SMS in a loop
@ -50,7 +52,7 @@ $WaitFullyConnected;
:if ($Phone = $Settings->"allowed-number" && \ :if ($Phone = $Settings->"allowed-number" && \
($SmsVal->"message")~("^:cmd " . $Settings->"secret" . " script ")) do={ ($SmsVal->"message")~("^:cmd " . $Settings->"secret" . " script ")) do={
$LogPrintExit2 debug $0 ("Removing SMS, which started a script.") false; $LogPrintExit2 debug $ScriptName ("Removing SMS, which started a script.") false;
/tool/sms/inbox/remove $Sms; /tool/sms/inbox/remove $Sms;
} else={ } else={
:set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \ :set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \
@ -58,7 +60,7 @@ $WaitFullyConnected;
:foreach Hook in=$SmsForwardHooks do={ :foreach Hook in=$SmsForwardHooks do={
:if ($Phone~($Hook->"allowed-number") && ($SmsVal->"message")~($Hook->"match")) do={ :if ($Phone~($Hook->"allowed-number") && ($SmsVal->"message")~($Hook->"match")) do={
:if ([ $ValidateSyntax ($Hook->"command") ] = true) do={ :if ([ $ValidateSyntax ($Hook->"command") ] = true) do={
$LogPrintExit2 info $0 ("Running hook '" . $Hook->"match" . "': " . \ $LogPrintExit2 info $ScriptName ("Running hook '" . $Hook->"match" . "': " . \
$Hook->"command") false; $Hook->"command") false;
:do { :do {
:local Command [ :parse ($Hook->"command") ]; :local Command [ :parse ($Hook->"command") ];
@ -66,11 +68,11 @@ $WaitFullyConnected;
:set Messages ($Messages . "\n\nRan hook '" . $Hook->"match" . "':\n" . \ :set Messages ($Messages . "\n\nRan hook '" . $Hook->"match" . "':\n" . \
$Hook->"command"); $Hook->"command");
} on-error={ } on-error={
$LogPrintExit2 warning $0 ("The code for hook '" . $Hook->"match" . \ $LogPrintExit2 warning $ScriptName ("The code for hook '" . $Hook->"match" . \
"' failed to run!") false; "' failed to run!") false;
} }
} else={ } else={
$LogPrintExit2 warning $0 ("The code for hook '" . $Hook->"match" . \ $LogPrintExit2 warning $ScriptName ("The code for hook '" . $Hook->"match" . \
"' failed syntax validation!") false; "' failed syntax validation!") false;
} }
} }
@ -81,7 +83,7 @@ $WaitFullyConnected;
:if ([ :len $Messages ] > 0) do={ :if ([ :len $Messages ] > 0) do={
:local Count [ :len $Delete ]; :local Count [ :len $Delete ];
$SendNotification2 ({ origin=$0; \ $SendNotification2 ({ origin=$ScriptName; \
subject=([ $SymbolForNotification "incoming-envelope" ] . "SMS Forwarding from " . $Phone); \ subject=([ $SymbolForNotification "incoming-envelope" ] . "SMS Forwarding from " . $Phone); \
message=("Received " . [ $IfThenElse ($Count = 1) "this message" ("these " . $Count . " messages") ] . \ message=("Received " . [ $IfThenElse ($Count = 1) "this message" ("these " . $Count . " messages") ] . \
" by " . $Identity . " from " . $Phone . ":" . $Messages) }); " by " . $Identity . " from " . $Phone . ":" . $Messages) });
@ -90,3 +92,6 @@ $WaitFullyConnected;
} }
} }
} }
}
$Main [ :jobname ];