Skip to content

Commit

Permalink
Refactor Spamblocker
Browse files Browse the repository at this point in the history
Eject unneeded srcs from tests
Fix some unused param warnings
  • Loading branch information
Royna2544 committed Nov 18, 2024
1 parent 743630f commit 2e46a8a
Show file tree
Hide file tree
Showing 23 changed files with 326 additions and 340 deletions.
44 changes: 32 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ find_package(ZLIB REQUIRED)
find_package(Protobuf)
find_package(fmt REQUIRED)
find_package(jsoncpp REQUIRED) # Required by tgbot-cpp anyways...
find_package(CURL REQUIRED)
# If we couldn't find protobuf or dirty abseil fix is enabled.
if (DIRTY_ABSEIL_FIX OR NOT Protobuf_FOUND)
# Then we can just include source-abseil
Expand Down Expand Up @@ -355,9 +356,35 @@ add_my_library(
)

################### The Bot's main functionaility ###################
add_my_library(
NAME Regex
SRCS
src/global_handlers/RegEXHandler.cpp
LIBS absl::status
STATIC
)

add_my_library(
NAME PPImpl
SRCS
SRCS
src/Authorization.cpp
src/api/CommandModule.cpp
src/global_handlers/SpamBlocker.cpp
src/global_handlers/ChatObserver.cpp
src/socket/interface/impl/bot/SocketDataHandler.cpp
src/socket/interface/impl/bot/TgBotSocketInterface.cpp
LIBS TgBot TgBotUtils TgBotDBImpl
TgBotPPImpl_shared_deps TgBotStringRes ${CMAKE_DL_LIBS} TgBotSocket JsonCpp::JsonCpp
CURL::libcurl
STATIC
)
#####################################################################

