From b90087188f3779b863c0929b0a31f17350a519d8 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Tue, 30 Jan 2024 17:35:50 +0100 Subject: [PATCH] je suis aigri --- includes/channel.hpp | 4 +- includes/config.hpp | 3 +- srcs/channel.cpp | 117 ++++++++++++++++++++------------------ srcs/main.cpp | 2 +- srcs/server_functions.cpp | 10 ++-- 5 files changed, 73 insertions(+), 63 deletions(-) diff --git a/includes/channel.hpp b/includes/channel.hpp index ab73e15..33d36bf 100644 --- a/includes/channel.hpp +++ b/includes/channel.hpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:34:25 by maldavid #+# #+# */ -/* Updated: 2024/01/29 20:04:19 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 17:23:28 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -42,7 +42,7 @@ namespace irc inline std::size_t getNumberOfClients() const { return _clients.size(); } inline int getChannelSize() const { return _channel_size; } - void ModOperator(unstd::SharedPtr client, const std::string &clientname, bool mode); + void modOperator(unstd::SharedPtr client, const std::string &clientname, bool mode); inline bool removeOperator(unstd::SharedPtr op) { return _operators.erase(op); } void changeMode(unstd::SharedPtr client, const Message& msg); bool isOp(unstd::SharedPtr client) const; diff --git a/includes/config.hpp b/includes/config.hpp index 35c4df1..8079e43 100644 --- a/includes/config.hpp +++ b/includes/config.hpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/22 09:45:10 by maldavid #+# #+# */ -/* Updated: 2024/01/30 01:37:03 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 17:34:11 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -18,4 +18,5 @@ #define MAX_USERS 20 #define NULL_SOCKET -1 #define LEGAL_CHARACTER "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789{}[]\\|^`-_" + #endif diff --git a/srcs/channel.cpp b/srcs/channel.cpp index 81f0b80..7cc9822 100644 --- a/srcs/channel.cpp +++ b/srcs/channel.cpp @@ -6,12 +6,13 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */ -/* Updated: 2024/01/29 23:47:14 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 17:33:40 by maldavid ### ########.fr */ /* */ /******************************************************************************/ #include #include +#include #include #include #include @@ -37,38 +38,37 @@ namespace irc sendWho(client); } - void Channel::ModOperator(unstd::SharedPtr client, const std::string &clientname, bool mode) + void Channel::modOperator(unstd::SharedPtr client, const std::string& clientname, bool mode) { client_it it; logs::report(log_message, "mode operator, mode = %d", mode); - if (mode) + if(mode) { - for (it = _clients.begin(); it != _clients.end(); ++it) + for(it = _clients.begin(); it != _clients.end(); ++it) { - if (const_cast&>(*it)->getNickName() == clientname) + if(const_cast&>(*it)->getNickName() == clientname) { _operators.insert(const_cast&>(*it)); - break ; - } - } - if (it == _clients.end()) - client->sendCode(ERR_USERNOTINCHANNEL, "User not in channel"); - } - else - { - for (it = _operators.begin(); it != _operators.end(); ++it) - { - logs::report(log_message, "nickname %s, clientname : %s", const_cast&>(*it)->getNickName().c_str(), clientname.c_str()); - if (const_cast&>(*it)->getNickName() == clientname) - { - logs::report(log_message, "found %s to erase", const_cast&>(*it)->getNickName().c_str()); - _operators.erase(it); break; } } - if (it == _operators.end()) + if(it == _clients.end()) client->sendCode(ERR_USERNOTINCHANNEL, "User not in channel"); + return; } + + for(it = _operators.begin(); it != _operators.end(); ++it) + { + logs::report(log_message, "nickname %s, clientname : %s", const_cast&>(*it)->getNickName().c_str(), clientname.c_str()); + if(const_cast&>(*it)->getNickName() == clientname) + { + logs::report(log_message, "found %s to erase", const_cast&>(*it)->getNickName().c_str()); + _operators.erase(it); + break; + } + } + if(it == _operators.end()) + client->sendCode(ERR_USERNOTINCHANNEL, "User not in channel"); } bool Channel::removeClient(unstd::SharedPtr client) @@ -116,7 +116,7 @@ namespace irc std::string modes; std::string out = "MODE "; - for (std::vector::iterator it = mode.begin() + 1; it != mode.end(); ++it) + for(std::vector::iterator it = mode.begin() + 1; it != mode.end(); ++it) { modes += *it; modes += " "; @@ -125,67 +125,76 @@ namespace irc for(client_it it = _clients.begin(); it != _clients.end(); ++it) const_cast&>(*it)->sendMsg(client->getNickName(), out, modes); } + void Channel::showModes(unstd::SharedPtr client) { std::string modes = " +"; - if (_invite_only) + if(_invite_only) modes += 'i'; - if (_topic_op_restrict) + if(_topic_op_restrict) modes += 't'; - if (_password.size() > 0) + if(_password.size() > 0) modes += 'k'; - if (_channel_size != -1) + if(_channel_size != -1) modes += 'l'; - if (modes.size() <= 2) + if(modes.size() <= 2) return ; client->sendCode(RPL_CHANNELMODEIS, modes); } + void Channel::changeMode(unstd::SharedPtr client, const Message& msg) { bool modevalue = (msg.getTokens()[2][0] != '-'); - // attention on est sur un truc solidement MERDIQUE, a toucher a tes propres risques (je suis proche du nervous breakdown) gl hf :) logs::report(log_message, "tokensize : %d, mode : %c, modevalue %d", msg.getTokens().size(), msg.getTokens()[2][1], modevalue); - switch (msg.getTokens()[2][1]) + switch(msg.getTokens()[2][1]) { - case 'i': - _invite_only = modevalue; - break; - case 't': - _topic_op_restrict = modevalue; - break; + case 'i': _invite_only = modevalue; break; + case 't': _topic_op_restrict = modevalue; break; case 'k': - if (modevalue && msg.getTokens().size() == 4) + { + if(modevalue && msg.getTokens().size() == 4) { - logs::report(log_message, "%s password set as %s", getName().c_str(), msg.getTokens()[3].c_str()); - _password = msg.getTokens()[3]; + logs::report(log_message, "%s password set as %s", _name.c_str(), msg.getArgs()[2].c_str()); + _password = msg.getArgs()[2]; } - else if (msg.getTokens().size() < 4) + else if(msg.getTokens().size() < 4) { - _password = ""; - logs::report(log_message, "password removed on %s", getName().c_str()); + _password.clear(); + logs::report(log_message, "password removed on %s", _name.c_str()); } break; + } case 'o': - if (isOp(client)) - ModOperator(client, msg.getTokens()[3], modevalue); - else if (!isOp(client)) + { + if(isOp(client)) + modOperator(client, msg.getTokens()[3], modevalue); + else if(!isOp(client)) client->sendCode(ERR_CHANOPRIVSNEEDED, "You need to be operator to execute this command"); break; + } case 'l': - if (msg.getTokens().size() < 3 && modevalue) - return ; - if (!modevalue) + { + if(msg.getTokens().size() < 3 && modevalue) + return; + if(!modevalue) { _channel_size = -1; - break ; + break; } - if (modevalue) - _channel_size = std::atoi(msg.getTokens()[3].c_str()); - break ; - default : - client->sendCode(":yipirc " ERR_UNKNOWNMODE " @", getName().c_str(), "Unknown mode"); - return ; + if(modevalue) + { + char* end; + long tmp = std::strtol(msg.getArgs()[2].c_str(), &end, 10); + if(errno == ERANGE || *end != 0 || tmp < 0) + logs::report(log_error, "invalid channel size"); + else + _channel_size = tmp; + } + break; + } + + default : client->sendCode(":yipirc " ERR_UNKNOWNMODE " @", getName().c_str(), "Unknown mode"); return; } logs::report(log_message, "%s MODES : i:%d t:%d k:%s l:%d", getName().c_str(), _invite_only, _topic_op_restrict, _password.c_str(), _channel_size); showModesModify(client, msg); diff --git a/srcs/main.cpp b/srcs/main.cpp index ed83f12..712d4ca 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 09:27:04 by maldavid #+# #+# */ -/* Updated: 2024/01/30 17:16:04 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 17:35:22 by maldavid ### ########.fr */ /* */ /******************************************************************************/ diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index 3df0bc3..1071c7d 100644 --- a/srcs/server_functions.cpp +++ b/srcs/server_functions.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */ -/* Updated: 2024/01/30 17:15:46 by vvaas ### ########.fr */ +/* Updated: 2024/01/30 17:35:15 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -60,7 +60,7 @@ namespace irc std::string oldNick = (client->getNickName().size() > 0) ? client->getNickName() : msg.getTokens()[1]; // get nickname before /nick execution inside server (oldNick) client->printUserHeader(); client->setNewNickName(msg.getTokens()[1]); - for (client_it it = _client.begin(); it != _client.end(); ++it) + for(client_it it = _client.begin(); it != _client.end(); ++it) (*it)->sendMsg(oldNick, "NICK", msg.getTokens()[1]); client->welcome(); std::cout << "new nickname, " << client->getNickName() << std::endl; @@ -74,12 +74,12 @@ namespace irc logs::report(log_error, "USER, invalid command '%s'", msg.getRawMsg().c_str()); return; } - if (client->isRegistered()) + if(client->isRegistered()) { client->sendCode(ERR_ALREADYREGISTRED, "You are already registered"); return ; } - if (msg.getTokens()[4][0] != ':') + if(msg.getTokens()[4][0] != ':') return ; client->printUserHeader(); client->setNewUserName(msg.getTokens()[1]); @@ -88,7 +88,7 @@ namespace irc client->printUserHeader(); std::string realname; - for (std::vector::const_iterator it = msg.getTokens().begin() + 4; it != msg.getTokens().end(); ++it) + for(std::vector::const_iterator it = msg.getTokens().begin() + 4; it != msg.getTokens().end(); ++it) { realname.append(*it); realname.append(" ");