#include "./transfer_auto_accept.hpp" #include #include // for comp transfer tox filekind (TODO: generalize -> content system?) #include #include #include TransferAutoAccept::TransferAutoAccept(ObjectStore2& os, RegistryMessageModelI& rmm, ConfigModelI& conf) : _os(os), _rmm(rmm), _conf(conf) { //_os.subscribe(this, ObjectStore_Event::object_update); _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", int64_t(50l*1024l*1024l)); // sane default } } void TransferAutoAccept::iterate(void) { for (auto& it : _accept_queue) { if (it.all_of()) { continue; // already accepted } it.emplace( // TODO: contact to entry _conf.get_string("TransferAutoAccept", "save_path").value_or("tmp_save_dir"), false ); std::cout << "TAA: auto accepted transfer\n"; _os.throwEventUpdate(it); //_rmm.throwEventUpdate(it); } _accept_queue.clear(); } void TransferAutoAccept::checkObj(ObjectHandle o) { if (!o) { return; // invalid object } if (o.all_of()) { return; // already accepted } if (o.all_of()) { return; // alreay have } //if (!h.all_of()) { // TODO: tag receiving ?? if (!o.all_of()) { return; } if (!o.any_of()) { return; // dont know enough } // dont touch avatars for now // TODO: more generic file types?? if (const auto* fk = o.try_get(); fk != nullptr && fk->kind != 0) { return; } uint64_t total_size {0u}; if (const auto* si = o.try_get(); si != nullptr) { if (si->file_name.empty()) { return; // bad file } total_size = si->file_size; } else if (const auto* ci = o.try_get(); 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(); // TODO: contact to entry if (total_size > uint64_t(_conf.get_int("TransferAutoAccept", "autoaccept_limit").value_or(1024*1024))) { return; // too large } _accept_queue.push_back(o); } void TransferAutoAccept::checkMsg(Message3Handle h) { if (!h.all_of()) { return; } checkObj(h.get().o); } bool TransferAutoAccept::onEvent(const Message::Events::MessageConstruct& e) { checkMsg(e.e); return false; } bool TransferAutoAccept::onEvent(const Message::Events::MessageUpdated& e) { checkMsg(e.e); return false; } bool TransferAutoAccept::onEvent(const ObjectStore::Events::ObjectUpdate&) { // too expensive rn //checkObj(e.e); return false; }