Merge pull request 'invite-cmd' (#32) from invite-cmd into main

Reviewed-on: #32
This commit is contained in:
2026-05-23 19:51:47 +00:00
7 changed files with 93 additions and 11 deletions

View File

@@ -55,7 +55,12 @@ const std::set<int> &Channel::getMembers() const
}
// Members
void Channel::addMember(int fd) { this->members_.insert(fd); }
void Channel::addMember(int fd)
{
invitedMembers_.erase(fd);
this->members_.insert(fd);
}
void Channel::removeMember(int fd) { this->members_.erase(fd); }
bool Channel::hasMember(int fd) const { return (this->members_.count(fd) > 0); }
@@ -94,5 +99,10 @@ void Channel::setMode(std::string& mode, std::string& args)
(void) args;
}
void Channel::inviteMember(const User& client)
{
invitedMembers_.insert(client.getFd());
}
bool Channel::isInviteOnly() const { return isInviteOnly_; }
bool Channel::isInvited(int fd) const { return invitedMembers_.count(fd) > 0; }

View File

@@ -53,13 +53,12 @@ class Channel
void removeOperator(int fd);
bool hasOperator(int fd) const;
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);
void inviteMember(const User& client);
bool isInviteOnly() const;
bool isInvited(int fd) const;

View File

@@ -4,7 +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
cmds/mode.cpp cmds/invite.cpp
HEADERS = Server/Server.hpp User/User.hpp

View File

@@ -6,7 +6,7 @@
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
/* Updated: 2026/05/23 18:03:11 by iherman- ### ########.fr */
/* Updated: 2026/05/23 21:31:06 by iherman- ### ########.fr */
/* */
/* ************************************************************************** */
@@ -119,6 +119,7 @@ Server::Server(int port, const std::string& password) :
commands_["QUIT"] = &Server::quit_cmd;
commands_["PRIVMSG"] = &Server::privmsg_cmd;
commands_["MODE"] = & Server::mode_cmd;
commands_["INVITE"] = &Server::invite_cmd;
std::cout << "Server port: " << port_
<< "\nServer Password: " << password_

View File

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

72
cmds/invite.cpp Normal file
View File

@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}

View File

@@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2026/05/23 20:25:02 by iherman- ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,7 +19,7 @@ void Server::mode_cmd(User &client, std::istringstream &ss)
std::string args;
ss >> target >> mode;
std::getline(ss, args);
std::getline(ss, args); // might include space in channel name, need to fix
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"));
@@ -27,7 +27,7 @@ void Server::mode_cmd(User &client, std::istringstream &ss)
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");
client.send(":" SERVER_NAME " 403 " + client.getNick() + target + ":No such channel\r\n");
return ;
}
@@ -37,5 +37,5 @@ void Server::mode_cmd(User &client, std::istringstream &ss)
return ;
}
channel->second.setMode(mode, args);
channel->second.setMode(mode, args); // args should prob be a stringstream :(
}