Improving commands and added quit

This commit is contained in:
aortigos
2026-05-16 11:47:45 +02:00
parent 3de8940560
commit 66bd57bfde
9 changed files with 108 additions and 11 deletions

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/15 22:18:52 by aortigos ### ########.fr */
/* Updated: 2026/05/16 11:22:00 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -28,6 +28,7 @@ void Server::join_cmd(User &client, std::istringstream &ss)
channels_[args] = Channel(args);
channels_[args].addMember(client.getFd());
channels_[args].addOperator(client.getFd());
client.joinChannel(args);
return (client.send("Channel created\r\n"));
}
else
@@ -35,6 +36,9 @@ void Server::join_cmd(User &client, std::istringstream &ss)
if (it->second.hasMember(client.getFd()))
client.send("You are already in this channel\r\n");
else
{
client.joinChannel(it->first);
it->second.addMember(client.getFd());
}
}
}

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
/* Updated: 2026/05/15 23:04:35 by aortigos ### ########.fr */
/* Updated: 2026/05/16 11:12:12 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -46,9 +46,14 @@ void Server::nick_cmd(User &client, std::istringstream &ss)
if (client.isRegistered())
{
// Change nick to an already registered user
// Send message to his channels
//client.broadcast(":" + oldNick + "!~" + client.getUsername() + "@hostt NICK " + args + "\r\n");
std::string msg = ":" + oldNick + " NICK " + args + "\r\n";
const std::set<std::string> &userChannels = client.getChannels();
for (std::set<std::string>::const_iterator it = userChannels.begin(); it != userChannels.end(); it++)
{
std::map<std::string, Channel>::iterator ch = channels_.find(*it);
if (ch != channels_.end())
ch->second.broadcast(msg, clients_, -1);
}
return ;
}

View File

@@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2026/05/16 11:28:47 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -36,6 +36,6 @@ void Server::privmsg_cmd(User &client, std::istringstream &ss)
if (message[0] == ' ')
message = message.substr(1);
std::string msg = ":" + client.getNick() + " 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()));
}

42
cmds/quit.cpp Normal file
View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* quit.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/16 11:46:45 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::quit_cmd(User &client, std::istringstream &ss)
{
std::string reason;
std::getline(ss, reason);
if (!reason.empty() && reason[0] == ' ')
reason = reason.substr(1);
if (!reason.empty() && reason[0] == ':')
reason = reason.substr(1);
if (reason.empty())
reason = "Leaving";
std::string msg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost QUIT :" + reason + "\r\n";
const std::set<std::string> channels = client.getChannels();
for (std::set<std::string>::const_iterator it = channels.begin(); it != channels.end(); it++)
{
std::map<std::string, Channel>::iterator ch = channels_.find(*it);
if (ch != channels_.end())
{
ch->second.broadcast(msg, clients_, client.getFd());
ch->second.removeMember(client.getFd());
}
}
close(client.getFd());
}