aboutsummaryrefslogblamecommitdiffstats
path: root/sms-forward
blob: 9c5c4941cd9dde66d85099cf4fc19ed2ccf12ae8 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
     
                              
                                                         

                       
                 
 
                         
                   
                                                                                                      
                                         
 
                                   
 
                       




                                                                   
                                                
 
                                                                                

                                                         
                                                                     



                                   
                                                         



                                                             
   
 
#!rsc
# RouterOS script: sms-forward
# Copyright (c) 2013-2019 Christian Hesse <mail@eworm.de>
#
# forward SMS to e-mail

:global Identity;

:global SendNotification;

# check mail server
:if ([ / tool netwatch get [ find where comment=[ / tool e-mail get address ] ] status ] != "up") do={
  :log warning "Mail server is not up.";
  :error "Warning: See log for details.";
}

: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;
    }
  }
}