Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
sdk:scripts:sms-control [2015/07/23 06:58] dotolisdk:scripts:sms-control [2022/08/26 11:04] (current) – created dodenhoeft
Line 1: Line 1:
-====== SDK Script sms-control.are ======+====== SMS-Control ====== 
 <code c sms-control.are> <code c sms-control.are>
 +
 /* DESC: This script will execute commands received by SMS. /* DESC: This script will execute commands received by SMS.
  * Copyright (C) 2012 NetModule AG, Switzerland  * Copyright (C) 2012 NetModule AG, Switzerland
 + 
 + * sms-commands:
 + * <reboot>: reboot netmodule router
 + * <connect>: configure wanlink 0 permanent
 + * <disconnect>: configure wanlink 0 disable 
 + * <status>: get system status summary
 + * <output [1|2] [on|off]> set dio n on or off
  */  */
  
Line 8: Line 17:
 MAXMSG          = 5;                            /* process max. 5 msgs                          */ MAXMSG          = 5;                            /* process max. 5 msgs                          */
 MAXAGE          = 300;                          /* message mustn't be older than 5 mins         */ MAXAGE          = 300;                          /* message mustn't be older than 5 mins         */
-MAXLINES = 32; /* max. number of lines in msg (incl. header)   */+MAXLINES        = 32;                           /* max. number of lines in msg (incl. header)   */
 AUTH            = 1;                            /* perform authentication                       */ AUTH            = 1;                            /* perform authentication                       */
 ADMPWD          = "";                           /* password used for authentication             */ ADMPWD          = "";                           /* password used for authentication             */
Line 33: Line 42:
     }     }
     if (IGNORECASE) ADMPWD = tolower(ADMPWD);     if (IGNORECASE) ADMPWD = tolower(ADMPWD);
 +}
 +
 +/* split a message into an array */
 +array split(string msg) {
 +  args=mkarray();
 +  pos=strchr(msg, "\r");
 +  if(pos==0) msg="";
 +  if(pos>0) msg=left(msg, pos);
 +  pos=strchr(msg, "\n");
 +  if(pos==0) msg="";
 +  if(pos>0) msg=left(msg, pos);
 +  for(n=0;;) {
 +    pos=strchr(msg, " ");
 +    if(is_void(pos)) pos=strchr(msg, "\t");
 +    if(is_void(pos)) pos=strchr(msg, "\f");
 +    if(pos==0) {
 +      msg=trim(msg);
 +    } else if(pos>0) {
 +      args[n++]=trim(left(msg, pos));
 +      msg=trim(substr(msg, pos));
 +    } else {
 +      if(msg!="") args[n++]=trim(msg);
 +      break;
 +    }
 +  }
 +  return args;
 } }
  
 /* parse message */ /* parse message */
