diff --git a/examples/companion_radio/FirmwareBot.cpp b/examples/companion_radio/FirmwareBot.cpp index 56a3395..de44fb9 100644 --- a/examples/companion_radio/FirmwareBot.cpp +++ b/examples/companion_radio/FirmwareBot.cpp @@ -163,6 +163,10 @@ void recordCommandCooldown(BotCommandCooldown* cooldowns, size_t cooldown_count, } bool parseCommand(const char* text, size_t text_len, BotCommand* command) { + return parseCommand(text, text_len, command, false); +} + +bool parseCommand(const char* text, size_t text_len, BotCommand* command, bool allow_prefixless) { if (!command) return false; memset(command, 0, sizeof(*command)); command->id = BOT_COMMAND_NONE; @@ -171,9 +175,13 @@ bool parseCommand(const char* text, size_t text_len, BotCommand* command) { size_t normalized_len = 0; normalizeText(text, text_len, normalized, sizeof(normalized), &normalized_len); - if (normalized_len < 2 || (normalized[0] != '!' && normalized[0] != '/')) return false; + if (normalized_len == 0) return false; + + bool has_prefix = normalized[0] == '!' || normalized[0] == '/'; + if (!has_prefix && !allow_prefixless) return false; + if (has_prefix && normalized_len < 2) return false; - size_t pos = 1; + size_t pos = has_prefix ? 1 : 0; while (pos < normalized_len && normalized[pos] == ' ') pos++; size_t name_start = pos; while (pos < normalized_len && !isCommandDelimiter(normalized[pos])) pos++; @@ -187,6 +195,7 @@ bool parseCommand(const char* text, size_t text_len, BotCommand* command) { } command->name[copy_name_len] = 0; command->id = name_len > BOT_MAX_COMMAND_NAME_LEN ? BOT_COMMAND_UNKNOWN : commandIdForName(command->name, copy_name_len); + if (!has_prefix && command->id == BOT_COMMAND_UNKNOWN) return false; while (pos < normalized_len && isCommandDelimiter(normalized[pos])) pos++; size_t args_len = normalized_len - pos; diff --git a/examples/companion_radio/FirmwareBot.h b/examples/companion_radio/FirmwareBot.h index 42b9c5b..e248adc 100644 --- a/examples/companion_radio/FirmwareBot.h +++ b/examples/companion_radio/FirmwareBot.h @@ -6,6 +6,7 @@ namespace FirmwareBot { BotWriteResult normalizeText(const char* input, size_t input_len, char* output, size_t output_len, size_t* written); bool parseCommand(const char* text, size_t text_len, BotCommand* command); +bool parseCommand(const char* text, size_t text_len, BotCommand* command, bool allow_prefixless); bool splitChannelText(const char* text, size_t text_len, char* sender, size_t sender_len, const char** body, size_t* body_len); BotWriteResult writeResponse(char* output, size_t output_len, const char* text, size_t text_len, size_t* written); diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 13ead48..ec6a780 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -1011,7 +1011,7 @@ void MyMesh::recordBotObservation(const BotMessage &message, const ContactInfo * sendQueuedBotResponses(); BotCommand command; - if (!FirmwareBot::parseCommand(message.text, message.text_len, &command)) { + if (!FirmwareBot::parseCommand(message.text, message.text_len, &command, true)) { if (message.text_len > 0 && (message.text[0] == '!' || message.text[0] == '/')) bot_stats.parse_errors++; return; }