From 0ead4441d15c5f31af9fc797fc5790af604b587a Mon Sep 17 00:00:00 2001 From: Eliza Sikira Date: Thu, 29 Jan 2026 23:41:08 +0100 Subject: [PATCH 1/4] feat: added BotMessageBuffer --- Makefile | 12 ++--- ascii/xavier.txt | 77 ++++++++++++++++++++++++++++++++ incs/bot/BotClient.hpp | 1 + incs/bot/BotMessageBuffer.hpp | 26 +++++++++++ incs/bot/NielBot.hpp | 22 +++++++++ incs/core/Server.hpp | 6 +++ incs/network/SocketException.hpp | 0 srcs/bot/BotMessageBuffer.cpp | 65 +++++++++++++++++++++++++++ srcs/core/Server.cpp | 38 +++++++++++++--- 9 files changed, 236 insertions(+), 11 deletions(-) create mode 100644 ascii/xavier.txt create mode 100644 incs/bot/BotMessageBuffer.hpp create mode 100644 incs/bot/NielBot.hpp delete mode 100644 incs/network/SocketException.hpp create mode 100644 srcs/bot/BotMessageBuffer.cpp diff --git a/Makefile b/Makefile index 77d1a14..579d955 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,8 @@ TEST_DIR = ./tests INCLUDES = -I$(INC_DIR) \ -I$(INC_DIR)/core \ -I$(INC_DIR)/network \ - -I$(INC_DIR)/commands -# -I$(INC_DIR)/bot + -I$(INC_DIR)/commands \ + -I$(INC_DIR)/bot FLAGS = -Wall -Wextra -Werror -std=c++98 -g3 $(INCLUDES) @@ -50,9 +50,11 @@ SRCS = $(SRCS_DIR)/main.cpp \ $(SRCS_DIR)/modes/OperatorMode.cpp \ $(SRCS_DIR)/modes/KeyMode.cpp \ $(SRCS_DIR)/modes/UserLimitMode.cpp \ - $(SRCS_DIR)/protocol/IrcUtils.cpp -# $(SRCS_DIR)/bot/BotClient.cpp - + $(SRCS_DIR)/protocol/IrcUtils.cpp \ + $(SRCS_DIR)/bot/BotMessageBuffer.cpp \ + $(SRCS_DIR)/bot/BotClient.cpp + + OBJ = $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o) all: $(NAME) diff --git a/ascii/xavier.txt b/ascii/xavier.txt new file mode 100644 index 0000000..edf5dc2 --- /dev/null +++ b/ascii/xavier.txt @@ -0,0 +1,77 @@ +::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-----::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::::::::::----------------------------------------------------------------------------------------------------------------::::::::::::::::::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::--------------------------------------------------------------------------------------------------------------------------------------------::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::------------------------------------------------------------------------------------------------------=------==-===-=---=--=-----------------------------::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::------------------------------------------------------------------------------------------=====-=+===++=+=++=++==---=---------:::::::::::::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::--::::::::::::::::::::::::::::::::::::::::::::---==++++==+==++++++*****+**#+**++==-==----:::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::--::::-*=++=+**###**+*+*+**##**++*#+++++***+----:::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-+===+=++=+=***##%####%%%#%%%##**+++=*+++====-:::::::::::::::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-:---=+===+=+##%%%##%%%%%%@%%%%%%%%#%##*+==-===++=---:::::::::::::::::::::::::::::::::::.:::::::: +..........::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::--==++===+#%%%%%%@%@@@@@@%@@@@@%@@@%%%%%##*+-=+++==---::::::::::::::::::::::::::::::::::::::::::: +.....:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-===+*+*%%%%%%%@@@@@@@@@@@@@@@@@@@@@@@%%%%%#*+==+**+==---:::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::----++*%%%%%%%%@@@@@@@@@@@@@@@@@@@@@@@@@%%%%%%#*=--==------::::::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::--:-+*#%%%%%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%%%%%##++*+==-:::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::--::+*%%%%%%%%%@@@@@@@@@@@@@@@@@@@@@@@@@@%%%%%%%%%##*++=--:::-:::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::---=#%%##%%%%%%%%%@@@@@@@@@@@@@@@@@@@@@@%%%%%%%%%%%##+++-:-:::-::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::----::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-----*###%#%%%%%%%%%@@@@@@@@@@@@@@@@@%%@%%%%%%%#########*=-:-----::::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::::::-----::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-=--=#####%%%%%%%%%%%%@%%@@@@@%@%@@@@%%%%%%%%%%%########*+--:::--::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::----::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::---=*#######%#%%#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#####******+=::-:---:::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::---::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::--:-=*###########%%%#%%%%%%%%%%%%%%%%%%%%%%%%%#####********=--:----:::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::--::-=***#*####%%###%####%%######%%%%####%%%%#######********=-::-----::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-::-=****########%%#%%#%%%%%%%%%%%%%%%%%%%%%%%%#*#********+=-:::-:::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-:---:-=+****#####%%%%%%%%%%#######%%#%%%%%%%#####*##*******++--:::::-:::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::----:-=+****###%%%%%%%%%%%%%%%##%#%%##%%%%%%%#######******++=-::::::-:::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::------=+**###%%%%%#%%#%%#%%%%%%%%%%%%%%%%%%%%%%#%%###*#****+==::::::--::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::-----::::::::::::::::::::::::::::::::::::::::::::::::::::---:--=+**#%%%%%##*#####*#%%######%#########%####%%%##%#***++=-:::----::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::-----:::::::::::::::::::::::::::::::::::::::::::::::::::---::--=**####%####%#**+++++****#**************#####****+*****=-:::::--::::::::::::::::::::::::::::::::::: +::::::::::::::::------:::::::::::::::::::::::::-----------:::::::::::::::::::::::::::::::::::::::::::::::--:::--+*#****+==++==--==-===+++=+++=+=====-=-===========+**#*=-::---=-::::::::::::::::::::::::::::::::::: +::::::::------------------------------------------===========---:::::::::::::::::::::::::::::::::::::::::---:---*##*===------:::::::--========----------:--------=+**#*+--:--==--::::::::::::::::::::::::::::------ +:::::----=======----------------===============+++++++++++++++==-------::::::::::::::::::::::::::::::::::----:-=*##*=-:-----------------==++==-----------------=-=***#*+-::=--==---:::::::::::::::::::::::::----=== +::::---====++++=====-------================+++++**************++==------:::::::::::::::::::::::::::::::::-:----=*###+=--:::::::::::-::-=+**++=---::::::::-::---===**#**+--:---===--::::::::::::::::::::::::----==== +:::---===+++++++======================+++++++++*****######*****+===------::::::::::::::::::::::::::::::::::==--=**#*++=-:--------::::::+###%#=::::::::---:::---=*+****+=-:-======--::::::::::::::::::::::::----==== +:::---===++++++======================+++++++++*****##########**++==------::::::::::::::::::::::::::::::::::+=-==**###*++++======------=+#%%%%+------------+=+*=******++==-==+=+==--:::::::::::::::::::::::::---==== +::::--===+++======-------=================+++++*****#######****+==-------:::::::::::::::::::::::::::::::::-+=-=+*###%#####+-:::--:--=+*###%%%#==:--=====+++==*##*##***+==--==++==--::::::::::::::::::::::::::----== +:::::----=====-------------------===========++++**************++==------::::::::::::::::::::::::::::::::::-**===+######%%%##*+=+++++*##%*##%%##+=--:----::-+***####***+==--=+++===---::::::::::::::::::::::::::---- +::::::-------:--------------------------=====+++****++++******++==------:::::::::::::::::::::::::::::::::::+#+==+#########%%%#%#########%%%%%#*##*++++++*#%%%%%##****+=-=-=++++====--:::::::::::::::::::::::::::--- +:::::::::::::::::::--------------------====++*******++++*******++==-----::::::::::::::::::::::::::::::::::::=*#+=****+++*####%#%%%%#**####%%%#####%%%%%%%%%%%#******++==***++++=====---:::::::::::::::::::::::::--- +:::::::::---------------------------=====+++*******************++==----------::::::::::::::::::::::::::::::--***+++++=+##*******==-=+*#####%%####*-=++####**####****++-+***++++====----:---:::::::::::::::::::----- +::::::------------------------------=====+++*******************++==--------------------------:::::::::::-----+++=+====***++==+=----**#****####***##=--==++*++***+++++===*+++======-----------::::::::::::::::------ +::::--------------------------------=====++++******++++++++++*++==-------------------------------------------=++=+=-==========---==****###*##*++****----==++++++*+=++++=+++======-----------------------------===== +:-------------==========++++++++++++======+++++++++++====++++++===-----====================+++++++++==========++++==-========---+**====##******++====----=======++==++++++=======--------------=-===============+++ +-==========+++++***#####%%%%%%%%%%%%###*******+++++++++++++++++==========+++++++++++==++++++++****+++++++=++++++++=========----+***=-::-=====++=--=++=----==========+++++===-==--------======================++++++ +===++++++++++***####%%%%%%@@@@@@@@@%%%%####*******++++++++++++++=======+++++++++++++++++++++*******++++++++++==--+======---::--=+**+==--:-===-::-=+*+==----=========++======================================+++++++ +=+++++++++++****###%%%%%@@@@@@@@@@@@%%%%####*********+++++++++++=======+++++++++++++++++++++********+++++====----+=======------=+**++==---:::---=+++++=---:-========+----=====================================+++++ ++++++++++++++***###%%%%%%@@@@@@@@@@@%%%%#####*******+++++++++++++======+++++++++++++++++++++++****++++======-----========-:----===+====--------==+=+===-------=====++:::---========================--============== +====++++++++++***####%%%%%%%@@@@@@%%%%#####*******+++++++++++++++=======++++++===========+++++++++=========-----::==+=====--------------------=======---------======-::::---------===------------------------------ +==============+++*****#######%%%%#####******+++++++++++++++========---==================================------:::::=====+==-----------:::::-----:::::-------========:::::::::::------------------------------------ +--------------====================================================-----===============-==------========-------:::::-=========++*****++============+**++===-========-::::-:::::::----------------------------------- +--------=============================+++++++++++++++++++++++***++++==+++******+==========------:--====-------::::::-========++***++=+++++=======+******+==========:::::::--:::::::-------------------------======== +----====++=++++******************************#####################******#+==============------------------:::-:::::-*-====--=++++++=====-==========+++*++========:::::::::---::::::::::-----------------------===== +---==+++++++++****###******************################%%%%%%%%%%###*+================---:-----:::------:::::::::::=#=--==--=+**+++====----========++++++=======:::::::::::----::::::::::::::::::::---------------= +---==++++++++**************************##########################*================-------:-------------::::::::::::-%===-===-==++**++++=========++++++++=======#:::::::::::::-::::::::::::::::::::::::::----------- +---===+++++++++++******************************###############+=============---------------------------:::::::::::::%*==-=====-==*++++++++====+****+++========+%::::::::::::::::::-::::::::::::::::::::::::::::::-- +::---========================================+++++++++++++=================----------------------------:::::::::::::#%====-====-==++===++++===+++++++=========+%::::::::::::::::::::::::::::::::::::::::::::::::::: +::::-----====----------------------------------------=------===-==========-----------::::::::::-------::::::::::::::#%#======================================-+@::::::::::::::::::::::::::::::::::::::::::::::::::: +::::::-----------:::::::::::::-----------------------==----==============----------::::::::::::-------::::::::::::::+%%====================================-::+%::::::::::::::::::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::---------------==-===========-------------:::::::::::::-------::::::::::::::-%%*----================+++==+========-:::*%::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::---------------=--=========--------------:::::::::::---------:::::::::::::::#%#=:---===========++++++++++=======-:::-%%::::::::::::::::::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::::---------:-----=-=========-------------:::::::::::----------:::::::::::::::#%%+::--=========++++++++++++=====--::::*%#::::::::::::::::::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::::::----------:-------====------------------:::::::::::-:-----:::::::::::::::::*%%#-::--========++++++++++++====-:::::=%@*::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::-----------------::------====----------------:::::::::::::::--::::::::::::::::::::=%%%+::::--=======++++++++++====-:::::-%%@+::::::::::::::::::::::::::::::::::::::::::::::::::: +::::::::::::::::--------------------===========--------------=------------------:::::::::::::::::::::::::::::::::::::-#%%#=:::---======++++++++++==--:::::-#+*#*::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::--------------------------=========+++++++=-:----::-------------------:----::::::::::::::::::::::::::::::::::::::#%%%#-::-----==+==++++++++==--:::::=#=**#:::::::::::::::::::::::::::::::::::::::::::::::::::: +:::-------------------------==========+++******+-::---::-----------------------:::::::::::::::::::::::::::::::::::::::#%%%%#=:------====+++++++==---::::+*==**::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::-------------------------===========+++**++++--::--:::::-------------------::::::::::::::::::::::::::::::::::::::::*%%%%*#=-------===+++++==-----:::+*+==*:::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::-------------------------------------========-:::::::::::------------------::::::::::::::::::::::::::::::::::::::::=%*+*++**-------========-----:::**++=+-:::::::::::::::::::::::::::::::::::::::::::::::::::::: +::::-----------------------------------:::------::::::::::::::---------------::::::::::::::::::::::::::::::::::::::::::#*+++++***------=====------::-**++==--:::::::::::::::::::::::::::::::::::::::::::::::::::::: +::::-----------------------------------::::::----::::::::::::::--------------:::::::::::::-::::::::::::::::::::::::::::**++++***###--------------::-***+====-:::::::::::::::::::::::::::::::::::::::::::::::::::::: +::::------------------------======+++++****#*-----:::::::::::::::------------::::::::::::::::::::::::::::::::::::::::::+=+++++****##*-:---------:::*#*+=====::::::::::::::::::::::::::::::::::::::::::::::::::::::: +----===============++++++****####%%%@@@@@@@@@=----:::::::::::::::::::------::::::::::::::::::::::::::::::::::::::::::::-=++++++*****##+:::::---:::=##*+++===::::::::::::::::::::::::::::::::::::::::::::::::::::::: +==+++++++++++++******######%%%%%@@@@@@@@@@@@@@----::::::::::::::::::::::----:::::::::::::::::::::::::::::::::::::::::::===++++++******#*+-::::::::##*+++++==::::::::::::::::::::::::::::::::::::::::::::::::::::::: +****####*******#######%%%%%%%%@@@@@@@@@@@@@@@@@+--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::==++**++++**+++**###+-:::-+#***++++=-::::::::::::::::::::::::::::::::::::::::::::::::::::::: +******##********###########%%%%%%%@@@@@@@@@@@@@:::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-=+++++++++++++++++++++=-+#****+++++-:::::::::::::::::::::::::::::::::::::::::::::::::::.:.. ++++++++++++++++++++++++++***********#########%-:::::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-===++++++===++++++++++++##********+-:::::::::::::::::::::::::::::::::::::::::::::::::::::.. +--------------------------------------------------::::-::::::::::::...::::::::::::::::::::::::::::::::::::::::::::::::::+====++++++===++++++++*###*******++::::::::::::::::::::::::::::::::::::::::::::::::::...... + + diff --git a/incs/bot/BotClient.hpp b/incs/bot/BotClient.hpp index 0af9130..d4caa05 100644 --- a/incs/bot/BotClient.hpp +++ b/incs/bot/BotClient.hpp @@ -3,6 +3,7 @@ #include "IClient.hpp" #include "IServer.hpp" #include "IChannel.hpp" +#include "BotMessageBuffer.hpp" class IBot; class BotMessageBuffer; diff --git a/incs/bot/BotMessageBuffer.hpp b/incs/bot/BotMessageBuffer.hpp new file mode 100644 index 0000000..0bc704d --- /dev/null +++ b/incs/bot/BotMessageBuffer.hpp @@ -0,0 +1,26 @@ +#pragma once +#include +#include + +#include "core/IMessageBuffer.hpp" + +class BotMessageBuffer : public IMessageBuffer +{ + private: + std::string m_readBuffer; + std::string m_writeBuffer; + public: + BotMessageBuffer(); + virtual ~BotMessageBuffer(); + + void appendRead(const std::string& data); + bool hasCompleteMessage() const; + std::string getNextMessage(); + size_t getReadBufferSize() const; + + // write + void appendWrite(const std::string& data); + void consumeWriteBuffer(size_t bytes); + const std::string& getWriteBuffer() const; + void clearWriteBuffer(); +}; \ No newline at end of file diff --git a/incs/bot/NielBot.hpp b/incs/bot/NielBot.hpp new file mode 100644 index 0000000..08f9905 --- /dev/null +++ b/incs/bot/NielBot.hpp @@ -0,0 +1,22 @@ +#pragma one + +#include "bot/IBot.hpp" +#include "core/IServer.hpp" + +class NielBot : public IBot +{ + private: + IServer* m_server; + IClient* m_client; + bool contains42(const std::string& msg)const; + void sendReply(IChannel* channel); + + public: + NielBot(IServer* server, IClient* botClient); + virtual ~NielBot(); + + void onPrivateMessage(IClient* sender, const std::string& msg); + void onChannelMessage(IClient* sender, IChannel* channel, const std::string& msg); + + IClient* getClient(); +}; \ No newline at end of file diff --git a/incs/core/Server.hpp b/incs/core/Server.hpp index 8793d97..8acbb43 100644 --- a/incs/core/Server.hpp +++ b/incs/core/Server.hpp @@ -26,6 +26,7 @@ #include "core/Config.hpp" #include "core/IServer.hpp" #include "network/ISocketManager.hpp" +#include "bot/IBot.hpp" #define RESET "\033[0m" #define RED "\033[91m" @@ -63,6 +64,8 @@ class Server : public IServer void checkClientTimeouts(); + std::vector m_bots; + public: Server(const Config& cfg); ~Server(); @@ -82,4 +85,7 @@ class Server : public IServer size_t getChannelCount() const; std::string getServerName() const; bool requiresPassword() const; + + void registerBot(IBot* bot); + void unregisterBot(IBot* bot); }; diff --git a/incs/network/SocketException.hpp b/incs/network/SocketException.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/srcs/bot/BotMessageBuffer.cpp b/srcs/bot/BotMessageBuffer.cpp new file mode 100644 index 0000000..47bfa35 --- /dev/null +++ b/srcs/bot/BotMessageBuffer.cpp @@ -0,0 +1,65 @@ +#include "bot/BotMessageBuffer.hpp" + +BotMessageBuffer::BotMessageBuffer() : m_readBuffer(), m_writeBuffer() +{ +} + +BotMessageBuffer::~BotMessageBuffer(){} + +void BotMessageBuffer::appendRead(const std::string& data) +{ + m_readBuffer += data; +} + +bool BotMessageBuffer::hasCompleteMessage() const +{ + return (m_readBuffer.find("\r\n") != std::string::npos || + m_readBuffer.find('\n') != std::string::npos); +} + +std::string BotMessageBuffer::getNextMessage() +{ + std::string::size_type pos = m_readBuffer.find("\r\n"); + if (pos != std::string::npos) + { + std::string line = m_readBuffer.substr(0, pos); + m_readBuffer.erase(0, pos + 2); + return (line); + } + pos = m_readBuffer.find('\n'); + if (pos != std::string::npos) + { + std::string line = m_readBuffer.substr(0, pos); + m_readBuffer.erase(0, pos + 1); + return (line); + } + return (""); +} + +size_t BotMessageBuffer::getReadBufferSize() const +{ + return m_readBuffer.size(); +} + +void BotMessageBuffer::appendWrite(const std::string& data) +{ + m_writeBuffer += data; +} + +const std::string& BotMessageBuffer::getWriteBuffer() const +{ + return (m_writeBuffer); +} + +void BotMessageBuffer::consumeWriteBuffer(size_t bytes) +{ + if (bytes >= m_writeBuffer.size()) + m_writeBuffer.clear(); + else + m_writeBuffer.erase(0, bytes); +} + +void BotMessageBuffer::clearWriteBuffer() +{ + m_writeBuffer.clear(); +} diff --git a/srcs/core/Server.cpp b/srcs/core/Server.cpp index 07d0552..aec534b 100644 --- a/srcs/core/Server.cpp +++ b/srcs/core/Server.cpp @@ -17,24 +17,47 @@ #include #include #include +#include extern volatile sig_atomic_t g_shutdown; -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int Server::getPort() const { return m_cfg.getPort(); } +void Server::registerBot(IBot* bot) +{ + if (!bot) + return; + IClient* botClient = bot->getClient(); + + if (!botClient) + return; + + if (std::find(m_bots.begin(), m_bots.end(), bot) != m_bots.end()) + return; + m_clientsByNick[botClient->getNickname()] = botClient; + + m_bots.push_back(bot); +} + +void Server::unregisterBot(IBot* bot) +{ + if (!bot) + return; + std::vector::iterator it = std::find(m_bots.begin(), m_bots.end(), bot); + if (it != m_bots.end()) + m_bots.erase(it); + IClient* botClient = bot->getClient(); + if (botClient) + m_clientsByNick.erase(botClient->getNickname()); +} + const std::string& Server::getPassword() const { return m_cfg.getPassword(); } -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -// getClientByNickname -// register -// unregister void Server::deleteChannelIfEmpty(IChannel* channel) { @@ -472,5 +495,8 @@ Server::~Server() delete (m_sm); + + //cleaning bots here !!!! LOG_INFO << "All resources freed. I can die in peace! :D" << std::endl; } + From 032cea7f85e5510328089a883bcc6534feefe8a6 Mon Sep 17 00:00:00 2001 From: Eliza Sikira Date: Thu, 29 Jan 2026 23:44:04 +0100 Subject: [PATCH 2/4] refacto: removed dummy class BotMessageBuffer in BotClient --- incs/bot/BotClient.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/incs/bot/BotClient.hpp b/incs/bot/BotClient.hpp index d4caa05..b63b8e1 100644 --- a/incs/bot/BotClient.hpp +++ b/incs/bot/BotClient.hpp @@ -6,7 +6,6 @@ #include "BotMessageBuffer.hpp" class IBot; -class BotMessageBuffer; class BotClient : public IClient { private: From 55ac9fe2a0f090e7e7727a78d86b0c0af42b670b Mon Sep 17 00:00:00 2001 From: Yanis Andry Date: Sat, 31 Jan 2026 12:30:49 +0100 Subject: [PATCH 3/4] build: when your cpu owes you money or something --- death.sh | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 death.sh diff --git a/death.sh b/death.sh new file mode 100755 index 0000000..6096229 --- /dev/null +++ b/death.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +if [[ $# -lt 3 ]]; then + echo "Usage: $0 " + echo "Example: $0 127.0.0.1 6667 lol" + exit 1 +fi + +HOST="$1" +PORT="$2" +PASS="$3" + +CLIENTS=1000 +CHANNEL="#the-void" +MAX_DEATHS=10 +MESSAGE="this is a test message" +SLEEP_BETWEEN_MSGS=0.01 # set to 0 for max pain + +rand_nick() { + echo "lab$(tr -dc a-z0-9 /dev/null 2>&1 + ) & +done + +wait +echo "[*] Tests complete, surely your server hasn't died right :)?" From 99bfffb37911df97b0302305c2bcb532f1bedb1c Mon Sep 17 00:00:00 2001 From: Eliza Sikira Date: Sat, 31 Jan 2026 13:25:31 +0100 Subject: [PATCH 4/4] feat: methods of BotMessageBuffer done --- incs/bot/BotClient.hpp | 2 +- incs/bot/BotMessageBuffer.hpp | 20 ++++++++++-- incs/core/IClient.hpp | 1 - incs/core/Server.hpp | 2 -- srcs/bot/BotClient.cpp | 2 +- srcs/bot/BotMessageBuffer.cpp | 57 ++++++++++++++++++++++++++++++++++- 6 files changed, 76 insertions(+), 8 deletions(-) diff --git a/incs/bot/BotClient.hpp b/incs/bot/BotClient.hpp index b63b8e1..44bdf88 100644 --- a/incs/bot/BotClient.hpp +++ b/incs/bot/BotClient.hpp @@ -3,7 +3,7 @@ #include "IClient.hpp" #include "IServer.hpp" #include "IChannel.hpp" -#include "BotMessageBuffer.hpp" +#include "bot/BotMessageBuffer.hpp" class IBot; class BotClient : public IClient diff --git a/incs/bot/BotMessageBuffer.hpp b/incs/bot/BotMessageBuffer.hpp index 0bc704d..b6f5853 100644 --- a/incs/bot/BotMessageBuffer.hpp +++ b/incs/bot/BotMessageBuffer.hpp @@ -1,16 +1,32 @@ #pragma once #include #include +#include #include "core/IMessageBuffer.hpp" +#include "core/IServer.hpp" +#include "bot/BotClient.hpp" +#include "bot/IBot.hpp" + class BotMessageBuffer : public IMessageBuffer { private: + IServer &m_server; + IBot *m_bot; std::string m_readBuffer; std::string m_writeBuffer; + + void processIncomingMessage(const std::string& raw); + void parseAndDispatch(const std::string& prefix, + const std::string& command, + const std::vector& params); + + void setBot(IBot* bot); + void setBotClient(IClient* client); + public: - BotMessageBuffer(); + BotMessageBuffer(IServer& server); virtual ~BotMessageBuffer(); void appendRead(const std::string& data); @@ -18,9 +34,9 @@ class BotMessageBuffer : public IMessageBuffer std::string getNextMessage(); size_t getReadBufferSize() const; - // write void appendWrite(const std::string& data); void consumeWriteBuffer(size_t bytes); const std::string& getWriteBuffer() const; void clearWriteBuffer(); + }; \ No newline at end of file diff --git a/incs/core/IClient.hpp b/incs/core/IClient.hpp index 45f4bf6..2a24f42 100644 --- a/incs/core/IClient.hpp +++ b/incs/core/IClient.hpp @@ -6,7 +6,6 @@ #include class IMessageBuffer; -// class MessageBuffer; class IClient { diff --git a/incs/core/Server.hpp b/incs/core/Server.hpp index 8acbb43..17f4e0a 100644 --- a/incs/core/Server.hpp +++ b/incs/core/Server.hpp @@ -19,8 +19,6 @@ #include #include -// #include "PollSocketManager.hpp" - #include "IChannel.hpp" #include "IClient.hpp" #include "core/Config.hpp" diff --git a/srcs/bot/BotClient.cpp b/srcs/bot/BotClient.cpp index 5170447..cc00ffc 100644 --- a/srcs/bot/BotClient.cpp +++ b/srcs/bot/BotClient.cpp @@ -6,7 +6,7 @@ int BotClient::s_nextBotId = -1; BotClient::BotClient(const std::string& nick, IServer& server) : m_id(s_nextBotId--), m_nickname(nick), m_username(""), m_realname(""), - m_hostname("internal"), m_server(server), _passwordProvided(true), + m_hostname("internal"), m_server(server), m_buffer(server), _passwordProvided(true), m_lastActivity(std::time(NULL)), m_lastPingSent(0) { } diff --git a/srcs/bot/BotMessageBuffer.cpp b/srcs/bot/BotMessageBuffer.cpp index 47bfa35..ec51fcf 100644 --- a/srcs/bot/BotMessageBuffer.cpp +++ b/srcs/bot/BotMessageBuffer.cpp @@ -1,6 +1,6 @@ #include "bot/BotMessageBuffer.hpp" -BotMessageBuffer::BotMessageBuffer() : m_readBuffer(), m_writeBuffer() +BotMessageBuffer::BotMessageBuffer(IServer& server) : m_server(server), m_readBuffer(), m_writeBuffer() { } @@ -63,3 +63,58 @@ void BotMessageBuffer::clearWriteBuffer() { m_writeBuffer.clear(); } + + +void BotMessageBuffer::parseAndDispatch(const std::string& prefix, const std::string& command, const std::vector& params) +{ + if (command != "PRIVMSG" || params.size() < 2) + return; + + std::string target = params[0]; + std::string text = params[1]; + + std::string senderNick = prefix.substr(0, prefix.find('!')); + IClient* sender = m_server.getClientByNickname(senderNick); + if (!sender) + return; + + if (target[0] == '#') + { + IChannel* channel = m_server.getChannel(target); + if (channel) + m_bot->onChannelMessage(sender, channel, text); + } + else + { + m_bot->onPrivateMessage(sender, text); + } +} + +void BotMessageBuffer::processIncomingMessage(const std::string& raw) +{ + std::string line = raw; + if (line.size() >= 2 && line.substr(line.size()-2) == "\r\n") + line = line.substr(0, line.size()-2); + //\r\n removed + + std::string prefix; + std::string command; + std::vector params; + + size_t pos = 0; + if (line[0] == ':') + { + pos = line.find(' '); + prefix = line.substr(1, pos - 1); + pos++; + } + //we've got the prefix + + size_t nextSpace = line.find(' ', pos); + command = line.substr(pos, nextSpace - pos); + //we've got the command + + parseAndDispatch(prefix, command, params); +} + +//:prefix COMMAND param1 param2 :trailing parameter with spaces \ No newline at end of file