69 lines
2.8 KiB
C++
69 lines
2.8 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* nick.cpp :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
|
|
/* Updated: 2026/05/16 11:12:12 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"));
|
|
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())
|
|
{
|
|
std::string msg = ":" + oldNick + " NICK " + args + "\r\n";
|
|
const std::set<std::string> &userChannels = client.getChannels();
|
|
for (std::set<std::string>::const_iterator it = userChannels.begin(); it != userChannels.end(); it++)
|
|
{
|
|
std::map<std::string, Channel>::iterator ch = channels_.find(*it);
|
|
if (ch != channels_.end())
|
|
ch->second.broadcast(msg, clients_, -1);
|
|
}
|
|
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");
|
|
}
|
|
}
|