From e9c1d60f464e62c47187bc8fa6ee82caea1d803a Mon Sep 17 00:00:00 2001 From: mochaoui Date: Sun, 28 Apr 2024 15:54:08 +0100 Subject: [PATCH] fix fix fix --- .vscode/settings.json | 56 +++++++++++++++++++++++++++++++++++++++++++ Server.cpp | 30 ++++++++++++++++++++++- channel.cpp | 11 +++++++++ channel.hpp | 2 ++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ee39ead --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,56 @@ +{ + "files.associations": { + "iostream": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "map": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/Server.cpp b/Server.cpp index f857467..02a0193 100644 --- a/Server.cpp +++ b/Server.cpp @@ -560,7 +560,8 @@ void Server::handleClientData(int fd) std::cout << "Received data from client " << fd << ": " << command << std::endl; int auth = getClientByFd(fd).getAuthentication(); -//******************* FROM THERE IM STARTING TOP GGG ************ . +//******************* FROM THERE IM STARTING TOP GGG ************ + if ((startsWith(command, "pass") || startsWith(command, "PASS")) && auth == 0) { std::string cmd, password; @@ -579,7 +580,34 @@ void Server::handleClientData(int fd) getClientByFd(fd).setAuthentication(1); } } + else if (startsWith(command, "QUIT")) + { + // Iterate over channels + std::map::iterator it; + for (it = channels.begin(); it != channels.end(); ++it) + { + Channel& channel = it->second; + std::string nickname = channel.getNickname(fd); + // Check if the user is part of the channel + if (channel.isUserInChannel(nickname)) + { + // Mark the user as disconnected in the channel + channel.ejectUserfromusers(fd); + } + } + std::map::iterator userIt; + userIt = usernames.find(fd); + if (userIt != usernames.end()) { + usernames.erase(userIt); + } + std::map::iterator nickIt; + userIt = nicknames.find(fd); + if (userIt != nicknames.end()) { + nicknames.erase(userIt); + } + clientCleanup(fd); + } else if (startsWith(command, "PING")) { std::istringstream iss(command); diff --git a/channel.cpp b/channel.cpp index 26fb056..f87f0d2 100644 --- a/channel.cpp +++ b/channel.cpp @@ -80,6 +80,17 @@ int Channel::getUserFd(const std::string& username) const { return -1; // Return -1 if username not found } + +bool Channel::isUserInChannel(const std::string& nickname) const { + // Search for the nickname in the userMap + std::map::const_iterator it = userFdMap.find(nickname); + // If the nickname is found and the user is connected, return true + if (it != userFdMap.end() && it->second) { + return true; + } + // Otherwise, return false + return false; +} // Get all clients' usernames in the channel std::vector Channel::getClients() const { std::vector clients; diff --git a/channel.hpp b/channel.hpp index 131249a..669c659 100644 --- a/channel.hpp +++ b/channel.hpp @@ -32,6 +32,7 @@ private: int opperatorfd; bool issettop; bool isinveted; + bool channelconnect; public: @@ -62,6 +63,7 @@ public: void addClientinveted(const std::string& client, int fd); void addOperator(const std::string& operatorName, int fd); int getUserFd(const std::string& username) const; + bool isUserInChannel(const std::string& nickname) const; std::vector getClients() const; std::string getNickname(int fd) const; bool isOperator(int fd);