aboutsummaryrefslogtreecommitdiffstats
path: root/sms-forward
blob: 8f38a4a816515e84fac267eb7338045841f383d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!rsc
# RouterOS script: sms-forward
# Copyright (c) 2013-2020 Christian Hesse <mail@eworm.de>
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
#
# forward SMS to e-mail
# https://git.eworm.de/cgit/routeros-scripts/about/doc/sms-forward.md

:global Identity;

:global LogPrintExit;
:global MailServerIsUp;
:global SendNotification;
:global SymbolForNotification;
:global WaitFullyConnected;

:if ([ / tool sms get receive-enabled ] = false) do={
  $LogPrintExit warning "Receiving of SMS is not enabled." true;
}

$WaitFullyConnected;

: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={
      $LogPrintExit debug ("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");
      :set Delete ($Delete, $Sms);
    }
  }

  :if ([ :len $Messages ] > 0) do={
    $SendNotification ([ $SymbolForNotification "incoming-envelope" ] . "SMS Forwarding from " . $Phone) \
        ("These message(s) were received by " . $Identity . \
        " from " . $Phone . ":" . $Messages);
    :foreach Sms in=$Delete do={
      / tool sms inbox remove $Sms;
    }
  }
}