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
4 changes: 2 additions & 2 deletions incs/bot/BotClient.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include "BotMessageBuffer.hpp"
#include "IBot.hpp"
#include "IChannel.hpp"
#include "IClient.hpp"
#include "IServer.hpp"
#include "BotMessageBuffer.hpp"
#include "IBot.hpp"
#include "bot/BotMessageBuffer.hpp"

class BotClient : public IClient
Expand Down
54 changes: 27 additions & 27 deletions incs/bot/BotMessageBuffer.hpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
#pragma once

#include <iostream>
#include <cstddef>
#include <iostream>
#include <vector>

#include "bot/IBot.hpp"
#include "core/IMessageBuffer.hpp"
#include "core/IServer.hpp"
#include "bot/IBot.hpp"
#include "protocol/Message.hpp"

class BotMessageBuffer : public IMessageBuffer
class BotMessageBuffer : public IMessageBuffer
{
private:
IServer &m_server;
IBot *m_bot;
std::string m_readBuffer;
std::string m_writeBuffer;

void processIncomingMessage(const std::string& raw);
void parseAndDispatch(const Message &message);

public:
BotMessageBuffer(IServer& server);
virtual ~BotMessageBuffer();

void appendRead(const std::string& data);
bool hasCompleteMessage() const;
std::string getNextMessage();
size_t getReadBufferSize() const;

void appendWrite(const std::string& data);
void consumeWriteBuffer(size_t bytes);
const std::string& getWriteBuffer() const;
void clearWriteBuffer();

void setBot(IBot* bot);
private:
IServer& m_server;
IBot* m_bot;
std::string m_readBuffer;
std::string m_writeBuffer;

void processIncomingMessage(const std::string& raw);
void parseAndDispatch(const Message& message);

public:
BotMessageBuffer(IServer& server);
virtual ~BotMessageBuffer();

void appendRead(const std::string& data);
bool hasCompleteMessage() const;
std::string getNextMessage();
size_t getReadBufferSize() const;

void appendWrite(const std::string& data);
void consumeWriteBuffer(size_t bytes);
const std::string& getWriteBuffer() const;
void clearWriteBuffer();

void setBot(IBot* bot);
};
2 changes: 1 addition & 1 deletion incs/bot/MiaouBot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "IChannel.hpp"
#include "IClient.hpp"
#include "IServer.hpp"
#include "bot/IBot.hpp"
#include "bot/BotClient.hpp"
#include "bot/IBot.hpp"

class MiaouBot : public IBot
{
Expand Down
26 changes: 13 additions & 13 deletions incs/bot/NielBot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
#include "bot/IBot.hpp"
#include <fstream>

class NielBot : public IBot
class NielBot : public IBot
{
private:
IServer& m_server;
BotClient* m_client;
private:
IServer& m_server;
BotClient* m_client;

void sendToChannel(IChannel* channel, const std::string& msg);
void sendToChannel(IChannel* channel, const std::string& msg);

public:
NielBot(IServer& server, const std::string& nick = "NielBot");
~NielBot();
public:
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);
void onPrivateMessage(IClient* sender, const std::string& msg);
void onChannelMessage(IClient* sender, IChannel* channel, const std::string& msg);

IClient* getClient();
IClient* getClient();

void joinChannel(const std::string& channelName);
void loadAscii(const std::string& path);
void joinChannel(const std::string& channelName);
void loadAscii(const std::string& path);
};
12 changes: 6 additions & 6 deletions incs/core/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

#include "IChannel.hpp"
#include "IClient.hpp"
#include "bot/IBot.hpp"
#include "core/Config.hpp"
#include "core/IServer.hpp"
#include "network/ISocketManager.hpp"
#include "bot/IBot.hpp"

#define RESET "\033[0m"
#define RED "\033[91m"
Expand Down Expand Up @@ -63,7 +63,7 @@ class Server : public IServer

void checkClientTimeouts();

std::vector<IBot*> m_bots;
std::vector< IBot* > m_bots;

public:
Server(const Config& cfg);
Expand All @@ -87,8 +87,8 @@ class Server : public IServer

void markForDisconnect(int fd);

#ifdef BONUS
void registerBot(IBot* bot);
void unregisterBot(IBot* bot);
#endif
#ifdef BONUS
void registerBot(IBot* bot);
void unregisterBot(IBot* bot);
#endif
};
6 changes: 3 additions & 3 deletions srcs/bot/BotClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
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), m_buffer(server), _passwordProvided(true),
m_lastActivity(std::time(NULL)), m_lastPingSent(0)
: m_id(s_nextBotId--), m_nickname(nick), m_username(""), m_realname(""), m_hostname("internal"),
m_server(server), m_buffer(server), _passwordProvided(true), m_lastActivity(std::time(NULL)),
m_lastPingSent(0)
{
}

Expand Down
23 changes: 12 additions & 11 deletions srcs/bot/BotMessageBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
#include "protocol/Message.hpp"
#include "protocol/MessageParser.hpp"

BotMessageBuffer::BotMessageBuffer(IServer& server) : m_server(server), m_bot(NULL)
BotMessageBuffer::BotMessageBuffer(IServer& server) : m_server(server), m_bot(NULL)
{
}

BotMessageBuffer::~BotMessageBuffer(){}
BotMessageBuffer::~BotMessageBuffer()
{
}

