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/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..9dbe5ef 100644 --- a/incs/bot/NielBot.hpp +++ b/incs/bot/NielBot.hpp @@ -1,22 +1,29 @@ -#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" +#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 new file mode 100644 index 0000000..e473760 --- /dev/null +++ b/srcs/bot/NielBot.cpp @@ -0,0 +1,73 @@ +#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" + + +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);