From 4a50dced99d53bbeced935b0421d7fcda68da87a Mon Sep 17 00:00:00 2001 From: Bettercallous Date: Wed, 1 May 2024 12:02:12 +0100 Subject: [PATCH] fixing last bugs --- Server.cpp | 98 ++++++++++++++++++++++++++++-------------------------- Server.hpp | 2 ++ 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/Server.cpp b/Server.cpp index 3156cda..858aaa5 100644 --- a/Server.cpp +++ b/Server.cpp @@ -570,54 +570,9 @@ void Server::handleClientData(int fd) client.setAuthentication(1); } } - else if (startsWith(command, "QUIT ")) + 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; - nickIt = nicknames.find(fd); - if (nickIt != nicknames.end()) { - nicknames.erase(nickIt); - } - - for (std::map::iterator it = channels.begin(); it != channels.end(); ++it) { - std::map &usersfdmap = it->second.getUserFdMap(); - for (std::map::iterator it3 = usersfdmap.begin(); it3 != usersfdmap.end(); ++it3) { - if (it3->second == fd) - usersfdmap.erase(it3); - } - } - for (std::map::iterator it1 = channels.begin(); it1 != channels.end(); ++it1) { - std::map &invitedusrmap = it1->second.invitedUserss(); - for (std::map::iterator it3 = invitedusrmap.begin(); it3 != invitedusrmap.end(); ++it3) { - if (it3->second == fd) - invitedusrmap.erase(it3); - } - } - for (std::map::iterator it2 = channels.begin(); it2 != channels.end(); ++it2) { - std::map &operatorsmap = it2->second.getOperators(); - for (std::map::iterator it3 = operatorsmap.begin(); it3 != operatorsmap.end(); ++it3) { - if (it3->second == fd) - operatorsmap.erase(it3); - } - } + cleanChannel(fd); clientCleanup(fd); } else if (startsWith(command, "PING")) @@ -1170,9 +1125,11 @@ void Server::handleClientData(int fd) else if (bytesRead == 0) { std::cout << "Client <" << fd << "> Disconnected" << std::endl; + cleanChannel(fd); clientCleanup(fd); } else if (bytesRead == -1) { std::cerr << "Error reading data from client <" << fd << ">" << std::endl; + cleanChannel(fd); clientCleanup(fd); } } @@ -1227,3 +1184,50 @@ void Server::closeFds() close(_serverSocketFd); _fds.clear(); } + +void Server::cleanChannel(int fd) { + 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; + nickIt = nicknames.find(fd); + if (nickIt != nicknames.end()) { + nicknames.erase(nickIt); + } + + for (std::map::iterator it = channels.begin(); it != channels.end(); ++it) { + std::map &usersfdmap = it->second.getUserFdMap(); + for (std::map::iterator it3 = usersfdmap.begin(); it3 != usersfdmap.end(); ++it3) { + if (it3->second == fd) + usersfdmap.erase(it3); + } + } + for (std::map::iterator it1 = channels.begin(); it1 != channels.end(); ++it1) { + std::map &invitedusrmap = it1->second.invitedUserss(); + for (std::map::iterator it3 = invitedusrmap.begin(); it3 != invitedusrmap.end(); ++it3) { + if (it3->second == fd) + invitedusrmap.erase(it3); + } + } + for (std::map::iterator it2 = channels.begin(); it2 != channels.end(); ++it2) { + std::map &operatorsmap = it2->second.getOperators(); + for (std::map::iterator it3 = operatorsmap.begin(); it3 != operatorsmap.end(); ++it3) { + if (it3->second == fd) + operatorsmap.erase(it3); + } + } +} \ No newline at end of file diff --git a/Server.hpp b/Server.hpp index e9bb355..07ed79a 100644 --- a/Server.hpp +++ b/Server.hpp @@ -75,9 +75,11 @@ class Server { void handleClientConnection(); void handleClientData(int fd); void clientCleanup(int fd); + void cleanChannel(int fd); void closeFds(); Client& getClientByFd(int fd); + }; int randomInRange(int min, int max);