From a21bac6e47281c46979cb5ccb3bfee579d001695 Mon Sep 17 00:00:00 2001 From: aortigos Date: Fri, 15 May 2026 23:11:02 +0200 Subject: [PATCH] now broadcast send message to every member of channel, excluding the author of message --- Channel/Channel.cpp | 13 ++++++++++--- Channel/Channel.hpp | 2 +- Makefile | 2 +- Server/Server.cpp | 3 ++- Server/Server.hpp | 3 ++- cmds/join.cpp | 11 ++++++----- cmds/nick.cpp | 4 ++-- cmds/privmsg.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 cmds/privmsg.cpp diff --git a/Channel/Channel.cpp b/Channel/Channel.cpp index 104fec5..8b3db45 100644 --- a/Channel/Channel.cpp +++ b/Channel/Channel.cpp @@ -57,7 +57,14 @@ 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() +void Channel::broadcast(const std::string &msg, std::map &clients, int excludedFd) { - return ; -} \ No newline at end of file + for (std::set::iterator member = members_.begin(); member != members_.end(); member++) + { + if ((*member) == excludedFd) continue; + std::map::iterator user = clients.find(*member); + + if (user != clients.end()) + user->second.send(msg); + } +} diff --git a/Channel/Channel.hpp b/Channel/Channel.hpp index 60202b3..f5abd85 100644 --- a/Channel/Channel.hpp +++ b/Channel/Channel.hpp @@ -49,7 +49,7 @@ class Channel void removeOperator(int fd); bool hasOperator(int fd) const; - void broadcast(); + void broadcast(const std::string &msg, std::map &clients, int excludedFd); }; diff --git a/Makefile b/Makefile index 5b956c1..142239d 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ NAME = ircserv 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/join.cpp cmds/privmsg.cpp \ HEADERS = Server/Server.hpp User/User.hpp diff --git a/Server/Server.cpp b/Server/Server.cpp index 865b4c1..7378a43 100644 --- a/Server/Server.cpp +++ b/Server/Server.cpp @@ -6,7 +6,7 @@ /* By: aortigos > 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")); std::map::iterator it = channels_.find(args); if (it == channels_.end()) { - // Channel doesnt exists, create and join channels_[args] = Channel(args); channels_[args].addMember(client.getFd()); channels_[args].addOperator(client.getFd()); @@ -33,7 +32,9 @@ void Server::join_cmd(User &client, std::istringstream &ss) } else { - if (it->second.hasMember(client.getFd())) return (client.send("You are already in this channel\r\n")); - it->second.addMember(client.getFd()); + if (it->second.hasMember(client.getFd())) + client.send("You are already in this channel\r\n"); + else + it->second.addMember(client.getFd()); } } diff --git a/cmds/nick.cpp b/cmds/nick.cpp index d0a0631..4b7a794 100644 --- a/cmds/nick.cpp +++ b/cmds/nick.cpp @@ -6,7 +6,7 @@ /* By: aortigos > args; 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 (!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::iterator it = clients_.begin(); it != clients_.end(); it++) { if (it->second.getNick() == args) diff --git a/cmds/privmsg.cpp b/cmds/privmsg.cpp new file mode 100644 index 0000000..d57d3a7 --- /dev/null +++ b/cmds/privmsg.cpp @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* privmsg.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos > channel; + getline(ss, message); + + if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n")); + + std::map::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())); +} -- 2.49.1