Merge pull request 'user command has been init' (#24) from user-command into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/24
This commit is contained in:
3
Makefile
3
Makefile
@@ -1,6 +1,7 @@
|
||||
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
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
commands_["PASS"] = &Server::pass_cmd;
|
||||
commands_["NICK"] = &Server::nick_cmd;
|
||||
commands_["USER"] = &Server::user_cmd;
|
||||
|
||||
std::cout << "Server port: " << port_
|
||||
<< "\nServer Password: " << password_
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
void pass_cmd(User &client, std::istringstream &ss);
|
||||
void nick_cmd(User &client, std::istringstream &ss);
|
||||
void user_cmd(User &client, std::istringstream &ss);
|
||||
|
||||
|
||||
public:
|
||||
|
||||
@@ -6,25 +6,42 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
std::string args;
|
||||
|
||||
ss >> args;
|
||||
if (!client.isAuthenticated())
|
||||
return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
|
||||
if (args.empty())
|
||||
return (client.send(":" SERVER_NAME " 431 * :Not nickname given\r\n"));
|
||||
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++)
|
||||
{
|
||||
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();
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
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
50
cmds/user.cpp
Normal 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user