This commit is contained in:
Ladebeze66 2024-06-07 15:32:58 +02:00
parent 3386ad9772
commit a604e93070
7 changed files with 49 additions and 28 deletions

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/04 14:00:47 by fgras-ca ### ########.fr */
/* Updated: 2024/06/07 12:24:49 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -47,6 +47,7 @@ class ModeHandler
void applyModeK(Client *client, Channel* channel, bool adding, const std::string& argument);
void applyModeT(Channel* channel, bool adding);
void applyModeO(Client *client, Channel* channel, bool adding, const std::string& argument);
bool needsArgument(char mode);
};
#endif

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:12:47 by fgras-ca #+# #+# */
/* Updated: 2024/06/06 19:36:05 by fgras-ca ### ########.fr */
/* Updated: 2024/06/07 14:00:45 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -106,6 +106,13 @@ inline std::string RPL_WHOISSERVER(Client *client, const std::string& targetNick
return oss.str();
}
inline std::string RPL_WHOISOPERATOR(Client* client, const std::string& nick)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 313 " << CLIENT_NICK(client) << " " << nick << " :is an IRC operator\r\n";
return oss.str();
}
inline std::string RPL_ENDOFWHO(Client *client, const std::string& channel)
{
std::ostringstream oss;
@ -183,10 +190,10 @@ inline std::string RPL_ENDOFINVITELIST(Client *client, const std::string& channe
return oss.str();
}
inline std::string RPL_INVITING(Client* client, const std::string& channel)
inline std::string RPL_INVITING(Client* client, Client* clientTarget, const std::string& channel)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 341 " << CLIENT_NICK(client) << " " << CLIENT_NICK(client) << " " << channel << "\r\n";
oss << ":" << SERVER_NAME << " 341 " << CLIENT_NICK(client) << " " << CLIENT_NICK(clientTarget) << " " << channel << "\r\n";
return oss.str();
}
@ -215,7 +222,7 @@ inline std::string RPL_WHOREPLY(const std::string& channel, Client* target)
{
std::ostringstream oss;
oss << ":" << SERVER_NAME << " 352 " << CLIENT_NICK(target) << " " << channel << " "
<< CLIENT_USER(target) << " " << CLIENT_HOST(target) << " " << SERVER_NAME << " "
<< CLIENT_USER(target) << " " << CLIENT_HOST(target) << SERVER_NAME << " "
<< CLIENT_NICK(target) << " H :0 " << CLIENT_REALNAME(target) << "\r\n";
return oss.str();
}
@ -548,4 +555,11 @@ inline std::string BOTMESSAGE(Client *client, const std::string &channel, const
return oss.str();
}
inline std::string MODELCHANGE(Client *client, std::string channel, const std::string& mode)
{
std::ostringstream oss;
oss << SERVER_NAME <<":" << client->getNickname() << " MODE " << channel << " " << mode << " :" << "\r\n";
return oss.str();
}
#endif

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/06 18:41:08 by fgras-ca ### ########.fr */
/* Updated: 2024/06/07 11:34:13 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -107,6 +107,8 @@ void AdditionalCommands::handlePartCommand(Server *server, Client *client, const
partMsg << ":" << client->getNickname() << " PART " << channelName << "\r\n";
server->sendToClient(client->getFd(), partMsg.str());
channel->broadcast(partMsg.str(), client, server);
if (channel->isEmpty())
{
delete channel;

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 18:26:34 by fgras-ca #+# #+# */
/* Updated: 2024/06/05 09:57:55 by fgras-ca ### ########.fr */
/* Updated: 2024/06/07 11:32:32 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -268,12 +268,10 @@ void CommandHandler::handlePingCommand(Client* client, const std::vector<std::st
return;
}
std::string serverName = tokens[1];
std::string pongMessage = ":" + serverName + " PONG " + serverName + "\r\n";
_server->sendToClient(client->getFd(), pongMessage);
std::string latence = tokens[1];
_server->sendToClient(client->getFd(), RPL_PONG(latence));
}
void CommandHandler::handleQuitCommand(Client* client, const std::vector<std::string>& tokens)
{
std::string reason = "Quit: ";

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/01 19:11:22 by fgras-ca ### ########.fr */
/* Updated: 2024/06/07 11:44:34 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,7 +39,7 @@ void InviteHandler::handleInviteCommand(Client* client, const std::string& comma
return;
}
if (!channel->isOperator(client) && channel->isInviteOnly())
if (!channel->isOperator(client))
{
_server->sendToClient(client->getFd(), ERR_CHANOPRIVSNEEDED(client, channelName));
return;
@ -59,7 +59,7 @@ void InviteHandler::handleInviteCommand(Client* client, const std::string& comma
}
channel->addInvitedClient(targetClient);
_server->sendToClient(client->getFd(), RPL_INVITING(client, channel->getName()));
_server->sendToClient(client->getFd(), RPL_INVITING(client, targetClient, channel->getName()));
std::ostringstream inviteMsg;
inviteMsg << ":" << client->getNickname() << " INVITE " << nickname << " " << channelName << "\r\n";

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/04 16:12:50 by fgras-ca ### ########.fr */
/* Updated: 2024/06/07 14:10:23 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -104,24 +104,21 @@ void ModeHandler::handleChannelMode(Client* client, const std::vector<std::strin
{
adding = true;
_server->log("Adding mode: " + std::string(1, mode), GREEN);
} else if (mode == '-')
}
else if (mode == '-')
{
adding = false;
_server->log("Removing mode: " + std::string(1, mode), RED);
} else if (mode == 'b' || mode == 'l' || mode == 'i' || mode == 'k' || mode == 't' || mode == 'o')
}
else
{
std::string argument;
if (argIndex < tokens.size())
if (needsArgument(mode) && argIndex < tokens.size())
{
argument = tokens[argIndex++];
}
setChannelMode(client, channel, std::string(1, mode), adding, argument);
}
else
{
_server->sendToClient(client->getFd(), ERR_UNKNOWNMODE(client, mode, channelName));
_server->log("Unknown mode: " + std::string(1, mode), RED);
}
}
}
@ -161,6 +158,7 @@ void ModeHandler::applyModeL(Client *client, Channel* channel, bool adding, cons
}
std::ostringstream oss;
oss << limit;
_server->sendToClient(client->getFd(), MODELCHANGE(client, channel->getName(), argument));
_server->log("Applying mode L: Setting limit to " + oss.str(), GREEN);
channel->setClientLimit(limit);
}
@ -231,15 +229,17 @@ void ModeHandler::applyModeK(Client *client, Channel *channel, bool adding, cons
void ModeHandler::applyModeT(Channel* channel, bool adding)
{
std::string modeChange;
bool isAlreadySet;
std::string modeChange;
bool isAlreadySet;
modeChange = adding ? "+t" : "-t";
isAlreadySet = channel->isInviteOnly() == adding;
isAlreadySet = channel->getTopicProtection() == adding;
if (!isAlreadySet)
{
_server->log("Applying mode T: " + std::string(adding ? "Setting topic protection" : "Removing topic protection"), GREEN);
channel->setTopicProtection(adding);
// Inform the client and channel about the mode change
channel->broadcast(MODEACCEPTMESSAGE(NULL, channel->getName(), modeChange), NULL, _server);
}
}
@ -274,4 +274,9 @@ void ModeHandler::applyModeO(Client* client, Channel* channel, bool adding, cons
std::string message = ":" + client->getNickname() + " MODE " + channel->getName() + " -o " + targetClient->getNickname() + "\r\n";
channel->broadcast(message, NULL, _server);
}
}
}
bool ModeHandler::needsArgument(char mode)
{
return mode == 'k' || mode == 'l' || mode == 'o';
}

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/05/30 16:56:03 by fgras-ca ### ########.fr */
/* Updated: 2024/06/07 13:43:54 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -68,4 +68,5 @@ void WhoHandler::handleWhoisCommand(Client *client, const std::string &command)
_server->sendToClient(client->getFd(), RPL_WHOISUSER(client, targetClient));
_server->sendToClient(client->getFd(), RPL_WHOISSERVER(client, target, "IRC server info"));
_server->sendToClient(client->getFd(), RPL_ENDOFWHOIS(client, target));
_server->sendToClient(client->getFd(), RPL_WHOISOPERATOR(client, client->getNickname()));
}