From 714ff9e4101477185c0fe4f6eafb3b997964a664 Mon Sep 17 00:00:00 2001 From: Ladebeze66 Date: Wed, 12 Jun 2024 13:51:03 +0200 Subject: [PATCH] test --- ft_irc3/includes/AdditionalCommands.hpp | 4 +- ft_irc3/includes/BotFilter.hpp | 3 +- ft_irc3/includes/Channel.hpp | 88 ++++++++++---------- ft_irc3/includes/Client.hpp | 4 +- ft_irc3/includes/ClientManager.hpp | 3 +- ft_irc3/includes/CommandHandler.hpp | 10 ++- ft_irc3/includes/InviteHandler.hpp | 3 +- ft_irc3/includes/Join.hpp | 8 +- ft_irc3/includes/KickHandler.hpp | 3 +- ft_irc3/includes/ModeHandler.hpp | 3 +- ft_irc3/includes/Server.hpp | 13 ++- ft_irc3/includes/TopicHandler.hpp | 3 +- ft_irc3/includes/Welcome.hpp | 7 +- ft_irc3/includes/Who.hpp | 3 +- ft_irc3/src/AdditionalCommands.cpp | 5 +- ft_irc3/src/BotFilter.cpp | 4 +- ft_irc3/src/Client.cpp | 11 ++- ft_irc3/src/ClientManager.cpp | 102 +++++++++++++----------- ft_irc3/src/CommandHandler.cpp | 18 +++-- ft_irc3/src/InviteHandler.cpp | 4 +- ft_irc3/src/Join.cpp | 46 ++++++++++- ft_irc3/src/KickHandler.cpp | 4 +- ft_irc3/src/ModeHandler.cpp | 4 +- ft_irc3/src/Server.cpp | 41 +++++++--- ft_irc3/src/TopicHandler.cpp | 4 +- ft_irc3/src/Welcome.cpp | 6 +- ft_irc3/src/Who.cpp | 4 +- 27 files changed, 274 insertions(+), 134 deletions(-) diff --git a/ft_irc3/includes/AdditionalCommands.hpp b/ft_irc3/includes/AdditionalCommands.hpp index 4f31577..890f428 100644 --- a/ft_irc3/includes/AdditionalCommands.hpp +++ b/ft_irc3/includes/AdditionalCommands.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/21 18:09:05 by fgras-ca #+# #+# */ -/* Updated: 2024/06/11 13:30:49 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:39:51 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,7 @@ class Server; class Client; class Channel; class BotFilter; +class KickHandler; class AdditionalCommands { private: @@ -41,6 +42,7 @@ class AdditionalCommands public: AdditionalCommands(Server *server); + ~AdditionalCommands(); void processCommand(Client *client, const std::string &command); void broadcastChannelList(Client *client, Server *server); void handlePartCommand(Server *server, Client *client, const std::string &command); diff --git a/ft_irc3/includes/BotFilter.hpp b/ft_irc3/includes/BotFilter.hpp index 4af0558..9c85b3e 100644 --- a/ft_irc3/includes/BotFilter.hpp +++ b/ft_irc3/includes/BotFilter.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/06 11:44:44 by fgras-ca #+# #+# */ -/* Updated: 2024/06/10 14:46:42 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:02:00 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,7 @@ class BotFilter { public: BotFilter(Server* server); + ~BotFilter(); void loadBadWords(const std::string& fileName); bool checkMessage(Client* client, Channel* channel, const std::string& message); void warnClient(Client* client, Channel* channel); diff --git a/ft_irc3/includes/Channel.hpp b/ft_irc3/includes/Channel.hpp index a364b5c..7776d31 100644 --- a/ft_irc3/includes/Channel.hpp +++ b/ft_irc3/includes/Channel.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 12:41:35 by fgras-ca #+# #+# */ -/* Updated: 2024/06/06 21:03:34 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:02:31 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,54 +29,54 @@ class Client; class Channel { public: - Channel(const std::string &name); - ~Channel(); + Channel(const std::string &name); + ~Channel(); - const std::string &getName() const; - void addClient(Client *client); - void removeClient(Client *client); - bool isEmpty() const; - const std::vector &getClients() const; - void addOperator(Client *client); - bool isOperator(Client *client) const; - void removeOperator(Client *client); - bool hasClient(Client *client) const; - void broadcast(const std::string &message, Client *_client, Server *_server); - bool isBanned(Client *client) const; - void banClient(Client *client, Channel *channel); - bool isFull() const; - bool isInviteOnly() const; - bool isInvited(Client *client) const; - bool checkKey(const std::string &key) const; - const std::string &getTopic() const; - const std::string &getTopicSetter() const; - time_t getTopicTime() const; - void setTopic(const std::string &topic, const std::string &setter); - std::string getKey(); + const std::string &getName() const; + void addClient(Client *client); + void removeClient(Client *client); + bool isEmpty() const; + const std::vector &getClients() const; + void addOperator(Client *client); + bool isOperator(Client *client) const; + void removeOperator(Client *client); + bool hasClient(Client *client) const; + void broadcast(const std::string &message, Client *_client, Server *_server); + bool isBanned(Client *client) const; + void banClient(Client *client, Channel *channel); + bool isFull() const; + bool isInviteOnly() const; + bool isInvited(Client *client) const; + bool checkKey(const std::string &key) const; + const std::string &getTopic() const; + const std::string &getTopicSetter() const; + time_t getTopicTime() const; + void setTopic(const std::string &topic, const std::string &setter); + std::string getKey(); - void setClientLimit(size_t limit); - size_t getClientLimit() const; - void setInviteOnly(bool inviteOnly); - void setKey(const std::string &key); - void setTopicProtection(bool protection); - std::string getModes() const; - bool getTopicProtection() const; - void addInvitedClient(Client* client); + void setClientLimit(size_t limit); + size_t getClientLimit() const; + void setInviteOnly(bool inviteOnly); + void setKey(const std::string &key); + void setTopicProtection(bool protection); + std::string getModes() const; + bool getTopicProtection() const; + void addInvitedClient(Client* client); private: Server *_server; - std::string _name; - std::vector _clients; - std::vector _operators; - std::set _bannedClients; - std::set _invitedClients; - std::string _key; - std::string _topic; - std::string _topicSetter; - time_t _topicTime; - size_t _clientLimit; - bool _inviteOnly; - bool _topicProtection; + std::string _name; + std::vector _clients; + std::vector _operators; + std::set _bannedClients; + std::set _invitedClients; + std::string _key; + std::string _topic; + std::string _topicSetter; + time_t _topicTime; + size_t _clientLimit; + bool _inviteOnly; + bool _topicProtection; }; #endif diff --git a/ft_irc3/includes/Client.hpp b/ft_irc3/includes/Client.hpp index 03d18a5..d589887 100644 --- a/ft_irc3/includes/Client.hpp +++ b/ft_irc3/includes/Client.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 12:15:42 by fgras-ca #+# #+# */ -/* Updated: 2024/06/06 19:59:39 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 12:58:08 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,13 @@ #include #include +#include class Client { public: Client(int fd, const std::string &nickname, const std::string &user, const std::string &host, const std::string &password, const std::string &realname); + ~Client(); int getFd() const; const std::string &getNickname() const; diff --git a/ft_irc3/includes/ClientManager.hpp b/ft_irc3/includes/ClientManager.hpp index 0844e76..9e4aca5 100644 --- a/ft_irc3/includes/ClientManager.hpp +++ b/ft_irc3/includes/ClientManager.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 18:30:07 by fgras-ca #+# #+# */ -/* Updated: 2024/06/06 13:01:05 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:03:49 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,7 @@ class ClientManager { public: ClientManager(Server *server); + ~ClientManager(); void acceptClient(); void handleClientNext(int client_fd, char * buffer, int bytes_received); void handleClient(int client_fd); diff --git a/ft_irc3/includes/CommandHandler.hpp b/ft_irc3/includes/CommandHandler.hpp index 3ca3a75..408c2a3 100644 --- a/ft_irc3/includes/CommandHandler.hpp +++ b/ft_irc3/includes/CommandHandler.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 18:14:12 by fgras-ca #+# #+# */ -/* Updated: 2024/06/01 18:47:51 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:38:19 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ #include "Join.hpp" #include "Welcome.hpp" #include "ModeHandler.hpp" +#include "Join.hpp" +#include "Welcome.hpp" #include #include @@ -34,6 +36,8 @@ class Channel; class WhoHandler; class AdditionalCommands; class ModeHandler; +class JoinHandler; +class WelcomeHandler; class CommandHandler { @@ -41,10 +45,12 @@ private: Server *_server; AdditionalCommands *_additionalCommands; ModeHandler *_modeHandler; - + JoinHandler *_joinHandler; + WelcomeHandler *_welcomeHandler; public: CommandHandler(Server *server); + ~CommandHandler(); bool isValidNickname(const std::string& nickname); bool isNicknameInUse(const std::string& nickname); void handleNick(Client* client, const std::vector& tokens); diff --git a/ft_irc3/includes/InviteHandler.hpp b/ft_irc3/includes/InviteHandler.hpp index 6d74f64..7c6d6da 100644 --- a/ft_irc3/includes/InviteHandler.hpp +++ b/ft_irc3/includes/InviteHandler.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/30 13:01:50 by fgras-ca #+# #+# */ -/* Updated: 2024/06/01 18:51:00 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:05:09 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,7 @@ class InviteHandler { public: InviteHandler(Server* server); + ~InviteHandler(); void handleInviteCommand(Client* client, const std::string& command); private: diff --git a/ft_irc3/includes/Join.hpp b/ft_irc3/includes/Join.hpp index e3a3c2b..56505ac 100644 --- a/ft_irc3/includes/Join.hpp +++ b/ft_irc3/includes/Join.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/21 19:51:08 by fgras-ca #+# #+# */ -/* Updated: 2024/06/11 13:50:53 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:31:09 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,6 @@ #include "Channel.hpp" #include "RPL.hpp" #include "Utils.hpp" -#include "Server.hpp" #include "Channel.hpp" #include "Client.hpp" #include "AdditionalCommands.hpp" @@ -32,9 +31,14 @@ class JoinHandler { public: + JoinHandler(Server *server); + ~JoinHandler(); void handleJoinCommand(Client *client, const std::string &channelName, Server *server); void sendJoinSuccess(Client *client, Channel *channel, Server *server); std::string getUsersList(Channel *channel); + + private: + Server *_server; }; diff --git a/ft_irc3/includes/KickHandler.hpp b/ft_irc3/includes/KickHandler.hpp index 7c00a4d..041456f 100644 --- a/ft_irc3/includes/KickHandler.hpp +++ b/ft_irc3/includes/KickHandler.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/01 16:59:42 by fgras-ca #+# #+# */ -/* Updated: 2024/06/01 18:51:26 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:10:14 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ class KickHandler { public: KickHandler(Server* server); + ~KickHandler(); void handleKickCommand(Client* client, const std::string& command); private: diff --git a/ft_irc3/includes/ModeHandler.hpp b/ft_irc3/includes/ModeHandler.hpp index 76be6f9..61d55c6 100644 --- a/ft_irc3/includes/ModeHandler.hpp +++ b/ft_irc3/includes/ModeHandler.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/30 11:12:57 by fgras-ca #+# #+# */ -/* Updated: 2024/06/08 18:53:15 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:10:55 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,7 @@ class ModeHandler { public: ModeHandler(Server* server); + ~ModeHandler(); void handleModeCommand(Client* client, const std::string& command); private: diff --git a/ft_irc3/includes/Server.hpp b/ft_irc3/includes/Server.hpp index 53fd493..9ca213f 100644 --- a/ft_irc3/includes/Server.hpp +++ b/ft_irc3/includes/Server.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 12:15:13 by fgras-ca #+# #+# */ -/* Updated: 2024/06/11 13:33:28 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 12:58:02 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ #include "ModeHandler.hpp" #include "TopicHandler.hpp" #include "BotFilter.hpp" +#include "Join.hpp" +#include "Welcome.hpp" #include #include @@ -36,6 +38,8 @@ #include #include #include +#include + class Client; class Channel; @@ -45,6 +49,8 @@ class AdditionalCommands; class ModeHandler; class TopicHandler; class BotFilter; +class JoinHandler; +class WelcomeHandler; class Server { @@ -79,6 +85,10 @@ class Server TopicHandler *_topicHandler; BotFilter *_botFilter; AdditionalCommands *_additionalCommands; + JoinHandler *_joinHandler; + WelcomeHandler *_welcomeHandler; + static Server *instance; + static void signalHandler(int signum); friend class ClientManager; friend class CommandHandler; @@ -88,6 +98,7 @@ class Server private: void initServer(); void handleServerCommands(); + bool bool_exit; }; #endif diff --git a/ft_irc3/includes/TopicHandler.hpp b/ft_irc3/includes/TopicHandler.hpp index b92ee40..74d5207 100644 --- a/ft_irc3/includes/TopicHandler.hpp +++ b/ft_irc3/includes/TopicHandler.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/30 17:04:33 by fgras-ca #+# #+# */ -/* Updated: 2024/06/01 19:02:10 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:11:49 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ class TopicHandler { public: TopicHandler(Server* server); + ~TopicHandler(); void handleTopicCommand(Client* client, const std::string& command); private: diff --git a/ft_irc3/includes/Welcome.hpp b/ft_irc3/includes/Welcome.hpp index aa3f330..0209d18 100644 --- a/ft_irc3/includes/Welcome.hpp +++ b/ft_irc3/includes/Welcome.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/21 19:53:17 by fgras-ca #+# #+# */ -/* Updated: 2024/06/01 19:02:40 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:13:27 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,8 +22,13 @@ class WelcomeHandler { public: + WelcomeHandler(Server *server); + ~WelcomeHandler(); void sendWelcomeMessages(Client *client, Server *server); void sendMotd(Client *client, Server *server); + + private: + Server *_server; }; #endif diff --git a/ft_irc3/includes/Who.hpp b/ft_irc3/includes/Who.hpp index f32f32a..0a70222 100644 --- a/ft_irc3/includes/Who.hpp +++ b/ft_irc3/includes/Who.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/17 16:08:48 by fgras-ca #+# #+# */ -/* Updated: 2024/06/01 19:03:06 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:14:47 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,7 @@ class WhoHandler { public: WhoHandler(Server *server); + ~WhoHandler(); void handleWhoCommand(Client *client, const std::string &command); void handleWhoisCommand(Client *client, const std::string &command); diff --git a/ft_irc3/src/AdditionalCommands.cpp b/ft_irc3/src/AdditionalCommands.cpp index 949ada1..57f2bd2 100644 --- a/ft_irc3/src/AdditionalCommands.cpp +++ b/ft_irc3/src/AdditionalCommands.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/16 15:27:29 by fgras-ca #+# #+# */ -/* Updated: 2024/06/11 11:45:43 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:01:35 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,9 @@ std::string toUpperCase(const std::string &str) return upperStr; } +AdditionalCommands::~AdditionalCommands() +{} + void AdditionalCommands::processCommand(Client *client, const std::string &command) { std::istringstream iss(command); diff --git a/ft_irc3/src/BotFilter.cpp b/ft_irc3/src/BotFilter.cpp index 102b1f5..43fd210 100644 --- a/ft_irc3/src/BotFilter.cpp +++ b/ft_irc3/src/BotFilter.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/06 11:45:43 by fgras-ca #+# #+# */ -/* Updated: 2024/06/10 14:56:40 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:02:19 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,8 @@ void BotFilter::loadBadWords(const std::string &fileName) } } +BotFilter::~BotFilter() {} + bool BotFilter::checkMessage(Client* client, Channel* channel, const std::string& message) { if (containsBadWords(message)) diff --git a/ft_irc3/src/Client.cpp b/ft_irc3/src/Client.cpp index 75b3deb..b66c4e5 100644 --- a/ft_irc3/src/Client.cpp +++ b/ft_irc3/src/Client.cpp @@ -6,14 +6,21 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 12:17:42 by fgras-ca #+# #+# */ -/* Updated: 2024/06/06 20:06:29 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 12:51:56 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "Client.hpp" Client::Client(int fd, const std::string &nickname, const std::string &user, const std::string &host, const std::string &password, const std::string &realname) - : _fd(fd), _nickname(nickname), _user(user), _host(host), _password(password), _realname(realname), _authenticated(false), _operator(false), _away(false) {} + : _fd(fd), _nickname(nickname), _user(user), _host(host), _password(password), _realname(realname), _authenticated(false), _operator(false), _away(false) +{ + std::memset(buffer, 0, sizeof(buffer)); + std::memset(buffer2, 0, sizeof(buffer2)); +} + +Client::~Client() +{} int Client::getFd() const { diff --git a/ft_irc3/src/ClientManager.cpp b/ft_irc3/src/ClientManager.cpp index 0661b56..1006fc1 100644 --- a/ft_irc3/src/ClientManager.cpp +++ b/ft_irc3/src/ClientManager.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 18:32:23 by fgras-ca #+# #+# */ -/* Updated: 2024/06/06 19:59:09 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 13:04:16 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,11 @@ ClientManager::ClientManager(Server *server) _botFilter->loadBadWords("badwords.txt"); } +ClientManager::~ClientManager() +{ + delete _botFilter; +} + void ClientManager::acceptClient() { int client_fd = accept(_server->_server_fd, NULL, NULL); @@ -33,6 +38,7 @@ void ClientManager::acceptClient() struct pollfd client_pollfd; client_pollfd.fd = client_fd; client_pollfd.events = POLLIN; + client_pollfd.revents = 0; _server->_poll_fds.push_back(client_pollfd); std::stringstream ss; @@ -42,60 +48,60 @@ void ClientManager::acceptClient() void ClientManager::handleClient(int client_fd) { - Client* client = _server->getClients()[client_fd]; - std::memset(client->buffer, 0, sizeof(client->buffer)); - int bytes_received = recv(client_fd, client->buffer, sizeof(client->buffer), 0); + Client* client = _server->getClients()[client_fd]; + std::memset(client->buffer, 0, sizeof(client->buffer)); + int bytes_received = recv(client_fd, client->buffer, sizeof(client->buffer), 0); - if (bytes_received <= 0) - { - std::ostringstream oss; - oss << "Client disconnected: " << client_fd; - _server->log(oss.str(), RED); - removeClient(client_fd); - return; - } + if (bytes_received <= 0) + { + std::ostringstream oss; + oss << "Client disconnected: " << client_fd; + _server->log(oss.str(), RED); + removeClient(client_fd); + return; + } - std::cout << std::string(client->buffer).size() << " client->buffer " << std::string(client->buffer).find('\n') << std::endl; + std::cout << std::string(client->buffer).size() << " client->buffer " << std::string(client->buffer).find('\n') << std::endl; - for (size_t i = 0; client->buffer[i]; i++) - { - std::cout << client->buffer[i] << " .. "; - } - std::cout << std::endl; + for (size_t i = 0; client->buffer[i]; i++) + { + std::cout << client->buffer[i] << " .. "; + } + std::cout << std::endl; - if (std::string(client->buffer).find('\n') != std::string::npos) - { - strcat(client->buffer2, client->buffer); + if (std::string(client->buffer).find('\n') != std::string::npos) + { + strcat(client->buffer2, client->buffer); - bool messageAllowed = true; - std::set::const_iterator it; - for (it = client->getChannels().begin(); it != client->getChannels().end(); ++it) - { - const std::string& channelName = *it; - Channel* channel = _server->getChannelByName(channelName); - if (channel && !_botFilter->checkMessage(client, channel, std::string(client->buffer2))) - { - messageAllowed = false; - break; - } - } + bool messageAllowed = true; + std::set::const_iterator it; + for (it = client->getChannels().begin(); it != client->getChannels().end(); ++it) + { + const std::string& channelName = *it; + Channel* channel = _server->getChannelByName(channelName); + if (channel && !_botFilter->checkMessage(client, channel, std::string(client->buffer2))) + { + messageAllowed = false; + break; + } + } - if (messageAllowed) - { - handleClientNext(client_fd, client->buffer2, std::string(client->buffer2).size()); - } + if (messageAllowed) + { + handleClientNext(client_fd, client->buffer2, std::string(client->buffer2).size()); + } - std::memset(client->buffer2, 0, std::string(client->buffer2).size()); - } - else - { - strcat(client->buffer2, client->buffer); - for (size_t i = 0; client->buffer2[i]; i++) - { - std::cout << client->buffer2[i] << " . "; - } - } - std::cout << std::endl; + std::memset(client->buffer2, 0, std::string(client->buffer2).size()); + } + else + { + strcat(client->buffer2, client->buffer); + for (size_t i = 0; client->buffer2[i]; i++) + { + std::cout << client->buffer2[i] << " . "; + } + } + std::cout << std::endl; } void ClientManager::handleClientNext(int client_fd, char * buffer, int bytes_received) diff --git a/ft_irc3/src/CommandHandler.cpp b/ft_irc3/src/CommandHandler.cpp index 79b6e61..443dd82 100644 --- a/ft_irc3/src/CommandHandler.cpp +++ b/ft_irc3/src/CommandHandler.cpp @@ -6,14 +6,14 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 18:26:34 by fgras-ca #+# #+# */ -/* Updated: 2024/06/08 19:14:48 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 12:15:47 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "CommandHandler.hpp" CommandHandler::CommandHandler(Server *server) - : _server(server), _additionalCommands(new AdditionalCommands(server)), _modeHandler(new ModeHandler(server)) + : _server(server), _additionalCommands(new AdditionalCommands(server)), _modeHandler(new ModeHandler(server)), _joinHandler(new JoinHandler(server)), _welcomeHandler(new WelcomeHandler(server)) { // Ensure that _server is not null if (!_server) @@ -23,6 +23,14 @@ CommandHandler::CommandHandler(Server *server) } } +CommandHandler::~CommandHandler() +{ + delete _welcomeHandler; + delete _joinHandler; + delete _modeHandler; + delete _additionalCommands; +} + void CommandHandler::handleCommand(Client* client, const std::string& command) { std::vector tokens = split(command, " \n\r\t"); @@ -71,8 +79,7 @@ void CommandHandler::handleCommand(Client* client, const std::string& command) else if (commandType == "JOIN") { std::string joinParams = command.substr(command.find(" ") + 1); - JoinHandler joinHandler; - joinHandler.handleJoinCommand(client, joinParams, _server); + _joinHandler->handleJoinCommand(client, joinParams, _server); } else { @@ -248,8 +255,7 @@ void CommandHandler::handleUser(Client* client, const std::vector& if (client->getPassword() == _server->_password && !client->getNickname().empty()) { client->authenticate(); - WelcomeHandler welcomeHandler; - welcomeHandler.sendWelcomeMessages(client, _server); + _welcomeHandler->sendWelcomeMessages(client, _server); _server->log("Client " + client->getNickname() + " authenticated.", GREEN); } else diff --git a/ft_irc3/src/InviteHandler.cpp b/ft_irc3/src/InviteHandler.cpp index eaba4a0..63f1b9d 100644 --- a/ft_irc3/src/InviteHandler.cpp +++ b/ft_irc3/src/InviteHandler.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/30 13:02:09 by fgras-ca #+# #+# */ -/* Updated: 2024/06/08 19:15:29 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:05:29 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ InviteHandler::InviteHandler(Server* server) : _server(server) {} +InviteHandler::~InviteHandler() {} + void InviteHandler::handleInviteCommand(Client* client, const std::string& command) { std::istringstream iss(command); diff --git a/ft_irc3/src/Join.cpp b/ft_irc3/src/Join.cpp index c6a4638..4de3328 100644 --- a/ft_irc3/src/Join.cpp +++ b/ft_irc3/src/Join.cpp @@ -12,6 +12,11 @@ #include "Join.hpp" +JoinHandler::JoinHandler(Server *server) : _server(server) +{} + +JoinHandler::~JoinHandler() {} + void JoinHandler::handleJoinCommand(Client* client, const std::string& params, Server* server) { std::map& channels = server->getChannels(); @@ -66,7 +71,46 @@ void JoinHandler::handleJoinCommand(Client* client, const std::string& params, S it->second->broadcast(partMsg.str(), client, server); } } - // Leave all channels + + ////////////////////////// + + + + /*std::vector channels = split(channelNames, ","); + std::map &channelMap = server->getChannels(); + + for (size_t i = 0; i < channels.size(); ++i) + { + std::string &channelName = channels[i]; + if (channelMap.find(channelName) == channelMap.end()) + { + server->sendToClient(client->getFd(), ERR_NOSUCHCHANNEL(client, channelName)); + continue; + } + + Channel *channel = channelMap[channelName]; + if (!channel->hasClient(client)) { + server->sendToClient(client->getFd(), ERR_NOTONCHANNEL(client, channelName)); + continue; + } + + channel->removeClient(client); + + std::ostringstream partMsg; + partMsg << ":" << client->getNickname() << " PART " << channelName << " Leaving" << "\r\n"; + server->sendToClient(client->getFd(), partMsg.str()); + + channel->broadcast(partMsg.str(), client, server); + + if (channel->isEmpty()) + { + delete channel; + channelMap.erase(channelName); + } + + server->log("Client " + client->getNickname() + " left channel " + channelName, MAGENTA); + } + // Leave all channels*/ return; } diff --git a/ft_irc3/src/KickHandler.cpp b/ft_irc3/src/KickHandler.cpp index facb301..b5b5506 100644 --- a/ft_irc3/src/KickHandler.cpp +++ b/ft_irc3/src/KickHandler.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/01 17:00:31 by fgras-ca #+# #+# */ -/* Updated: 2024/06/08 19:16:10 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:40:37 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ KickHandler::KickHandler(Server* server) : _server(server) {} +KickHandler::~KickHandler() {} + void KickHandler::handleKickCommand(Client* client, const std::string& command) { std::vector tokens = split(command, " "); diff --git a/ft_irc3/src/ModeHandler.cpp b/ft_irc3/src/ModeHandler.cpp index 212e209..2e30cf8 100644 --- a/ft_irc3/src/ModeHandler.cpp +++ b/ft_irc3/src/ModeHandler.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/30 11:13:08 by fgras-ca #+# #+# */ -/* Updated: 2024/06/11 11:27:28 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:11:16 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ ModeHandler::ModeHandler(Server* server) : _server(server) {} +ModeHandler::~ModeHandler() {} + void ModeHandler::handleModeCommand(Client* client, const std::string& command) { _server->log("Received MODE command: " + command, BLUE); diff --git a/ft_irc3/src/Server.cpp b/ft_irc3/src/Server.cpp index 365d7d1..33f4016 100644 --- a/ft_irc3/src/Server.cpp +++ b/ft_irc3/src/Server.cpp @@ -6,26 +6,34 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 12:17:12 by fgras-ca #+# #+# */ -/* Updated: 2024/06/11 13:34:13 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 12:45:22 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "Server.hpp" +Server *Server::instance = NULL; + Server::Server(int port, const std::string &password) - : _port(port), _password(password), _clientManager(new ClientManager(this)), _commandHandler(new CommandHandler(this)), _modeHandler(new ModeHandler(this)), _topicHandler(new TopicHandler(this)), _botFilter(new BotFilter(this)), _additionalCommands(new AdditionalCommands(this)) + : _port(port), _password(password), _clientManager(new ClientManager(this)), _commandHandler(new CommandHandler(this)), _modeHandler(new ModeHandler(this)), _topicHandler(new TopicHandler(this)), _botFilter(new BotFilter(this)), _additionalCommands(new AdditionalCommands(this)), _joinHandler(new JoinHandler(this)), _welcomeHandler(new WelcomeHandler(this)) { initServer(); - _botFilter = new BotFilter(this); _botFilter->loadBadWords("badwords.txt"); + bool_exit = 0; + instance = this; // Stocke l'instance actuelle + signal(SIGINT, Server::signalHandler); // Enregistre le gestionnaire de signal } Server::~Server() { delete _clientManager; + delete _welcomeHandler; + delete _joinHandler; delete _commandHandler; delete _topicHandler; delete _botFilter; + delete _modeHandler; + delete _additionalCommands; for (std::map::iterator it = _clients.begin(); it != _clients.end(); ++it) { @@ -78,6 +86,15 @@ void Server::initServer() log("Server initialized.", GREEN); } +void Server::signalHandler(int signum) +{ + if (signum == SIGINT) + { + instance->bool_exit = 1; // Définit bool_exit à 1 lorsque Ctrl-C est capturé + std::cout << RED << "SIGINT detected" << RESET << std::endl; + } +} + void Server::run() { struct pollfd server_pollfd; @@ -86,22 +103,24 @@ void Server::run() server_pollfd.revents = 0; _poll_fds.push_back(server_pollfd); - struct pollfd stdin_pollfd; stdin_pollfd.fd = STDIN_FILENO; stdin_pollfd.events = POLLIN; stdin_pollfd.revents = 0; _poll_fds.push_back(stdin_pollfd); - while (true) + bool running = true; + + while (running) { + if (bool_exit == 1) + return; int poll_count = poll(&_poll_fds[0], _poll_fds.size(), -1); if (poll_count == -1) { log("Poll error.", RED); - exit(EXIT_FAILURE); + return; // Vous pouvez remplacer par une gestion d'erreur plus propre } - for (size_t i = 0; i < _poll_fds.size(); ++i) { if (_poll_fds[i].revents & POLLIN) @@ -119,11 +138,12 @@ void Server::run() _clientManager->handleClient(_poll_fds[i].fd); } } - } + } } } + std::map &Server::getChannels() { return _channels; @@ -141,8 +161,9 @@ void Server::handleServerCommands() if (command == "quit") { - log("Server shutting down.", YELLOW); - exit(EXIT_SUCCESS); + bool_exit = 1; + //log("Server shutting down.", YELLOW); + //exit(EXIT_SUCCESS); } else if (command == "channels") { diff --git a/ft_irc3/src/TopicHandler.cpp b/ft_irc3/src/TopicHandler.cpp index a8d973e..f59e234 100644 --- a/ft_irc3/src/TopicHandler.cpp +++ b/ft_irc3/src/TopicHandler.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/30 17:04:58 by fgras-ca #+# #+# */ -/* Updated: 2024/06/08 19:17:37 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:12:14 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ TopicHandler::TopicHandler(Server* server) : _server(server) {} +TopicHandler::~TopicHandler() {} + void TopicHandler::handleTopicCommand(Client* client, const std::string& command) { _server->log("Received TOPIC command: " + command, "\033[1;34m"); diff --git a/ft_irc3/src/Welcome.cpp b/ft_irc3/src/Welcome.cpp index 28cb40a..f665d7a 100644 --- a/ft_irc3/src/Welcome.cpp +++ b/ft_irc3/src/Welcome.cpp @@ -6,12 +6,16 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/21 17:53:52 by fgras-ca #+# #+# */ -/* Updated: 2024/05/30 16:43:14 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:14:28 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "Welcome.hpp" +WelcomeHandler::WelcomeHandler(Server *server) : _server(server) {} + +WelcomeHandler::~WelcomeHandler() {} + void WelcomeHandler::sendWelcomeMessages(Client *client, Server *server) { server->sendToClient(client->getFd(), RPL_WELCOME(client)); diff --git a/ft_irc3/src/Who.cpp b/ft_irc3/src/Who.cpp index f422767..804c972 100644 --- a/ft_irc3/src/Who.cpp +++ b/ft_irc3/src/Who.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/17 16:09:20 by fgras-ca #+# #+# */ -/* Updated: 2024/06/07 13:43:54 by fgras-ca ### ########.fr */ +/* Updated: 2024/06/12 11:15:09 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ WhoHandler::WhoHandler(Server *server) { } +WhoHandler::~WhoHandler() {} + void WhoHandler::handleWhoCommand(Client *client, const std::string &command) { std::istringstream iss(command);