add management commands (add to admin or moderator list)

This commit is contained in:
Green Sky 2023-12-05 19:45:04 +01:00
parent b0b117e615
commit 92cf42ead5
No known key found for this signature in database
4 changed files with 161 additions and 0 deletions

View File

@ -15,6 +15,9 @@ add_executable(totato
./message_command_dispatcher.hpp ./message_command_dispatcher.hpp
./message_command_dispatcher.cpp ./message_command_dispatcher.cpp
./managment_commands.hpp
./managment_commands.cpp
./tox_commands.hpp ./tox_commands.hpp
./tox_commands.cpp ./tox_commands.cpp
) )

View File

@ -15,6 +15,7 @@
#include "./message_cleanser.hpp" #include "./message_cleanser.hpp"
#include "./message_command_dispatcher.hpp" #include "./message_command_dispatcher.hpp"
#include "./managment_commands.hpp"
#include "./tox_commands.hpp" #include "./tox_commands.hpp"
//#include <solanaceae/message3/components.hpp> //#include <solanaceae/message3/components.hpp>
@ -241,6 +242,7 @@ int main(int argc, char** argv) {
} }
} }
registerManagementCommands(mcd, conf, cr, rmm);
registerToxCommands(mcd, conf, cr, rmm, tc, tpi); registerToxCommands(mcd, conf, cr, rmm, tc, tpi);
conf.dump(); 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 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"; std::cout << "\nTOTATO shutting down...\n";
return 0; return 0;

138
src/managment_commands.cpp Normal file
View File

@ -0,0 +1,138 @@
#include "./managment_commands.hpp"
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/util/config_model.hpp>
#include "./message_command_dispatcher.hpp"
#include <solanaceae/message3/components.hpp>
#include <solanaceae/contact/components.hpp>
#include <solanaceae/toxcore/utils.hpp>
#include <iostream>
#include <algorithm>
static std::optional<Contact3> getContactFromIDStr(
Contact3Registry& cr,
std::string_view id_str
) {
const std::vector<uint8_t> id = hex2bin(std::string{id_str});
const auto view = cr.view<Contact::Components::ID>();
const auto found_contact = std::find_if(view.begin(), view.end(), [&id, &view](const Contact3 c) -> bool {
return view.get<Contact::Components::ID>(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<Contact::Components::Name>()) {
status_str += " name: " + c.get<Contact::Components::Name>().name;
} else {
status_str += " name: not found";
}
// connection state
if (c.all_of<Contact::Components::ConnectionState>()) {
status_str += "\n connection: " + std::to_string(c.get<Contact::Components::ConnectionState>().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<Message::Components::ContactFrom>().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
);
}

View File

@ -0,0 +1,16 @@
#pragma once
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/message3/registry_message_model.hpp>
// fwd
class MessageCommandDispatcher;
struct ConfigModelI;
void registerManagementCommands(
MessageCommandDispatcher& mcd,
ConfigModelI& conf,
Contact3Registry& cr,
RegistryMessageModel& rmm
);