user command has been init #24

Merged
aortigos merged 1 commits from user-command into main 2026-05-15 12:25:10 +00:00
5 changed files with 76 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
NAME = ircserv NAME = ircserv
SRC = main.cpp Server/Server.cpp User/User.cpp SRC = main.cpp Server/Server.cpp User/User.cpp \
cmds/pass.cpp cmds/nick.cpp cmds/user.cpp \
HEADERS = Server/Server.hpp User/User.hpp HEADERS = Server/Server.hpp User/User.hpp

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 20:35:49 by aortigos ### ########.fr */ /* Updated: 2026/05/15 12:48:10 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -114,6 +114,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; commands_["NICK"] = &Server::nick_cmd;
commands_["USER"] = &Server::user_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/15 11:40:28 by aortigos ### ########.fr */ /* Updated: 2026/05/15 12:47:58 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -58,6 +58,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); void nick_cmd(User &client, std::istringstream &ss);
void user_cmd(User &client, std::istringstream &ss);
public: public:

View File

@@ -6,25 +6,42 @@
/* 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/15 11:46:03 by aortigos ### ########.fr */ /* Updated: 2026/05/15 12:49:37 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../Server/Server.hpp" #include "../Server/Server.hpp"
static bool isValidNick(const std::string &nick)
{
const std::string special = "[]\\`_^{|}";
if (!isalpha(nick[0]) && special.find(nick[0]) == std::string::npos)
return (false);
for (size_t i = 1; i < nick.size(); i++)
{
if (!isalnum(nick[i]) && special.find(nick[i]) == std::string::npos && nick[i] != '-')
return (false);
}
return (true);
}
void Server::nick_cmd(User &client, std::istringstream &ss) void Server::nick_cmd(User &client, std::istringstream &ss)
{ {
std::string args; std::string args;
ss >> args; ss >> args;
if (!client.isAuthenticated())
return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
if (args.empty()) if (args.empty())
return (client.send(":" SERVER_NAME " 431 * :Not nickname given\r\n")); return (client.send(":" SERVER_NAME " 431 * :Not nickname given\r\n"));
if (!isValidNick(args)) if (!isValidNick(args))
return (client.send(":" SERVER_NAME " 432 * " + args + ":Erroneous nickname\r\n")) return (client.send(":" SERVER_NAME " 432 * " + args + " Erroneous nickname\r\n"));
for (std::map<int, User>::iterator it = clients_.begin(); it != clients_.end(); it++) for (std::map<int, User>::iterator it = clients_.begin(); it != clients_.end(); it++)
{ {
if (it->second.getNick() == args) if (it->second.getNick() == args)
return (client.send(":" SERVER_NAME " 433 * " + args + " :Nickname is already in use\r\n")) return (client.send(":" SERVER_NAME " 433 * " + args + " :Nickname is already in use\r\n"));
} }
std::string oldNick = client.getNick(); std::string oldNick = client.getNick();
@@ -47,17 +64,3 @@ void Server::nick_cmd(User &client, std::istringstream &ss)
client.send(":" SERVER_NAME " 004 " + args + " :" SERVER_NAME " 1.0\r\n"); client.send(":" SERVER_NAME " 004 " + args + " :" SERVER_NAME " 1.0\r\n");
} }
} }
static bool isValidNick(const std::string &nick)
{
const std::string special = "[]\\`_^{|}";
if (!isalpha(nick[0]) && special.find(nick[0]) == std::string::npos)
return (false);
for (size_t i = 1; i < nick.size(); i++)
{
if (!isalnum(nick[i])) && special.find(nick[i] == std::string::npos && nick[i] != '-')
return (false);
}
return (true);
}

50
cmds/user.cpp Normal file
View File

@@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* user.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
/* Updated: 2026/05/15 12:49:49 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::user_cmd(User &client, std::istringstream &ss)
{
std::string username;
std::string hostname;
std::string servername;
std::string realname;
ss >> username >> hostname >> servername;
std::getline(ss, realname);
if (!client.isAuthenticated())
return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
if (client.isRegistered())
return (client.send(":" SERVER_NAME " 462 " + client.getNick() + " :Unauthorized command (already registered)\r\n"));
if (!realname.empty() && realname[0] == ' ')
realname = realname.substr(1);
if (realname.empty() || realname[0] != ':')
return (client.send(":" SERVER_NAME " 461 * USER :Not enough parameters\r\n"));
if (!realname.empty() && realname[0] == ':')
realname = realname.substr(1);
if (username.empty() || hostname.empty() || servername.empty() || realname.empty())
return (client.send(":" SERVER_NAME " 461 * USER :Not enough parameters\r\n"));
client.setUsername(username);
client.setRealname(realname);
if (!client.getNick().empty())
{
client.setRegistered(true);
client.send(":" SERVER_NAME " 001 " + client.getNick() + " :Welcome to the IRC Network " + client.getNick() + "\r\n");
client.send(":" SERVER_NAME " 002 " + client.getNick() + " :Your host is " SERVER_NAME ", running version 1.0\r\n");
client.send(":" SERVER_NAME " 003 " + client.getNick() + " :This server was created May 2026\r\n");
client.send(":" SERVER_NAME " 004 " + client.getNick() + " :" SERVER_NAME " 1.0\r\n");
}
}