One file per command

This commit is contained in:
2026-04-17 12:01:16 +02:00
parent 00a08b4f30
commit bb66fcc097
8 changed files with 169 additions and 4 deletions

38
commands/nick.cpp Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nick.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::cmd_nick(int fd, std::istringstream &ss)
{
std::string args;
ss >> args;
if(!clients[fd].isAuthenticated())
{
send(fd, ERR_NOPASS.c_str(), ERR_NOPASS.size(), 0);
return ;
}
if (!args.empty())
{
clients[fd].setNick(args);
std::string msg = "Your nick has been updated!\r\n";
send(fd, msg.c_str(), msg.size(), 0);
}
else
{
send(fd, ERR_NONICK.c_str(), ERR_NONICK.size(), 0);
}
}

33
commands/pass.cpp Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pass.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::cmd_pass(int fd, std::istringstream &ss)
{
std::string args;
ss >> args;
if (args == this->password)
{
clients[fd].setAuthenticated(true);
std::string msg = "You have been authenticated!\r\n";
send(fd, msg.c_str(), msg.size(), 0);
}
else
{
send(fd, ERR_PASSWORD.c_str(), ERR_PASSWORD.size(), 0);
removeClient(fd);
}
}

47
commands/user.cpp Normal file
View File

@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* user.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::cmd_user(int fd, std::istringstream &ss)
{
std::string username, unused1, unused2, realname;
ss >> username >> unused1 >> unused2;
std::getline(ss, realname);
if (clients[fd].getNick().empty())
{
send(fd, ERR_NONICK.c_str(), ERR_NONICK.size(), 0);
return ;
}
if(clients[fd].isRegistered())
{
send(fd, ERR_REREGISTER.c_str(), ERR_REREGISTER.size(), 0);
return ;
}
if (username.empty())
{
send(fd, ERR_NOUSER.c_str(), ERR_NOUSER.size(), 0);
return ;
}
clients[fd].setUsername(username);
clients[fd].setRealname(realname);
clients[fd].setRegistered(true);
std::string msg = "You has been registered!\r\n";
send(fd, msg.c_str(), msg.size(), 0);
}