lower cmake version, static flake build and mcd getting along

This commit is contained in:
Green Sky 2023-12-03 16:01:33 +01:00
parent 317bd5bb52
commit d44574c284
No known key found for this signature in database
5 changed files with 255 additions and 33 deletions

View File

@ -12,6 +12,8 @@
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem (system:
let let
pkgs = import nixpkgs { inherit system; }; pkgs = import nixpkgs { inherit system; };
# get deps
toxcore-src = pkgs.fetchFromGitHub { toxcore-src = pkgs.fetchFromGitHub {
owner = "Green-Sky"; repo = "c-toxcore"; owner = "Green-Sky"; repo = "c-toxcore";
fetchSubmodules = true; fetchSubmodules = true;
@ -53,8 +55,7 @@
rev = "89e74b35f83d888f8aa2e5230811b8a5e2b101a7"; rev = "89e74b35f83d888f8aa2e5230811b8a5e2b101a7";
hash = "sha256-PQw2290ahYfU13tHGzBttwrvZBXK+wKh6UF4xfUaRWQ="; hash = "sha256-PQw2290ahYfU13tHGzBttwrvZBXK+wKh6UF4xfUaRWQ=";
}; };
in {
packages.default = pkgs.stdenv.mkDerivation {
pname = "totato"; pname = "totato";
version = "0.0.0-dev"; version = "0.0.0-dev";
@ -80,17 +81,26 @@
"-DFETCHCONTENT_SOURCE_DIR_JSON=${pkgs.nlohmann_json.src}" # we care less about version here "-DFETCHCONTENT_SOURCE_DIR_JSON=${pkgs.nlohmann_json.src}" # we care less about version here
]; ];
buildInputs = with pkgs; [
#(libsodium.override { stdenv = pkgs.pkgsStatic.stdenv; })
#pkgsStatic.libsodium
libsodium
];
# TODO: replace with install command # TODO: replace with install command
installPhase = '' installPhase = ''
mkdir -p $out/bin mkdir -p $out/bin
mv bin/totato $out/bin mv bin/totato $out/bin
''; '';
in {
packages.default = pkgs.stdenv.mkDerivation {
inherit pname version src nativeBuildInputs cmakeFlags installPhase;
buildInputs = with pkgs; [
#(libsodium.override { stdenv = pkgs.pkgsStatic.stdenv; })
#pkgsStatic.libsodium
libsodium
];
};
packages.static = pkgs.pkgsStatic.stdenv.mkDerivation {
inherit pname version src nativeBuildInputs cmakeFlags installPhase;
buildInputs = with pkgs.pkgsStatic; [
libsodium
];
}; };
#apps.default = { #apps.default = {

View File

@ -11,6 +11,9 @@ add_executable(totato
./message_cleanser.hpp ./message_cleanser.hpp
./message_cleanser.cpp ./message_cleanser.cpp
./message_command_dispatcher.hpp
./message_command_dispatcher.cpp
) )
target_compile_features(totato PUBLIC cxx_std_17) target_compile_features(totato PUBLIC cxx_std_17)

View File

@ -13,6 +13,7 @@
#include "./tox_client.hpp" #include "./tox_client.hpp"
#include "./auto_dirty.hpp" #include "./auto_dirty.hpp"
#include "./message_cleanser.hpp" #include "./message_cleanser.hpp"
#include "./message_command_dispatcher.hpp"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@ -158,6 +159,19 @@ int main(int argc, char** argv) {
RegistryMessageModel rmm{cr}; RegistryMessageModel rmm{cr};
MessageTimeSort mts{rmm}; MessageTimeSort mts{rmm};
MessageCleanser mc{cr, rmm}; MessageCleanser mc{cr, rmm};
MessageCommandDispatcher mcd{cr, rmm, conf};
{ // setup basic commands for bot
mcd.registerCommand(
"host", "",
"info",
[](std::string_view) -> bool {
std::cout << "INFO got called :)\n";
return true;
},
"Get basic information about this bot"
);
}
PluginManager pm; PluginManager pm;

View File

