Nick command now do all the checks #23

Merged
aortigos merged 1 commits from nick-command into main 2026-05-15 09:52:11 +00:00
2 changed files with 46 additions and 9 deletions

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 20:35:22 by aortigos ### ########.fr */ /* Updated: 2026/05/15 11:40:28 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -22,10 +22,12 @@
# include <vector> # include <vector>
# include <map> # include <map>
# include <cctype>
# include "../User/User.hpp" # include "../User/User.hpp"
# define SERVER_NAME irc.server # define SERVER_NAME "irc.server"
# define PORT_DEFAULT 9898 # define PORT_DEFAULT 9898
class Server class Server

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 20:38:44 by aortigos ### ########.fr */ /* Updated: 2026/05/15 11:46:03 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -19,10 +19,45 @@ void Server::nick_cmd(User &client, std::istringstream &ss)
ss >> args; ss >> args;
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"));
// Invalid characters in nick if (!isValidNick(args))
// 432 * badnick : Erroneous nickname return (client.send(":" SERVER_NAME " 432 * " + args + ":Erroneous nickname\r\n"))
// If nick exists for (std::map<int, User>::iterator it = clients_.begin(); it != clients_.end(); it++)
// 4233 * takenick :Nickname is already in use {
if (client.getUsername()) if (it->second.getNick() == args)
return (); // Broadcast in channels :oldnick!user@host NICK newnick return (client.send(":" SERVER_NAME " 433 * " + args + " :Nickname is already in use\r\n"))
}
std::string oldNick = client.getNick();
client.setNick(args);
if (client.isRegistered())
{
// Change nick to an already registered user
// Send message to his channels
//client.broadcast(":" + oldNick + "!~" + client.getUsername() + "@hostt NICK " + args + "\r\n");
return ;
}
if (!client.getUsername().empty())
{
client.setRegistered(true);
client.send(":" SERVER_NAME " 001 " + args + " :Welcome to the IRC Network " + args + "\r\n");
client.send(":" SERVER_NAME " 002 " + args + " :Your host is " SERVER_NAME ", running version 1.0\r\n");
client.send(":" SERVER_NAME " 003 " + args + " :This server was created May 2026\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);
} }