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

@@ -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/privmsg.cpp \ cmds/join.cpp cmds/privmsg.cpp cmds/quit.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:16:34 by aortigos ### ########.fr */ /* Updated: 2026/05/16 11:31:25 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_["QUIT"] = &Server::quit_cmd;
commands_["PRIVMSG"] = &Server::privmsg_cmd; commands_["PRIVMSG"] = &Server::privmsg_cmd;
std::cout << "Server port: " << port_ std::cout << "Server port: " << port_
@@ -151,6 +152,12 @@ void Server::parseCommand(User& client)
args >> command; args >> command;
if (command.empty())
{
client.clearBuffer();
return ;
}
std::map<std::string, void (Server::*)(User&, std::istringstream&)>::iterator it = commands_.find(command); std::map<std::string, void (Server::*)(User&, std::istringstream&)>::iterator it = commands_.find(command);
if (it == commands_.end()) if (it == commands_.end())
{ {
@@ -180,7 +187,12 @@ bool Server::handleClient(User& client)
return true; return true;
} }
client.appendBuffer(std::string(buffer, recv_amount)); std::string data(buffer, recv_amount);
size_t pos = data.find('\r');
if (pos != std::string::npos)
data.erase(pos, 1);
client.appendBuffer(data);
if (client.getBuffer().find('\n') != std::string::npos) if (client.getBuffer().find('\n') != std::string::npos)
parseCommand(client); parseCommand(client);

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:16:07 by aortigos ### ########.fr */ /* Updated: 2026/05/16 11:45:11 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -25,6 +25,8 @@
# include <cctype> # include <cctype>
# include <sstream> # include <sstream>
# include <unistd.h>
# include "../User/User.hpp" # include "../User/User.hpp"
# include "../Channel/Channel.hpp" # include "../Channel/Channel.hpp"
@@ -64,6 +66,7 @@ class Server
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); void privmsg_cmd(User &client, std::istringstream &ss);
void quit_cmd(User &client, std::istringstream &ss);
public: public:

View File

@@ -43,6 +43,7 @@ User& User::operator=(const User &other)
buffer = other.buffer; buffer = other.buffer;
authenticated = other.authenticated; authenticated = other.authenticated;
registered = other.registered; registered = other.registered;
channels_ = other.channels_;
} }
// std::cout << "User copy assignment operator called" << std::endl; // std::cout << "User copy assignment operator called" << std::endl;
return (*this); return (*this);
@@ -59,6 +60,27 @@ void User::send(const std::string &msg)
::send(fd, msg.c_str(), msg.size(), 0); ::send(fd, msg.c_str(), msg.size(), 0);
} }
// Channels
void User::joinChannel(const std::string &channel)
{
channels_.insert(channel);
}
void User::leaveChannel(const std::string &channel)
{
channels_.erase(channel);
}
bool User::isInChannel(const std::string &channel) const
{
return (channels_.count(channel) > 0);
}
const std::set<std::string> &User::getChannels() const
{
return (channels_);
}
// Getters // Getters
int User::getFd() const { return (this->fd); } int User::getFd() const { return (this->fd); }

View File

@@ -16,6 +16,7 @@
# include <iostream> # include <iostream>
# include <sys/socket.h> # include <sys/socket.h>
# include <set>
class User class User
{ {
@@ -28,6 +29,8 @@ class User
bool authenticated; bool authenticated;
bool registered; bool registered;
std::set<std::string> channels_;
public: public:
User(); User();
User(int fd); User(int fd);
@@ -37,6 +40,12 @@ class User
void send(const std::string &msg); void send(const std::string &msg);
// Channels
void joinChannel(const std::string &channel);
void leaveChannel( const std::string &channel);
bool isInChannel(const std::string &channel) const;
const std::set<std::string> &getChannels() const;
// Getters // Getters
int getFd() const; int getFd() const;

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: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] = Channel(args);
channels_[args].addMember(client.getFd()); channels_[args].addMember(client.getFd());
channels_[args].addOperator(client.getFd()); channels_[args].addOperator(client.getFd());
client.joinChannel(args);
return (client.send("Channel created\r\n")); return (client.send("Channel created\r\n"));
} }
else else
@@ -35,6 +36,9 @@ void Server::join_cmd(User &client, std::istringstream &ss)
if (it->second.hasMember(client.getFd())) if (it->second.hasMember(client.getFd()))
client.send("You are already in this channel\r\n"); client.send("You are already in this channel\r\n");
else else
{
client.joinChannel(it->first);
it->second.addMember(client.getFd()); 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 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()) if (client.isRegistered())
{ {
// Change nick to an already registered user std::string msg = ":" + oldNick + " NICK " + args + "\r\n";
// Send message to his channels const std::set<std::string> &userChannels = client.getChannels();
//client.broadcast(":" + oldNick + "!~" + client.getUsername() + "@hostt NICK " + args + "\r\n"); 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 ; return ;
} }

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 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] == ' ') if (message[0] == ' ')
message = message.substr(1); 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())); 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());
}