now broadcast send message to every member of channel, excluding the author of message

This commit is contained in:
aortigos
2026-05-15 23:11:02 +02:00
parent 9a2d7919fb
commit a21bac6e47
8 changed files with 65 additions and 14 deletions

View File

@@ -57,7 +57,14 @@ void Channel::addOperator(int fd) { this->operators_.insert(fd); }
void Channel::removeOperator(int fd) { this->operators_.erase(fd); } void Channel::removeOperator(int fd) { this->operators_.erase(fd); }
bool Channel::hasOperator(int fd) const { return (this->operators_.count(fd) > 0); } bool Channel::hasOperator(int fd) const { return (this->operators_.count(fd) > 0); }
void Channel::broadcast() void Channel::broadcast(const std::string &msg, std::map<int, User> &clients, int excludedFd)
{ {
return ; 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);
if (user != clients.end())
user->second.send(msg);
}
}

View File

@@ -49,7 +49,7 @@ class Channel
void removeOperator(int fd); void removeOperator(int fd);
bool hasOperator(int fd) const; bool hasOperator(int fd) const;
void broadcast(); void broadcast(const std::string &msg, std::map<int, User> &clients, int excludedFd);
}; };

View File

@@ -3,7 +3,7 @@ NAME = ircserv
SRC = main.cpp Server/Server.cpp User/User.cpp \ SRC = main.cpp Server/Server.cpp User/User.cpp \
Channel/Channel.cpp \ Channel/Channel.cpp \
cmds/pass.cpp cmds/nick.cpp cmds/user.cpp \ cmds/pass.cpp cmds/nick.cpp cmds/user.cpp \
cmds/join.cpp cmds/join.cpp cmds/privmsg.cpp \
HEADERS = Server/Server.hpp User/User.hpp HEADERS = Server/Server.hpp User/User.hpp

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */ /* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
/* Updated: 2026/05/15 22:00:02 by aortigos ### ########.fr */ /* Updated: 2026/05/15 22:16:34 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -116,6 +116,7 @@ Server::Server(int port, const std::string& password) :
commands_["NICK"] = &Server::nick_cmd; commands_["NICK"] = &Server::nick_cmd;
commands_["USER"] = &Server::user_cmd; commands_["USER"] = &Server::user_cmd;
commands_["JOIN"] = &Server::join_cmd; commands_["JOIN"] = &Server::join_cmd;
commands_["PRIVMSG"] = &Server::privmsg_cmd;
std::cout << "Server port: " << port_ std::cout << "Server port: " << port_
<< "\nServer Password: " << password_ << "\nServer Password: " << password_

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */ /* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
/* Updated: 2026/05/15 22:00:08 by aortigos ### ########.fr */ /* Updated: 2026/05/15 22:16:07 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -63,6 +63,7 @@ class Server
void nick_cmd(User &client, std::istringstream &ss); void nick_cmd(User &client, std::istringstream &ss);
void user_cmd(User &client, std::istringstream &ss); void user_cmd(User &client, std::istringstream &ss);
void join_cmd(User &client, std::istringstream &ss); void join_cmd(User &client, std::istringstream &ss);
void privmsg_cmd(User &client, std::istringstream &ss);
public: public:

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */ /* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/15 22:06:36 by aortigos ### ########.fr */ /* Updated: 2026/05/15 22:18:52 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -18,14 +18,13 @@ void Server::join_cmd(User &client, std::istringstream &ss)
std::string args; std::string args;
ss >> args; ss >> args;
if (!client.isRegistered()) return (client.send("You are not registered")); if (!client.isRegistered()) return (client.send("You are not registered\r\n"));
if (args.empty()) return (client.send("No name specified\r\n")); if (args.empty()) return (client.send("No name specified\r\n"));
std::map<std::string, Channel>::iterator it = channels_.find(args); std::map<std::string, Channel>::iterator it = channels_.find(args);
if (it == channels_.end()) if (it == channels_.end())
{ {
// Channel doesnt exists, create and join
channels_[args] = Channel(args); channels_[args] = Channel(args);
channels_[args].addMember(client.getFd()); channels_[args].addMember(client.getFd());
channels_[args].addOperator(client.getFd()); channels_[args].addOperator(client.getFd());
@@ -33,7 +32,9 @@ void Server::join_cmd(User &client, std::istringstream &ss)
} }
else else
{ {
if (it->second.hasMember(client.getFd())) return (client.send("You are already in this channel\r\n")); if (it->second.hasMember(client.getFd()))
it->second.addMember(client.getFd()); client.send("You are already in this channel\r\n");
else
it->second.addMember(client.getFd());
} }
} }

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */ /* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
/* Updated: 2026/05/15 22:08:10 by aortigos ### ########.fr */ /* Updated: 2026/05/15 23:04:35 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -34,7 +34,7 @@ void Server::nick_cmd(User &client, std::istringstream &ss)
ss >> args; ss >> args;
if (!client.isAuthenticated()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n")); 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 (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")); 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++) for (std::map<int, User>::iterator it = clients_.begin(); it != clients_.end(); it++)
{ {
if (it->second.getNick() == args) if (it->second.getNick() == args)

41
cmds/privmsg.cpp Normal file
View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* privmsg.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/15 23:09:57 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
// Test function, need to change all the error messages
void Server::privmsg_cmd(User &client, std::istringstream &ss)
{
std::string channel;
std::string message;
ss >> channel;
getline(ss, message);
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n"));
std::map<std::string, Channel>::iterator it = channels_.find(channel);
if (it == channels_.end())
return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n"));
if (!it->second.hasMember(client.getFd()))
return (client.send(":" SERVER_NAME " 404 " + client.getNick() + " " + channel + " :Cannot send to channel\r\n"));
if (message.empty()) return (client.send(":" SERVER_NAME " 412 " + client.getNick() + " :No text to send\r\n"));
if (message[0] == ' ')
message = message.substr(1);
std::string msg = ":" + client.getNick() + " PRIVMSG " + channel + " :" + message + "\r\n";
return (it->second.broadcast(msg, clients_, client.getFd()));
}