From 7b103dbcdc5149fef2aebeb2a2411d0a6ac019c6 Mon Sep 17 00:00:00 2001 From: Eliza Sikira Date: Sat, 31 Jan 2026 16:18:33 +0100 Subject: [PATCH 1/2] build: pulled from main --- incs/bot/BotMessageBuffer.hpp | 1 + incs/bot/NielBot.hpp | 8 ++++++-- srcs/bot/NielBot.cpp | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 srcs/bot/NielBot.cpp diff --git a/incs/bot/BotMessageBuffer.hpp b/incs/bot/BotMessageBuffer.hpp index dc86085..4eb36ac 100644 --- a/incs/bot/BotMessageBuffer.hpp +++ b/incs/bot/BotMessageBuffer.hpp @@ -1,4 +1,5 @@ #pragma once + #include #include #include diff --git a/incs/bot/NielBot.hpp b/incs/bot/NielBot.hpp index 08f9905..b08c840 100644 --- a/incs/bot/NielBot.hpp +++ b/incs/bot/NielBot.hpp @@ -1,7 +1,11 @@ -#pragma one +#pragma once +#include "IChannel.hpp" +#include "IClient.hpp" +#include "IServer.hpp" +#include "bot/BotClient.hpp" #include "bot/IBot.hpp" -#include "core/IServer.hpp" + class NielBot : public IBot { diff --git a/srcs/bot/NielBot.cpp b/srcs/bot/NielBot.cpp new file mode 100644 index 0000000..33e8738 --- /dev/null +++ b/srcs/bot/NielBot.cpp @@ -0,0 +1,5 @@ +#include "NielBot.hpp" + +void onPrivateMessage(IClient* sender, const std::string& msg); + +void onChannelMessage(IClient* sender, IChannel* channel, const std::string& msg); \ No newline at end of file From 4394f1ca013438ef73dee198163b7e685e9fed1a Mon Sep 17 00:00:00 2001 From: Eliza Sikira Date: Sat, 31 Jan 2026 18:29:41 +0100 Subject: [PATCH 2/2] feat:xavier niel bot done, resize the file ? --- Makefile | 4 ++- incs/bot/NielBot.hpp | 17 +++++----- srcs/bot/NielBot.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++-- srcs/main.cpp | 5 +++ 4 files changed, 89 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 914c321..51a106f 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,9 @@ OBJ = $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o) BONUS_SRCS := $(SRCS_DIR)/bot/BotMessageBuffer.cpp \ $(SRCS_DIR)/bot/BotClient.cpp \ - $(SRCS_DIR)/bot/SixSevenBot.cpp + $(SRCS_DIR)/bot/SixSevenBot.cpp \ + $(SRCS_DIR)/bot/NielBot.cpp + OBJS := $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o) OBJS_BONUS := $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/bonus/%.o) \ diff --git a/incs/bot/NielBot.hpp b/incs/bot/NielBot.hpp index b08c840..9dbe5ef 100644 --- a/incs/bot/NielBot.hpp +++ b/incs/bot/NielBot.hpp @@ -5,22 +5,25 @@ #include "IServer.hpp" #include "bot/BotClient.hpp" #include "bot/IBot.hpp" - +#include class NielBot : public IBot { private: - IServer* m_server; - IClient* m_client; - bool contains42(const std::string& msg)const; - void sendReply(IChannel* channel); + IServer& m_server; + BotClient* m_client; + + void sendToChannel(IChannel* channel, const std::string& msg); public: - NielBot(IServer* server, IClient* botClient); - virtual ~NielBot(); + NielBot(IServer& server, const std::string& nick = "NielBot"); + ~NielBot(); void onPrivateMessage(IClient* sender, const std::string& msg); void onChannelMessage(IClient* sender, IChannel* channel, const std::string& msg); IClient* getClient(); + + void joinChannel(const std::string& channelName); + void loadAscii(const std::string& path); }; \ No newline at end of file diff --git a/srcs/bot/NielBot.cpp b/srcs/bot/NielBot.cpp index 33e8738..e473760 100644 --- a/srcs/bot/NielBot.cpp +++ b/srcs/bot/NielBot.cpp @@ -1,5 +1,73 @@ -#include "NielBot.hpp" +#include "bot/NielBot.hpp" +#include "CommandType.hpp" +#include "Logger.hpp" +#include "bot/BotClient.hpp" +#include "bot/BotMessageBuffer.hpp" +#include "protocol/Message.hpp" +#include "protocol/MessageParser.hpp" -void onPrivateMessage(IClient* sender, const std::string& msg); -void onChannelMessage(IClient* sender, IChannel* channel, const std::string& msg); \ No newline at end of file +NielBot::NielBot(IServer& server, const std::string& nick) : m_server(server) +{ + m_client = new BotClient(nick, server); + m_client->setBot(this); + BotMessageBuffer *bmb = dynamic_cast(&m_client->getBuffer()); + bmb->setBot(this); +} + +NielBot::~NielBot() +{ + delete m_client; +} + +void NielBot::onChannelMessage(IClient* sender, IChannel* channel, const std::string& msg) +{ + if (sender == m_client) + return; + + bool has42 = msg.find("42") != std::string::npos; + if (has42) + { + std::ifstream file("ascii/xavier.txt"); + std::string line; + while (std::getline(file, line)) + sendToChannel(channel, line); + } +} + +void NielBot::onPrivateMessage(IClient* sender, const std::string& msg) +{ + if (sender == m_client) + return; + (void)msg; +} + +void NielBot::joinChannel(const std::string& channelName) +{ + IChannel* channel = m_server.getChannel(channelName); + if (!channel) + channel = m_server.createChannel(channelName, m_client); + if (!channel->hasMember(m_client)) + { + channel->addMember(m_client); + m_client->joinChannel(channel); + } +} + +void NielBot::sendToChannel(IChannel* channel, const std::string& message) +{ + Message msg; + msg.m_prefix = m_client->getPrefix(); + msg.m_command = "PRIVMSG"; + msg.m_command_type = irc::PRIVMSG; + msg.m_params.push_back(channel->getName()); + msg.m_params.push_back(message); + + std::string serialized = MessageParser::serialize(msg); + channel->broadcast(serialized, m_client); +} + +IClient* NielBot::getClient() +{ + return (m_client); +} \ No newline at end of file diff --git a/srcs/main.cpp b/srcs/main.cpp index 6d37ca6..83ada1d 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -1,5 +1,6 @@ #include "Logger.hpp" #include "bot/SixSevenBot.hpp" +#include "bot/NielBot.hpp" #include "core/Server.hpp" #include #include @@ -32,6 +33,10 @@ int main(int argc, char** argv) SixSevenBot *sixSevenBot = new SixSevenBot(srv); srv.registerBot(sixSevenBot); sixSevenBot->joinChannel("#eighty-nine"); + + NielBot *nielBot = new NielBot(srv); + srv.registerBot(nielBot); + nielBot->joinChannel("#42"); #endif srv.run(); return (0);