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

@@ -33,6 +33,8 @@ Channel& Channel::operator=(const Channel &other)
name_ = other.name_;
members_ = other.members_;
operators_ = other.operators_;
isInviteOnly_ = other.isInviteOnly_;
invitedMembers_ = other.invitedMembers_;
}
// std::cout << "Channel copy assignment operator called" << std::endl;
return (*this);
@@ -41,7 +43,7 @@ Channel& Channel::operator=(const Channel &other)
// Constructor with name
Channel::Channel(std::string &name) : name_(name) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
Channel::Channel(std::string &name) : name_(name), isInviteOnly_(false) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
// Getters
@@ -62,14 +64,35 @@ void Channel::addOperator(int fd) { this->operators_.insert(fd); }
void Channel::removeOperator(int fd) { this->operators_.erase(fd); }
bool Channel::hasOperator(int fd) const { return (this->operators_.count(fd) > 0); }
void Channel::broadcast(const std::string &msg, std::map<int, User> &clients, int excludedFd)
void Channel::broadcast(const std::string &msg, const std::map<int, User> &clients, int excludedFd)
{
for (std::set<int>::iterator member = members_.begin(); member != members_.end(); member++)
{
if ((*member) == excludedFd) continue;
std::map<int, User>::iterator user = clients.find(*member);
std::map<int, User>::const_iterator user = clients.find(*member);
if (user != clients.end())
user->second.send(msg);
}
}
void Channel::setMode(std::string& mode, std::string& args)
{
bool value;
if (mode[0] == '+')
value = true;
else
value = false;
// very simple to test
if (mode[1] == 'i')
{
isInviteOnly_ = value;
return ;
}
(void) args;
}
bool Channel::isInviteOnly() const { return isInviteOnly_; }
bool Channel::isInvited(int fd) const { return invitedMembers_.count(fd) > 0; }

View File

@@ -24,9 +24,13 @@ class Channel
{
private:
std::string name_;
std::set<int> members_;
std::set<int> operators_;
bool isInviteOnly_;
std::set<int> invitedMembers_;
public:
Channel();
Channel(std::string &name);
@@ -39,7 +43,6 @@ class Channel
std::string getName() const;
const std::set<int> &getMembers() const;
// Users
void addMember(int fd);
void removeMember(int fd);
@@ -50,8 +53,15 @@ class Channel
void removeOperator(int fd);
bool hasOperator(int fd) const;
void broadcast(const std::string &msg, std::map<int, User> &clients, int excludedFd);
void inviteMember(User& client); // not implemented
void broadcast(const std::string &msg, const std::map<int, User> &clients, int excludedFd);
// modes
void setMode(std::string& mode, std::string& args);
bool isInviteOnly() const;
bool isInvited(int fd) const;
};

View File

@@ -4,6 +4,7 @@ SRC = main.cpp Server/Server.cpp User/User.cpp \
Channel/Channel.cpp \
cmds/pass.cpp cmds/nick.cpp cmds/user.cpp \
cmds/join.cpp cmds/privmsg.cpp cmds/quit.cpp \
cmds/mode.cpp
HEADERS = Server/Server.hpp User/User.hpp

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
/* Updated: 2026/05/16 11:31:25 by aortigos ### ########.fr */
/* Updated: 2026/05/23 18:03:11 by iherman- ### ########.fr */
/* */
/* ************************************************************************** */
@@ -118,6 +118,7 @@ Server::Server(int port, const std::string& password) :
commands_["JOIN"] = &Server::join_cmd;
commands_["QUIT"] = &Server::quit_cmd;
commands_["PRIVMSG"] = &Server::privmsg_cmd;
commands_["MODE"] = & Server::mode_cmd;
std::cout << "Server port: " << port_
<< "\nServer Password: " << password_
@@ -233,6 +234,8 @@ std::vector<struct pollfd>::iterator Server::removeClient(std::vector<struct pol
std::cout << "Client with fd: " << client->fd << " disconnected " << std::endl;
// should also remove client from all channels its in
clients_.erase(client->fd);
return (sockets_.erase(client));
}

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
/* Updated: 2026/05/16 11:45:11 by aortigos ### ########.fr */
/* Updated: 2026/05/23 17:16:21 by iherman- ### ########.fr */
/* */
/* ************************************************************************** */
@@ -67,6 +67,7 @@ class Server
void join_cmd(User &client, std::istringstream &ss);
void privmsg_cmd(User &client, std::istringstream &ss);
void quit_cmd(User &client, std::istringstream &ss);
void mode_cmd(User &client, std::istringstream &ss);
public:

View File

@@ -55,7 +55,7 @@ User::~User()
}
void User::send(const std::string &msg)
void User::send(const std::string &msg) const
{
::send(fd, msg.c_str(), msg.size(), 0);
}

View File

@@ -38,7 +38,7 @@ class User
User& operator=(const User &other);
~User();
void send(const std::string &msg);
void send(const std::string &msg) const;
// Channels
void joinChannel(const std::string &channel);

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,15 +52,24 @@ 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);
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++)
@@ -75,5 +85,4 @@ void Server::join_cmd(User &client, std::istringstream &ss)
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);
}