2023-08-19 23:22:33 +02:00
|
|
|
#include "./transfer_auto_accept.hpp"
|
|
|
|
|
2024-07-31 20:07:14 +02:00
|
|
|
#include <solanaceae/object_store/meta_components_file.hpp>
|
|
|
|
|
2023-08-19 23:22:33 +02:00
|
|
|
#include <solanaceae/message3/components.hpp>
|
2023-10-14 01:08:45 +02:00
|
|
|
// for comp transfer tox filekind (TODO: generalize -> content system?)
|
2024-07-31 20:07:14 +02:00
|
|
|
#include <solanaceae/tox_messages/obj_components.hpp>
|
2023-08-19 23:22:33 +02:00
|
|
|
|
|
|
|
#include <solanaceae/util/config_model.hpp>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2024-10-06 11:56:20 +02:00
|
|
|
TransferAutoAccept::TransferAutoAccept(ObjectStore2& os, RegistryMessageModelI& rmm, ConfigModelI& conf) : _os(os), _rmm(rmm), _conf(conf) {
|
2024-07-31 20:07:14 +02:00
|
|
|
//_os.subscribe(this, ObjectStore_Event::object_update);
|
|
|
|
|
2023-08-19 23:22:33 +02:00
|
|
|
_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")) {
|
2023-08-19 23:25:36 +02:00
|
|
|
_conf.set("TransferAutoAccept", "autoaccept_limit", int64_t(50l*1024l*1024l)); // sane default
|
2023-08-19 23:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransferAutoAccept::iterate(void) {
|
|
|
|
for (auto& it : _accept_queue) {
|
2024-07-31 20:07:14 +02:00
|
|
|
if (it.all_of<ObjComp::Ephemeral::File::ActionTransferAccept>()) {
|
2023-08-19 23:22:33 +02:00
|
|
|
continue; // already accepted
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:07:14 +02:00
|
|
|
it.emplace<ObjComp::Ephemeral::File::ActionTransferAccept>(
|
2023-08-19 23:22:33 +02:00
|
|
|
// TODO: contact to entry
|
2023-10-14 14:35:58 +02:00
|
|
|
_conf.get_string("TransferAutoAccept", "save_path").value_or("tmp_save_dir"),
|
|
|
|
false
|
2023-08-19 23:22:33 +02:00
|
|
|
);
|
2023-10-14 01:08:45 +02:00
|
|
|
std::cout << "TAA: auto accepted transfer\n";
|
2024-07-31 20:07:14 +02:00
|
|
|
_os.throwEventUpdate(it);
|
|
|
|
//_rmm.throwEventUpdate(it);
|
2023-08-19 23:22:33 +02:00
|
|
|
}
|
|
|
|
_accept_queue.clear();
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:07:14 +02:00
|
|
|
void TransferAutoAccept::checkObj(ObjectHandle o) {
|
2024-10-30 15:02:22 +01:00
|
|
|
if (!o) {
|
|
|
|
return; // invalid object
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:07:14 +02:00
|
|
|
if (o.all_of<ObjComp::Ephemeral::File::ActionTransferAccept>()) {
|
2023-08-19 23:22:33 +02:00
|
|
|
return; // already accepted
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:07:14 +02:00
|
|
|
if (o.all_of<ObjComp::F::TagLocalHaveAll>()) {
|
|
|
|
return; // alreay have
|
|
|
|
}
|
|
|
|
|
|
|
|
//if (!h.all_of<Message::Components::Transfer::TagReceiving, Message::Components::Transfer::TagPaused, Message::Components::Transfer::FileInfo>()) {
|
|
|
|
// TODO: tag receiving ??
|
|
|
|
if (!o.all_of</*Message::Components::Transfer::TagReceiving, */ObjComp::Ephemeral::File::TagTransferPaused>()) {
|
2023-08-19 23:22:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:07:14 +02:00
|
|
|
if (!o.any_of<ObjComp::F::SingleInfo, ObjComp::F::CollectionInfo>()) {
|
|
|
|
return; // dont know enough
|
|
|
|
}
|
|
|
|
|
2023-10-14 01:08:45 +02:00
|
|
|
// dont touch avatars for now
|
2024-07-31 20:07:14 +02:00
|
|
|
// TODO: more generic file types??
|
|
|
|
if (const auto* fk = o.try_get<ObjComp::Tox::FileKind>(); fk != nullptr && fk->kind != 0) {
|
2023-10-14 01:08:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:07:14 +02:00
|
|
|
uint64_t total_size {0u};
|
|
|
|
if (const auto* si = o.try_get<ObjComp::F::SingleInfo>(); si != nullptr) {
|
|
|
|
if (si->file_name.empty()) {
|
|
|
|
return; // bad file
|
|
|
|
}
|
|
|
|
total_size = si->file_size;
|
|
|
|
} else if (const auto* ci = o.try_get<ObjComp::F::CollectionInfo>(); ci != nullptr) {
|
|
|
|
if (ci->file_list.empty() || ci->file_list.front().file_name.empty()) {
|
|
|
|
return; // bad file
|
|
|
|
}
|
|
|
|
total_size = ci->total_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
//const auto& file_info = h.get<Message::Components::Transfer::FileInfo>();
|
2023-08-19 23:22:33 +02:00
|
|
|
// TODO: contact to entry
|
2024-07-31 20:07:14 +02:00
|
|
|
if (total_size > uint64_t(_conf.get_int("TransferAutoAccept", "autoaccept_limit").value_or(1024*1024))) {
|
2023-08-19 23:22:33 +02:00
|
|
|
return; // too large
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:07:14 +02:00
|
|
|
_accept_queue.push_back(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransferAutoAccept::checkMsg(Message3Handle h) {
|
|
|
|
if (!h.all_of<Message::Components::MessageFileObject>()) {
|
|
|
|
return;
|
2023-08-19 23:22:33 +02:00
|
|
|
}
|
|
|
|
|
2024-07-31 20:07:14 +02:00
|
|
|
checkObj(h.get<Message::Components::MessageFileObject>().o);
|
2023-08-19 23:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TransferAutoAccept::onEvent(const Message::Events::MessageConstruct& e) {
|
|
|
|
checkMsg(e.e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransferAutoAccept::onEvent(const Message::Events::MessageUpdated& e) {
|
|
|
|
checkMsg(e.e);
|
2024-07-31 20:07:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-08-19 23:22:33 +02:00
|
|
|
|
2024-07-31 20:07:14 +02:00
|
|
|
bool TransferAutoAccept::onEvent(const ObjectStore::Events::ObjectUpdate&) {
|
|
|
|
// too expensive rn
|
|
|
|
//checkObj(e.e);
|
2023-08-19 23:22:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|