Fixed error messages in commands

This commit is contained in:
2026-04-17 13:12:22 +02:00
parent bb66fcc097
commit 49fa432411
6 changed files with 69 additions and 33 deletions

View File

@@ -1,4 +1,4 @@
NAME = ircserver NAME = ircserv
SRC = main.cpp Server/Server.cpp Client/Client.cpp \ SRC = main.cpp Server/Server.cpp Client/Client.cpp \
commands/pass.cpp commands/nick.cpp commands/user.cpp \ commands/pass.cpp commands/nick.cpp commands/user.cpp \

View File

@@ -108,7 +108,6 @@ void Server::readClient(int fd)
} }
clients[fd].appendBuffer(buf); clients[fd].appendBuffer(buf);
std::cout << "fd=" << fd << " dice: " << buf;
std::string &clientBuf = clients[fd].getBuffer(); std::string &clientBuf = clients[fd].getBuffer();
size_t pos; size_t pos;
@@ -155,6 +154,4 @@ void Server::parseCommand(int fd, std::string line)
cmd_nick(fd, ss); cmd_nick(fd, ss);
else if (command == "USER") else if (command == "USER")
cmd_user(fd, ss); cmd_user(fd, ss);
else
std::cout << "Comando desconocido: " << command << "\n";
} }

View File

@@ -25,13 +25,6 @@
# include <map> # include <map>
# include <sstream> # include <sstream>
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 class Server
{ {
private: private:

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/17 11:56:55 by aortigos #+# #+# */ /* Created: 2026/04/17 11:56:55 by aortigos #+# #+# */
/* Updated: 2026/04/17 11:59:33 by aortigos ### ########.fr */ /* Updated: 2026/04/17 12:45:36 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -20,19 +20,40 @@ void Server::cmd_nick(int fd, std::istringstream &ss)
if(!clients[fd].isAuthenticated()) if(!clients[fd].isAuthenticated())
{ {
send(fd, ERR_NOPASS.c_str(), ERR_NOPASS.size(), 0); std::string msg = ":ircserv 451 * :You have not registered\r\n";
send(fd, msg.c_str(), msg.size(), 0);
return ; return ;
} }
if (!args.empty()) if (args.empty())
{ {
clients[fd].setNick(args); std::string msg = ":ircserv 431 * :No nickname given\r\n";
std::string msg = "Your nick has been updated!\r\n";
send(fd, msg.c_str(), msg.size(), 0); send(fd, msg.c_str(), msg.size(), 0);
return ;
} }
else
for (std::map<int, Client>::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);
} }

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/17 11:53:56 by aortigos #+# #+# */ /* Created: 2026/04/17 11:53:56 by aortigos #+# #+# */
/* Updated: 2026/04/17 11:56:21 by aortigos ### ########.fr */ /* Updated: 2026/04/17 13:04:05 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -18,16 +18,32 @@ void Server::cmd_pass(int fd, std::istringstream &ss)
ss >> args; ss >> args;
if (args == this->password) if (clients[fd].isAuthenticated())
{ {
clients[fd].setAuthenticated(true); std::string msg = ":ircserv 462 "
+ clients[fd].getNick()
std::string msg = "You have been authenticated!\r\n"; + " :You may not reregister\r\n";
send(fd, msg.c_str(), msg.size(), 0); send(fd, msg.c_str(), msg.size(), 0);
return ;
} }
else
if (args.empty())
{ {
send(fd, ERR_PASSWORD.c_str(), ERR_PASSWORD.size(), 0); std::string msg = ":ircserv 461 * PASS :Not enough parameters\r\n";
removeClient(fd); 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);
} }

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/17 11:58:23 by aortigos #+# #+# */ /* Created: 2026/04/17 11:58:23 by aortigos #+# #+# */
/* Updated: 2026/04/17 11:58:40 by aortigos ### ########.fr */ /* Updated: 2026/04/17 13:10:07 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -21,19 +21,24 @@ void Server::cmd_user(int fd, std::istringstream &ss)
if (clients[fd].getNick().empty()) if (clients[fd].getNick().empty())
{ {
send(fd, ERR_NONICK.c_str(), ERR_NONICK.size(), 0); std::string msg = ":ircserv 451 * :You have not registered\r\n";
send(fd, msg.c_str(), msg.size(), 0);
return ; return ;
} }
if(clients[fd].isRegistered()) if(clients[fd].isRegistered())
{ {
send(fd, ERR_REREGISTER.c_str(), ERR_REREGISTER.size(), 0); std::string msg = ":ircserv 462 "
+ clients[fd].getNick()
+ " :You may not reregister\r\n";
send(fd, msg.c_str(), msg.size(), 0);
return ; return ;
} }
if (username.empty()) if (username.empty())
{ {
send(fd, ERR_NOUSER.c_str(), ERR_NOUSER.size(), 0); std::string msg = ":ircserv 461 * USER :Not enough parameters\r\n";
send(fd, msg.c_str(), msg.size(), 0);
return ; return ;
} }
@@ -42,6 +47,10 @@ void Server::cmd_user(int fd, std::istringstream &ss)
clients[fd].setRealname(realname); clients[fd].setRealname(realname);
clients[fd].setRegistered(true); clients[fd].setRegistered(true);
std::string msg = "You has been registered!\r\n"; std::string msg = ":ircserv 001 "
+ clients[fd].getNick()
+ " :Welcome to IRC "
+ clients[fd].getNick()
+ "\r\n";
send(fd, msg.c_str(), msg.size(), 0); send(fd, msg.c_str(), msg.size(), 0);
} }