Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions incs/bot/BotClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion incs/bot/BotMessageBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "bot/IBot.hpp"
#include "protocol/Message.hpp"


class BotMessageBuffer : public IMessageBuffer
{
private:
Expand Down
28 changes: 28 additions & 0 deletions incs/bot/MiaouBot.hpp
Original file line number Diff line number Diff line change
@@ -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);
};
89 changes: 89 additions & 0 deletions srcs/bot/MiaouBot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "bot/MiaouBot.hpp"
#include "CommandType.hpp"
#include "protocol/Message.hpp"
#include "protocol/MessageParser.hpp"
#include <ctime>
#include <cstdlib>



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<BotMessageBuffer *>(&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);
}
3 changes: 2 additions & 1 deletion srcs/commands/InviteCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "core/IMessageBuffer.hpp"
#include "protocol/MessageParser.hpp"
#include <new>
#include <iostream>

REGISTER_COMMAND(InviteCommand, irc::INVITE, "INVITE");

Expand Down Expand Up @@ -51,7 +52,7 @@ void InviteCommand::doExecute(IClient* client, const Message& message)
sendReply(client,
NumericReply::userOnChannel(client->getNickname(), targetNick, channelName));
return;
}
}

channel->addInvite(target);

Expand Down
5 changes: 5 additions & 0 deletions srcs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "core/Server.hpp"
#include <csignal>
#include <cstdlib>
#include "bot/MiaouBot.hpp"

volatile sig_atomic_t g_shutdown = 0;

Expand Down Expand Up @@ -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);
Expand Down