From a6bf2504d136c0a04841f8dbe471d69604a8a2fb Mon Sep 17 00:00:00 2001 From: aroualid Date: Wed, 28 Jan 2026 19:12:24 +0100 Subject: [PATCH 1/2] last commit bc i go to sleep --- Makefile | 4 +- incs/bot/BotClient.hpp | 62 ++++++++++++++++ incs/core/Client.hpp | 2 +- srcs/bot/BotClient.cpp | 162 +++++++++++++++++++++++++++++++++++++++++ srcs/core/Client.cpp | 9 ++- 5 files changed, 233 insertions(+), 6 deletions(-) create mode 100644 incs/bot/BotClient.hpp create mode 100644 srcs/bot/BotClient.cpp diff --git a/Makefile b/Makefile index 03c96c1..8d83b22 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ INCLUDES = -I$(INC_DIR) \ -I$(INC_DIR)/core \ -I$(INC_DIR)/network \ -I$(INC_DIR)/commands \ + -I$(INC_DIR)/bot FLAGS = -Wall -Wextra -Werror -std=c++98 $(INCLUDES) @@ -48,7 +49,8 @@ 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)/protocol/IrcUtils.cpp \ + $(SRCS_DIR)/bot/BotClient.cpp OBJ = $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o) diff --git a/incs/bot/BotClient.hpp b/incs/bot/BotClient.hpp new file mode 100644 index 0000000..0af9130 --- /dev/null +++ b/incs/bot/BotClient.hpp @@ -0,0 +1,62 @@ +#pragma once + +#include "IClient.hpp" +#include "IServer.hpp" +#include "IChannel.hpp" + +class IBot; +class BotMessageBuffer; +class BotClient : public IClient +{ +private: + static int s_nextBotId; // Starts at -1, decrements + int m_id; // Negative FD + std::string m_nickname; + std::string m_username; + std::string m_realname; + std::string m_hostname; + IServer& m_server; + std::set m_channels; + BotMessageBuffer m_buffer; + bool _passwordProvided; + + std::time_t m_lastActivity; + std::time_t m_lastPingSent; + +public: + BotClient(const std::string& nick, IServer& server); + ~BotClient(); + + int getFd() const; // Returns negative m_id + const std::string& getNickname() const; + const std::string& getUsername() const; + const std::string& getRealname() const; + const std::string& getHostname() const; // Returns "internal" + IServer* getServer() const; + std::string getPrefix() const; // "nick!bot@internal" + + bool isRegistered() const; // Always returns true + bool isPasswordProvided() const; // Always returns true + + void setNickname(const std::string& nick); + void setUsername(const std::string& user); + void setRealname(const std::string& real); + void setPasswordProvided(bool provided); + void attemptRegistration(); + + void joinChannel(IChannel* channel); + void leaveChannel(IChannel* channel); + bool isInChannel(const std::string& channel) const; + const std::set< IChannel* >& getChannels() const; + // Buffer access + + std::time_t getLastActivity() const; + void updateLastActivity(); + std::time_t getLastPingSent() const; + void setLastPingSent(std::time_t last_ping); + + IMessageBuffer& getBuffer(); + const IMessageBuffer& getBuffer() const; + void setBot(IBot* bot); // Links to bot handler + +}; diff --git a/incs/core/Client.hpp b/incs/core/Client.hpp index 2b590f5..7c1ba2f 100644 --- a/incs/core/Client.hpp +++ b/incs/core/Client.hpp @@ -31,7 +31,7 @@ class Client : public IClient bool _passwordProvided; MessageBuffer _buffer; - IServer* _server; + IServer& _server; std::set< IChannel* > _channels; public: diff --git a/srcs/bot/BotClient.cpp b/srcs/bot/BotClient.cpp new file mode 100644 index 0000000..5170447 --- /dev/null +++ b/srcs/bot/BotClient.cpp @@ -0,0 +1,162 @@ +#include "bot/BotClient.hpp" +#include "IChannel.hpp" +#include "core/IServer.hpp" + +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_lastActivity(std::time(NULL)), m_lastPingSent(0) +{ +} + +BotClient::~BotClient() +{ +} + +IServer* BotClient::getServer() const +{ + return &m_server; +} + +int BotClient::getFd() const +{ + return m_id; +} + +const std::string& BotClient::getNickname() const +{ + return (m_nickname); +} + +const std::string& BotClient::getUsername() const +{ + return (m_username); +} + +const std::string& BotClient::getRealname() const +{ + return (m_realname); +} + +const std::string& BotClient::getHostname() const +{ + return (m_hostname); +} + +bool BotClient::isPasswordProvided() const +{ + return true; +} + +bool BotClient::isRegistered() const +{ + return true; +} + +void BotClient::setNickname(const std::string& nick) +{ + m_nickname = nick; + attemptRegistration(); +} + +void BotClient::setUsername(const std::string& user) +{ + m_username = user; + attemptRegistration(); +} + +void BotClient::setRealname(const std::string& real) +{ + m_realname = real; +} + +void BotClient::setPasswordProvided(bool provided) +{ + _passwordProvided = provided; + attemptRegistration(); +} + +void BotClient::joinChannel(IChannel* channel) +{ + m_channels.insert(channel); +} + +void BotClient::leaveChannel(IChannel* channel) +{ + m_channels.erase(channel); +} + +bool BotClient::isInChannel(const std::string& channelName) const +{ + for (std::set< IChannel* >::const_iterator it = m_channels.begin(); it != m_channels.end(); ++it) + { + if ((*it)->getName() == channelName) + return true; + } + return false; +} + +const std::set< IChannel* >& BotClient::getChannels() const +{ + return m_channels; +} + +void BotClient::attemptRegistration() +{ + if (m_nickname.empty()) + return; + if (m_username.empty()) + return; + if (m_server.requiresPassword() && !_passwordProvided) + return; + m_server.registerClient(m_nickname, this); +} + +IMessageBuffer& BotClient::getBuffer() +{ + return m_buffer; +} + +const IMessageBuffer& BotClient::getBuffer() const +{ + return m_buffer; +} + +std::time_t BotClient::getLastActivity() const +{ + return m_lastActivity; +} + +void BotClient::updateLastActivity() +{ + m_lastActivity = std::time(NULL); + m_lastPingSent = 0; +} + +std::time_t BotClient::getLastPingSent() const +{ + return m_lastPingSent; +} + +void BotClient::setLastPingSent(std::time_t last_ping) +{ + m_lastPingSent = last_ping; +} + +std::string BotClient::getPrefix() const +{ + if (m_nickname.empty()) + return m_hostname; + + std::string prefix = m_nickname; + if (!m_username.empty()) + prefix += "!" + m_username + "@" + m_hostname; + return prefix; +} + +void BotClient::setBot(IBot* bot) +{ + (void) bot; +} diff --git a/srcs/core/Client.cpp b/srcs/core/Client.cpp index f6b653d..e84ef24 100644 --- a/srcs/core/Client.cpp +++ b/srcs/core/Client.cpp @@ -5,7 +5,7 @@ Client::Client(int fd, const std::string& hostname, IServer& server) : _fd(fd), _nickname(""), _username(""), _realname(""), _hostname(hostname), m_lastActivity(std::time(NULL)), m_lastPingSent(0), _state(HANDSHAKE), - _passwordProvided(false), _server(&server) + _passwordProvided(false), _server(server) { } @@ -15,7 +15,7 @@ Client::~Client() IServer* Client::getServer() const { - return _server; + return &_server; } int Client::getFd() const @@ -109,10 +109,10 @@ void Client::attemptRegistration() return; if (_username.empty()) return; - if (_server->requiresPassword() && !_passwordProvided) + if (_server.requiresPassword() && !_passwordProvided) return; _state = REGISTERED; - _server->registerClient(_nickname, this); + _server.registerClient(_nickname, this); } IMessageBuffer& Client::getBuffer() @@ -156,3 +156,4 @@ std::string Client::getPrefix() const prefix += "!" + _username + "@" + _hostname; return prefix; } + From 0efb9ba3c4f4df49ddb390a69816d411bf45ff49 Mon Sep 17 00:00:00 2001 From: aroualid Date: Thu, 29 Jan 2026 19:37:52 +0100 Subject: [PATCH 2/2] it should work now --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 8d83b22..cecdfab 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 $(INCLUDES) @@ -49,8 +49,8 @@ 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/BotClient.cpp OBJ = $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o)