From 66bd57bfde30d037f3c7db49d27ce70d2fecd856 Mon Sep 17 00:00:00 2001 From: aortigos Date: Sat, 16 May 2026 11:47:45 +0200 Subject: [PATCH] Improving commands and added quit --- Makefile | 2 +- Server/Server.cpp | 16 ++++++++++++++-- Server/Server.hpp | 5 ++++- User/User.cpp | 22 ++++++++++++++++++++++ User/User.hpp | 9 +++++++++ cmds/join.cpp | 6 +++++- cmds/nick.cpp | 13 +++++++++---- cmds/privmsg.cpp | 4 ++-- cmds/quit.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 cmds/quit.cpp diff --git a/Makefile b/Makefile index 142239d..28e2dab 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/privmsg.cpp \ + cmds/join.cpp cmds/privmsg.cpp cmds/quit.cpp \ HEADERS = Server/Server.hpp User/User.hpp diff --git a/Server/Server.cpp b/Server/Server.cpp index 7378a43..890a5c3 100644 --- a/Server/Server.cpp +++ b/Server/Server.cpp @@ -6,7 +6,7 @@ /* By: aortigos > command; + if (command.empty()) + { + client.clearBuffer(); + return ; + } + std::map::iterator it = commands_.find(command); if (it == commands_.end()) { @@ -180,7 +187,12 @@ bool Server::handleClient(User& client) 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) parseCommand(client); diff --git a/Server/Server.hpp b/Server/Server.hpp index 1cfe7ce..9cf9cd1 100644 --- a/Server/Server.hpp +++ b/Server/Server.hpp @@ -6,7 +6,7 @@ /* By: aortigos # include +# include + # include "../User/User.hpp" # include "../Channel/Channel.hpp" @@ -64,6 +66,7 @@ class Server void user_cmd(User &client, std::istringstream &ss); void join_cmd(User &client, std::istringstream &ss); void privmsg_cmd(User &client, std::istringstream &ss); + void quit_cmd(User &client, std::istringstream &ss); public: diff --git a/User/User.cpp b/User/User.cpp index 34b2ef2..8be3a76 100644 --- a/User/User.cpp +++ b/User/User.cpp @@ -43,6 +43,7 @@ User& User::operator=(const User &other) buffer = other.buffer; authenticated = other.authenticated; registered = other.registered; + channels_ = other.channels_; } // std::cout << "User copy assignment operator called" << std::endl; return (*this); @@ -59,6 +60,27 @@ void User::send(const std::string &msg) ::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 &User::getChannels() const +{ + return (channels_); +} + // Getters int User::getFd() const { return (this->fd); } diff --git a/User/User.hpp b/User/User.hpp index 73dedc6..92a8214 100644 --- a/User/User.hpp +++ b/User/User.hpp @@ -16,6 +16,7 @@ # include # include +# include class User { @@ -28,6 +29,8 @@ class User bool authenticated; bool registered; + std::set channels_; + public: User(); User(int fd); @@ -37,6 +40,12 @@ class User 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 &getChannels() const; + // Getters int getFd() const; diff --git a/cmds/join.cpp b/cmds/join.cpp index 5e0b822..1b491a5 100644 --- a/cmds/join.cpp +++ b/cmds/join.cpp @@ -6,7 +6,7 @@ /* By: aortigos second.hasMember(client.getFd())) client.send("You are already in this channel\r\n"); else + { + client.joinChannel(it->first); it->second.addMember(client.getFd()); + } } } diff --git a/cmds/nick.cpp b/cmds/nick.cpp index 4b7a794..e87f8ea 100644 --- a/cmds/nick.cpp +++ b/cmds/nick.cpp @@ -6,7 +6,7 @@ /* By: aortigos &userChannels = client.getChannels(); + for (std::set::const_iterator it = userChannels.begin(); it != userChannels.end(); it++) + { + std::map::iterator ch = channels_.find(*it); + if (ch != channels_.end()) + ch->second.broadcast(msg, clients_, -1); + } return ; } diff --git a/cmds/privmsg.cpp b/cmds/privmsg.cpp index d57d3a7..4285738 100644 --- a/cmds/privmsg.cpp +++ b/cmds/privmsg.cpp @@ -6,7 +6,7 @@ /* By: aortigos second.broadcast(msg, clients_, client.getFd())); } diff --git a/cmds/quit.cpp b/cmds/quit.cpp new file mode 100644 index 0000000..2e43e50 --- /dev/null +++ b/cmds/quit.cpp @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* quit.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos channels = client.getChannels(); + for (std::set::const_iterator it = channels.begin(); it != channels.end(); it++) + { + std::map::iterator ch = channels_.find(*it); + if (ch != channels_.end()) + { + ch->second.broadcast(msg, clients_, client.getFd()); + ch->second.removeMember(client.getFd()); + } + } + close(client.getFd()); +} -- 2.49.1