diff --git a/incs/bot/BotClient.hpp b/incs/bot/BotClient.hpp index 9bbd409..73da7b2 100644 --- a/incs/bot/BotClient.hpp +++ b/incs/bot/BotClient.hpp @@ -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 diff --git a/incs/bot/BotMessageBuffer.hpp b/incs/bot/BotMessageBuffer.hpp index ad02b3a..f3dbb27 100644 --- a/incs/bot/BotMessageBuffer.hpp +++ b/incs/bot/BotMessageBuffer.hpp @@ -1,38 +1,38 @@ #pragma once -#include #include +#include #include +#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); }; diff --git a/incs/bot/MiaouBot.hpp b/incs/bot/MiaouBot.hpp index 2e5c970..6a65d93 100644 --- a/incs/bot/MiaouBot.hpp +++ b/incs/bot/MiaouBot.hpp @@ -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 { diff --git a/incs/bot/NielBot.hpp b/incs/bot/NielBot.hpp index 9dbe5ef..0490ca9 100644 --- a/incs/bot/NielBot.hpp +++ b/incs/bot/NielBot.hpp @@ -7,23 +7,23 @@ #include "bot/IBot.hpp" #include -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); }; \ No newline at end of file diff --git a/incs/core/Server.hpp b/incs/core/Server.hpp index 7191f39..2baf097 100644 --- a/incs/core/Server.hpp +++ b/incs/core/Server.hpp @@ -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" @@ -63,7 +63,7 @@ class Server : public IServer void checkClientTimeouts(); - std::vector m_bots; + std::vector< IBot* > m_bots; public: Server(const Config& cfg); @@ -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 }; diff --git a/srcs/bot/BotClient.cpp b/srcs/bot/BotClient.cpp index 44d6c88..ea30b99 100644 --- a/srcs/bot/BotClient.cpp +++ b/srcs/bot/BotClient.cpp @@ -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) { } diff --git a/srcs/bot/BotMessageBuffer.cpp b/srcs/bot/BotMessageBuffer.cpp index b5917f3..139e3b5 100644 --- a/srcs/bot/BotMessageBuffer.cpp +++ b/srcs/bot/BotMessageBuffer.cpp @@ -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) { @@ -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) @@ -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 diff --git a/srcs/bot/MiaouBot.cpp b/srcs/bot/MiaouBot.cpp index b9baf32..6254b53 100644 --- a/srcs/bot/MiaouBot.cpp +++ b/srcs/bot/MiaouBot.cpp @@ -2,16 +2,15 @@ #include "CommandType.hpp" #include "protocol/Message.hpp" #include "protocol/MessageParser.hpp" -#include #include +#include - - -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(&m_client->getBuffer()); + BotMessageBuffer* bmb = dynamic_cast< BotMessageBuffer* >(&m_client->getBuffer()); bmb->setBot(this); std::srand(std::time(0)); } @@ -21,14 +20,12 @@ 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) @@ -36,21 +33,18 @@ void MiaouBot::onChannelMessage(IClient *sender, IChannel *channel, const std::s _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(); @@ -75,7 +69,6 @@ void MiaouBot::joinChannel(const std::string& channelName) } } - void MiaouBot::onPrivateMessage(IClient* sender, const std::string& message) { if (sender == m_client) diff --git a/srcs/bot/NielBot.cpp b/srcs/bot/NielBot.cpp index e473760..55235fe 100644 --- a/srcs/bot/NielBot.cpp +++ b/srcs/bot/NielBot.cpp @@ -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(&m_client->getBuffer()); + BotMessageBuffer* bmb = dynamic_cast< BotMessageBuffer* >(&m_client->getBuffer()); bmb->setBot(this); } @@ -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; @@ -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) @@ -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(); @@ -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); } \ No newline at end of file diff --git a/srcs/bot/SixSevenBot.cpp b/srcs/bot/SixSevenBot.cpp index 19dcedc..e2542d2 100644 --- a/srcs/bot/SixSevenBot.cpp +++ b/srcs/bot/SixSevenBot.cpp @@ -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(&m_client->getBuffer()); + BotMessageBuffer* bmb = dynamic_cast< BotMessageBuffer* >(&m_client->getBuffer()); bmb->setBot(this); } diff --git a/srcs/commands/InviteCommand.cpp b/srcs/commands/InviteCommand.cpp index 830ac6b..9fbca68 100644 --- a/srcs/commands/InviteCommand.cpp +++ b/srcs/commands/InviteCommand.cpp @@ -3,8 +3,8 @@ #include "commands/CommandType.hpp" #include "core/IMessageBuffer.hpp" #include "protocol/MessageParser.hpp" -#include #include +#include REGISTER_COMMAND(InviteCommand, irc::INVITE, "INVITE"); @@ -52,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/commands/QuitCommand.cpp b/srcs/commands/QuitCommand.cpp index 92e1cef..6c508b5 100644 --- a/srcs/commands/QuitCommand.cpp +++ b/srcs/commands/QuitCommand.cpp @@ -2,10 +2,10 @@ #include "IChannel.hpp" #include "IClient.hpp" #include "IServer.hpp" -#include "core/IMessageBuffer.hpp" #include "commands/ACommand.hpp" #include "commands/CommandRegistration.hpp" #include "commands/CommandType.hpp" +#include "core/IMessageBuffer.hpp" #include "protocol/Message.hpp" #include "protocol/MessageParser.hpp" #include diff --git a/srcs/core/Client.cpp b/srcs/core/Client.cpp index e84ef24..9363e80 100644 --- a/srcs/core/Client.cpp +++ b/srcs/core/Client.cpp @@ -156,4 +156,3 @@ std::string Client::getPrefix() const prefix += "!" + _username + "@" + _hostname; return prefix; } - diff --git a/srcs/core/Server.cpp b/srcs/core/Server.cpp index 2aae568..cc2c218 100644 --- a/srcs/core/Server.cpp +++ b/srcs/core/Server.cpp @@ -11,13 +11,13 @@ #include "commands/CommandFactory.hpp" #include "protocol/Message.hpp" #include "protocol/MessageParser.hpp" +#include #include #include #include #include #include #include -#include extern volatile sig_atomic_t g_shutdown; @@ -27,7 +27,7 @@ int Server::getPort() const } #ifdef BONUS -void Server::registerBot(IBot* bot) +void Server::registerBot(IBot* bot) { if (!bot) return; @@ -35,7 +35,7 @@ void Server::registerBot(IBot* bot) if (!botClient) return; - + if (std::find(m_bots.begin(), m_bots.end(), bot) != m_bots.end()) return; m_clientsByNick[botClient->getNickname()] = botClient; @@ -43,11 +43,11 @@ void Server::registerBot(IBot* bot) m_bots.push_back(bot); } -void Server::unregisterBot(IBot* bot) +void Server::unregisterBot(IBot* bot) { if (!bot) return; - std::vector::iterator it = std::find(m_bots.begin(), m_bots.end(), bot); + std::vector< IBot* >::iterator it = std::find(m_bots.begin(), m_bots.end(), bot); if (it != m_bots.end()) m_bots.erase(it); IClient* botClient = bot->getClient(); @@ -264,6 +264,14 @@ void Server::writeClientsData(int fd) return; } + if (out.size() > 1048576) + { + LOG_NOTICE << "Write buffer too large for Client #" << fd << ", disconnecting it" + << std::endl; + disconnectClient(fd); + return; + } + ssize_t sent = send(fd, out.data(), out.size(), MSG_NOSIGNAL); if (sent > 0) @@ -281,13 +289,6 @@ void Server::writeClientsData(int fd) else m_sm->modifySocket(fd, EPOLLIN | EPOLLOUT); } - - else if (sent == -1) - { - if (errno == EAGAIN || errno == EWOULDBLOCK) - return; - disconnectClient(fd); - } } void Server::readClientsData(int fd) @@ -300,57 +301,38 @@ void Server::readClientsData(int fd) } char buffer[4096]; - while (true) + ssize_t receiving = recv(fd, buffer, sizeof(buffer), 0); + if (receiving > 0) { - ssize_t receiving = recv(fd, buffer, sizeof(buffer), 0); - if (receiving > 0) + client->updateLastActivity(); + client->getBuffer().appendRead(std::string(buffer, receiving)); + while (client->getBuffer().hasCompleteMessage()) { - client->updateLastActivity(); - client->getBuffer().appendRead(std::string(buffer, receiving)); - while (client->getBuffer().hasCompleteMessage()) - { - std::string line = client->getBuffer().getNextMessage(); - onIrcLine(fd, line); - } - if (client->getBuffer().getReadBufferSize() > 65536) - { - LOG_NOTICE << "Input buffer is too big, so I'm killing Client #" << fd << std::endl; - disconnectClient(fd); - return; - } + std::string line = client->getBuffer().getNextMessage(); + onIrcLine(fd, line); } - else if (receiving == 0) + if (client->getBuffer().getReadBufferSize() > 65536) { + LOG_NOTICE << "Input buffer is too big, so I'm killing Client #" << fd << std::endl; disconnectClient(fd); - return; - } - else - { - if (errno == EAGAIN || errno == EWOULDBLOCK) - return; - disconnectClient(fd); - return; } } + else if (receiving == 0) + disconnectClient(fd); + else + return; } void Server::acceptNewClients() { - while (true) - { - int clientFd = accept(m_listenFd, 0, 0); - if (clientFd == -1) - { - if (errno == EAGAIN || errno == EWOULDBLOCK) - break; - throw std::runtime_error("Acceptation of a New Client failed\n"); - } - setNonBlocking(clientFd); - m_sm->addSocket(clientFd, EPOLLIN); + int clientFd = accept(m_listenFd, 0, 0); + if (clientFd == -1) + return; + setNonBlocking(clientFd); + m_sm->addSocket(clientFd, EPOLLIN); - m_clients[clientFd] = new Client(clientFd, "unknown", *this); - LOG_NOTICE << "New client has joined! (#" << clientFd << ")" << std::endl; - } + m_clients[clientFd] = new Client(clientFd, "unknown", *this); + LOG_NOTICE << "New client has joined! (#" << clientFd << ")" << std::endl; } void Server::handleDisconnections(int fd, unsigned int evt) @@ -498,7 +480,7 @@ Server::~Server() delete it->second; m_channels.clear(); - for (std::vector ::iterator it = m_bots.begin(); it != m_bots.end(); it++) + for (std::vector< IBot* >::iterator it = m_bots.begin(); it != m_bots.end(); it++) delete *it; if (m_listenFd != -1) @@ -506,8 +488,6 @@ Server::~Server() delete (m_sm); - - //cleaning bots here !!!! + // cleaning bots here !!!! LOG_INFO << "All resources freed. I can die in peace! :D" << std::endl; } - diff --git a/srcs/main.cpp b/srcs/main.cpp index 095c721..03306b4 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -1,10 +1,10 @@ #include "Logger.hpp" -#include "bot/SixSevenBot.hpp" +#include "bot/MiaouBot.hpp" #include "bot/NielBot.hpp" +#include "bot/SixSevenBot.hpp" #include "core/Server.hpp" #include #include -#include "bot/MiaouBot.hpp" volatile sig_atomic_t g_shutdown = 0; @@ -30,19 +30,19 @@ int main(int argc, char** argv) Config cfg = Config::checkArgs(argc, argv); Server srv(cfg); - #ifdef BONUS - SixSevenBot *sixSevenBot = new SixSevenBot(srv); +#ifdef BONUS + SixSevenBot* sixSevenBot = new SixSevenBot(srv); srv.registerBot(sixSevenBot); sixSevenBot->joinChannel("#eighty-nine"); - - MiaouBot *miaouBot = new MiaouBot(srv); - srv.registerBot(miaouBot); + + MiaouBot* miaouBot = new MiaouBot(srv); + srv.registerBot(miaouBot); miaouBot->joinChannel("#cat"); - NielBot *nielBot = new NielBot(srv); + NielBot* nielBot = new NielBot(srv); srv.registerBot(nielBot); nielBot->joinChannel("#42"); - #endif +#endif srv.run(); return (0); }