This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
sdk:scripts:sms-control [2015/05/05 15:04] – external edit 127.0.0.1 | sdk:scripts:sms-control [2022/08/26 10:52] – removed dodenhoeft | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== SDK Script sms-control.are ====== | ||
- | <code c sms-control.are> | ||
- | /* DESC: This script will execute commands received by SMS. | ||
- | * Copyright (C) 2012 NetModule AG, Switzerland | ||
- | */ | ||
- | |||
- | INTERVAL | ||
- | MAXMSG | ||
- | MAXAGE | ||
- | MAXLINES = 32; /* max. number of lines in msg (incl. header) | ||
- | AUTH = 1; /* perform authentication | ||
- | ADMPWD | ||
- | IGNORECASE | ||
- | SENDERS | ||
- | |||
- | /* check if we should perform authentication */ | ||
- | if (argc == 2 && argv[1] == " | ||
- | AUTH = 0; | ||
- | } | ||
- | |||
- | /* retrieve password */ | ||
- | if (AUTH) { | ||
- | if (strlen(ADMPWD) == 0) { | ||
- | /* use configured admin password */ | ||
- | ADMPWD = nb_config_get(" | ||
- | if (strlen(ADMPWD) > 0) { | ||
- | nb_syslog(" | ||
- | } else { | ||
- | /* not there -> use a default password instead */ | ||
- | nb_syslog(" | ||
- | ADMPWD = " | ||
- | } | ||
- | } | ||
- | if (IGNORECASE) ADMPWD = tolower(ADMPWD); | ||
- | } | ||
- | |||
- | /* parse message */ | ||
- | string parse (string msg) | ||
- | { | ||
- | /* read by line */ | ||
- | lnr = 0; | ||
- | ishdr = 1; | ||
- | tlnr = 0; | ||
- | allowed = (length(SENDERS) == 0) ? 1 : 0; | ||
- | | ||
- | lp = msg; | ||
- | for (lnr = 0; lnr < MAXLINES && strlen(lp) > 0; lnr++) { | ||
- | pos = strchr(lp, " | ||
- | if (is_void(pos)) pos = strlen(lp); | ||
- | |||
- | line = left(lp, pos); | ||
- | lp = substr(lp, pos + 1); | ||
- | |||
- | if (strlen(line) == 0) { | ||
- | /* saw header separator */ | ||
- | ishdr = 0; | ||
- | continue; | ||
- | } | ||
- | if (ishdr) { | ||
- | /* saw header line */ | ||
- | if (left(line, 5) == " | ||
- | /* check age of message */ | ||
- | sentdate = trim(substr(line, | ||
- | sent = strptime(sentdate, | ||
- | | ||
- | if (!is_void(sent)) { | ||
- | /* got a valid sent date */ | ||
- | now = localtime(time()); | ||
- | age = mktime(now) - mktime(sent); | ||
- | nb_syslog(" | ||
- | if (age > MAXAGE) { | ||
- | nb_syslog(" | ||
- | return ""; | ||
- | } | ||
- | } else { | ||
- | nb_syslog(" | ||
- | } | ||
- | } else if (left(line, 5) == " | ||
- | from = substr(line, | ||
- | if (length(SENDERS) > 0) { | ||
- | for (s = 0; s < length(SENDERS); | ||
- | sender = SENDERS[s]; | ||
- | if (left(from, strlen(sender)) == sender) { | ||
- | allowed = 1; | ||
- | break; | ||
- | } | ||
- | } | ||
- | } | ||
- | if (allowed == 0) { | ||
- | nb_syslog(" | ||
- | return ""; | ||
- | } else { | ||
- | nb_syslog(" | ||
- | } | ||
- | } | ||
- | } else { | ||
- | /* saw text line */ | ||
- | if (IGNORECASE) line = tolower(line); | ||
- | |||
- | if (AUTH && tlnr == 0) { | ||
- | /* first line of message must contain the password */ | ||
- | if (left(line, strlen(ADMPWD)) != ADMPWD) { | ||
- | nb_syslog(" | ||
- | return ""; | ||
- | } else { | ||
- | nb_syslog(" | ||
- | } | ||
- | } else if ((AUTH && tlnr == 1) || (!AUTH && tlnr == 0)) { | ||
- | /* this line must contain the command */ | ||
- | if (left(line, 6) == " | ||
- | return " | ||
- | } else if (left(line, 7) == " | ||
- | return " | ||
- | } else if (left(line, 10) == " | ||
- | return " | ||
- | } else if (left(line, 6) == " | ||
- | return " | ||
- | } else if (left(line, 6) == " | ||
- | return left(line, 13); | ||
- | } | ||
- | } else { | ||
- | break; | ||
- | } | ||
- | tlnr++; | ||
- | } | ||
- | } | ||
- | | ||
- | nb_syslog(" | ||
- | |||
- | return ""; | ||
- | } | ||
- | |||
- | int setdio (string cmd) | ||
- | { | ||
- | newstate = substr(cmd, 9, 3); | ||
- | port = substr(cmd, 7, 1); | ||
- | | ||
- | if (port != " | ||
- | nb_syslog(" | ||
- | return -1; | ||
- | } | ||
- | if (newstate == " | ||
- | st = 1; | ||
- | } else if (newstate == " | ||
- | st = 0; | ||
- | } else { | ||
- | nb_syslog(" | ||
- | return -1; | ||
- | } | ||
- | | ||
- | rc = nb_dio_set(sprintf(" | ||
- | if (rc) { | ||
- | nb_syslog(" | ||
- | return -1; | ||
- | } else { | ||
- | nb_syslog(" | ||
- | return 0; | ||
- | } | ||
- | } | ||
- | |||
- | /* check if we got stressed */ | ||
- | LASTFILE = "/ | ||
- | last = 0; | ||
- | |||
- | fp = fopen(LASTFILE, | ||
- | if (fp) { | ||
- | /* we have been run at least one time */ | ||
- | str = fread(fp, 32); | ||
- | if (str) last = (int) str; | ||
- | fclose(fp); | ||
- | } | ||
- | |||
- | now = mktime(localtime(time())); | ||
- | elapsed = now - last; | ||
- | if (elapsed > 0 && elapsed < INTERVAL) { | ||
- | nb_syslog(" | ||
- | exit(0); | ||
- | } | ||
- | |||
- | /* record timestamp */ | ||
- | fp = fopen(LASTFILE, | ||
- | if (fp) { | ||
- | str = sprintf(" | ||
- | fwrite(fp, str); | ||
- | fclose(fp); | ||
- | } | ||
- | |||
- | /* read inbox */ | ||
- | msgs = nb_sms_list(); | ||
- | nr_msgs = length(msgs); | ||
- | |||
- | if (nr_msgs == 0) { | ||
- | nb_syslog(" | ||
- | exit(0); | ||
- | } | ||
- | |||
- | nb_syslog(" | ||
- | |||
- | /* track states */ | ||
- | reboot = 0; | ||
- | connecting = 0; | ||
- | disconnecting = 0; | ||
- | |||
- | |||
- | /* only process latest messages */ | ||
- | start = nr_msgs - MAXMSG; | ||
- | if (start < 0) start = 0; | ||
- | |||
- | for (i=start; i< | ||
- | msg = nb_sms_retrieve(msgs[i]); | ||
- | if (!msg) continue; | ||
- | |||
- | nb_syslog(" | ||
- | i, nr_msgs, msgs[i]); | ||
- | |||
- | cmd = parse(msg); | ||
- | if (cmd == " | ||
- | nb_syslog(" | ||
- | reboot = 1; | ||
- | } else if (cmd == " | ||
- | nb_syslog(" | ||
- | if (connecting) { | ||
- | nb_syslog(" | ||
- | } else { | ||
- | /* enable first wanlink */ | ||
- | nb_config_set(" | ||
- | connecting = 1; | ||
- | } | ||
- | } else if (cmd == " | ||
- | nb_syslog(" | ||
- | if (disconnecting) { | ||
- | nb_syslog(" | ||
- | } else { | ||
- | /* disable first wanlink */ | ||
- | nb_config_set(" | ||
- | disconnecting = 1; | ||
- | } | ||
- | } else if (cmd == " | ||
- | nb_syslog(" | ||
- | rcpt = nb_sms_header(msgs[i], | ||
- | if (rcpt) { | ||
- | id = nb_sms_send(rcpt, | ||
- | if (!id) { | ||
- | nb_syslog(" | ||
- | } else { | ||
- | nb_syslog(" | ||
- | } | ||
- | } | ||
- | } else if (left(cmd, | ||
- | nb_syslog(" | ||
- | setdio(cmd); | ||
- | } else { | ||
- | nb_syslog(" | ||
- | } | ||
- | |||
- | /* delete message | ||
- | ret = nb_sms_delete(msgs[i]); | ||
- | if (ret == 0) nb_syslog(" | ||
- | } | ||
- | |||
- | if (reboot == 1) { | ||
- | /* trigger reboot | ||
- | nb_syslog(" | ||
- | nb_reboot(); | ||
- | } | ||
- | |||
- | exit(0); | ||
- | |||
- | </ | ||