Added MODE and invite only functionality

This commit is contained in:
iherman-
2026-05-23 18:39:18 +02:00
parent 982ca33116
commit dd4de38e5f
9 changed files with 124 additions and 36 deletions

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* join.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/16 17:04:29 by aortigos ### ########.fr */
/* Updated: 2026/05/23 18:32:37 by iherman- ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,6 +23,7 @@ void Server::join_cmd(User &client, std::istringstream &ss)
std::map<std::string, Channel>::iterator it = channels_.find(args);
// creates channel
if (it == channels_.end())
{
channels_[args] = Channel(args);
@@ -51,29 +52,37 @@ void Server::join_cmd(User &client, std::istringstream &ss)
else
{
if (it->second.hasMember(client.getFd()))
client.send(":" SERVER_NAME " 443 " + client.getNick() + " " + args + " :is already on channel\r\n");
else
{
client.joinChannel(it->first);
it->second.addMember(client.getFd());
std::string joinMsg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost JOIN " + args + "\r\n";
channels_[args].broadcast(joinMsg, clients_, -1);
std::string namesList = ":" SERVER_NAME " 353 " + client.getNick() + " = " + args + " :";
const std::set<int> &members = channels_[args].getMembers();
for (std::set<int>::const_iterator m = members.begin(); m != members.end(); m++)
{
std::map<int, User>::iterator u = clients_.find(*m);
if (u != clients_.end())
{
if (channels_[args].hasOperator(*m))
namesList += "@";
namesList += u->second.getNick() + " ";
}
}
client.send(namesList + "\r\n");
client.send(":" SERVER_NAME " 366 " + client.getNick() + " " + args + " :End of /NAMES list\r\n");
client.send(":" SERVER_NAME " 443 " + client.getNick() + " " + args + " :is already on channel\r\n");
return ;
}
if (it->second.isInviteOnly() && !it->second.isInvited(client.getFd()))
{
client.send(":" SERVER_NAME " 473 " + client.getNick() + " " + args + " :Cannot join channel (+i)\r\n");
return ;
}
it->second.addMember(client.getFd());
client.joinChannel(it->first);
std::string joinMsg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost JOIN " + args + "\r\n";
channels_[args].broadcast(joinMsg, clients_, -1);
std::string namesList = ":" SERVER_NAME " 353 " + client.getNick() + " = " + args + " :";
const std::set<int> &members = channels_[args].getMembers();
for (std::set<int>::const_iterator m = members.begin(); m != members.end(); m++)
{
std::map<int, User>::iterator u = clients_.find(*m);
if (u != clients_.end())
{
if (channels_[args].hasOperator(*m))
namesList += "@";
namesList += u->second.getNick() + " ";
}
}
client.send(namesList + "\r\n");
client.send(":" SERVER_NAME " 366 " + client.getNick() + " " + args + " :End of /NAMES list\r\n");
}
}

41
cmds/mode.cpp Normal file
View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mode.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/23 17:15:27 by iherman- #+# #+# */
/* Updated: 2026/05/23 18:35:51 by iherman- ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::mode_cmd(User &client, std::istringstream &ss)
{
std::string target;
std::string mode;
std::string args;
ss >> target >> mode;
std::getline(ss, args);
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
if (target.empty() || mode.empty()) return (client.send(":" SERVER_NAME " 461 " + client.getNick() + " MODE :Not enough parameters\r\n"));
std::map<std::string, Channel>::iterator channel = channels_.find(target);
if (channel == channels_.end())
{
client.send(":" SERVER_NAME " 403 " + client.getNick() + target + ":No such channel");
return ;
}
if (!channel->second.hasOperator(client.getFd()))
{
client.send(std::string(":") + SERVER_NAME + " 482 " + client.getNick() + " " + channel->second.getName() + " :You're not channel operator\r\n");
return ;
}
channel->second.setMode(mode, args);
}