From edb23b19616dc83b8d1e3847b21e0f13de1a1fe4 Mon Sep 17 00:00:00 2001 From: alexLinux Date: Sun, 21 Apr 2024 17:43:55 +0200 Subject: [PATCH 1/4] fix bug QUIT without register --- src/commands/QuitCommand.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/commands/QuitCommand.cpp b/src/commands/QuitCommand.cpp index 59d086e..e6d3ed1 100644 --- a/src/commands/QuitCommand.cpp +++ b/src/commands/QuitCommand.cpp @@ -22,6 +22,12 @@ void QuitCommand::execute(int clientFd) { Server &server = Server::getInstance(); User &user = server.getUserByFd(clientFd); + if (!user.isRegistered()) { + server.sendMessage(clientFd, QUIT_MSG(std::string(""), std::string(""),std::string(""), + _message.empty() ? std::string("") : _message)); + server.handleClientDisconnection(clientFd); + } + std::string nickname = user.getNickname(); std::vector &channels = server.getChannels(); From d57b470f9fa0f20d443dddabe2b5bb3fe99d6ab6 Mon Sep 17 00:00:00 2001 From: alexLinux Date: Mon, 22 Apr 2024 14:38:56 +0200 Subject: [PATCH 2/4] fix Bug register but not in channel. --- src/commands/QuitCommand.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/commands/QuitCommand.cpp b/src/commands/QuitCommand.cpp index e6d3ed1..24cae5c 100644 --- a/src/commands/QuitCommand.cpp +++ b/src/commands/QuitCommand.cpp @@ -22,12 +22,6 @@ void QuitCommand::execute(int clientFd) { Server &server = Server::getInstance(); User &user = server.getUserByFd(clientFd); - if (!user.isRegistered()) { - server.sendMessage(clientFd, QUIT_MSG(std::string(""), std::string(""),std::string(""), - _message.empty() ? std::string("") : _message)); - server.handleClientDisconnection(clientFd); - } - std::string nickname = user.getNickname(); std::vector &channels = server.getChannels(); @@ -43,10 +37,17 @@ void QuitCommand::execute(int clientFd) { usersChannel.clear(); } + if (allUsers.empty() && user.isRegistered()) { + server.sendMessage(clientFd, + QUIT_MSG(nickname, user.getUsername(), user.getHostname(), + _message.empty() ? nickname : _message)); + } + for (it = allUsers.begin(); it != allUsers.end(); it++) { server.sendMessage(it->getFd(), QUIT_MSG(nickname, user.getUsername(), user.getHostname(), _message.empty() ? nickname : _message)); } + server.handleClientDisconnection(clientFd); } \ No newline at end of file From e21b5e33be49719056dd5b88997233d7af58bc0f Mon Sep 17 00:00:00 2001 From: alexLinux Date: Tue, 23 Apr 2024 11:33:34 +0200 Subject: [PATCH 3/4] fix clean code --- src/commands/QuitCommand.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/commands/QuitCommand.cpp b/src/commands/QuitCommand.cpp index 24cae5c..02f8bf3 100644 --- a/src/commands/QuitCommand.cpp +++ b/src/commands/QuitCommand.cpp @@ -37,11 +37,8 @@ void QuitCommand::execute(int clientFd) { usersChannel.clear(); } - if (allUsers.empty() && user.isRegistered()) { - server.sendMessage(clientFd, - QUIT_MSG(nickname, user.getUsername(), user.getHostname(), - _message.empty() ? nickname : _message)); - } + if (allUsers.empty() && user.isRegistered()) + allUsers.insert(user); for (it = allUsers.begin(); it != allUsers.end(); it++) { server.sendMessage(it->getFd(), From 4222685e85bac74683c51f3b8362053dc5889063 Mon Sep 17 00:00:00 2001 From: alexLinux Date: Tue, 23 Apr 2024 12:21:12 +0200 Subject: [PATCH 4/4] fix improving the code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit He probado con mas de un usuario y funciona correctamente también he buscado info del set, esta curioso como funciona. Se ordena de forma ascendente, lo parámetros de dentro de un set no se pueden modificar solo insertar o eliminar y en nuestro caso hacerlo único no hay duplicados. --- src/commands/QuitCommand.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/commands/QuitCommand.cpp b/src/commands/QuitCommand.cpp index 02f8bf3..e3d8eda 100644 --- a/src/commands/QuitCommand.cpp +++ b/src/commands/QuitCommand.cpp @@ -27,6 +27,9 @@ void QuitCommand::execute(int clientFd) { std::set allUsers; std::set::iterator it; + + if (user.isRegistered()) + allUsers.insert(user); for (size_t i = 0; i < channels.size(); i++) { if (!channels[i].isUserInChannel(nickname)) continue; @@ -37,14 +40,10 @@ void QuitCommand::execute(int clientFd) { usersChannel.clear(); } - if (allUsers.empty() && user.isRegistered()) - allUsers.insert(user); - for (it = allUsers.begin(); it != allUsers.end(); it++) { server.sendMessage(it->getFd(), QUIT_MSG(nickname, user.getUsername(), user.getHostname(), _message.empty() ? nickname : _message)); } - server.handleClientDisconnection(clientFd); } \ No newline at end of file