modif3005

This commit is contained in:
Ladebeze66
2024-05-30 13:55:15 +02:00
parent 8d7b06ec25
commit 7f71877981
13 changed files with 392 additions and 14 deletions

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/05/29 13:41:49 by fgras-ca ### ########.fr */
/* Updated: 2024/05/30 13:07:33 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,6 +35,11 @@ void AdditionalCommands::processCommand(Client *client, const std::string &comma
WhoHandler whoHandler(_server);
whoHandler.handleWhoisCommand(client, command);
}
else if (command.find("INVITE") == 0)
{
InviteHandler inviteHandler(_server);
inviteHandler.handleInviteCommand(client, command);
}
else if (command.find("LIST") == 0)
{
broadcastChannelList(client, _server);

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:42:57 by fgras-ca #+# #+# */
/* Updated: 2024/05/28 13:06:02 by fgras-ca ### ########.fr */
/* Updated: 2024/05/30 13:09:45 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -116,4 +116,35 @@ void Channel::setTopic(const std::string &topic, const std::string &setter)
_topicTime = std::time(NULL);
}
void Channel::setClientLimit(size_t limit) {
_clientLimit = limit;
}
size_t Channel::getClientLimit() const {
return _clientLimit;
}
void Channel::setInviteOnly(bool inviteOnly) {
_inviteOnly = inviteOnly;
}
void Channel::setKey(const std::string &key) {
_key = key;
}
void Channel::setTopicProtection(bool protection) {
_topicProtection = protection;
}
void Channel::addInvitedClient(Client* client) {
_invitedClients.insert(client);
}
std::string Channel::getModes() const {
std::string modes;
if (_inviteOnly) modes += 'i';
if (!_key.empty()) modes += 'k';
if (_clientLimit > 0) modes += 'l';
if (_topicProtection) modes += 't';
return modes;
}

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/05/29 13:41:25 by fgras-ca ### ########.fr */
/* Updated: 2024/05/30 12:39:58 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "CommandHandler.hpp"
CommandHandler::CommandHandler(Server *server)
: _server(server), _additionalCommands(new AdditionalCommands(server))
: _server(server), _additionalCommands(new AdditionalCommands(server)), _modeHandler(new ModeHandler(server))
{
// Ensure that _server is not null
if (!_server)
@@ -51,6 +51,10 @@ void CommandHandler::handleCommand(Client* client, const std::string& command)
handleErrorCommand(client, tokens[1]);
}
}
else if (commandType == "MODE")
{
_modeHandler->handleModeCommand(client, command);
}
else if (commandType == "JOIN") {
JoinHandler joinHandler;
joinHandler.handleJoinCommand(client, tokens[1], _server);

View File

@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* InviteHandler.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 13:02:09 by fgras-ca #+# #+# */
/* Updated: 2024/05/30 13:08:33 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "InviteHandler.hpp"
InviteHandler::InviteHandler(Server* server) : _server(server) {}
void InviteHandler::handleInviteCommand(Client* client, const std::string& command) {
std::istringstream iss(command);
std::string cmd, nickname, channelName;
iss >> cmd >> nickname >> channelName;
if (nickname.empty() || channelName.empty()) {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "INVITE"));
return;
}
Channel* channel = _server->getChannelByName(channelName);
if (!channel) {
_server->sendToClient(client->getFd(), ERR_NOSUCHCHANNEL(client->getFd(), channelName));
return;
}
if (!channel->hasClient(client)) {
_server->sendToClient(client->getFd(), ERR_NOTONCHANNEL(client->getFd(), channelName));
return;
}
if (!channel->isOperator(client) && channel->isInviteOnly()) {
_server->sendToClient(client->getFd(), ERR_CHANOPRIVSNEEDED(client->getFd(), channelName));
return;
}
Client* targetClient = _server->getClientByName(nickname);
if (!targetClient) {
_server->sendToClient(client->getFd(), ERR_NOSUCHNICK(client->getFd(), nickname));
return;
}
if (channel->hasClient(targetClient)) {
_server->sendToClient(client->getFd(), ERR_USERONCHANNEL(client, nickname, channelName));
return;
}
channel->addInvitedClient(targetClient);
_server->sendToClient(client->getFd(), RPL_INVITING(client, nickname, channelName));
std::ostringstream inviteMsg;
inviteMsg << ":" << client->getNickname() << " INVITE " << nickname << " " << channelName << "\r\n";
_server->sendToClient(targetClient->getFd(), inviteMsg.str());
}

154
ft_irc3/src/ModeHandler.cpp Normal file
View File

@@ -0,0 +1,154 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ModeHandler.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 11:13:08 by fgras-ca #+# #+# */
/* Updated: 2024/05/30 13:33:36 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "ModeHandler.hpp"
ModeHandler::ModeHandler(Server* server) : _server(server) {}
void ModeHandler::handleModeCommand(Client* client, const std::string& command) {
_server->log("Received MODE command: " + command, "\033[1;34m");
std::vector<std::string> tokens = split(command, " \n\r\t");
if (tokens.size() < 2) {
_server->sendToClient(client->getFd(), ERR_NEEDMOREPARAMS(client, "MODE"));
_server->log("MODE command error: Need more parameters", "\033[1;31m");
return;
}
std::string target = tokens[1];
if (target[0] == '#' || target[0] == '&') {
_server->log("Handling channel mode for target: " + target, "\033[1;34m");
handleChannelMode(client, tokens);
} else {
_server->log("Handling user mode for target: " + target, "\033[1;34m");
handleUserMode(client, tokens);
}
}
void ModeHandler::handleUserMode(Client* client, const std::vector<std::string>& tokens) {
std::string target = tokens[1];
if (target != client->getNickname()) {
_server->sendToClient(client->getFd(), ERR_USERSDONTMATCH(client->getFd()));
_server->log("User mode error: Users don't match", "\033[1;31m");
return;
}
if (tokens.size() == 2) {
_server->sendToClient(client->getFd(), RPL_UMODEIS(client->getFd(), "")); // Ajout de la chaîne vide pour les modes utilisateur
_server->log("Sent user modes to client", "\033[1;32m");
} else {
std::string modeString = tokens[2];
// Gestion des modes utilisateur (ajout ou suppression)
// Par exemple, vous pouvez gérer l'ajout ou la suppression du mode invisible ici
_server->log("Updating user modes: " + modeString, "\033[1;34m");
}
}
void ModeHandler::handleChannelMode(Client* client, const std::vector<std::string>& tokens) {
std::string channelName = tokens[1];
Channel* channel = _server->getChannelByName(channelName);
if (!channel) {
_server->sendToClient(client->getFd(), ERR_NOSUCHCHANNEL(client->getFd(), channelName));
_server->log("Channel mode error: No such channel " + channelName, "\033[1;31m");
return;
}
if (tokens.size() == 2) {
// Retourner les modes actuels du canal
std::ostringstream oss;
oss << RPL_CHANNELMODEIS(client->getFd(), channel->getName(), channel->getModes());
oss << RPL_CREATIONTIME(client->getFd(), channel->getName(), channel->getTopicTime());
_server->sendToClient(client->getFd(), oss.str());
_server->log("Sent channel modes to client", "\033[1;32m");
return;
}
if (!channel->isOperator(client)) {
_server->sendToClient(client->getFd(), ERR_CHANOPRIVSNEEDED(client->getFd(), channelName));
_server->log("Channel mode error: Channel operator privileges needed for " + channelName, "\033[1;31m");
return;
}
std::string modeString = tokens[2];
bool adding = true;
size_t argIndex = 3;
for (size_t i = 0; i < modeString.length(); ++i) {
char mode = modeString[i];
if (mode == '+') {
adding = true;
} else if (mode == '-') {
adding = false;
} else if (mode == 'b' || mode == 'l' || mode == 'i' || mode == 'k' || mode == 't') {
std::string argument;
if (argIndex < tokens.size()) {
argument = tokens[argIndex++];
}
setChannelMode(channel, std::string(1, mode), adding, argument);
} else {
_server->sendToClient(client->getFd(), ERR_UNKNOWNMODE(client, mode, channelName));
}
}
}
void ModeHandler::setChannelMode(Channel* channel, const std::string& mode, bool adding, const std::string& argument) {
/*if (mode == "b") {
applyModeB(channel, adding, argument);
}*/
if (mode == "l") {
applyModeL(channel, adding, argument);
} else if (mode == "i") {
applyModeI(channel, adding);
} else if (mode == "k") {
applyModeK(channel, adding, argument);
} else if (mode == "t") {
applyModeT(channel, adding);
}
}
/*void ModeHandler::applyModeB(Channel* channel, bool adding, const std::string& argument) {
// Implémentation de l'ajout ou de la suppression de bannissements
// Note : Vous pouvez ajouter des méthodes à la classe Channel pour gérer la liste des bannissements
if (adding) {
// Ajouter le masque à la liste des bannissements
// channel->banClient(argument);
} else {
// Supprimer le masque de la liste des bannissements
// channel->unbanClient(argument);
}
}*/
void ModeHandler::applyModeL(Channel* channel, bool adding, const std::string& argument) {
if (adding) {
int limit = std::atoi(argument.c_str());
channel->setClientLimit(limit);
} else {
channel->setClientLimit(0); // Retirer la limite
}
}
void ModeHandler::applyModeI(Channel* channel, bool adding) {
channel->setInviteOnly(adding);
}
void ModeHandler::applyModeK(Channel* channel, bool adding, const std::string& argument) {
if (adding) {
channel->setKey(argument);
} else {
channel->setKey("");
}
}
void ModeHandler::applyModeT(Channel* channel, bool adding) {
channel->setTopicProtection(adding);
}

View File

@@ -6,14 +6,14 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 12:17:12 by fgras-ca #+# #+# */
/* Updated: 2024/05/21 20:31:51 by fgras-ca ### ########.fr */
/* Updated: 2024/05/30 12:18:40 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Server.hpp"
Server::Server(int port, const std::string &password)
: _port(port), _password(password), _clientManager(new ClientManager(this)), _commandHandler(new CommandHandler(this))
: _port(port), _password(password), _clientManager(new ClientManager(this)), _commandHandler(new CommandHandler(this)), _modeHandler(new ModeHandler(this))
{
initServer();
}
@@ -208,6 +208,14 @@ Client* Server::getClientByName(const std::string &name)
return NULL; // Remplacez nullptr par NULL
}
Channel* Server::getChannelByName(const std::string &name) {
std::map<std::string, Channel *>::iterator it = _channels.find(name);
if (it != _channels.end()) {
return it->second;
}
return NULL;
}
void Server::sendChannelListToClient(Client *client)
{
std::map<std::string, Channel *> &channels = getChannels();