diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 53f7aa9..55ef988 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,9 @@ add_executable(totato ./message_command_dispatcher.hpp ./message_command_dispatcher.cpp + ./managment_commands.hpp + ./managment_commands.cpp + ./tox_commands.hpp ./tox_commands.cpp ) diff --git a/src/main.cpp b/src/main.cpp index b24c3c7..0c0d129 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ #include "./message_cleanser.hpp" #include "./message_command_dispatcher.hpp" +#include "./managment_commands.hpp" #include "./tox_commands.hpp" //#include @@ -241,6 +242,7 @@ int main(int argc, char** argv) { } } + registerManagementCommands(mcd, conf, cr, rmm); registerToxCommands(mcd, conf, cr, rmm, tc, tpi); conf.dump(); @@ -269,6 +271,8 @@ int main(int argc, char** argv) { std::this_thread::sleep_for(std::chrono::milliseconds(20)); // HACK: until i figure out the best main loop } + conf.dump(); + std::cout << "\nTOTATO shutting down...\n"; return 0; diff --git a/src/managment_commands.cpp b/src/managment_commands.cpp new file mode 100644 index 0000000..6dc5a0c --- /dev/null +++ b/src/managment_commands.cpp @@ -0,0 +1,138 @@ +#include "./managment_commands.hpp" + +#include +#include + +#include "./message_command_dispatcher.hpp" + +#include +#include +#include + +#include +#include + +static std::optional getContactFromIDStr( + Contact3Registry& cr, + std::string_view id_str +) { + const std::vector id = hex2bin(std::string{id_str}); + const auto view = cr.view(); + const auto found_contact = std::find_if(view.begin(), view.end(), [&id, &view](const Contact3 c) -> bool { + return view.get(c).data == id; + }); + + if (found_contact != view.end()) { + return *found_contact; + } else { + return std::nullopt; + } +} + +static std::string getStatusFromContact( + Contact3Handle c +) { + std::string status_str; + + // name + if (c.all_of()) { + status_str += " name: " + c.get().name; + } else { + status_str += " name: not found"; + } + + // connection state + if (c.all_of()) { + status_str += "\n connection: " + std::to_string(c.get().state); + } else { + status_str += "\n connection: not found"; + } + + return status_str; +} + +bool handleContactAddToGroup( + ConfigModelI& conf, + Contact3Registry& cr, + RegistryMessageModel& rmm, + + std::string_view params, + Message3Handle m, + + const std::string_view group +) { + const auto contact_from = m.get().c; + + // TODO: move parameter parsing to mcd + if (params.empty()) { + rmm.sendText( + contact_from, + std::string{"error: command requires the ID for the contact to promote to "} + std::string{group} + ); + + return true; + } + + const auto target_opt = getContactFromIDStr(cr, params); + if (!target_opt.has_value()) { + rmm.sendText( + contact_from, + "error: unknown contact\n" + "For practicality reasons the contact has to be observed once, before it can be added." + ); + + return true; + } + + std::string reply; + + if (conf.get_bool("MessageCommandDispatcher", group, params).value_or(false)) { + reply += "warning: contact already "; + reply += group; + reply += "!\n"; + } + + conf.set("MessageCommandDispatcher", group, params, true); + + reply += "Added '"; + reply += params; + reply += "' to the "; + reply += group; + reply += "s.\n"; + reply += getStatusFromContact(Contact3Handle{cr, target_opt.value()}); + + rmm.sendText( + contact_from, + reply + ); + + return true; +} + +void registerManagementCommands( + MessageCommandDispatcher& mcd, + ConfigModelI& conf, + Contact3Registry& cr, + RegistryMessageModel& rmm +) { + mcd.registerCommand( + "Management", "manage", + "admin-add", + [&](std::string_view params, Message3Handle m) -> bool { + return handleContactAddToGroup(conf, cr, rmm, params, m, "admin"); + }, + "Add an admin by ID.", + MessageCommandDispatcher::Perms::ADMIN + ); + + mcd.registerCommand( + "Management", "manage", + "mod-add", + [&](std::string_view params, Message3Handle m) -> bool { + return handleContactAddToGroup(conf, cr, rmm, params, m, "moderator"); + }, + "Add a moderator by ID.", + MessageCommandDispatcher::Perms::ADMIN + ); +} + diff --git a/src/managment_commands.hpp b/src/managment_commands.hpp new file mode 100644 index 0000000..df01eb5 --- /dev/null +++ b/src/managment_commands.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +// fwd +class MessageCommandDispatcher; +struct ConfigModelI; + +void registerManagementCommands( + MessageCommandDispatcher& mcd, + ConfigModelI& conf, + Contact3Registry& cr, + RegistryMessageModel& rmm +); +