@ -1,6 +1,20 @@
#include "./message_command_dispatcher.hpp" #include "./message_command_dispatcher.hpp"
#include <solanaceae/util/config_model.hpp> #include <solanaceae/util/config_model.hpp>
#include <solanaceae/message3/components.hpp>
#include <utility>
#include <iostream>
//MessageCommandDispatcher::Command::Command(Command&& other) :
//m(std::move(other.m)),
//m_prefix(std::move(other.m_prefix)),
//command(std::move(other.command)),
//fn(std::move(other.fn)),
//help_text(std::move(other.help_text))
//{
//// is this really needed?
//}
MessageCommandDispatcher::MessageCommandDispatcher( MessageCommandDispatcher::MessageCommandDispatcher(
Contact3Registry& cr, Contact3Registry& cr,
@ -9,6 +23,18 @@ MessageCommandDispatcher::MessageCommandDispatcher(
) : ) :
_cr(cr), _rmm(rmm), _conf(conf) _cr(cr), _rmm(rmm), _conf(conf)
{ {
_rmm.subscribe(this, RegistryMessageModel_Event::message_construct);
{ // setup basic commands for bot
registerCommand(
"host", "",
"help",
[this](std::string_view params) -> bool {
return helpCommand(params);
},
"Get help"
);
}
} }
MessageCommandDispatcher::~MessageCommandDispatcher(void) { MessageCommandDispatcher::~MessageCommandDispatcher(void) {
@ -17,11 +43,161 @@ MessageCommandDispatcher::~MessageCommandDispatcher(void) {
void MessageCommandDispatcher::iterate(float time_delta) { void MessageCommandDispatcher::iterate(float time_delta) {
} }
static std::string_view get_first_word(std::string_view text) {
if (text.empty()) {
return text;
}
// trim
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?
}
text = text.substr(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
return text;
} else {
return text.substr(0, pos_first_space);
}
}
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::string_view help_text
) {
std::string full_command_string = std::string{m_prefix} + std::string{command};
if (_command_map.count(full_command_string)) {
std::cout << "MCD warning: overwriting existing '" << full_command_string << "'\n";
}
_command_map[full_command_string] = Command{
std::string{m},
std::string{m_prefix},
std::string{command},
std::move(fn),
std::string{help_text}
};
}
bool MessageCommandDispatcher::helpCommand(std::string_view params) {
std::cout << "MCD: help got called '" << params << "'\n";
return true;
}
bool MessageCommandDispatcher::onEvent(const Message::Events::MessageConstruct& e) { bool MessageCommandDispatcher::onEvent(const Message::Events::MessageConstruct& e) {
if (!e.e.all_of<Message::Components::MessageText, Message::Components::TagUnread>()) {
std::cout << "MCD: got message that is not";
if (!e.e.all_of<Message::Components::MessageText>()) {
std::cout << " text";
}
if (!e.e.all_of<Message::Components::TagUnread>()) {
std::cout << " unread";
}
std::cout << "\n";
return false; return false;
} }
bool MessageCommandDispatcher::onEvent(const Message::Events::MessageUpdated& e) { if (e.e.any_of<Message::Components::TagMessageIsAction>()) {
std::cout << "MCD: got message that is";
if (e.e.all_of<Message::Components::TagMessageIsAction>()) {
std::cout << " action";
}
std::cout << "\n";
return false;
}
std::string_view message_text = e.e.get<Message::Components::MessageText>().text;
if (message_text.empty()) {
// empty message?
return false;
}
// TODO: skip unrelyable synced
// TODO: is private
// TODO: has the permissions
if (false) { // is private
} else {
// check for command prefix
if (
message_text.at(0) != '!' &&
message_text.at(0) != '/'
) {
// does not start with command prefix, not for us
return false;
}
// remove c prefix
message_text = message_text.substr(1);
}
if (message_text.empty()) {
// empty message?
std::cout << "MCD: got empty command\n";
return false;
}
std::cout << "MCD: got command '" << message_text << "'\n";
std::string_view first_word;
std::string_view second_word;
first_word = get_first_word(message_text);
std::cout << "------- first_word:'" << first_word << "'\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()
)
);
}
std::cout << "------- second_word:'" << second_word << "' empty:" << second_word.empty() << "\n";
// first search first + space + second word
if (!second_word.empty()) {
std::string query {first_word};
query += " ";
query += second_word;
const auto command_it = _command_map.find(query);
if (command_it != _command_map.cend()) {
command_it->second.fn(message_text);
return true;
}
}
// 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);
return true;
}
return false;
}
bool MessageCommandDispatcher::onEvent(const Message::Events::MessageUpdated&) {
// do i need this?
return false; return false;
} }

View File

@ -10,6 +10,17 @@ class MessageCommandDispatcher : public RegistryMessageModelEventI {
RegistryMessageModel& _rmm; RegistryMessageModel& _rmm;
ConfigModelI& _conf; ConfigModelI& _conf;
struct Command {
std::string m; // module
std::string m_prefix; // module prefix (if any)
std::string command; // command
std::function<bool(std::string_view params)> fn;
std::string help_text;
//Command(const Command&) = delete;
};
std::unordered_map<std::string, Command> _command_map;
public: public:
MessageCommandDispatcher(Contact3Registry& cr, RegistryMessageModel& rmm, ConfigModelI& conf); MessageCommandDispatcher(Contact3Registry& cr, RegistryMessageModel& rmm, ConfigModelI& conf);
~MessageCommandDispatcher(void); ~MessageCommandDispatcher(void);
@ -23,11 +34,19 @@ class MessageCommandDispatcher : public RegistryMessageModelEventI {
// - everyone else? // - everyone else?
// callable // callable
// help text? // help text?
void registerCommand( void registerCommand(
std::string_view module, std::string_view m, // module
std::string_view command std::string_view m_prefix, // module prefix (if any)
std::string_view command, // command
std::function<bool(std::string_view params)>&& fn,
std::string_view help_text
); );
// generates a help
bool helpCommand(std::string_view params);
protected: // mm protected: // mm
bool onEvent(const Message::Events::MessageConstruct& e) override; bool onEvent(const Message::Events::MessageConstruct& e) override;
bool onEvent(const Message::Events::MessageUpdated& e) override; bool onEvent(const Message::Events::MessageUpdated& e) override;