################# The Bot's main launcher (program) #################
add_my_executable(
NAME main
SRCS
src/main.cpp
src/ManagedThread.cpp
src/ThreadManager.cpp
src/Authorization.cpp
Expand All @@ -375,6 +402,7 @@ add_my_library(
src/api/components/UnknownCommand.cpp
src/global_handlers/RegEXHandler.cpp
src/global_handlers/SpamBlocker.cpp
src/global_handlers/SpamBlockManager.cpp
src/global_handlers/ChatObserver.cpp
src/ml/ChatDataCollector.cpp
src/web/TgBotWebServer.cpp
Expand All @@ -383,17 +411,9 @@ add_my_library(
src/socket/interface/impl/bot/TgBotSocketInterface.cpp
src/socket/interface/impl/backends/ServerBackend.cpp
src/socket/interface/impl/backends/ServerBackend_${TARGET_VARIANT}.cpp
LIBS TgBot TgBotUtils TgBotWeb TgBotDBImpl absl::status TgBotRandom TgBot_restartfmt_parser
TgBotPPImpl_shared_deps TgBotStringRes ${CMAKE_DL_LIBS} TgBotSocket JsonCpp::JsonCpp TgBotsighandler
STATIC
)
#####################################################################

################# The Bot's main launcher (program) #################
add_my_executable(
NAME main
SRCS src/main.cpp
LIBS TgBotPPImpl TgBotDBLoading TgBot_restartfmt_parser fruit
LIBS TgBotDBLoading TgBot_restartfmt_parser fruit TgBot TgBotUtils TgBotWeb TgBotDBImpl
absl::status TgBotRandom TgBot_restartfmt_parser CURL::libcurl TgBotRegex
TgBotPPImpl_shared_deps TgBotStringRes ${CMAKE_DL_LIBS} TgBotSocket JsonCpp::JsonCpp TgBotsighandler
)

if (UNIX)
Expand Down
6 changes: 3 additions & 3 deletions src/api/CommandModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DLWrapper {
RAIIHandle underlying() {
RAIIHandle tmp(nullptr, &dlclose);
std::swap(handle, tmp);
return std::move(tmp);
return tmp;
}

// dlfcn functions.
Expand All @@ -57,7 +57,7 @@ class DLWrapper {
};

CommandModule::CommandModule(std::filesystem::path filePath)
: filePath(std::move(filePath)), handle(nullptr, &dlclose) {}
: handle(nullptr, &dlclose), filePath(std::move(filePath)) {}

bool CommandModule::load() {
if (handle != nullptr) {
Expand Down Expand Up @@ -103,7 +103,7 @@ bool CommandModule::load() {
_module->isEnforced(), _module->name,
fmt::ptr(modulePtr));
#endif
handle = std::move(dlwrapper.underlying());
handle = dlwrapper.underlying();
return true;
}

Expand Down
11 changes: 5 additions & 6 deletions src/api/TgBotApiImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
#include <trivial_helpers/_tgbot.h>

#include <Authorization.hpp>
#include <ConfigManager.hpp>
#include <StringResLoader.hpp>
#include <api/CommandModule.hpp>
#include <api/MessageExt.hpp>
#include <api/TgBotApi.hpp>
#include <api/TgBotApiImpl.hpp>
#include <api/Utils.hpp>
#include <api/components/ChatJoinRequest.hpp>
#include <api/components/FileCheck.hpp>
#include <api/components/ModuleManagement.hpp>
#include <api/components/OnAnyMessage.hpp>
#include <api/components/OnCallbackQuery.hpp>
Expand All @@ -26,12 +31,6 @@
#include <string_view>
#include <utility>

#include "ConfigManager.hpp"
#include "StringResLoader.hpp"
#include "api/CommandModule.hpp"
#include "api/MessageExt.hpp"
#include "api/components/FileCheck.hpp"

bool TgBotApiImpl::validateValidArgs(const DynModule* module,
MessageExt::Ptr& message) {
if (!module->valid_args.enabled) {
Expand Down
68 changes: 68 additions & 0 deletions src/global_handlers/SpamBlockManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <tgbot/TgException.h>
#include <trivial_helpers/_std_chrono_templates.h>
#include <trivial_helpers/_tgbot.h>

#include <global_handlers/SpamBlockManager.hpp>

void SpamBlockManager::runFunction(const std::stop_token &token) {
while (!token.stop_requested()) {
consumeAndDetect();
delayUnlessStop(sSpamDetectDelay);
}
}

void SpamBlockManager::onDetected(ChatId chat, UserId user,
std::vector<MessageId> messageIds) const {
// Initial set - all false set
static auto perms = std::make_shared<TgBot::ChatPermissions>();
switch (_config) {
case Config::DELETE_AND_MUTE:
LOG(INFO) << fmt::format("Try mute offending user");
try {
_api->muteChatMember(chat, user, perms,
to_secs(kMuteDuration).count());
} catch (const TgBot::TgException &e) {
LOG(WARNING) << fmt::format("Cannot mute: {}", e.what());
}
[[fallthrough]];
case Config::DELETE: {
try {
_api->deleteMessages(chat, messageIds);
} catch (const TgBot::TgException &e) {
DLOG(INFO) << "Error deleting messages: " << e.what();
}
[[fallthrough]];
}
case Config::LOGGING_ONLY:
SpamBlockBase::onDetected(chat, user, messageIds);
break;
default:
break;
};
}

bool SpamBlockManager::shouldBeSkipped(const Message::Ptr &message) const {
if (_auth->isAuthorized(message, AuthContext::Flags::None)) {
return true;
}

// Ignore old messages
if (!AuthContext::isUnderTimeLimit(message)) {
return true;
}

// Bot's PM is not a concern
if (message->chat->type == TgBot::Chat::Type::Private) {
return true;
}
return false;
}

SpamBlockManager::SpamBlockManager(TgBotApi::Ptr api, AuthContext *auth)
: _api(api), _auth(auth) {
api->onAnyMessage([this](TgBotApi::CPtr, const Message::Ptr &message) {
addMessage(message);
return TgBotApi::AnyMessageResult::Handled;
});
run();
}
Loading

0 comments on commit 2e46a8a

Please sign in to comment.