aboutsummaryrefslogtreecommitdiffstats
path: root/sms-forward.rsc
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2024-03-04 13:48:01 +0100
committerGravatar Christian Hesse <mail@eworm.de>2024-03-04 21:40:34 +0100
commit6b1c6a711926e32e0cc6cf4ba8359a96f1fc79c5 (patch)
treefb242b87e98fed2a8358cb13c75ba0f38d601288 /sms-forward.rsc
parent721b6c783bb492cf850874d5608214293275b4fc (diff)
sms-forward: move code into function
Diffstat (limited to 'sms-forward.rsc')
-rw-r--r--sms-forward.rsc125
1 files changed, 65 insertions, 60 deletions
diff --git a/sms-forward.rsc b/sms-forward.rsc
index f6d8e12..fee1539 100644
--- a/sms-forward.rsc
+++ b/sms-forward.rsc
@@ -9,84 +9,89 @@
# forward SMS to e-mail
# https://git.eworm.de/cgit/routeros-scripts/about/doc/sms-forward.md
-:local 0 [ :jobname ];
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
-:global Identity;
-:global SmsForwardHooks;
+:local Main do={
+ :local ScriptName [ :tostr $1 ];
-:global IfThenElse;
-:global LogPrintExit2;
-:global LogPrintOnce;
-:global ScriptLock;
-:global SendNotification2;
-:global SymbolForNotification;
-:global ValidateSyntax;
-:global WaitFullyConnected;
+ :global Identity;
+ :global SmsForwardHooks;
-$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={
- $LogPrintOnce warning $0 ("Receiving of SMS is not enabled.") true;
-}
+ $ScriptLock $ScriptName;
+
+ :if ([ /tool/sms/get receive-enabled ] = false) do={
+ $LogPrintOnce warning $ScriptName ("Receiving of SMS is not enabled.") true;
+ }
-$WaitFullyConnected;
+ $WaitFullyConnected;
-:local Settings [ /tool/sms/get ];
+ :local Settings [ /tool/sms/get ];
-:if ([ /interface/lte/get ($Settings->"port") running ] != true) do={
- $LogPrintExit2 info $0 ("The LTE interface is not in running state, skipping.") true;
-}
+ :if ([ /interface/lte/get ($Settings->"port") running ] != true) do={
+ $LogPrintExit2 info $ScriptName ("The LTE interface is not in running state, skipping.") true;
+ }
-# forward SMS in a loop
-:while ([ :len [ /tool/sms/inbox/find ] ] > 0) do={
- :local Phone [ /tool/sms/inbox/get ([ find ]->0) phone ];
- :local Messages "";
- :local Delete ({});
+ # forward SMS in a loop
+ :while ([ :len [ /tool/sms/inbox/find ] ] > 0) do={
+ :local Phone [ /tool/sms/inbox/get ([ find ]->0) phone ];
+ :local Messages "";
+ :local Delete ({});
- :foreach Sms in=[ /tool/sms/inbox/find where phone=$Phone ] do={
- :local SmsVal [ /tool/sms/inbox/get $Sms ];
+ :foreach Sms in=[ /tool/sms/inbox/find where phone=$Phone ] do={
+ :local SmsVal [ /tool/sms/inbox/get $Sms ];
- :if ($Phone = $Settings->"allowed-number" && \
- ($SmsVal->"message")~("^:cmd " . $Settings->"secret" . " script ")) do={
- $LogPrintExit2 debug $0 ("Removing SMS, which started a script.") false;
- /tool/sms/inbox/remove $Sms;
- } else={
- :set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \
- " type " . $SmsVal->"type" . ":\n" . $SmsVal->"message");
- :foreach Hook in=$SmsForwardHooks do={
- :if ($Phone~($Hook->"allowed-number") && ($SmsVal->"message")~($Hook->"match")) do={
- :if ([ $ValidateSyntax ($Hook->"command") ] = true) do={
- $LogPrintExit2 info $0 ("Running hook '" . $Hook->"match" . "': " . \
- $Hook->"command") false;
- :do {
- :local Command [ :parse ($Hook->"command") ];
- $Command Phone=$Phone Message=($SmsVal->"message");
- :set Messages ($Messages . "\n\nRan hook '" . $Hook->"match" . "':\n" . \
- $Hook->"command");
- } on-error={
- $LogPrintExit2 warning $0 ("The code for hook '" . $Hook->"match" . \
- "' failed to run!") false;
+ :if ($Phone = $Settings->"allowed-number" && \
+ ($SmsVal->"message")~("^:cmd " . $Settings->"secret" . " script ")) do={
+ $LogPrintExit2 debug $ScriptName ("Removing SMS, which started a script.") false;
+ /tool/sms/inbox/remove $Sms;
+ } else={
+ :set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \
+ " type " . $SmsVal->"type" . ":\n" . $SmsVal->"message");
+ :foreach Hook in=$SmsForwardHooks do={
+ :if ($Phone~($Hook->"allowed-number") && ($SmsVal->"message")~($Hook->"match")) do={
+ :if ([ $ValidateSyntax ($Hook->"command") ] = true) do={
+ $LogPrintExit2 info $ScriptName ("Running hook '" . $Hook->"match" . "': " . \
+ $Hook->"command") false;
+ :do {
+ :local Command [ :parse ($Hook->"command") ];
+ $Command Phone=$Phone Message=($SmsVal->"message");
+ :set Messages ($Messages . "\n\nRan hook '" . $Hook->"match" . "':\n" . \
+ $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={
- :local Count [ :len $Delete ];
- $SendNotification2 ({ origin=$0; \
- subject=([ $SymbolForNotification "incoming-envelope" ] . "SMS Forwarding from " . $Phone); \
- message=("Received " . [ $IfThenElse ($Count = 1) "this message" ("these " . $Count . " messages") ] . \
- " by " . $Identity . " from " . $Phone . ":" . $Messages) });
- :foreach Sms in=$Delete do={
- /tool/sms/inbox/remove $Sms;
+ :if ([ :len $Messages ] > 0) do={
+ :local Count [ :len $Delete ];
+ $SendNotification2 ({ origin=$ScriptName; \
+ subject=([ $SymbolForNotification "incoming-envelope" ] . "SMS Forwarding from " . $Phone); \
+ message=("Received " . [ $IfThenElse ($Count = 1) "this message" ("these " . $Count . " messages") ] . \
+ " by " . $Identity . " from " . $Phone . ":" . $Messages) });
+ :foreach Sms in=$Delete do={
+ /tool/sms/inbox/remove $Sms;
+ }
}
}
}
+
+$Main [ :jobname ];