Merge pull request 'join cmd and channel fixes' (#30) from channel-fixes into main

Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/30
This commit is contained in:
2026-05-17 11:45:47 +00:00
3 changed files with 46 additions and 5 deletions

View File

@@ -47,6 +47,11 @@ Channel::Channel(std::string &name) : name_(name) { /* std::cout << "Channel wit
// Getters
std::string Channel::getName() const { return (this->name_); }
const std::set<int> &Channel::getMembers() const
{
return (members_);
}
// Members
void Channel::addMember(int fd) { this->members_.insert(fd); }
void Channel::removeMember(int fd) { this->members_.erase(fd); }

View File

@@ -37,6 +37,7 @@ class Channel
// Getters
std::string getName() const;
const std::set<int> &getMembers() const;
// Users

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/16 11:22:00 by aortigos ### ########.fr */
/* Updated: 2026/05/16 17:04:29 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,8 +18,8 @@ void Server::join_cmd(User &client, std::istringstream &ss)
std::string args;
ss >> args;
if (!client.isRegistered()) return (client.send("You are not registered\r\n"));
if (args.empty()) return (client.send("No name specified\r\n"));
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
if (args.empty()) return (client.send(":" SERVER_NAME " 461 " + client.getNick() + " JOIN :Not enough parameters\r\n"));
std::map<std::string, Channel>::iterator it = channels_.find(args);
@@ -29,16 +29,51 @@ void Server::join_cmd(User &client, std::istringstream &ss)
channels_[args].addMember(client.getFd());
channels_[args].addOperator(client.getFd());
client.joinChannel(args);
return (client.send("Channel created\r\n"));
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++)
{
std::map<int, User>::iterator u = clients_.find(*m);
if (u != clients_.end())
{
if (channels_[args].hasOperator(*m))
namesList += "@";
namesList += u->second.getNick() + " ";
}
}
client.send(namesList + "\r\n");
client.send(":" SERVER_NAME " 366 " + client.getNick() + " " + args + " :End of /NAMES list\r\n");
}
else
{
if (it->second.hasMember(client.getFd()))
client.send("You are already in this channel\r\n");
client.send(":" SERVER_NAME " 443 " + client.getNick() + " " + args + " :is already on channel\r\n");
else
{
client.joinChannel(it->first);
it->second.addMember(client.getFd());
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++)
{
std::map<int, User>::iterator u = clients_.find(*m);
if (u != clients_.end())
{
if (channels_[args].hasOperator(*m))
namesList += "@";
namesList += u->second.getNick() + " ";
}
}
client.send(namesList + "\r\n");
client.send(":" SERVER_NAME " 366 " + client.getNick() + " " + args + " :End of /NAMES list\r\n");
}
}
}