From b0b117e6157baf9431e924136fbd9e4c494fb341 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 5 Dec 2023 10:41:23 +0100 Subject: [PATCH] add moderator --- src/CMakeLists.txt | 3 + src/main.cpp | 136 ++------------------------ src/message_command_dispatcher.cpp | 11 ++- src/tox_commands.cpp | 147 +++++++++++++++++++++++++++++ src/tox_commands.hpp | 20 ++++ 5 files changed, 187 insertions(+), 130 deletions(-) create mode 100644 src/tox_commands.cpp create mode 100644 src/tox_commands.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 22ddd25..53f7aa9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,9 @@ add_executable(totato ./message_command_dispatcher.hpp ./message_command_dispatcher.cpp + + ./tox_commands.hpp + ./tox_commands.cpp ) target_compile_features(totato PUBLIC cxx_std_17) diff --git a/src/main.cpp b/src/main.cpp index 6ced6a5..b24c3c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,10 +15,12 @@ #include "./message_cleanser.hpp" #include "./message_command_dispatcher.hpp" -#include -#include -#include -#include +#include "./tox_commands.hpp" + +//#include +//#include +//#include +//#include #include @@ -203,130 +205,6 @@ int main(int argc, char** argv) { ToxMessageManager tmm{rmm, cr, tcm, tc, tc}; ToxTransferManager ttm{rmm, cr, tcm, tc, tc}; - { // setup more advanced commands - mcd.registerCommand( - "tox", "tox", - "status", - [&](std::string_view, Message3Handle m) -> bool { - const auto tox_self_status = tc.toxSelfGetConnectionStatus(); - - const auto contact_from = m.get().c; - - std::string reply{"dht:"}; - - if (tox_self_status == Tox_Connection::TOX_CONNECTION_UDP) { - reply += "upd-direct"; - } else if (tox_self_status == Tox_Connection::TOX_CONNECTION_TCP) { - reply += "tcp-relayed"; - } - - if (cr.all_of(contact_from)) { - const auto con_opt = tc.toxFriendGetConnectionStatus(cr.get(contact_from).friend_number); - if (!con_opt.has_value() || con_opt.value() == Tox_Connection::TOX_CONNECTION_NONE) { - reply += "\nfriend:offline"; - } else if (con_opt.value() == Tox_Connection::TOX_CONNECTION_UDP) { - reply += "\nfriend:udp-direct"; - } else if (con_opt.value() == Tox_Connection::TOX_CONNECTION_TCP) { - reply += "\nfriend:tcp-relayed"; - } - } else if (cr.all_of(contact_from)) { - const auto [group_number, peer_number] = cr.get(contact_from); - - const auto [con_opt, _] = tc.toxGroupPeerGetConnectionStatus(group_number, peer_number); - if (!con_opt.has_value() || con_opt.value() == Tox_Connection::TOX_CONNECTION_NONE) { - reply += "\ngroup-peer:offline"; - } else if (con_opt.value() == Tox_Connection::TOX_CONNECTION_UDP) { - reply += "\ngroup-peer:udp-direct"; - } else if (con_opt.value() == Tox_Connection::TOX_CONNECTION_TCP) { - reply += "\ngroup-peer:tcp-relayed"; - } - } else if (cr.any_of(contact_from)) { - reply += "\noffline"; - } else { - reply += "\nunk"; - } - - rmm.sendText( - contact_from, - reply - ); - - return true; - }, - "Query the tox status of dht and to you.", - MessageCommandDispatcher::Perms::EVERYONE - ); - - mcd.registerCommand( - "tox", "tox", - "add", - [&](std::string_view params, Message3Handle m) -> bool { - const auto contact_from = m.get().c; - - if (params.size() != 38*2) { - rmm.sendText( - contact_from, - "error adding friend, id is not the correct size!" - ); - return true; - } - - // TODO: add tcm interface - const auto [_, err] = tc.toxFriendAdd(hex2bin(std::string{params}), "Add me, I am totato"); - - if (err == Tox_Err_Friend_Add::TOX_ERR_FRIEND_ADD_OK) { - rmm.sendText( - contact_from, - "freind request sent" - ); - } else { - rmm.sendText( - contact_from, - "error adding friend, error code " + std::to_string(err) - ); - } - - return true; - }, - "add a tox friend by id" - ); - - mcd.registerCommand( - "tox", "tox", - "join", - [&](std::string_view params, Message3Handle m) -> bool { - const auto contact_from = m.get().c; - - if (params.size() != 32*2) { - rmm.sendText( - contact_from, - "error joining group, id is not the correct size!" - ); - return true; - } - - auto name_opt = conf.get_string("tox", "name"); - - // TODO: add tcm interface - const auto [_, err] = tc.toxGroupJoin(hex2bin(std::string{params}), std::string{name_opt.value_or("no-name-found")}, ""); - if (err == Tox_Err_Group_Join::TOX_ERR_GROUP_JOIN_OK) { - rmm.sendText( - contact_from, - "joining group..." - ); - } else { - rmm.sendText( - contact_from, - "error joining group, error code " + std::to_string(err) - ); - } - - return true; - }, - "join a tox group by id" - ); - } - { // setup plugin instances g_provideInstance("ConfigModelI", "host", &conf); g_provideInstance("Contact3Registry", "host", &cr); @@ -363,6 +241,8 @@ int main(int argc, char** argv) { } } + registerToxCommands(mcd, conf, cr, rmm, tc, tpi); + conf.dump(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); // at startup, just to be safe diff --git a/src/message_command_dispatcher.cpp b/src/message_command_dispatcher.cpp index b49287d..738fe4e 100644 --- a/src/message_command_dispatcher.cpp +++ b/src/message_command_dispatcher.cpp @@ -29,8 +29,9 @@ MessageCommandDispatcher::MessageCommandDispatcher( ) : _cr(cr), _rmm(rmm), _conf(conf) { - // overwrite default admin to false + // overwrite default admin and moderator to false _conf.set("MessageCommandDispatcher", "admin", false); + _conf.set("MessageCommandDispatcher", "moderator", false); _rmm.subscribe(this, RegistryMessageModel_Event::message_construct); @@ -203,7 +204,13 @@ bool MessageCommandDispatcher::hasPermission(const Command& cmd, const Contact3 return is_admin_opt.value(); } - // TODO: moderator + + if ((cmd.perms & Perms::MODERATOR) != 0) { + auto is_mod_opt = _conf.get_bool("MessageCommandDispatcher", "moderator", id_str); + assert(is_mod_opt.has_value); + + return is_mod_opt.value(); + } return false; } diff --git a/src/tox_commands.cpp b/src/tox_commands.cpp new file mode 100644 index 0000000..b177c67 --- /dev/null +++ b/src/tox_commands.cpp @@ -0,0 +1,147 @@ +#include "./tox_commands.hpp" + +#include +#include +#include +#include + +#include "./message_command_dispatcher.hpp" + +#include +#include +#include +#include + +#include + +void registerToxCommands( + MessageCommandDispatcher& mcd, + ConfigModelI& conf, + Contact3Registry& cr, + RegistryMessageModel& rmm, + ToxI& t, + ToxPrivateI& tp +) { + mcd.registerCommand( + "tox", "tox", + "status", + [&](std::string_view, Message3Handle m) -> bool { + const auto tox_self_status = t.toxSelfGetConnectionStatus(); + + const auto contact_from = m.get().c; + + std::string reply{"dht:"}; + + if (tox_self_status == Tox_Connection::TOX_CONNECTION_UDP) { + reply += "upd-direct"; + } else if (tox_self_status == Tox_Connection::TOX_CONNECTION_TCP) { + reply += "tcp-relayed"; + } + + if (cr.all_of(contact_from)) { + const auto con_opt = t.toxFriendGetConnectionStatus(cr.get(contact_from).friend_number); + if (!con_opt.has_value() || con_opt.value() == Tox_Connection::TOX_CONNECTION_NONE) { + reply += "\nfriend:offline"; + } else if (con_opt.value() == Tox_Connection::TOX_CONNECTION_UDP) { + reply += "\nfriend:udp-direct"; + } else if (con_opt.value() == Tox_Connection::TOX_CONNECTION_TCP) { + reply += "\nfriend:tcp-relayed"; + } + } else if (cr.all_of(contact_from)) { + const auto [group_number, peer_number] = cr.get(contact_from); + + const auto [con_opt, _] = t.toxGroupPeerGetConnectionStatus(group_number, peer_number); + if (!con_opt.has_value() || con_opt.value() == Tox_Connection::TOX_CONNECTION_NONE) { + reply += "\ngroup-peer:offline"; + } else if (con_opt.value() == Tox_Connection::TOX_CONNECTION_UDP) { + reply += "\ngroup-peer:udp-direct"; + } else if (con_opt.value() == Tox_Connection::TOX_CONNECTION_TCP) { + reply += "\ngroup-peer:tcp-relayed"; + } + } else if (cr.any_of(contact_from)) { + reply += "\noffline"; + } else { + reply += "\nunk"; + } + + rmm.sendText( + contact_from, + reply + ); + + return true; + }, + "Query the tox status of dht and to you.", + MessageCommandDispatcher::Perms::EVERYONE + ); + + mcd.registerCommand( + "tox", "tox", + "add", + [&](std::string_view params, Message3Handle m) -> bool { + const auto contact_from = m.get().c; + + if (params.size() != 38*2) { + rmm.sendText( + contact_from, + "error adding friend, id is not the correct size!" + ); + return true; + } + + // TODO: add tcm interface + const auto [_, err] = t.toxFriendAdd(hex2bin(std::string{params}), "Add me, I am totato"); + + if (err == Tox_Err_Friend_Add::TOX_ERR_FRIEND_ADD_OK) { + rmm.sendText( + contact_from, + "freind request sent" + ); + } else { + rmm.sendText( + contact_from, + "error adding friend, error code " + std::to_string(err) + ); + } + + return true; + }, + "add a tox friend by id" + ); + + mcd.registerCommand( + "tox", "tox", + "join", + [&](std::string_view params, Message3Handle m) -> bool { + const auto contact_from = m.get().c; + + if (params.size() != 32*2) { + rmm.sendText( + contact_from, + "error joining group, id is not the correct size!" + ); + return true; + } + + auto name_opt = conf.get_string("tox", "name"); + + // TODO: add tcm interface + const auto [_, err] = t.toxGroupJoin(hex2bin(std::string{params}), std::string{name_opt.value_or("no-name-found")}, ""); + if (err == Tox_Err_Group_Join::TOX_ERR_GROUP_JOIN_OK) { + rmm.sendText( + contact_from, + "joining group..." + ); + } else { + rmm.sendText( + contact_from, + "error joining group, error code " + std::to_string(err) + ); + } + + return true; + }, + "join a tox group by id" + ); +} + diff --git a/src/tox_commands.hpp b/src/tox_commands.hpp new file mode 100644 index 0000000..a9fdbb3 --- /dev/null +++ b/src/tox_commands.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +// fwd +class MessageCommandDispatcher; +struct ConfigModelI; +class ToxI; +class ToxPrivateI; + +void registerToxCommands( + MessageCommandDispatcher& mcd, + ConfigModelI& conf, + Contact3Registry& cr, + RegistryMessageModel& rmm, + ToxI& t, + ToxPrivateI& tp +); +