diff --git a/Makefile b/Makefile index 51a106f..e8f0f8a 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,7 @@ OBJ = $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o) BONUS_SRCS := $(SRCS_DIR)/bot/BotMessageBuffer.cpp \ $(SRCS_DIR)/bot/BotClient.cpp \ + $(SRCS_DIR)/bot/MiaouBot.cpp \ $(SRCS_DIR)/bot/SixSevenBot.cpp \ $(SRCS_DIR)/bot/NielBot.cpp diff --git a/incs/bot/BotClient.hpp b/incs/bot/BotClient.hpp index 150d89b..9bbd409 100644 --- a/incs/bot/BotClient.hpp +++ b/incs/bot/BotClient.hpp @@ -3,10 +3,10 @@ #include "IChannel.hpp" #include "IClient.hpp" #include "IServer.hpp" -#include "IChannel.hpp" +#include "BotMessageBuffer.hpp" +#include "IBot.hpp" #include "bot/BotMessageBuffer.hpp" -class IBot; class BotClient : public IClient { private: diff --git a/incs/bot/BotMessageBuffer.hpp b/incs/bot/BotMessageBuffer.hpp index 4eb36ac..ad02b3a 100644 --- a/incs/bot/BotMessageBuffer.hpp +++ b/incs/bot/BotMessageBuffer.hpp @@ -9,7 +9,6 @@ #include "bot/IBot.hpp" #include "protocol/Message.hpp" - class BotMessageBuffer : public IMessageBuffer { private: diff --git a/incs/bot/MiaouBot.hpp b/incs/bot/MiaouBot.hpp new file mode 100644 index 0000000..2e5c970 --- /dev/null +++ b/incs/bot/MiaouBot.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "IChannel.hpp" +#include "IClient.hpp" +#include "IServer.hpp" +#include "bot/IBot.hpp" +#include "bot/BotClient.hpp" + +class MiaouBot : public IBot +{ +private: + IServer& m_server; + BotClient* m_client; + int _count_message; + int _index; + void sendToChannel(IChannel* channel, const std::string& message); + +public: + MiaouBot(IServer& server, const std::string& nick = "Larry"); + ~MiaouBot(); + + void onPrivateMessage(IClient* sender, const std::string& message); + void onChannelMessage(IClient* sender, IChannel* channel, const std::string& message); + + IClient* getClient(); + + void joinChannel(const std::string& channelName); +}; diff --git a/srcs/bot/MiaouBot.cpp b/srcs/bot/MiaouBot.cpp new file mode 100644 index 0000000..b9baf32 --- /dev/null +++ b/srcs/bot/MiaouBot.cpp @@ -0,0 +1,89 @@ +#include "bot/MiaouBot.hpp" +#include "CommandType.hpp" +#include "protocol/Message.hpp" +#include "protocol/MessageParser.hpp" +#include +#include + + + +MiaouBot::MiaouBot(IServer& server, const std::string& nick) : m_server(server), _count_message(0), _index(0) +{ + m_client = new BotClient(nick, server); + m_client->setBot(this); + BotMessageBuffer *bmb = dynamic_cast(&m_client->getBuffer()); + bmb->setBot(this); + std::srand(std::time(0)); +} + +MiaouBot::~MiaouBot() +{ + delete m_client; +} + + +void MiaouBot::onChannelMessage(IClient *sender, IChannel *channel, const std::string &message) +{ + + if (sender == m_client) + return; + if (message.empty()) + return; + _count_message++; + + if (_count_message >= 5) + { + _count_message = 0; + _index++; + + const char* responses[] = { + "MIAOUUUUUUUUU", + "Quack i'm a DUCK NOW LET'S GOOO! đŸĻ†đŸĻ†đŸĻ†đŸĻ†", + "Meow i'm a british cat đŸ‡Ŧ🇧 ", + "WAF im a weird cat", + "miaou I am shy uwu (,,>īš<,,)👉👈", + "check tes mails." + }; + + sendToChannel(channel, responses[_index%6]); + } + +} + +void MiaouBot::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); +} + +void MiaouBot::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 MiaouBot::onPrivateMessage(IClient* sender, const std::string& message) +{ + if (sender == m_client) + return; + (void)message; +} + +IClient* MiaouBot::getClient() +{ + return (m_client); +} diff --git a/srcs/commands/InviteCommand.cpp b/srcs/commands/InviteCommand.cpp index c547488..830ac6b 100644 --- a/srcs/commands/InviteCommand.cpp +++ b/srcs/commands/InviteCommand.cpp @@ -4,6 +4,7 @@ #include "core/IMessageBuffer.hpp" #include "protocol/MessageParser.hpp" #include +#include REGISTER_COMMAND(InviteCommand, irc::INVITE, "INVITE"); @@ -51,7 +52,7 @@ void InviteCommand::doExecute(IClient* client, const Message& message) sendReply(client, NumericReply::userOnChannel(client->getNickname(), targetNick, channelName)); return; - } + } channel->addInvite(target); diff --git a/srcs/main.cpp b/srcs/main.cpp index 83ada1d..095c721 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -4,6 +4,7 @@ #include "core/Server.hpp" #include #include +#include "bot/MiaouBot.hpp" volatile sig_atomic_t g_shutdown = 0; @@ -33,6 +34,10 @@ int main(int argc, char** argv) SixSevenBot *sixSevenBot = new SixSevenBot(srv); srv.registerBot(sixSevenBot); sixSevenBot->joinChannel("#eighty-nine"); + + MiaouBot *miaouBot = new MiaouBot(srv); + srv.registerBot(miaouBot); + miaouBot->joinChannel("#cat"); NielBot *nielBot = new NielBot(srv); srv.registerBot(nielBot);