privmsg-to-users #34

Merged
aortigos merged 2 commits from privmsg-to-users into main 2026-05-25 07:26:37 +00:00

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/16 11:28:47 by aortigos ### ########.fr */ /* Updated: 2026/05/25 09:25:49 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -26,16 +26,28 @@ void Server::privmsg_cmd(User &client, std::istringstream &ss)
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n")); if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\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);
if (channel[0] == '#')
{
std::map<std::string, Channel>::iterator it = channels_.find(channel); std::map<std::string, Channel>::iterator it = channels_.find(channel);
if (it == channels_.end()) if (it == channels_.end())
return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n")); return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n"));
if (!it->second.hasMember(client.getFd())) if (!it->second.hasMember(client.getFd()))
return (client.send(":" SERVER_NAME " 404 " + client.getNick() + " " + channel + " :Cannot send to channel\r\n")); 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() + "!" + client.getUsername() + "@localhost PRIVMSG " + channel + " :" + message + "\r\n"; std::string msg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost PRIVMSG " + channel + " :" + message + "\r\n";
return (it->second.broadcast(msg, clients_, client.getFd())); it->second.broadcast(msg, clients_, client.getFd());
} else {
for(std::map<int, User>::iterator it = clients_.begin(); it != clients_.end(); it++)
{
if (it->second.getNick() == channel)
return (it->second.send(":" + client.getNick() + "!" + client.getUsername() + "@localhost PRIVMSG " + channel + " :" + message + "\r\n"));
}
client.send(":" SERVER_NAME " 401 " + client.getNick() + " " + channel + " :No such nick\r\n");
}
} }