Fixed error messages in commands
This commit is contained in:
2
Makefile
2
Makefile
@@ -1,4 +1,4 @@
|
||||
NAME = ircserver
|
||||
NAME = ircserv
|
||||
|
||||
SRC = main.cpp Server/Server.cpp Client/Client.cpp \
|
||||
commands/pass.cpp commands/nick.cpp commands/user.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";
|
||||
}
|
||||
@@ -25,13 +25,6 @@
|
||||
# include <map>
|
||||
# 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
|
||||
{
|
||||
private:
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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())
|
||||
{
|
||||
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 ;
|
||||
}
|
||||
|
||||
if (!args.empty())
|
||||
if (args.empty())
|
||||
{
|
||||
clients[fd].setNick(args);
|
||||
|
||||
std::string msg = "Your nick has been updated!\r\n";
|
||||
std::string msg = ":ircserv 431 * :No nickname given\r\n";
|
||||
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);
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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())
|
||||
{
|
||||
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 ;
|
||||
}
|
||||
|
||||
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 ;
|
||||
}
|
||||
|
||||
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 ;
|
||||
}
|
||||
|
||||
@@ -42,6 +47,10 @@ void Server::cmd_user(int fd, std::istringstream &ss)
|
||||
clients[fd].setRealname(realname);
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user