params parsing + basic tox commands !! no permissions yet

This commit is contained in:
2023-12-03 20:01:47 +01:00
parent f9eae6ede9
commit bd30bc8fd5
3 changed files with 182 additions and 21 deletions

View File

@@ -1,8 +1,12 @@
#include "./message_command_dispatcher.hpp"
#include "nlohmann/detail/input/position_t.hpp"
#include "solanaceae/message3/registry_message_model.hpp"
#include <cwchar>
#include <solanaceae/util/config_model.hpp>
#include <solanaceae/message3/components.hpp>
#include <string_view>
#include <utility>
#include <iostream>
@@ -29,8 +33,8 @@ MessageCommandDispatcher::MessageCommandDispatcher(
registerCommand(
"host", "",
"help",
[this](std::string_view params) -> bool {
return helpCommand(params);
[this](std::string_view params, Message3Handle m) -> bool {
return helpCommand(params, m);
},
"Get help"
);
@@ -43,8 +47,9 @@ MessageCommandDispatcher::~MessageCommandDispatcher(void) {
void MessageCommandDispatcher::iterate(float time_delta) {
}
static std::string_view get_first_word(std::string_view text) {
static std::string_view get_first_word(std::string_view text, std::string_view::size_type& out_next) {
if (text.empty()) {
out_next = std::string_view::npos;
return text;
}
@@ -52,17 +57,21 @@ static std::string_view get_first_word(std::string_view text) {
const auto pos_first_non_space = text.find_first_not_of(' ');
if (pos_first_non_space == std::string_view::npos) {
// only contains spaces o.o
return ""; // should return text as is?
out_next = std::string_view::npos;
return "";
}
text = text.substr(pos_first_non_space);
out_next += pos_first_non_space;
const auto pos_first_space = text.find_first_of(' ');
if (pos_first_space == 0 || pos_first_space == std::string_view::npos) {
// does not contain spaces
// command is whole message
out_next = std::string_view::npos;
return text;
} else {
out_next += pos_first_space;
return text.substr(0, pos_first_space);
}
}
@@ -71,10 +80,10 @@ void MessageCommandDispatcher::registerCommand(
std::string_view m, // module
std::string_view m_prefix, // module prefix (if any)
std::string_view command, // command
std::function<bool(std::string_view params)>&& fn,
std::function<bool(std::string_view params, Message3Handle m)>&& fn,
std::string_view help_text
) {
std::string full_command_string = std::string{m_prefix} + std::string{command};
std::string full_command_string = (m_prefix.empty() ? "" : std::string{m_prefix} + " ") + std::string{command};
if (_command_map.count(full_command_string)) {
std::cout << "MCD warning: overwriting existing '" << full_command_string << "'\n";
@@ -89,8 +98,14 @@ void MessageCommandDispatcher::registerCommand(
};
}
bool MessageCommandDispatcher::helpCommand(std::string_view params) {
bool MessageCommandDispatcher::helpCommand(std::string_view params, Message3Handle m) {
std::cout << "MCD: help got called '" << params << "'\n";
_rmm.sendText(
m.get<Message::Components::ContactFrom>().c,
"I am still missing :), ping green for how it actually works."
);
return true;
}
@@ -158,20 +173,35 @@ bool MessageCommandDispatcher::onEvent(const Message::Events::MessageConstruct&
std::string_view first_word;
std::string_view second_word;
std::string_view::size_type pos_next = 0;
first_word = get_first_word(message_text);
std::cout << "------- first_word:'" << first_word << "'\n";
first_word = get_first_word(message_text, pos_next);
std::cout << "------- first_word:'" << first_word << "' pos_next:" << pos_next << "\n";
if (first_word.size() != message_text.size()) {
second_word = get_first_word(
message_text.substr(
// TODO: optimize this
message_text.find(first_word)
+ first_word.size()
)
message_text.substr(pos_next),
pos_next
);
}
std::cout << "------- second_word:'" << second_word << "' empty:" << second_word.empty() << "\n";
std::cout << "------- second_word:'" << second_word << "' empty:" << second_word.empty() << " pos_next:" << pos_next << "\n";
std::string params;
if (pos_next != std::string_view::npos && message_text.size() > pos_next+1) {
auto tmp_params = message_text.substr(pos_next);
const auto params_pos_first_non_space = tmp_params.find_first_not_of(' ');
if (params_pos_first_non_space == std::string_view::npos) {
tmp_params = {};
} else if (params_pos_first_non_space != 0) {
// trim leading whitespace
tmp_params = tmp_params.substr(params_pos_first_non_space);
}
params = tmp_params;
std::cout << "------- params:'" << params << "'\n";
}
// first search first + space + second word
if (!second_word.empty()) {
@@ -181,7 +211,7 @@ bool MessageCommandDispatcher::onEvent(const Message::Events::MessageConstruct&
const auto command_it = _command_map.find(query);
if (command_it != _command_map.cend()) {
command_it->second.fn(message_text);
command_it->second.fn(params, e.e);
return true;
}
}
@@ -189,7 +219,8 @@ bool MessageCommandDispatcher::onEvent(const Message::Events::MessageConstruct&
// then seach first word only
const auto command_it = _command_map.find(std::string{first_word});
if (command_it != _command_map.cend()) {
command_it->second.fn(message_text);
params = std::string{second_word} + " " + params;
command_it->second.fn(params, e.e);
return true;
}