Merge pull request 'pass has been refactorized and nick command init' (#22) from init-nick-command into main

Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/22
This commit is contained in:
2026-05-15 09:26:35 +00:00
4 changed files with 39 additions and 11 deletions

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */ /* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
/* Updated: 2026/05/14 17:00:22 by aortigos ### ########.fr */ /* Updated: 2026/05/14 20:35:49 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -113,6 +113,7 @@ Server::Server(int port, const std::string& password) :
// Add all new commands to commands_ here // Add all new commands to commands_ here
commands_["PASS"] = &Server::pass_cmd; commands_["PASS"] = &Server::pass_cmd;
commands_["NICK"] = &Server::nick_cmd;
std::cout << "Server port: " << port_ std::cout << "Server port: " << port_
<< "\nServer Password: " << password_ << "\nServer Password: " << password_

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */ /* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
/* Updated: 2026/05/14 16:58:31 by aortigos ### ########.fr */ /* Updated: 2026/05/14 20:35:22 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -24,6 +24,8 @@
# include "../User/User.hpp" # include "../User/User.hpp"
# define SERVER_NAME irc.server
# define PORT_DEFAULT 9898 # define PORT_DEFAULT 9898
class Server class Server
@@ -53,6 +55,7 @@ class Server
// Commands // Commands
void pass_cmd(User &client, std::istringstream &ss); void pass_cmd(User &client, std::istringstream &ss);
void nick_cmd(User &client, std::istringstream &ss);
public: public:

28
cmds/nick.cpp Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nick.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
/* Updated: 2026/05/14 20:38:44 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::nick_cmd(User &client, std::istringstream &ss)
{
std::string args;
ss >> args;
if (args.empty())
return (client.send(":" SERVER_NAME " 431 * :Not nickname given\r\n"));
// Invalid characters in nick
// 432 * badnick : Erroneous nickname
// If nick exists
// 4233 * takenick :Nickname is already in use
if (client.getUsername())
return (); // Broadcast in channels :oldnick!user@host NICK newnick
}

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */ /* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
/* Updated: 2026/05/14 17:23:56 by aortigos ### ########.fr */ /* Updated: 2026/05/14 20:21:31 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -17,17 +17,13 @@ void Server::pass_cmd(User &client, std::istringstream &ss)
std::string args; std::string args;
ss >> args; ss >> args;
if (args.empty())
return (client.send(":" SERVER_NAME " 461 * PASS :Not enough parameters\r\n"));
if (client.isAuthenticated()) if (client.isAuthenticated())
{ return (client.send(":" SERVER_NAME " 462 " + client.getNick() + " :Unauthorized command (already registered)\r\n"));
// Still need to find what server should reply to different errors
client.send("You are already logged in\r\n");
return ;
}
if (this->password_ == args) if (this->password_ == args)
client.setAuthenticated(true); client.setAuthenticated(true);
else { else {
// Still need to find what server should reply to different errors client.send(":" SERVER_NAME " 464 * :Password incorrect\r\n");
client.send("Invalid password\r\n");
send(client.getFd(), res.c_str(), res.size(), 0);
} }
} }