73 lines
2.7 KiB
C++
73 lines
2.7 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* invclient_ite.cpp :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2026/05/23 20:22:46 by iherman- #+# #+# */
|
|
/* Updated: 2026/05/23 21:14:14 by iherman- ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../Server/Server.hpp"
|
|
|
|
void Server::invite_cmd(User &client, std::istringstream &ss)
|
|
{
|
|
std::string target;
|
|
std::string channel;
|
|
|
|
ss >> target >> channel;
|
|
|
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
|
|
|
|
if (target.empty() || channel.empty())
|
|
{
|
|
return client.send(":" SERVER_NAME " 461 * INVITE :Not enough parameters\r\n");
|
|
}
|
|
|
|
// verify target exists
|
|
std::map<int, User>::iterator client_it = clients_.begin();
|
|
for (; client_it != clients_.end(); ++client_it)
|
|
{
|
|
if (client_it->second.getNick() == target)
|
|
break;
|
|
}
|
|
if (client_it == clients_.end())
|
|
{
|
|
client.send(":" SERVER_NAME " 401 " + target + " :No such nick\r\n");
|
|
return ;
|
|
}
|
|
|
|
// verify channel exist & user is on channel
|
|
std::map<std::string, Channel>::iterator channel_it = channels_.find(channel);
|
|
if (channel_it == channels_.end())
|
|
{
|
|
client.send(":" SERVER_NAME " 401 " + target + " :No such channel\r\n");
|
|
return ;
|
|
}
|
|
|
|
if (!channel_it->second.hasMember(client.getFd()))
|
|
{
|
|
client.send(":" SERVER_NAME " 442 " + channel + " :You are not on that channel\r\n");
|
|
return ;
|
|
}
|
|
|
|
if (channel_it->second.hasMember(client_it->second.getFd()))
|
|
{
|
|
client.send(":" SERVER_NAME " 443 " + channel + " :is already on channel\r\n");
|
|
return ;
|
|
}
|
|
|
|
if (channel_it->second.isInviteOnly() && !channel_it->second.hasOperator(client.getFd()))
|
|
{
|
|
client.send(":" SERVER_NAME " 482 " + channel + ":You're not channel operator\r\n");
|
|
return ;
|
|
}
|
|
|
|
client_it->second.send(":" + client.getNick() + "!" + client.getUsername() + "@localhost INVITE " + client_it->second.getNick() + " :" + channel_it->second.getName());
|
|
client.send(std::string(":") + SERVER_NAME " 341 " + client.getNick() + " " + client_it->second.getNick() + " " + channel_it->second.getName());
|
|
channel_it->second.inviteMember(client_it->second);
|
|
}
|
|
|