diff --git a/.gitignore b/.gitignore index d1bcc9d..e433b8c 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ compile_commands.json .DS_Store Thumbs.db + +.bonus diff --git a/Makefile b/Makefile index 67c3a43..66f2804 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,8 @@ TEST_DIR := ./tests INCLUDES := -I$(INC_DIR) \ -I$(INC_DIR)/core \ -I$(INC_DIR)/network \ - -I$(INC_DIR)/commands + -I$(INC_DIR)/commands \ + -I$(INC_DIR)/bot BONUS_INCLUDES := -I$(INC_DIR)/bot @@ -60,8 +61,11 @@ SRCS := $(SRCS_DIR)/main.cpp \ $(SRCS_DIR)/modes/UserLimitMode.cpp \ $(SRCS_DIR)/protocol/IrcUtils.cpp -BONUS_SRCS := $(SRCS_DIR)/bot/BotClient.cpp \ - $(SRCS_DIR)/bot/SixSevenBot.cpp + +OBJ = $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o) + +BONUS_SRCS := $(SRCS_DIR)/bot/BotMessageBuffer.cpp \ + $(SRCS_DIR)/bot/BotClient.cpp OBJS := $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o) OBJS_BONUS := $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/bonus/%.o) \ 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/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 :)?" diff --git a/incs/bot/BotClient.hpp b/incs/bot/BotClient.hpp index 0af9130..44bdf88 100644 --- a/incs/bot/BotClient.hpp +++ b/incs/bot/BotClient.hpp @@ -3,9 +3,9 @@ #include "IClient.hpp" #include "IServer.hpp" #include "IChannel.hpp" +#include "bot/BotMessageBuffer.hpp" class IBot; -class BotMessageBuffer; class BotClient : public IClient { private: diff --git a/incs/bot/BotMessageBuffer.hpp b/incs/bot/BotMessageBuffer.hpp new file mode 100644 index 0000000..0a587db --- /dev/null +++ b/incs/bot/BotMessageBuffer.hpp @@ -0,0 +1,41 @@ +#pragma once +#include +#include +#include + +#include "core/IMessageBuffer.hpp" +#include "core/IServer.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(IServer& server); + virtual ~BotMessageBuffer(); + + void appendRead(const std::string& data); + bool hasCompleteMessage() const; + std::string getNextMessage(); + size_t getReadBufferSize() const; + + void appendWrite(const std::string& data); + void consumeWriteBuffer(size_t bytes); + const std::string& getWriteBuffer() const; + void clearWriteBuffer(); + +}; 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/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 e9c8077..7191f39 100644 --- a/incs/core/Server.hpp +++ b/incs/core/Server.hpp @@ -19,13 +19,12 @@ #include #include -// #include "PollSocketManager.hpp" - #include "IChannel.hpp" #include "IClient.hpp" #include "core/Config.hpp" #include "core/IServer.hpp" #include "network/ISocketManager.hpp" +#include "bot/IBot.hpp" #define RESET "\033[0m" #define RED "\033[91m" @@ -64,6 +63,8 @@ class Server : public IServer void checkClientTimeouts(); + std::vector m_bots; + public: Server(const Config& cfg); ~Server(); @@ -83,5 +84,11 @@ class Server : public IServer size_t getChannelCount() const; std::string getServerName() const; bool requiresPassword() const; + void markForDisconnect(int fd); + + #ifdef BONUS + void registerBot(IBot* bot); + void unregisterBot(IBot* bot); + #endif }; diff --git a/incs/network/SocketException.hpp b/incs/network/SocketException.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/srcs/bot/BotClient.cpp b/srcs/bot/BotClient.cpp index 5170447..2b80766 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 new file mode 100644 index 0000000..ec51fcf --- /dev/null +++ b/srcs/bot/BotMessageBuffer.cpp @@ -0,0 +1,120 @@ +#include "bot/BotMessageBuffer.hpp" + +BotMessageBuffer::BotMessageBuffer(IServer& server) : m_server(server), 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(); +} + + +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 diff --git a/srcs/core/Server.cpp b/srcs/core/Server.cpp index 70577aa..916b610 100644 --- a/srcs/core/Server.cpp +++ b/srcs/core/Server.cpp @@ -17,24 +17,49 @@ #include #include #include +#include extern volatile sig_atomic_t g_shutdown; -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int Server::getPort() const { return m_cfg.getPort(); } +#ifdef BONUS +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()); +} +#endif + const std::string& Server::getPassword() const { return m_cfg.getPassword(); } -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -// getClientByNickname -// register -// unregister void Server::deleteChannelIfEmpty(IChannel* channel) { @@ -478,5 +503,8 @@ Server::~Server() delete (m_sm); + + //cleaning bots here !!!! LOG_INFO << "All resources freed. I can die in peace! :D" << std::endl; } +