This commit is contained in:
Ladebeze66 2024-06-12 13:51:03 +02:00
parent 6da78f6457
commit 714ff9e410
27 changed files with 274 additions and 134 deletions

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<Client *> &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<Client *> &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<Client *> _clients;
std::vector<Client *> _operators;
std::set<int> _bannedClients;
std::set<Client *> _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<Client *> _clients;
std::vector<Client *> _operators;
std::set<int> _bannedClients;
std::set<Client *> _invitedClients;
std::string _key;
std::string _topic;
std::string _topicSetter;
time_t _topicTime;
size_t _clientLimit;
bool _inviteOnly;
bool _topicProtection;
};
#endif

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
#include <set>
#include <cstring>
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;

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
#include <sstream>
@ -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<std::string>& tokens);

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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:

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
};

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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:

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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:

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <sys/socket.h>
#include <netinet/in.h>
@ -36,6 +38,8 @@
#include <vector>
#include <map>
#include <poll.h>
#include <csignal>
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

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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:

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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))

View File

@ -6,14 +6,21 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
{

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<std::string>::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<std::string>::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)

View File

@ -6,14 +6,14 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<std::string> 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<std::string>&
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

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -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<std::string, Channel*>& 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<std::string> channels = split(channelNames, ",");
std::map<std::string, Channel *> &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;
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<std::string> tokens = split(command, " ");

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,26 +6,34 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<int, Client*>::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<std::string, Channel *> &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")
{

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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");

View File

@ -6,12 +6,16 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);