void BotMessageBuffer::appendRead(const std::string& data)
{
Expand Down Expand Up @@ -70,20 +72,19 @@ void BotMessageBuffer::clearWriteBuffer()
m_writeBuffer.clear();
}

void BotMessageBuffer::setBot(IBot *bot)
void BotMessageBuffer::setBot(IBot* bot)
{
m_bot = bot;
}

void BotMessageBuffer::parseAndDispatch(const Message &message)
void BotMessageBuffer::parseAndDispatch(const Message& message)
{

if (message.m_command_type != irc::PRIVMSG)
return;

std::string target = message.m_params[0];
std::string text = message.m_params[1];
std::string senderNick = message.m_prefix.substr(0, message.m_prefix.find('!'));
std::string target = message.m_params[0];
std::string text = message.m_params[1];
std::string senderNick = message.m_prefix.substr(0, message.m_prefix.find('!'));

IClient* sender = m_server.getClientByNickname(senderNick);
if (!sender)
Expand All @@ -101,13 +102,13 @@ void BotMessageBuffer::parseAndDispatch(const Message &message)
}
}

void BotMessageBuffer::processIncomingMessage(const std::string& raw)
void BotMessageBuffer::processIncomingMessage(const std::string& raw)
{
Message receivedMsg = MessageParser::parse(raw);
if (!receivedMsg.isValid())
return ;
return;

parseAndDispatch(receivedMsg);
}

//:prefix COMMAND param1 param2 :trailing parameter with spaces
//: prefix COMMAND param1 param2 :trailing parameter with spaces
35 changes: 14 additions & 21 deletions srcs/bot/MiaouBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
#include "CommandType.hpp"
#include "protocol/Message.hpp"
#include "protocol/MessageParser.hpp"
#include <ctime>
#include <cstdlib>
#include <ctime>



MiaouBot::MiaouBot(IServer& server, const std::string& nick) : m_server(server), _count_message(0), _index(0)
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());
BotMessageBuffer* bmb = dynamic_cast< BotMessageBuffer* >(&m_client->getBuffer());
bmb->setBot(this);
std::srand(std::time(0));
}
Expand All @@ -21,36 +20,31 @@ MiaouBot::~MiaouBot()
delete m_client;
}


void MiaouBot::onChannelMessage(IClient *sender, IChannel *channel, const std::string &message)
void MiaouBot::onChannelMessage(IClient* sender, IChannel* channel, const std::string& message)
{

if (sender == m_client)
return;
if (message.empty())
return;
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."
};
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]);
sendToChannel(channel, responses[_index % 6]);
}

}

void MiaouBot::sendToChannel(IChannel *channel, const std::string &message)
void MiaouBot::sendToChannel(IChannel* channel, const std::string& message)
{
Message msg;
msg.m_prefix = m_client->getPrefix();
Expand All @@ -75,7 +69,6 @@ void MiaouBot::joinChannel(const std::string& channelName)
}
}


void MiaouBot::onPrivateMessage(IClient* sender, const std::string& message)
{
if (sender == m_client)
Expand Down
15 changes: 7 additions & 8 deletions srcs/bot/NielBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
#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<BotMessageBuffer *>(&m_client->getBuffer());
BotMessageBuffer* bmb = dynamic_cast< BotMessageBuffer* >(&m_client->getBuffer());
bmb->setBot(this);
}

Expand All @@ -20,7 +19,7 @@ NielBot::~NielBot()
delete m_client;
}

void NielBot::onChannelMessage(IClient* sender, IChannel* channel, const std::string& msg)
void NielBot::onChannelMessage(IClient* sender, IChannel* channel, const std::string& msg)
{
if (sender == m_client)
return;
Expand All @@ -29,20 +28,20 @@ void NielBot::onChannelMessage(IClient* sender, IChannel* channel, const std::st
if (has42)
{
std::ifstream file("ascii/xavier.txt");
std::string line;
std::string line;
while (std::getline(file, line))
sendToChannel(channel, line);
}
}

void NielBot::onPrivateMessage(IClient* sender, const std::string& msg)
void NielBot::onPrivateMessage(IClient* sender, const std::string& msg)
{
if (sender == m_client)
return;
(void)msg;
}

void NielBot::joinChannel(const std::string& channelName)
void NielBot::joinChannel(const std::string& channelName)
{
IChannel* channel = m_server.getChannel(channelName);
if (!channel)
Expand All @@ -54,7 +53,7 @@ void NielBot::joinChannel(const std::string& channelName)
}
}

void NielBot::sendToChannel(IChannel* channel, const std::string& message)
void NielBot::sendToChannel(IChannel* channel, const std::string& message)
{
Message msg;
msg.m_prefix = m_client->getPrefix();
Expand All @@ -67,7 +66,7 @@ void NielBot::sendToChannel(IChannel* channel, const std::string& message)
channel->broadcast(serialized, m_client);
}

IClient* NielBot::getClient()
IClient* NielBot::getClient()
{
return (m_client);
}
2 changes: 1 addition & 1 deletion srcs/bot/SixSevenBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SixSevenBot::SixSevenBot(IServer& server, const std::string& nick) : m_server(se
{
m_client = new BotClient(nick, server);
m_client->setBot(this);
BotMessageBuffer *bmb = dynamic_cast<BotMessageBuffer *>(&m_client->getBuffer());
BotMessageBuffer* bmb = dynamic_cast< BotMessageBuffer* >(&m_client->getBuffer());
bmb->setBot(this);
}

Expand Down
Loading