-string parse (string msg)+array parse (string msg)
 { {
     /* read by line */     /* read by line */
Line 47: Line 82:
     for (lnr = 0; lnr < MAXLINES && strlen(lp) > 0; lnr++) {     for (lnr = 0; lnr < MAXLINES && strlen(lp) > 0; lnr++) {
         pos = strchr(lp, "\n");         pos = strchr(lp, "\n");
-    if (is_void(pos)) pos = strlen(lp); +        if (is_void(pos)) pos = strlen(lp); 
-     +
         line = left(lp, pos);         line = left(lp, pos);
- lp = substr(lp, pos + 1); +        lp = substr(lp, pos + 1); 
- +        
         if (strlen(line) == 0) {         if (strlen(line) == 0) {
             /* saw header separator */             /* saw header separator */
Line 70: Line 105:
                     nb_syslog("message has been sent %ds ago", age);                     nb_syslog("message has been sent %ds ago", age);
                     if (age > MAXAGE) {                     if (age > MAXAGE) {
-                    nb_syslog("rejecting too old message"); +                        nb_syslog("rejecting too old message"); 
-                        return "";+                        return mkarray();
                     }                     }
                 } else {                 } else {
Line 89: Line 124:
                 if (allowed == 0) {                 if (allowed == 0) {
                     nb_syslog("rejecting message from unknown sender %s", from);                     nb_syslog("rejecting message from unknown sender %s", from);
-                    return "";+                    return mkarray();
                 } else {                 } else {
                     nb_syslog("sender %s can pass", from);                     nb_syslog("sender %s can pass", from);
Line 102: Line 137:
                 if (left(line, strlen(ADMPWD)) != ADMPWD) {                 if (left(line, strlen(ADMPWD)) != ADMPWD) {
                     nb_syslog("authentication failed");                     nb_syslog("authentication failed");
-                    return "";+                    return mkarray();
                 } else {                 } else {
                     nb_syslog("authentication succeeded");                     nb_syslog("authentication succeeded");
Line 108: Line 143:
             } else if ((AUTH && tlnr == 1) || (!AUTH && tlnr == 0)) {             } else if ((AUTH && tlnr == 1) || (!AUTH && tlnr == 0)) {
                 /* this line must contain the command */                 /* this line must contain the command */
-                if (left(line, 6) == "reboot") { +                return split(line);
-                    return "reboot"; +
-                } else if (left(line, 7) == "connect") { +
-                    return "connect"; +
-                } else if (left(line, 10) == "disconnect") { +
-                    return "disconnect"; +
-                } else if (left(line, 6) == "status") { +
-                    return "status"; +
-                } else if (left(line, 6) == "output") { +
-                    return left(line, 13); +
-                }+
             } else {             } else {
                 break;                 break;
Line 128: Line 153:
     nb_syslog("no command detected");     nb_syslog("no command detected");
  
-    return "";+    return mkarray();
 } }
  
-int setdio (string cmd+int setdio (string port, string newstate
 { {
-    newstate = substr(cmd, 9, 3); 
-    port = substr(cmd, 7, 1); 
-     
     if (port != "1" && port != "2") {     if (port != "1" && port != "2") {
         nb_syslog("invalid DIO port %s\n", port);                 nb_syslog("invalid DIO port %s\n", port);        
Line 199: Line 221:
 /* track states */ /* track states */
 reboot = 0; reboot = 0;
-connecting = 0; 
-disconnecting = 0; 
- 
  
 /* only process latest messages */ /* only process latest messages */
Line 214: Line 233:
                i, nr_msgs, msgs[i]);                i, nr_msgs, msgs[i]);
  
-    cmd = parse(msg); +    args = parse(msg); 
-    if (cmd == "reboot") {    +    if ((is_array(args)) && (length(args) > 0)) { 
-        nb_syslog("reboot command received"); +        if (args[0] == "") { 
-        reboot = 1; +            nb_syslog("no command"); 
-    } else if (cmd == "connect"+        } else if ((args[0] == "reboot"&& (length(args) == 1)) {    
-        nb_syslog("connect command received"); +            nb_syslog("reboot command received"); 
-        if (connecting) { +            reboot = 1; 
-            nb_syslog("already connecting"); +        } else if ((args[0] == "connect"&& (length(args) == 1)) { 
-        } else { +            nb_syslog("connect command received"); 
-            /* enable first wanlink */ +            if (nb_config_get("wanlink.0.mode") == "1") { 
-            nb_config_set("wanlink.0.mode=1"); +                nb_syslog("already connecting");
-            connecting = 1; +
-        +
-    } else if (cmd == "disconnect"+
-        nb_syslog("disconnect command received")+
-        if (disconnecting) { +
-            nb_syslog("already disconnecting"); +
-        } else { +
-            /* disable first wanlink */ +
-            nb_config_set("wanlink.0.mode=0")+
-            disconnecting = 1; +
-        } +
-    } else if (cmd == "status") { +
-        nb_syslog("status command received"); +
-        rcpt = nb_sms_header(msgs[i], "From"); +
-        if (rcpt) { +
-            id = nb_sms_send(rcpt, nb_status_summary()); +
-            if (!id) { +
-                nb_syslog("unable to send status message to %s", rcpt);+
             } else {             } else {
-                nb_syslog("successfully queued status message to %s (ID %s)", rcpt, id);+                /* enable first wanlink */ 
 +                nb_config_set("wanlink.0.mode=1");
             }             }
 +        } else if ((args[0] == "disconnect") && (length(args) == 1)) {
 +            nb_syslog("disconnect command received");
 +            if (nb_config_get("wanlink.0.mode") == "0") {
 +                nb_syslog("already disconnecting");
 +            } else {
 +                /* disable first wanlink */
 +                nb_config_set("wanlink.0.mode=0");
 +            }
 +        } else if ((args[0] == "status") && (length(args) == 1)) {
 +            nb_syslog("status command received");
 +            rcpt = nb_sms_header(msgs[i], "From");
 +            if (rcpt) {
 +                id = nb_sms_send(rcpt, nb_status_summary());
 +                if (!id) {
 +                    nb_syslog("unable to send status message to %s", rcpt);
 +                } else {
 +                    nb_syslog("successfully queued status message to %s (ID %s)", rcpt, id);
 +                }
 +            }
 +        } else if ((args[0] == "output") && (length(args) == 3)) {
 +            nb_syslog("dio out command received");
 +            setdio(args[1], args[2]);
 +        } else {
 +            nb_syslog("command <%s> not supported", msg);
         }         }
-    } else if (left(cmd,6) == "output") { 
-        nb_syslog("dio out command received"); 
-        setdio(cmd); 
     } else {     } else {
         nb_syslog("ignoring invalid message");         nb_syslog("ignoring invalid message");
     }     }
  
-    /* delete message  */+    /* delete message */
     ret = nb_sms_delete(msgs[i]);     ret = nb_sms_delete(msgs[i]);
     if (ret == 0) nb_syslog("deleted message %s", msgs[i]);     if (ret == 0) nb_syslog("deleted message %s", msgs[i]);
Line 260: Line 283:
  
 if (reboot == 1) { if (reboot == 1) {
-    /* trigger reboot  */+    /* trigger reboot */
     nb_syslog("rebooting system");     nb_syslog("rebooting system");
     nb_reboot();     nb_reboot();
Line 266: Line 289:
  
 exit(0); exit(0);
- 
 </code> </code>
- 
-Configuration:{{:sdk:sms-control.pdf|}}