From 49fa432411c623faa37ffbd826e83ce066790f44 Mon Sep 17 00:00:00 2001 From: aortigos Date: Fri, 17 Apr 2026 13:12:22 +0200 Subject: [PATCH] Fixed error messages in commands --- Makefile | 4 ++-- Server/Server.cpp | 3 --- Server/Server.hpp | 7 ------- commands/nick.cpp | 37 +++++++++++++++++++++++++++++-------- commands/pass.cpp | 32 ++++++++++++++++++++++++-------- commands/user.cpp | 19 ++++++++++++++----- 6 files changed, 69 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 85854e0..b749a96 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ -NAME = ircserver +NAME = ircserv SRC = main.cpp Server/Server.cpp Client/Client.cpp \ commands/pass.cpp commands/nick.cpp commands/user.cpp \ - + OBJ = $(SRC:.cpp=.o) diff --git a/Server/Server.cpp b/Server/Server.cpp index 8976fa7..0de9cb7 100644 --- a/Server/Server.cpp +++ b/Server/Server.cpp @@ -108,7 +108,6 @@ void Server::readClient(int fd) } clients[fd].appendBuffer(buf); - std::cout << "fd=" << fd << " dice: " << buf; std::string &clientBuf = clients[fd].getBuffer(); size_t pos; @@ -155,6 +154,4 @@ void Server::parseCommand(int fd, std::string line) cmd_nick(fd, ss); else if (command == "USER") cmd_user(fd, ss); - else - std::cout << "Comando desconocido: " << command << "\n"; } \ No newline at end of file diff --git a/Server/Server.hpp b/Server/Server.hpp index 5c7d91a..7fd67f5 100644 --- a/Server/Server.hpp +++ b/Server/Server.hpp @@ -25,13 +25,6 @@ # include # include - -const std::string ERR_PASSWORD = "ERROR :Password incorrect\r\n"; -const std::string ERR_NONICK = "ERROR :No nick given\r\n"; -const std::string ERR_NOPASS = "ERROR :Authenticate first\r\n"; -const std::string ERR_REREGISTER = "462 :You may not reregister\r\n"; -const std::string ERR_NOUSER = "ERROR :No username given\r\n"; - class Server { private: diff --git a/commands/nick.cpp b/commands/nick.cpp index 0f46fdc..4a129b6 100644 --- a/commands/nick.cpp +++ b/commands/nick.cpp @@ -6,7 +6,7 @@ /* By: aortigos ::iterator it = clients.begin(); it != clients.end(); it++) { - send(fd, ERR_NONICK.c_str(), ERR_NONICK.size(), 0); + if (it->second.getNick() == args) + { + std::string msg = ":ircserv 433 * " + + clients[fd].getNick() + + " :Nickname is already in use\r\n"; + send(fd, msg.c_str(), msg.size(), 0); + + return; + } } + + clients[fd].setNick(args); + + std::string msg = ":" + + clients[fd].getNick() + + " NICK " + + args + + "\r\n"; + send(fd, msg.c_str(), msg.size(), 0); + } \ No newline at end of file diff --git a/commands/pass.cpp b/commands/pass.cpp index f7dff62..339bb96 100644 --- a/commands/pass.cpp +++ b/commands/pass.cpp @@ -6,7 +6,7 @@ /* By: aortigos > args; - if (args == this->password) + if (clients[fd].isAuthenticated()) { - clients[fd].setAuthenticated(true); - - std::string msg = "You have been authenticated!\r\n"; + std::string msg = ":ircserv 462 " + + clients[fd].getNick() + + " :You may not reregister\r\n"; send(fd, msg.c_str(), msg.size(), 0); + + return ; } - else + + if (args.empty()) { - send(fd, ERR_PASSWORD.c_str(), ERR_PASSWORD.size(), 0); - removeClient(fd); + std::string msg = ":ircserv 461 * PASS :Not enough parameters\r\n"; + send(fd, msg.c_str(), msg.size(), 0); + + return ; } + + if (args != this->password) + { + std::string msg = ":ircserv 464 * :Password incorrect\r\n"; + send(fd, msg.c_str(), msg.size(), 0); + removeClient(fd); + return ; + } + + clients[fd].setAuthenticated(true); + } \ No newline at end of file diff --git a/commands/user.cpp b/commands/user.cpp index 8248e64..8fda0d6 100644 --- a/commands/user.cpp +++ b/commands/user.cpp @@ -6,7 +6,7 @@ /* By: aortigos