This commit is contained in:
Ladebeze66 2024-06-10 15:31:55 +02:00
parent a604e93070
commit 826de5d786
12 changed files with 131 additions and 60 deletions

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 18:09:05 by fgras-ca #+# #+# */ /* Created: 2024/05/21 18:09:05 by fgras-ca #+# #+# */
/* Updated: 2024/06/06 18:41:04 by fgras-ca ### ########.fr */ /* Updated: 2024/06/10 13:28:55 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 11:44:44 by fgras-ca #+# #+# */ /* Created: 2024/06/06 11:44:44 by fgras-ca #+# #+# */
/* Updated: 2024/06/06 18:50:12 by fgras-ca ### ########.fr */ /* Updated: 2024/06/10 14:46:42 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,6 +35,7 @@ public:
void loadBadWords(const std::string& fileName); void loadBadWords(const std::string& fileName);
bool checkMessage(Client* client, Channel* channel, const std::string& message); bool checkMessage(Client* client, Channel* channel, const std::string& message);
void warnClient(Client* client, Channel* channel); void warnClient(Client* client, Channel* channel);
void warnClientDirect(Client* client);
void kickClient(Client* client, Channel* channel); void kickClient(Client* client, Channel* channel);
private: private:

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 11:12:57 by fgras-ca #+# #+# */ /* Created: 2024/05/30 11:12:57 by fgras-ca #+# #+# */
/* Updated: 2024/06/07 12:24:49 by fgras-ca ### ########.fr */ /* Updated: 2024/06/08 18:53:15 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -45,7 +45,7 @@ class ModeHandler
void applyModeL(Client *client, Channel* channel, bool adding, const std::string& argument); void applyModeL(Client *client, Channel* channel, bool adding, const std::string& argument);
void applyModeI(Client *client, Channel* channel, bool adding); void applyModeI(Client *client, Channel* channel, bool adding);
void applyModeK(Client *client, Channel* channel, bool adding, const std::string& argument); void applyModeK(Client *client, Channel* channel, bool adding, const std::string& argument);
void applyModeT(Channel* channel, bool adding); void applyModeT(Client *client, Channel* channel, bool adding);
void applyModeO(Client *client, Channel* channel, bool adding, const std::string& argument); void applyModeO(Client *client, Channel* channel, bool adding, const std::string& argument);
bool needsArgument(char mode); bool needsArgument(char mode);
}; };

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:12:47 by fgras-ca #+# #+# */ /* Created: 2024/05/19 15:12:47 by fgras-ca #+# #+# */
/* Updated: 2024/06/07 14:00:45 by fgras-ca ### ########.fr */ /* Updated: 2024/06/08 20:20:39 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -391,10 +391,10 @@ inline std::string ERR_NOTREGISTERED(Client* client)
//RPL Password //RPL Password
// RPL Error Messages // RPL Error Messages
inline std::string ERR_NEEDMOREPARAMS(Client* client, const std::string& command) inline std::string ERR_NEEDMOREPARAMS(Client* client, std::string channel, const std::string& command)
{ {
std::ostringstream oss; std::ostringstream oss;
oss << ":" << SERVER_NAME << " 461 " << CLIENT_NICK(client) << " " << command << " :Not enough parameters\r\n"; oss << ":" << SERVER_NAME << " 461 " << CLIENT_NICK(client) << " " << channel << command << " :Not enough parameters\r\n";
return oss.str(); return oss.str();
} }
@ -541,10 +541,10 @@ inline std::string RPL_CAPEND(Client *client)
return oss.str(); return oss.str();
} }
inline std::string MODEACCEPTMESSAGE(Client *client, std::string channel, const std::string& mode) inline std::string MODEACCEPTMESSAGE(Client *client, std::string channel, const std::string& mode, const std::string& argument)
{ {
std::ostringstream oss; std::ostringstream oss;
oss << ":" << client->getNickname() << " MODE " << channel << " " << mode << " :" << "\r\n"; oss << ":" << client->getNickname() << " MODE " << channel << " " << mode << argument << "\r\n";
return oss.str(); return oss.str();
} }
@ -555,10 +555,10 @@ inline std::string BOTMESSAGE(Client *client, const std::string &channel, const
return oss.str(); return oss.str();
} }
inline std::string MODELCHANGE(Client *client, std::string channel, const std::string& mode) inline std::string MODELNEEDPARAM(const std::string& mode)
{ {
std::ostringstream oss; std::ostringstream oss;
oss << SERVER_NAME <<":" << client->getNickname() << " MODE " << channel << " " << mode << " :" << "\r\n"; oss << mode << ":NOT enought parameters" << "\r\n";
return oss.str(); return oss.str();
} }

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/16 15:27:29 by fgras-ca #+# #+# */ /* Created: 2024/05/16 15:27:29 by fgras-ca #+# #+# */
/* Updated: 2024/06/07 11:34:13 by fgras-ca ### ########.fr */ /* Updated: 2024/06/10 15:17:13 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,49 +14,74 @@
AdditionalCommands::AdditionalCommands(Server *server) : _server(server) {} AdditionalCommands::AdditionalCommands(Server *server) : _server(server) {}
std::string toUpperCase(const std::string &str)
{
std::string upperStr = str;
std::transform(upperStr.begin(), upperStr.end(), upperStr.begin(), ::toupper);
return upperStr;
}
void AdditionalCommands::processCommand(Client *client, const std::string &command) void AdditionalCommands::processCommand(Client *client, const std::string &command)
{ {
if (command.find("PART") == 0) std::istringstream iss(command);
std::string cmd;
iss >> cmd;
std::string upperCommand = toUpperCase(cmd) + command.substr(cmd.length());
// Log the received command
_server->log("Received command: " + command, BLUE);
_server->log("Converted command to uppercase: " + upperCommand, BLUE);
if (upperCommand.find("PART") == 0)
{ {
_server->log("Handling PART command", GREEN);
handlePartCommand(_server, client, command); handlePartCommand(_server, client, command);
} }
else if (command.find("PRIVMSG") == 0) else if (upperCommand.find("PRIVMSG") == 0)
{ {
_server->log("Handling PRIVMSG command", GREEN);
handlePrivmsgCommand(_server, client, command); handlePrivmsgCommand(_server, client, command);
} }
else if (command.find("WHO") == 0) else if (upperCommand.find("WHO") == 0)
{ {
_server->log("Handling WHO command", GREEN);
WhoHandler whoHandler(_server); WhoHandler whoHandler(_server);
whoHandler.handleWhoCommand(client, command); whoHandler.handleWhoCommand(client, command);
} }
else if (command.find("WHOIS") == 0) else if (upperCommand.find("WHOIS") == 0)
{ {
_server->log("Handling WHOIS command", GREEN);
WhoHandler whoHandler(_server); WhoHandler whoHandler(_server);
whoHandler.handleWhoisCommand(client, command); whoHandler.handleWhoisCommand(client, command);
} }
else if (command.find("INVITE") == 0) else if (upperCommand.find("INVITE") == 0)
{ {
_server->log("Handling INVITE command", GREEN);
InviteHandler inviteHandler(_server); InviteHandler inviteHandler(_server);
inviteHandler.handleInviteCommand(client, command); inviteHandler.handleInviteCommand(client, command);
} }
else if (command.find("TOPIC") == 0) else if (upperCommand.find("TOPIC") == 0)
{ {
_server->log("Handling TOPIC command", GREEN);
TopicHandler topichandler(_server); TopicHandler topichandler(_server);
topichandler.handleTopicCommand(client, command); topichandler.handleTopicCommand(client, command);
} }
else if (command.find("LIST") == 0) else if (upperCommand.find("LIST") == 0)
{ {
_server->log("Handling LIST command", GREEN);
broadcastChannelList(client, _server); broadcastChannelList(client, _server);
} }
else if (command.find("KICK") == 0) else if (upperCommand.find("KICK") == 0)
{ {
_server->log("Handling KICK command", GREEN);
KickHandler kickHandler(_server); KickHandler kickHandler(_server);
kickHandler.handleKickCommand(client, command); kickHandler.handleKickCommand(client, command);
} }
else else
{ {
_server->log("Unknown command received: " + command, RED);
_server->sendToClient(client->getFd(), ERR_UNKNOWNCOMMAND(client, command)); _server->sendToClient(client->getFd(), ERR_UNKNOWNCOMMAND(client, command));
_server->log("Message from client " + client->getNickname() + ": " + command, MAGENTA); _server->log("Sent unknown command error to client: " + client->getNickname(), MAGENTA);
} }
} }
@ -70,7 +95,6 @@ void AdditionalCommands::broadcastChannelList(Client *client, Server *server)
server->sendToClient(client->getFd(), RPL_LISTEND(client)); server->sendToClient(client->getFd(), RPL_LISTEND(client));
} }
// Fonction pour gérer la commande PART
void AdditionalCommands::handlePartCommand(Server *server, Client *client, const std::string &command) void AdditionalCommands::handlePartCommand(Server *server, Client *client, const std::string &command)
{ {
std::istringstream iss(command); std::istringstream iss(command);
@ -79,7 +103,7 @@ void AdditionalCommands::handlePartCommand(Server *server, Client *client, const
if (channelNames.empty()) if (channelNames.empty())
{ {
server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "PART")); server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "PART"));
return; return;
} }
@ -104,7 +128,7 @@ void AdditionalCommands::handlePartCommand(Server *server, Client *client, const
channel->removeClient(client); channel->removeClient(client);
std::ostringstream partMsg; std::ostringstream partMsg;
partMsg << ":" << client->getNickname() << " PART " << channelName << "\r\n"; partMsg << ":" << client->getNickname() << " PART " << channelName << " Leaving" << "\r\n";
server->sendToClient(client->getFd(), partMsg.str()); server->sendToClient(client->getFd(), partMsg.str());
channel->broadcast(partMsg.str(), client, server); channel->broadcast(partMsg.str(), client, server);
@ -126,31 +150,42 @@ void AdditionalCommands::handlePrivmsgCommand(Server *server, Client *client, co
iss >> cmd >> target; iss >> cmd >> target;
getline(iss, message); getline(iss, message);
// Remove the initial ':' in the message if present _server->log("Handling PRIVMSG command from: " + client->getNickname(), GREEN);
// Enlever le ':' initial dans le message si présent
if (!message.empty() && message[0] == ':') if (!message.empty() && message[0] == ':')
message = message.substr(1); message = message.substr(1);
_server->log("Target: " + target + ", Message: " + message, GREEN);
if (target.empty()) if (target.empty())
{ {
_server->log("No recipient specified for PRIVMSG", RED);
server->sendToClient(client->getFd(), ERR_NORECIPIENT(client, "PRIVMSG")); server->sendToClient(client->getFd(), ERR_NORECIPIENT(client, "PRIVMSG"));
return; return;
} }
if (message.empty()) if (message.empty())
{ {
_server->log("No text to send for PRIVMSG", RED);
server->sendToClient(client->getFd(), ERR_NOTEXTTOSEND(client)); server->sendToClient(client->getFd(), ERR_NOTEXTTOSEND(client));
return; return;
} }
// Récupérer les canaux du serveur
std::map<std::string, Channel *> &channels = server->getChannels(); std::map<std::string, Channel *> &channels = server->getChannels();
// Check if the target is a channel // Si la cible est un canal
if (channels.find(target) != channels.end()) if (channels.find(target) != channels.end())
{ {
Channel *channel = channels[target]; Channel *channel = channels[target];
_server->log("Target is a channel: " + target, GREEN);
// Vérifier les conditions spéciales du canal (ex: ban, modération)
if (channel->isBanned(client)) if (channel->isBanned(client))
{ {
_server->log("Client is banned from channel: " + target, RED);
server->sendToClient(client->getFd(), ERR_CANNOTSENDTOCHAN(client, target)); server->sendToClient(client->getFd(), ERR_CANNOTSENDTOCHAN(client, target));
return; return;
} }
@ -158,6 +193,7 @@ void AdditionalCommands::handlePrivmsgCommand(Server *server, Client *client, co
// Check the message with the bot filter before sending it // Check the message with the bot filter before sending it
if (!server->getBotFilter()->checkMessage(client, channel, message)) if (!server->getBotFilter()->checkMessage(client, channel, message))
{ {
_server->log("Message contains inappropriate content and was filtered", RED);
// If the message is inappropriate, the bot filter will handle warnings or kicks // If the message is inappropriate, the bot filter will handle warnings or kicks
return; return;
} }
@ -166,23 +202,28 @@ void AdditionalCommands::handlePrivmsgCommand(Server *server, Client *client, co
for (size_t i = 0; i < channelClients.size(); ++i) for (size_t i = 0; i < channelClients.size(); ++i)
{ {
// Envoyer le message à tous les clients du canal sauf l'émetteur
if (channelClients[i] != client) if (channelClients[i] != client)
{ {
std::stringstream privMsg; std::stringstream privMsg;
privMsg << ":" << client->getNickname() << " PRIVMSG " << target << " :" << message << "\r\n"; privMsg << ":" << client->getNickname() << " PRIVMSG " << target << " :" << message << "\r\n";
server->sendToClient(channelClients[i]->getFd(), privMsg.str()); server->sendToClient(channelClients[i]->getFd(), privMsg.str());
_server->log("Sent PRIVMSG to " + channelClients[i]->getNickname(), GREEN);
} }
} }
} }
// Si la cible est un utilisateur
else else
{ {
Client *targetClient = server->getClientByName(target); _server->log("Target is a user: " + target, GREEN);
Client *targetClient = server->getClientByName(target); // Utiliser getClientByName pour trouver le client par nom
if (targetClient) if (targetClient)
{ {
// Check the message with the bot filter before sending it
if (!server->getBotFilter()->checkMessage(client, NULL, message)) if (!server->getBotFilter()->checkMessage(client, NULL, message))
{ {
_server->log("Message contains inappropriate content and was filtered", RED);
// If the message is inappropriate, the bot filter will handle warnings or kicks // If the message is inappropriate, the bot filter will handle warnings or kicks
return; return;
} }
@ -190,15 +231,19 @@ void AdditionalCommands::handlePrivmsgCommand(Server *server, Client *client, co
std::stringstream privMsg; std::stringstream privMsg;
privMsg << ":" << client->getNickname() << " PRIVMSG " << target << " :" << message << "\r\n"; privMsg << ":" << client->getNickname() << " PRIVMSG " << target << " :" << message << "\r\n";
server->sendToClient(targetClient->getFd(), privMsg.str()); server->sendToClient(targetClient->getFd(), privMsg.str());
_server->log("Sent PRIVMSG to " + targetClient->getNickname(), GREEN);
// Check the message with the bot filter before sending it
if (targetClient->isAway()) if (targetClient->isAway())
{ {
server->sendToClient(client->getFd(), RPL_AWAY(client, targetClient->getNickname(), targetClient->getAwayMessage())); server->sendToClient(client->getFd(), RPL_AWAY(client, targetClient->getNickname(), targetClient->getAwayMessage()));
_server->log(targetClient->getNickname() + " is away, sent AWAY reply", GREEN);
} }
} }
else else
{ {
// If the target is neither a channel nor an existing user, send an error message _server->log("No such nick/channel: " + target, RED);
// Si la cible n'est ni un canal ni un utilisateur existant, envoyer un message d'erreur
server->sendToClient(client->getFd(), ERR_NOSUCHNICK(client, target)); server->sendToClient(client->getFd(), ERR_NOSUCHNICK(client, target));
} }
} }

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 11:45:43 by fgras-ca #+# #+# */ /* Created: 2024/06/06 11:45:43 by fgras-ca #+# #+# */
/* Updated: 2024/06/06 21:08:25 by fgras-ca ### ########.fr */ /* Updated: 2024/06/10 14:56:40 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,13 +27,19 @@ void BotFilter::loadBadWords(const std::string &fileName)
bool BotFilter::checkMessage(Client* client, Channel* channel, const std::string& message) bool BotFilter::checkMessage(Client* client, Channel* channel, const std::string& message)
{ {
if (containsBadWords(message)) if (containsBadWords(message))
{
if (channel != NULL)
{ {
warnClient(client, channel); warnClient(client, channel);
return false; // Message is not allowed
} }
return true; // Message is allowed else
{
warnClientDirect(client);
}
return false;
}
return true;
} }
bool BotFilter::containsBadWords(const std::string &message) bool BotFilter::containsBadWords(const std::string &message)
{ {
for (std::vector<std::string>::iterator it = _badWords.begin(); it != _badWords.end(); ++it) for (std::vector<std::string>::iterator it = _badWords.begin(); it != _badWords.end(); ++it)
@ -68,3 +74,20 @@ void BotFilter::kickClient(Client *client, Channel *channel)
_server->sendToClient(client->getFd(), BOTMESSAGE(client, channel->getName(), "You have been kicked for inappropriate language.\r\n")); _server->sendToClient(client->getFd(), BOTMESSAGE(client, channel->getName(), "You have been kicked for inappropriate language.\r\n"));
_server->disconnectClient(client->getFd()); _server->disconnectClient(client->getFd());
} }
void BotFilter::warnClientDirect(Client* client)
{
int fd = client->getFd();
_warnings[fd] += 1;
if (_warnings[fd] >= 3)
{
_server->sendToClient(fd, BOTMESSAGE(client, "", "You have been kicked from the server for inappropriate language.\r\n"));
_server->disconnectClient(fd);
}
else
{
std::ostringstream oss;
oss << "Warning: Inappropriate language detected. You have " << (3 - _warnings[fd]) << " warnings left before you are kicked.\r\n";
_server->sendToClient(fd, BOTMESSAGE(client, "", oss.str()));
}
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 18:26:34 by fgras-ca #+# #+# */ /* Created: 2024/05/15 18:26:34 by fgras-ca #+# #+# */
/* Updated: 2024/06/07 11:32:32 by fgras-ca ### ########.fr */ /* Updated: 2024/06/08 19:14:48 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -92,7 +92,7 @@ void CommandHandler::handleCapCommand(Client* client, const std::vector<std::str
{ {
if (tokens.size() < 2) if (tokens.size() < 2)
{ {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "CAP")); _server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "CAP"));
return; return;
} }
@ -111,7 +111,7 @@ void CommandHandler::handleCapCommand(Client* client, const std::vector<std::str
{ {
if (tokens.size() < 3) if (tokens.size() < 3)
{ {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "CAP")); _server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "CAP"));
return; return;
} }
std::string requestedCapabilities = tokens[2]; std::string requestedCapabilities = tokens[2];
@ -131,7 +131,7 @@ void CommandHandler::handlePassCommand(Client* client, const std::vector<std::st
{ {
if (tokens.size() < 2) if (tokens.size() < 2)
{ {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "PASS")); _server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "PASS"));
return; return;
} }
@ -215,7 +215,7 @@ void CommandHandler::handleUser(Client* client, const std::vector<std::string>&
{ {
if (tokens.size() < 5) if (tokens.size() < 5)
{ {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "USER")); _server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "USER"));
std::ostringstream oss; std::ostringstream oss;
oss << "Client " << client->getFd() << ": USER command failed - not enough parameters."; oss << "Client " << client->getFd() << ": USER command failed - not enough parameters.";
@ -264,7 +264,7 @@ void CommandHandler::handlePingCommand(Client* client, const std::vector<std::st
{ {
if (tokens.size() < 2) if (tokens.size() < 2)
{ {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "PING")); _server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "PING"));
return; return;
} }

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 13:02:09 by fgras-ca #+# #+# */ /* Created: 2024/05/30 13:02:09 by fgras-ca #+# #+# */
/* Updated: 2024/06/07 11:44:34 by fgras-ca ### ########.fr */ /* Updated: 2024/06/08 19:15:29 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,7 @@ void InviteHandler::handleInviteCommand(Client* client, const std::string& comma
if (nickname.empty() || channelName.empty()) if (nickname.empty() || channelName.empty())
{ {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "INVITE")); _server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "INVITE"));
return; return;
} }

View File

@ -23,7 +23,7 @@ void JoinHandler::handleJoinCommand(Client* client, const std::string& params, S
std::vector<std::string> parts = split(params, " "); std::vector<std::string> parts = split(params, " ");
if (parts.empty()) if (parts.empty())
{ {
server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "JOIN")); server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "JOIN"));
return; return;
} }
@ -40,7 +40,7 @@ void JoinHandler::handleJoinCommand(Client* client, const std::string& params, S
if (channelName.empty()) if (channelName.empty())
{ {
server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "JOIN")); server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "JOIN"));
return; return;
} }
@ -139,6 +139,7 @@ void JoinHandler::sendJoinSuccess(Client* client, Channel* channel, Server* serv
server->sendToClient(client->getFd(), usersList); server->sendToClient(client->getFd(), usersList);
} }
std::string JoinHandler::getUsersList(Channel* channel) std::string JoinHandler::getUsersList(Channel* channel)
{ {
std::vector<Client*> clients = channel->getClients(); std::vector<Client*> clients = channel->getClients();

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/01 17:00:31 by fgras-ca #+# #+# */ /* Created: 2024/06/01 17:00:31 by fgras-ca #+# #+# */
/* Updated: 2024/06/01 19:12:13 by fgras-ca ### ########.fr */ /* Updated: 2024/06/08 19:16:10 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ void KickHandler::handleKickCommand(Client* client, const std::string& command)
std::vector<std::string> tokens = split(command, " "); std::vector<std::string> tokens = split(command, " ");
if (tokens.size() < 3) if (tokens.size() < 3)
{ {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "KICK")); _server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "KICK"));
return; return;
} }

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 11:13:08 by fgras-ca #+# #+# */ /* Created: 2024/05/30 11:13:08 by fgras-ca #+# #+# */
/* Updated: 2024/06/07 14:10:23 by fgras-ca ### ########.fr */ /* Updated: 2024/06/08 22:12:20 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ void ModeHandler::handleModeCommand(Client* client, const std::string& command)
if (tokens.size() < 2) if (tokens.size() < 2)
{ {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "MODE")); _server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "MODE"));
_server->log("MODE command error: Need more parameters", RED); _server->log("MODE command error: Need more parameters", RED);
return; return;
} }
@ -139,7 +139,7 @@ void ModeHandler::setChannelMode(Client *client, Channel* channel, const std::st
} }
else if (mode == "t") else if (mode == "t")
{ {
applyModeT(channel, adding); applyModeT(client, channel, adding);
} }
else if (mode == "o") else if (mode == "o")
applyModeO(client, channel, adding, argument); applyModeO(client, channel, adding, argument);
@ -152,18 +152,19 @@ void ModeHandler::applyModeL(Client *client, Channel* channel, bool adding, cons
int limit = std::atoi(argument.c_str()); int limit = std::atoi(argument.c_str());
if (limit <= 0) if (limit <= 0)
{ {
_server->sendToClient(client->getFd(), ERR_INVALIDMODEPARAM(client, argument)); _server->sendToClient(client->getFd(), MODELNEEDPARAM("MODE +l"));
_server->log("Invalid limit for mode +l: " + argument, RED); _server->log("Invalid limit for mode +l: " + argument, RED);
return; return;
} }
std::ostringstream oss; std::ostringstream oss;
oss << limit; oss << limit;
_server->sendToClient(client->getFd(), MODELCHANGE(client, channel->getName(), argument)); _server->sendToClient(client->getFd(), MODEACCEPTMESSAGE(client, channel->getName(), "+l", " " + argument));
_server->log("Applying mode L: Setting limit to " + oss.str(), GREEN); _server->log("Applying mode L: Setting limit to " + oss.str(), GREEN);
channel->setClientLimit(limit); channel->setClientLimit(limit);
} }
else else
{ {
_server->sendToClient(client->getFd(), MODEACCEPTMESSAGE(client, channel->getName(), "-l", " " + argument));
_server->log("Applying mode L: Removing limit", RED); _server->log("Applying mode L: Removing limit", RED);
channel->setClientLimit(0); channel->setClientLimit(0);
} }
@ -178,7 +179,7 @@ void ModeHandler::applyModeI(Client *client, Channel *channel, bool adding)
isAlreadySet = channel->isInviteOnly() == adding; isAlreadySet = channel->isInviteOnly() == adding;
if (!isAlreadySet) if (!isAlreadySet)
{ {
_server->sendToClient(client->getFd(), MODEACCEPTMESSAGE(client, channel->getName(), modeChange)); _server->sendToClient(client->getFd(), MODEACCEPTMESSAGE(client, channel->getName(), modeChange, ""));
_server->log("Applying mode I: " + std::string(adding ? "Setting invite-only" : "Removing invite-only"), GREEN); _server->log("Applying mode I: " + std::string(adding ? "Setting invite-only" : "Removing invite-only"), GREEN);
channel->setInviteOnly(adding); channel->setInviteOnly(adding);
} }
@ -199,7 +200,7 @@ void ModeHandler::applyModeK(Client *client, Channel *channel, bool adding, cons
} }
if (argument.empty()) if (argument.empty())
{ {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "MODE +k")); _server->sendToClient(client->getFd(), MODELNEEDPARAM("MODE +k"));
_server->log("Mode +k error: No key provided", RED); _server->log("Mode +k error: No key provided", RED);
return; return;
} }
@ -209,7 +210,7 @@ void ModeHandler::applyModeK(Client *client, Channel *channel, bool adding, cons
_server->log("Invalid key for mode +k: contains spaces", RED); _server->log("Invalid key for mode +k: contains spaces", RED);
return; return;
} }
_server->sendToClient(client->getFd(), MODEACCEPTMESSAGE(client, channel->getName(), "+k " + argument)); _server->sendToClient(client->getFd(), MODEACCEPTMESSAGE(client, channel->getName(), "+k ", argument));
_server->log("Applying mode K: Setting key to " + argument, GREEN); _server->log("Applying mode K: Setting key to " + argument, GREEN);
channel->setKey(argument); channel->setKey(argument);
} }
@ -221,13 +222,13 @@ void ModeHandler::applyModeK(Client *client, Channel *channel, bool adding, cons
_server->log("Mode -k error: No key to remove", RED); _server->log("Mode -k error: No key to remove", RED);
return; return;
} }
_server->sendToClient(client->getFd(), MODEACCEPTMESSAGE(client, channel->getName(), "-k")); _server->sendToClient(client->getFd(), MODELNEEDPARAM("MODE +k "));
_server->log("Applying mode K: Removing key", RED); _server->log("Applying mode K: Removing key", RED);
channel->setKey(""); channel->setKey("");
} }
} }
void ModeHandler::applyModeT(Channel* channel, bool adding) void ModeHandler::applyModeT(Client *client, Channel* channel, bool adding)
{ {
std::string modeChange; std::string modeChange;
bool isAlreadySet; bool isAlreadySet;
@ -239,7 +240,7 @@ void ModeHandler::applyModeT(Channel* channel, bool adding)
_server->log("Applying mode T: " + std::string(adding ? "Setting topic protection" : "Removing topic protection"), GREEN); _server->log("Applying mode T: " + std::string(adding ? "Setting topic protection" : "Removing topic protection"), GREEN);
channel->setTopicProtection(adding); channel->setTopicProtection(adding);
// Inform the client and channel about the mode change // Inform the client and channel about the mode change
channel->broadcast(MODEACCEPTMESSAGE(NULL, channel->getName(), modeChange), NULL, _server); channel->broadcast(MODEACCEPTMESSAGE(client, channel->getName(), modeChange, ""), NULL, _server);
} }
} }

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 17:04:58 by fgras-ca #+# #+# */ /* Created: 2024/05/30 17:04:58 by fgras-ca #+# #+# */
/* Updated: 2024/06/01 19:17:25 by fgras-ca ### ########.fr */ /* Updated: 2024/06/08 19:17:37 by fgras-ca ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ void TopicHandler::handleTopicCommand(Client* client, const std::string& command
if (tokens.size() < 2) if (tokens.size() < 2)
{ {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "TOPIC")); _server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, NULL, "TOPIC"));
_server->log("TOPIC command error: Need more parameters", RED); _server->log("TOPIC command error: Need more parameters", RED);
return; return;
} }