add simple transfer autoaccept plugin

This commit is contained in:
Green Sky 2023-08-19 23:22:33 +02:00
parent 9d1d0e8665
commit 2e0bc95816
No known key found for this signature in database
4 changed files with 195 additions and 0 deletions

View File

@ -32,3 +32,17 @@ target_link_libraries(plugin_zox_ngc_hs PUBLIC
solanaceae_plugin
solanaceae_zox
)
########################################
add_library(plugin_transfer_auto_accept SHARED
./plugin_transfer_auto_accept.cpp
./transfer_auto_accept.hpp
./transfer_auto_accept.cpp
)
target_link_libraries(plugin_transfer_auto_accept PUBLIC
solanaceae_plugin
solanaceae_util
solanaceae_message3
)

View File

@ -0,0 +1,72 @@
#include <solanaceae/plugin/solana_plugin_v1.h>
#include "./transfer_auto_accept.hpp"
#include <solanaceae/util/config_model.hpp>
#include <memory>
#include <iostream>
#define RESOLVE_INSTANCE(x) static_cast<x*>(solana_api->resolveInstance(#x))
#define PROVIDE_INSTANCE(x, p, v) solana_api->provideInstance(#x, p, static_cast<x*>(v))
static std::unique_ptr<TransferAutoAccept> g_taa = nullptr;
extern "C" {
SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) {
return "TransferAutoAccept";
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) {
return SOLANA_PLUGIN_VERSION;
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) {
std::cout << "PLUGIN TAA START()\n";
if (solana_api == nullptr) {
return 1;
}
RegistryMessageModel* rmm = nullptr;
ConfigModelI* conf = nullptr;
{ // make sure required types are loaded
rmm = RESOLVE_INSTANCE(RegistryMessageModel);
conf = RESOLVE_INSTANCE(ConfigModelI);
if (rmm == nullptr) {
std::cerr << "PLUGIN TAA missing RegistryMessageModel\n";
return 2;
}
if (conf == nullptr) {
std::cerr << "PLUGIN TAA missing ConfigModelI\n";
return 2;
}
}
// static store, could be anywhere tho
// construct with fetched dependencies
g_taa = std::make_unique<TransferAutoAccept>(*rmm, *conf);
// register types
PROVIDE_INSTANCE(TransferAutoAccept, "TransferAutoAccept", g_taa.get());
return 0;
}
SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) {
std::cout << "PLUGIN TAA STOP()\n";
g_taa.reset();
}
SOLANA_PLUGIN_EXPORT void solana_plugin_tick(float delta) {
(void)delta;
//std::cout << "PLUGIN TAA TICK()\n";
g_taa->iterate();
}
} // extern C

View File

@ -0,0 +1,78 @@
#include "./transfer_auto_accept.hpp"
#include <solanaceae/message3/components.hpp>
#include <solanaceae/util/config_model.hpp>
#include <iostream>
TransferAutoAccept::TransferAutoAccept(RegistryMessageModel& rmm, ConfigModelI& conf) : _rmm(rmm), _conf(conf) {
_rmm.subscribe(this, RegistryMessageModel_Event::message_construct);
_rmm.subscribe(this, RegistryMessageModel_Event::message_updated);
if (!_conf.has_string("TransferAutoAccept", "save_path")) {
_conf.set("TransferAutoAccept", "save_path", std::string_view{"tmp_save_dir"});
}
// dont load module instead?
//if (!_conf.has_bool("TransferAutoAccept", "autoaccept")) {
//_conf.set("TransferAutoAccept", "autoaccept", false); // safe default
//}
if (!_conf.has_int("TransferAutoAccept", "autoaccept_limit")) {
_conf.set("TransferAutoAccept", "autoaccept_limit", 50l*1024l*1024l); // sane default
}
}
void TransferAutoAccept::iterate(void) {
for (auto& it : _accept_queue) {
if (it.all_of<Message::Components::Transfer::ActionAccept>()) {
continue; // already accepted
}
it.emplace<Message::Components::Transfer::ActionAccept>(
// TODO: contact to entry
_conf.get_string("TransferAutoAccept", "save_path").value_or("tmp_save_dir")
);
std::cout << "TAA: auto accpeted transfer\n";
_rmm.throwEventUpdate(it);
}
_accept_queue.clear();
}
void TransferAutoAccept::checkMsg(Message3Handle h) {
if (h.all_of<Message::Components::Transfer::ActionAccept>()) {
return; // already accepted
}
if (!h.all_of<Message::Components::Transfer::TagReceiving, Message::Components::Transfer::TagPaused, Message::Components::Transfer::FileInfo>()) {
return;
}
const auto& file_info = h.get<Message::Components::Transfer::FileInfo>();
// TODO: contact to entry
if (file_info.total_size > uint64_t(_conf.get_int("TransferAutoAccept", "autoaccept_limit").value_or(1024*1024))) {
return; // too large
}
if (file_info.file_list.empty() || file_info.file_list.front().file_name.empty()) {
return; // bad file
}
_accept_queue.push_back(h);
}
bool TransferAutoAccept::onEvent(const Message::Events::MessageConstruct& e) {
std::cout << "TAA: msg c\n";
checkMsg(e.e);
return false;
}
bool TransferAutoAccept::onEvent(const Message::Events::MessageUpdated& e) {
std::cout << "TAA: msg u\n";
checkMsg(e.e);
return false;
}

View File

@ -0,0 +1,31 @@
#pragma once
#include <solanaceae/message3/registry_message_model.hpp>
#include <vector>
// fwd
struct ConfigModelI;
class TransferAutoAccept : public RegistryMessageModelEventI {
RegistryMessageModel& _rmm;
//ContactModelI& _cm;
ConfigModelI& _conf;
std::vector<Message3Handle> _accept_queue;
public:
TransferAutoAccept(RegistryMessageModel& rmm, ConfigModelI& conf);
// TODO: iterate
void iterate(void);
protected:
void checkMsg(Message3Handle h);
protected: // mm
bool onEvent(const Message::Events::MessageConstruct& e) override;
bool onEvent(const Message::Events::MessageUpdated& e) override;
};