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,84 +9,89 @@
# 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; }
:global Identity; :local Main do={
:global SmsForwardHooks; :local ScriptName [ :tostr $1 ];
:global IfThenElse; :global Identity;
:global LogPrintExit2; :global SmsForwardHooks;
:global LogPrintOnce;
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:global ValidateSyntax;
:global WaitFullyConnected;
$ScriptLock $0; :global IfThenElse;
:global LogPrintExit2;
:global LogPrintOnce;
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:global ValidateSyntax;
:global WaitFullyConnected;
:if ([ /tool/sms/get receive-enabled ] = false) do={ $ScriptLock $ScriptName;
$LogPrintOnce warning $0 ("Receiving of SMS is not enabled.") true;
}
$WaitFullyConnected; :if ([ /tool/sms/get receive-enabled ] = false) do={
$LogPrintOnce warning $ScriptName ("Receiving of SMS is not enabled.") true;
}
:local Settings [ /tool/sms/get ]; $WaitFullyConnected;
:if ([ /interface/lte/get ($Settings->"port") running ] != true) do={ :local Settings [ /tool/sms/get ];
$LogPrintExit2 info $0 ("The LTE interface is not in running state, skipping.") true;
}
# forward SMS in a loop :if ([ /interface/lte/get ($Settings->"port") running ] != true) do={
:while ([ :len [ /tool/sms/inbox/find ] ] > 0) do={ $LogPrintExit2 info $ScriptName ("The LTE interface is not in running state, skipping.") true;
:local Phone [ /tool/sms/inbox/get ([ find ]->0) phone ]; }
:local Messages "";
:local Delete ({});
:foreach Sms in=[ /tool/sms/inbox/find where phone=$Phone ] do={ # forward SMS in a loop
:local SmsVal [ /tool/sms/inbox/get $Sms ]; :while ([ :len [ /tool/sms/inbox/find ] ] > 0) do={
:local Phone [ /tool/sms/inbox/get ([ find ]->0) phone ];
:local Messages "";
:local Delete ({});
:if ($Phone = $Settings->"allowed-number" && \ :foreach Sms in=[ /tool/sms/inbox/find where phone=$Phone ] do={
($SmsVal->"message")~("^:cmd " . $Settings->"secret" . " script ")) do={ :local SmsVal [ /tool/sms/inbox/get $Sms ];
$LogPrintExit2 debug $0 ("Removing SMS, which started a script.") false;
/tool/sms/inbox/remove $Sms; :if ($Phone = $Settings->"allowed-number" && \
} else={ ($SmsVal->"message")~("^:cmd " . $Settings->"secret" . " script ")) do={
:set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \ $LogPrintExit2 debug $ScriptName ("Removing SMS, which started a script.") false;
" type " . $SmsVal->"type" . ":\n" . $SmsVal->"message"); /tool/sms/inbox/remove $Sms;
:foreach Hook in=$SmsForwardHooks do={ } else={
:if ($Phone~($Hook->"allowed-number") && ($SmsVal->"message")~($Hook->"match")) do={ :set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \
:if ([ $ValidateSyntax ($Hook->"command") ] = true) do={ " type " . $SmsVal->"type" . ":\n" . $SmsVal->"message");
$LogPrintExit2 info $0 ("Running hook '" . $Hook->"match" . "': " . \ :foreach Hook in=$SmsForwardHooks do={
$Hook->"command") false; :if ($Phone~($Hook->"allowed-number") && ($SmsVal->"message")~($Hook->"match")) do={
:do { :if ([ $ValidateSyntax ($Hook->"command") ] = true) do={
:local Command [ :parse ($Hook->"command") ]; $LogPrintExit2 info $ScriptName ("Running hook '" . $Hook->"match" . "': " . \
$Command Phone=$Phone Message=($SmsVal->"message"); $Hook->"command") false;
:set Messages ($Messages . "\n\nRan hook '" . $Hook->"match" . "':\n" . \ :do {
$Hook->"command"); :local Command [ :parse ($Hook->"command") ];
} on-error={ $Command Phone=$Phone Message=($SmsVal->"message");
$LogPrintExit2 warning $0 ("The code for hook '" . $Hook->"match" . \ :set Messages ($Messages . "\n\nRan hook '" . $Hook->"match" . "':\n" . \
"' failed to run!") false; $Hook->"command");
} on-error={
$LogPrintExit2 warning $ScriptName ("The code for hook '" . $Hook->"match" . \
"' failed to run!") false;
}
} else={
$LogPrintExit2 warning $ScriptName ("The code for hook '" . $Hook->"match" . \
"' failed syntax validation!") false;
} }
} else={
$LogPrintExit2 warning $0 ("The code for hook '" . $Hook->"match" . \
"' failed syntax validation!") false;
} }
} }
:set Delete ($Delete, $Sms);
} }
:set Delete ($Delete, $Sms);
} }
}
: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) });
:foreach Sms in=$Delete do={ :foreach Sms in=$Delete do={
/tool/sms/inbox/remove $Sms; /tool/sms/inbox/remove $Sms;
}
} }
} }
} }
$Main [ :jobname ];