diff --git a/esp8266_deauther/SerialInterface.cpp b/esp8266_deauther/CLI.cpp similarity index 95% rename from esp8266_deauther/SerialInterface.cpp rename to esp8266_deauther/CLI.cpp index 8af5b57..d6b2ddc 100644 --- a/esp8266_deauther/SerialInterface.cpp +++ b/esp8266_deauther/CLI.cpp @@ -1,1166 +1,1166 @@ -#include "SerialInterface.h" - -/* - A short introduction for the reader: - This class is a huuuuuuuge mess of if statements!!!!!1!eleven - But it works and I tried to make it as compact as possible. - If someone was able to make an Arduino framework for parsing an input string in different commands with certain - rules, - required- and optional parameters of different datatypes to run different callback functions... shoot me a quick - message on GitHub or Twitter: @spacehuhn! - PS: the framework has to be good in both CPU performance and RAM usage, otherwise it's useless. Good luck! - I already wasted way too much time trying to come up with the best way of programming this, so I will just keep it - that way for now. - Also: NEVER CHANGE A RUNNING SYSTEM! - */ - -SerialInterface::SerialInterface() { - list = new SimpleList; -} - -void SerialInterface::load() { - checkFile(execPath, String()); - executing = true; -} - -void SerialInterface::load(String filepath) { - execPath = filepath; - load(); -} - -void SerialInterface::enable() { - enabled = true; - prntln(CLI_SERIAL_ENABLED); -} - -void SerialInterface::disable() { - enabled = true; - prntln(CLI_SERIAL_DISABLED); -} - -void SerialInterface::error(String message) { - prnt(CLI_ERROR); - prntln(message); -} - -void SerialInterface::parameterError(String parameter) { - prnt(CLI_ERROR_PARAMETER); - prnt(parameter); - prntln(DOUBLEQUOTES); -} - -bool SerialInterface::isInt(String str) { - if (eqls(str, STR_TRUE) || eqls(str, STR_FALSE)) return true; - - for (uint32_t i = 0; i < str.length(); i++) - if (!isDigit(str.charAt(i))) return false; - - return true; -} - -int SerialInterface::toInt(String str) { - if (eqls(str, STR_TRUE)) return 1; - else if (eqls(str, STR_FALSE)) return 0; - else return str.toInt(); -} - -uint32_t SerialInterface::getTime(String time) { - int value = time.toInt(); - - if (value < 0) value = -value; - - if (time.substring(time.length() - 1).equalsIgnoreCase(String(S))) value *= 1000; - else if (time.substring(time.length() - 3).equalsIgnoreCase(str(STR_MIN)) || - (time.charAt(time.length() - 1) == M)) value *= 60000; - return value; -} - -bool SerialInterface::eqlsCMD(int i, const char* keyword) { - return eqls(list->get(i).c_str(), keyword); -} - -void SerialInterface::stopScript() { - if (continuously) { - continuously = false; - prnt(CLI_STOPPED_SCRIPT); - prnt(execPath); - prntln(DOUBLEQUOTES); - prnt(SPACE); - prnt(CLI_CONTINUOUSLY); - } -} - -void SerialInterface::update() { - if (executing) { - if (execPath.charAt(0) != SLASH) execPath = SLASH + execPath; - prnt(CLI_EXECUTING); - prntln(execPath); - File f = SPIFFS.open(execPath, "r"); - - if (f.size() > 0) { - String line; - char tmp; - - while (f.available()) { - tmp = f.read(); - - if (tmp == NEWLINE) { - runCommands(line); - line = String(); - } else { - line += tmp; - } - } - runCommand(line); - } - - f.close(); - executing = false; - - if (continuously) prntln(CLI_SCRIPT_DONE_CONTINUE); - else prntln(CLI_SCRIPT_DONE); - - loopTime = currentTime; - } else { - if (enabled && (Serial.available() > 0)) runCommands(Serial.readStringUntil(NEWLINE)); - - if (continuously) { - if (currentTime - loopTime > continueTime) executing = true; - } - } -} - -void SerialInterface::runCommands(String input) { - String tmp; - - for (uint32_t i = 0; i < input.length(); i++) { - // when 2 semicolons in a row without a backslash escaping the first - if ((input.charAt(i) == SEMICOLON) && (input.charAt(i + 1) == SEMICOLON) && - (input.charAt(i - 1) != BACKSLASH)) { - runCommand(tmp); - tmp = String(); - i++; - } else { - tmp += input.charAt(i); - } - } - - tmp.replace(BACKSLASH + SEMICOLON + SEMICOLON, SEMICOLON + SEMICOLON); - - if (tmp.length() > 0) runCommand(tmp); -} - -void SerialInterface::runCommand(String input) { - input.replace(String(NEWLINE), String()); - input.replace(String(CARRIAGERETURN), String()); - - list->clear(); - - // parse/split input in list - String tmp; - bool withinQuotes = false; - bool escaped = false; - char c; - - for (uint32_t i = 0; i < input.length() && i < 512; i++) { - c = input.charAt(i); - - // when char is an unescaped - if (!escaped && (c == BACKSLASH)) { - escaped = true; - } - - // (when char is a unescaped space AND it's not within quotes) OR char is \r or \n - else if (((c == SPACE) && !escaped && !withinQuotes) || (c == CARRIAGERETURN) || (c == NEWLINE)) { - // when tmp string isn't empty, add it to the list - if (tmp.length() > 0) { - list->add(tmp); - tmp = String(); // reset tmp string - } - } - - // when char is an unescaped " - else if ((c == DOUBLEQUOTES) && !escaped) { - // update wheter or not the following chars are within quotes or not - withinQuotes = !withinQuotes; - - if ((tmp.length() == 0) && !withinQuotes) tmp += SPACE; // when exiting quotes and tmp string is empty, add - // a space - } - - // add character to tmp string - else { - tmp += c; - escaped = false; - } - } - - // add string if something is left from the loop above - if (tmp.length() > 0) list->add(tmp); - - // stop when input is empty/invalid - if (list->size() == 0) return; - - // print comments - if (list->get(0) == str(CLI_COMMENT)) { - prntln(input); - return; - } - - if (settings.getSerialEcho()) { - // print command - prnt(CLI_INPUT_PREFIX); - prntln(input); - } - - if (list->size() == 0) return; - - // ===== HELP ===== // - if (eqlsCMD(0, CLI_HELP)) { - prntln(CLI_HELP_HEADER); - - prntln(CLI_HELP_HELP); - prntln(CLI_HELP_SCAN); - prntln(CLI_HELP_SHOW); - prntln(CLI_HELP_SELECT); - prntln(CLI_HELP_DESELECT); - prntln(CLI_HELP_SSID_A); - prntln(CLI_HELP_SSID_B); - prntln(CLI_HELP_SSID_C); - prntln(CLI_HELP_NAME_A); - prntln(CLI_HELP_NAME_B); - prntln(CLI_HELP_NAME_C); - prntln(CLI_HELP_SET_NAME); - prntln(CLI_HELP_ENABLE_RANDOM); - prntln(CLI_HELP_DISABLE_RANDOM); - prntln(CLI_HELP_LOAD); - prntln(CLI_HELP_SAVE); - prntln(CLI_HELP_REMOVE_A); - prntln(CLI_HELP_REMOVE_B); - prntln(CLI_HELP_ATTACK); - prntln(CLI_HELP_ATTACK_STATUS); - prntln(CLI_HELP_STOP); - prntln(CLI_HELP_SYSINFO); - prntln(CLI_HELP_CLEAR); - prntln(CLI_HELP_FORMAT); - prntln(CLI_HELP_PRINT); - prntln(CLI_HELP_DELETE); - prntln(CLI_HELP_REPLACE); - prntln(CLI_HELP_COPY); - prntln(CLI_HELP_RENAME); - prntln(CLI_HELP_RUN); - prntln(CLI_HELP_WRITE); - prntln(CLI_HELP_GET); - prntln(CLI_HELP_SET); - prntln(CLI_HELP_RESET); - prntln(CLI_HELP_CHICKEN); - prntln(CLI_HELP_REBOOT); - prntln(CLI_HELP_INFO); - prntln(CLI_HELP_COMMENT); - prntln(CLI_HELP_SEND_DEAUTH); - prntln(CLI_HELP_SEND_BEACON); - prntln(CLI_HELP_SEND_PROBE); - prntln(CLI_HELP_LED_A); - prntln(CLI_HELP_LED_B); - prntln(CLI_HELP_LED_ENABLE); - prntln(CLI_HELP_DRAW); - prntln(CLI_HELP_SCREEN_ON); - prntln(CLI_HELP_SCREEN_MODE); - - prntln(CLI_HELP_FOOTER); - } - - // ===== SCAN ===== // - // scan [] [-t