Nick command now do all the checks

This commit is contained in:
aortigos
2026-05-15 11:46:36 +02:00
parent 81d71ea2ec
commit 61fae2d6a8
2 changed files with 46 additions and 9 deletions

View File

@@ -6,7 +6,7 @@
/* 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 */
/* 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;
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
if (!isValidNick(args))
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"))
}
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);
}