From bc8410a21dbf5ce3870ed224abb56c57d16146bd Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 5 Jun 2025 22:56:28 +0200 Subject: [PATCH] fix(core/error_code): now commands use the correct error code --- Makefile | 2 +- include/commands.hpp | 5 +++-- include/commands/invite.hpp | 2 +- include/commands/join.hpp | 2 +- include/commands/nick.hpp | 2 +- include/commands/notice.hpp | 2 +- include/commands/part.hpp | 4 ++-- include/commands/pass.hpp | 2 +- include/commands/ping.hpp | 2 +- include/commands/privmsg.hpp | 2 +- include/core/core.hpp | 8 +++++--- sources/commands/invite.cpp | 22 +++++++++++----------- sources/commands/join.cpp | 18 +++++++++--------- sources/commands/nick.cpp | 14 +++++++------- sources/commands/notice.cpp | 16 ++++++++-------- sources/commands/part.cpp | 17 +++++++++-------- sources/commands/pass.cpp | 12 ++++++------ sources/commands/ping.cpp | 10 +++++----- sources/commands/privmsg.cpp | 16 ++++++++-------- 19 files changed, 81 insertions(+), 77 deletions(-) diff --git a/Makefile b/Makefile index afc5b7e..386c3a3 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: omoudni +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/05/02 15:40:00 by rparodi #+# #+# # -# Updated: 2025/06/03 16:50:01 by rparodi ### ########.fr # +# Updated: 2025/06/05 22:54:26 by rparodi ### ########.fr # # # #******************************************************************************# diff --git a/include/commands.hpp b/include/commands.hpp index 47bc160..a133468 100644 --- a/include/commands.hpp +++ b/include/commands.hpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 23:31:58 by rparodi #+# #+# */ -/* Updated: 2025/05/29 12:47:57 by rparodi ### ########.fr */ +/* Updated: 2025/06/05 22:55:54 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #include "user.hpp" #include "channel.hpp" #include "server.hpp" +#include "core.hpp" #include "logs.hpp" namespace cmd @@ -39,7 +40,7 @@ namespace cmd std::vector _args; public: virtual void execute() = 0; - virtual bool checkArgs() = 0; + virtual e_code checkArgs() = 0; ~ACommand(); ACommand(User *user, Channel *channel, Server *server, const std::string &line); }; diff --git a/include/commands/invite.hpp b/include/commands/invite.hpp index be4ece6..8192dab 100644 --- a/include/commands/invite.hpp +++ b/include/commands/invite.hpp @@ -18,5 +18,5 @@ class cmd::Invite : public ACommand { public: Invite(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} virtual void execute(void); - virtual bool checkArgs(); + virtual e_code checkArgs(); }; diff --git a/include/commands/join.hpp b/include/commands/join.hpp index ce8d1f5..c5ab577 100644 --- a/include/commands/join.hpp +++ b/include/commands/join.hpp @@ -18,5 +18,5 @@ class cmd::Join : public ACommand { public: Join(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} virtual void execute(void); - virtual bool checkArgs(); + virtual e_code checkArgs(); }; diff --git a/include/commands/nick.hpp b/include/commands/nick.hpp index fdc88ac..5d2433a 100644 --- a/include/commands/nick.hpp +++ b/include/commands/nick.hpp @@ -18,5 +18,5 @@ class cmd::Nick : public ACommand { public: Nick(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} virtual void execute(void); - virtual bool checkArgs(); + virtual e_code checkArgs(); }; diff --git a/include/commands/notice.hpp b/include/commands/notice.hpp index 256eb28..8e7b669 100644 --- a/include/commands/notice.hpp +++ b/include/commands/notice.hpp @@ -18,5 +18,5 @@ class cmd::Notice : public ACommand { public: Notice(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} virtual void execute(void); - virtual bool checkArgs(); + virtual e_code checkArgs(); }; diff --git a/include/commands/part.hpp b/include/commands/part.hpp index 4a2bba5..898603d 100644 --- a/include/commands/part.hpp +++ b/include/commands/part.hpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:17:31 by rparodi #+# #+# */ -/* Updated: 2025/06/02 00:51:23 by rparodi ### ########.fr */ +/* Updated: 2025/06/04 23:49:09 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,5 +18,5 @@ class cmd::Part : public ACommand { public: Part(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} virtual void execute(void); - virtual bool checkArgs(); + virtual e_code checkArgs(); }; diff --git a/include/commands/pass.hpp b/include/commands/pass.hpp index bb289fe..8965c9a 100644 --- a/include/commands/pass.hpp +++ b/include/commands/pass.hpp @@ -19,6 +19,6 @@ namespace cmd { public: Pass(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} virtual void execute(void); - virtual bool checkArgs(); + virtual e_code checkArgs(); }; } diff --git a/include/commands/ping.hpp b/include/commands/ping.hpp index e5734c9..4e8b0d2 100644 --- a/include/commands/ping.hpp +++ b/include/commands/ping.hpp @@ -18,5 +18,5 @@ class cmd::Ping : public ACommand { public: Ping(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} virtual void execute(void); - virtual bool checkArgs(); + virtual e_code checkArgs(); }; diff --git a/include/commands/privmsg.hpp b/include/commands/privmsg.hpp index 6c012d2..a55c84d 100644 --- a/include/commands/privmsg.hpp +++ b/include/commands/privmsg.hpp @@ -18,5 +18,5 @@ class cmd::PrivMsg : public ACommand { public: PrivMsg(User *user, Channel *channel, Server *server, const std::string &line) : ACommand(user, channel, server, line) {} virtual void execute(void); - virtual bool checkArgs(); + virtual e_code checkArgs(); }; diff --git a/include/core/core.hpp b/include/core/core.hpp index b7f6d77..95590bf 100644 --- a/include/core/core.hpp +++ b/include/core/core.hpp @@ -6,7 +6,7 @@ /* By: omoudni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/12 14:16:03 by rparodi #+# #+# */ -/* Updated: 2025/06/04 23:45:35 by rparodi ### ########.fr */ +/* Updated: 2025/06/05 11:10:45 by rparodi ### ########.fr */ /* */ /******************************************************************************/ @@ -17,6 +17,8 @@ #endif enum e_code { + //command for parsing + _PARSING_OK = 000, // Reply codes (RPL_*) RPL_WELCOME = 001, // “ :Welcome to the Internet Relay Network !@” RPL_YOURHOST = 002, // “ :Your host is , running version ” @@ -153,7 +155,7 @@ enum e_code { ERR_TOOMANYTARGETS = 407, // “ :Duplicate recipients. No message delivered” ERR_NOSUCHSERVICE = 408, // “ :No such service ” ERR_NOORIGIN = 409, // “:No origin specified” - ERR_NORECIPIENT = 411, // “:No recipient given ()” + ERR_NOREC = 411, // “:No recipient given ()” ERR_NOTEXTTOSEND = 412, // “:No text to send” ERR_NOTOPLEVEL = 413, // “ :No toplevel domain specified” ERR_WILDTOPLEVEL = 414, // “ :Wildcard in toplevel domain” @@ -173,7 +175,7 @@ enum e_code { ERR_NOLOGIN = 444, // “ :User not logged in” ERR_SUMMONDISABLED = 445, // “:SUMMON has been disabled” ERR_USERSDISABLED = 446, // “:USERS has been disabled” - ERR_NOTREGISTERED = 451, // “:You have not registered” + ERR_NOTTERED = 451, // “:You have not registered” ERR_NEEDMOREPARAMS = 461, // “ :Not enough parameters” ERR_ALREADYREGISTERED = 462, // “:You may not reregister” ERR_NOPERMFORHOST = 463, // “:Your host isn't among the privileged” diff --git a/sources/commands/invite.cpp b/sources/commands/invite.cpp index ebfa953..509bbaf 100644 --- a/sources/commands/invite.cpp +++ b/sources/commands/invite.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/05/29 12:10:25 by rparodi ### ########.fr */ +/* Updated: 2025/06/05 11:10:36 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,44 +16,44 @@ using namespace cmd; -bool Invite::checkArgs() { +e_code Invite::checkArgs() { if (_args.size() < 3) { WARNING_MSG("Not enough arguments for INVITE command"); - return false; + return ERR_NEEDMOREPARAMS; } if (_args.at(1).at(0) != '#') { WARNING_MSG("Invalid channel name for INVITE command"); INFO_MSG("Channel names must start with a '#' character"); - return false; + return ERR_NOSUCHCHANNEL; } else _args.at(1).erase(0, 1); _cTarget = searchList(_channels, _args.at(1)); if (_cTarget == NULL) { WARNING_MSG("Channel not found for INVITE command"); INFO_MSG("You can only invite users to channels you are in"); - return false; + return ERR_NOSUCHCHANNEL; } else _args.at(1).erase(0, 1); if (searchList(_cTarget->getOperators(), _sender->getName()) != NULL) { WARNING_MSG("You are not an operator in the channel for INVITE command"); - return false; + return ERR_NOPRIVILEGES; } _uTarget = searchList(this->_users, _args.at(2)); if (this->_uTarget == NULL) { WARNING_MSG("User not found"); - return false; + return ERR_NOSUCHNICK; } if (this->_uTarget->isRegistered() == false) { WARNING_MSG("User is not registered for INVITE command"); INFO_MSG("You can only invite registered users"); - return false; + return ERR_NOSUCHNICK; } if (searchList(this->_cTarget->getUsers(), this->_uTarget->getName())) { WARNING_MSG("User is already in the channel for INVITE command"); INFO_MSG("You cannot invite a user who is already in the channel"); - return false; + return ERR_USERONCHANNEL; } - return true; + return _PARSING_OK; } /** @@ -61,7 +61,7 @@ bool Invite::checkArgs() { * @note To invite a peapol to join a channel (from an operator) */ void Invite::execute() { - if (checkArgs() == false) { + if (checkArgs() == _PARSING_OK) { ERROR_MSG("Invalid arguments for INVITE command (see warning message)"); return; } diff --git a/sources/commands/join.cpp b/sources/commands/join.cpp index 8935a74..ed68167 100644 --- a/sources/commands/join.cpp +++ b/sources/commands/join.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/02 22:42:17 by rparodi ### ########.fr */ +/* Updated: 2025/06/05 22:34:07 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,32 +16,32 @@ using namespace cmd; -bool Join::checkArgs() { +e_code Join::checkArgs() { if (_args.size() < 2) { WARNING_MSG("Not enough arguments for Join command"); - return false; + return ERR_NEEDMOREPARAMS; } if (_args.at(1).at(0) != '#') { WARNING_MSG("Invalid channel name for Join command"); INFO_MSG("Channel names must start with a '#' character"); - return false; + return ERR_NOSUCHCHANNEL; } else _args.at(1).erase(0, 1); _cTarget = searchList(_channels, _args.at(1)); if (_cTarget == NULL) { WARNING_MSG("Channel not found for Join command"); INFO_MSG("You can only Join users to channels you are in"); - return false; + return ERR_NOSUCHCHANNEL; } if (searchList(_cTarget->getOperators(), _sender->getName()) != NULL) { WARNING_MSG("You are not an operator in the channel for Join command"); - return false; + return ERR_NOPRIVILEGES; } if (searchList(_cTarget->getInvited(), _sender->getName()) != NULL) { WARNING_MSG("This channel is private and ur not invited"); - return false; + return ERR_INVITEONLYCHAN; } - return true; + return _PARSING_OK; } /** @@ -49,7 +49,7 @@ bool Join::checkArgs() { * @note To join a new channel */ void Join::execute() { - if (checkArgs() == false) { + if (checkArgs() == _PARSING_OK) { ERROR_MSG("Invalid arguments for Join command (see warning message)"); return; } diff --git a/sources/commands/nick.cpp b/sources/commands/nick.cpp index 1eec360..2227526 100644 --- a/sources/commands/nick.cpp +++ b/sources/commands/nick.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/02 00:55:45 by rparodi ### ########.fr */ +/* Updated: 2025/06/05 22:48:17 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,22 +16,22 @@ using namespace cmd; -bool Nick::checkArgs() { +e_code Nick::checkArgs() { if (this->_uTarget->isRegistered() == false) { WARNING_MSG("User is not registered for Nick command"); INFO_MSG("You can only Nick registered users"); - return false; + return ERR_NOSUCHNICK; } if (_args.size() < 2) { WARNING_MSG("Not enough arguments for Nick command"); - return false; + return ERR_NEEDMOREPARAMS; } _uTarget = searchList(this->_users, _args.at(1)); if (this->_uTarget != NULL) { WARNING_MSG(_uTarget->getName() << " is already taken") - return false; + return ERR_NICKNAMEINUSE; } - return true; + return _PARSING_OK; } /** @@ -39,7 +39,7 @@ bool Nick::checkArgs() { * @note To change the nickname of the user */ void Nick::execute() { - if (checkArgs() == false) { + if (checkArgs() == _PARSING_OK) { ERROR_MSG("Invalid arguments for Nick command (see warning message)"); return; } diff --git a/sources/commands/notice.cpp b/sources/commands/notice.cpp index e54d10e..8642d25 100644 --- a/sources/commands/notice.cpp +++ b/sources/commands/notice.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/02 22:44:49 by rparodi ### ########.fr */ +/* Updated: 2025/06/05 22:49:09 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,32 +16,32 @@ using namespace cmd; -bool Notice::checkArgs() { +e_code Notice::checkArgs() { if (_args.size() < 3) { WARNING_MSG("Not enough arguments for NOTICE command"); - return false; + return ERR_NEEDMOREPARAMS; } if (_args.at(1).at(0) != '#') { _uTarget = searchList(this->_users, _args.at(2)); if (this->_uTarget == NULL) { WARNING_MSG("User not found"); - return false; + return ERR_NOSUCHNICK; } if (this->_uTarget->isRegistered() == false) { WARNING_MSG("User is not registered for NOTICE command"); INFO_MSG("You can only NOTICE registered users"); - return false; + return ERR_NOSUCHNICK; } } else { _cTarget = searchList(_channels, _args.at(1)); if (_cTarget == NULL) { WARNING_MSG("Channel not found for NOTICE command"); INFO_MSG("You can only NOTICE users to channels you are in"); - return false; + return ERR_NOSUCHCHANNEL; } else _args.at(1).erase(0, 1); } - return true; + return _PARSING_OK; } /** @@ -49,7 +49,7 @@ bool Notice::checkArgs() { * @note To send a private message to a user / a channel (like privmsg but without error) */ void Notice::execute() { - if (checkArgs() == false) { + if (checkArgs() == _PARSING_OK) { ERROR_MSG("Invalid arguments for NOTICE command (see warning message)"); return; } diff --git a/sources/commands/part.cpp b/sources/commands/part.cpp index 584eb39..8e69256 100644 --- a/sources/commands/part.cpp +++ b/sources/commands/part.cpp @@ -6,40 +6,41 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/02 00:55:56 by rparodi ### ########.fr */ +/* Updated: 2025/06/05 22:50:21 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "part.hpp" #include "commands.hpp" +#include "core.hpp" #include "logs.hpp" using namespace cmd; -bool Part::checkArgs() { +e_code Part::checkArgs() { if (_args.size() < 2) { WARNING_MSG("Not enough arguments for PART command"); - return false; + return ERR_NEEDMOREPARAMS; } if (_args.at(1).at(0) != '#') { WARNING_MSG("Invalid channel name for PART command"); INFO_MSG("Channel names must start with a '#' character"); - return false; + return ERR_NOSUCHCHANNEL; } else _args.at(1).erase(0, 1); _cTarget = searchList(_channels, _args.at(1)); if (_cTarget == NULL) { WARNING_MSG("Channel not found for PART command"); INFO_MSG("You can only Part users to channels you are in"); - return false; + return ERR_NOSUCHCHANNEL; } else _args.at(1).erase(0, 1); if (searchList(this->_cTarget->getUsers(), this->_uTarget->getName())) { WARNING_MSG("User is not in the channel he wants to leave"); INFO_MSG("You cannot leave a channel where ur not a member"); - return false; + return ERR_USERNOTINCHANNEL; } - return true; + return _PARSING_OK; } /** @@ -47,7 +48,7 @@ bool Part::checkArgs() { * @note To leave a channel givent in parameter */ void Part::execute() { - if (checkArgs() == false) { + if (checkArgs() == _PARSING_OK) { ERROR_MSG("Invalid arguments for Part command (see warning message)"); return; } diff --git a/sources/commands/pass.cpp b/sources/commands/pass.cpp index a79a686..bd8a8d2 100644 --- a/sources/commands/pass.cpp +++ b/sources/commands/pass.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/03 15:04:10 by rparodi ### ########.fr */ +/* Updated: 2025/06/04 23:59:57 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,17 +16,17 @@ using namespace cmd; -bool Pass::checkArgs() { +e_code Pass::checkArgs() { if (_args.size() != 2) { WARNING_MSG("Not correct for Pass command"); - return false; + return ERR_NEEDMOREPARAMS; } DEBUG_MSG("coucou"); if (_sender->isRegistered()) { WARNING_MSG(_sender->getName() << " is already is already log in the server !"); - return false; + return ERR_ALREADYREGISTERED; } - return true; + return _PARSING_OK; } /** @@ -34,7 +34,7 @@ bool Pass::checkArgs() { * @note To connect a user with the password */ void Pass::execute() { - if (checkArgs() == false) { + if (checkArgs() == _PARSING_OK) { ERROR_MSG("Invalid arguments for Pass command (see warning message)"); DEBUG_MSG("skill issues"); return; diff --git a/sources/commands/ping.cpp b/sources/commands/ping.cpp index 488e262..a5c2e36 100644 --- a/sources/commands/ping.cpp +++ b/sources/commands/ping.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/03 14:49:27 by rparodi ### ########.fr */ +/* Updated: 2025/06/04 23:54:56 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,12 @@ using namespace cmd; -bool Ping::checkArgs() { +e_code Ping::checkArgs() { if (_args.size() < 3) { WARNING_MSG("Not enough arguments for PING command"); - return false; + return ERR_NEEDMOREPARAMS; } - return true; + return _PARSING_OK; } /** @@ -32,7 +32,7 @@ bool Ping::checkArgs() { */ void Ping::execute() { clock_t start = clock() / CLOCKS_PER_SEC; - if (checkArgs() == false) { + if (checkArgs() == _PARSING_OK) { ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)"); return; } diff --git a/sources/commands/privmsg.cpp b/sources/commands/privmsg.cpp index c1c3cd4..8137061 100644 --- a/sources/commands/privmsg.cpp +++ b/sources/commands/privmsg.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/02 01:15:00 by rparodi ### ########.fr */ +/* Updated: 2025/06/05 22:46:54 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,32 +16,32 @@ using namespace cmd; -bool PrivMsg::checkArgs() { +e_code PrivMsg::checkArgs() { if (_args.size() < 3) { WARNING_MSG("Not enough arguments for PRIVMSG command"); - return false; + return ERR_NEEDMOREPARAMS; } if (_args.at(1).at(0) != '#') { _uTarget = searchList(this->_users, _args.at(2)); if (this->_uTarget == NULL) { WARNING_MSG("User not found"); - return false; + return ERR_NOSUCHNICK; } if (this->_uTarget->isRegistered() == false) { WARNING_MSG("User is not registered for PRIVMSG command"); INFO_MSG("You can only privmsg registered users"); - return false; + return ERR_NOSUCHNICK; } } else { _cTarget = searchList(_channels, _args.at(1)); if (_cTarget == NULL) { WARNING_MSG("Channel not found for PRIVMSG command"); INFO_MSG("You can only privmsg users to channels you are in"); - return false; + return ERR_NOSUCHCHANNEL; } else _args.at(1).erase(0, 1); } - return true; + return _PARSING_OK; } /** @@ -49,7 +49,7 @@ bool PrivMsg::checkArgs() { * @note To send a private message to a user / a channel */ void PrivMsg::execute() { - if (checkArgs() == false) { + if (checkArgs() == _PARSING_OK) { ERROR_MSG("Invalid arguments for PRIVMSG command (see warning message)"); return; }