routeros-scripts/sms-forward
Christian Hesse ceaa83b83e global-functions: merge $LogAnd{Error,Put} to $LogPrintExit ...
... and fix logging.

Logging with severity from variable (:log $severity ...) is not
possible, this is considered a syntax error. Also the 'workaround' with
parsing code failed with missing message in log.

The reliable code is a lot longer, so merge the two functions to save a
lot of duplicate code.
2020-02-26 14:19:54 +01:00

48 lines
1.4 KiB
Text

#!rsc
# RouterOS script: sms-forward
# Copyright (c) 2013-2020 Christian Hesse <mail@eworm.de>
#
# forward SMS to e-mail
:global Identity;
:global SendNotification;
:global MailServerIsUp;
:global LogPrintExit;
# check mail server
:if ($MailServerIsUp = false) do={
$LogPrintExit warning "Mail server is not up." true;
}
:local Settings [ / tool sms get ];
# forward SMS in a loop
:while ([ / tool sms inbox print count-only ] > 0) do={
:local Phone [ / tool sms inbox get ([ find ]->0) phone ];
:local Messages "";
:local Delete [ :toarray "" ];
: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={
:log debug "Removing SMS, which started a script.";
/ tool sms inbox remove $Sms;
} else={
:set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \
" type " . $SmsVal->"type" . ":\n" . $SmsVal->"message");
:set Delete ($Delete, $Sms);
}
}
:if ([ :len $Messages ] > 0) do={
$SendNotification ("SMS Forwarding from " . $Phone) \
("These message(s) were received by " . $Identity . \
" from " . $Phone . ":" . $Messages);
:foreach Sms in=$Delete do={
/ tool sms inbox remove $Sms;
}
}
}