From 91e8f5c3f2df038c1e30c0c0d363bf8aee5a2dcc Mon Sep 17 00:00:00 2001 From: bettercallous Date: Tue, 30 Apr 2024 16:26:19 +0100 Subject: [PATCH] fixed bugs --- Client.cpp | 7 ++++++- Client.hpp | 1 + Server.cpp | 20 ++++++++++++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Client.cpp b/Client.cpp index 125975d..e864cb8 100644 --- a/Client.cpp +++ b/Client.cpp @@ -54,9 +54,14 @@ const std::string& Client::getCommand() const { } void Client::clearCommand() { + command.clear(); command = ""; } void Client::appendCommand(std::string str) { - this->command += str; + command += str; +} + +void Client::setCommand(std::string cmd) { + command = cmd; } \ No newline at end of file diff --git a/Client.hpp b/Client.hpp index 121363b..3e2abfa 100644 --- a/Client.hpp +++ b/Client.hpp @@ -38,6 +38,7 @@ class Client { void clearCommand(); void appendCommand(std::string str); + void setCommand(std::string cmd); }; #endif \ No newline at end of file diff --git a/Server.cpp b/Server.cpp index dbf0b5e..b54d354 100644 --- a/Server.cpp +++ b/Server.cpp @@ -529,10 +529,11 @@ void Server::handleClientData(int fd) client.appendCommand(buffer); - size_t newlinePos = client.getCommand().find("\n"); + size_t newlinePos = client.getCommand().find_first_of("\r\n"); if (newlinePos != std::string::npos) { // Extract the complete message up to the newline character std::string command = client.getCommand().substr(0, newlinePos); + client.setCommand(command); std::cout << "Received data from client " << fd << ": " << command << std::endl; int auth = getClientByFd(fd).getAuthentication(); @@ -556,6 +557,7 @@ void Server::handleClientData(int fd) { std::string errorMessage = "Error: Password cannot be empty\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); + client.clearCommand(); return; } std::string passwordoftheserver = getPassowrd(); @@ -620,6 +622,7 @@ void Server::handleClientData(int fd) { std::string errorMessage = "Error: Command requires only 1 parameter\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); + client.clearCommand(); return; } @@ -663,6 +666,7 @@ void Server::handleClientData(int fd) { std::string errorMessage = "Error: Command requires at least four parameters\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); + client.clearCommand(); return; } if (dontputthesameusername(username) == true) @@ -713,6 +717,7 @@ void Server::handleClientData(int fd) { std::string errorMessage = ":server.host NOTICE " + nick + " :Error: Channel start with #\r\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); + client.clearCommand(); return; } @@ -793,6 +798,7 @@ void Server::handleClientData(int fd) { std::string errorMessage = "Error: You Just missing an argument(5)\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); + client.clearCommand(); return; } message = trim(message); @@ -828,6 +834,7 @@ void Server::handleClientData(int fd) { std::string errorMessage = "Error: You Just missing an argument(4)\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); + client.clearCommand(); return; } std::getline(iss, reason); @@ -872,6 +879,7 @@ void Server::handleClientData(int fd) { std::string errorMessage = "Error: You Just missing an argument(3)\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); + client.clearCommand(); return; } channelName = channelName.substr(1); @@ -904,6 +912,7 @@ void Server::handleClientData(int fd) { std::string errorMessage = "Error: You Just missing an argument(2)\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); + client.clearCommand(); return; } channelName = trim(channelName); @@ -931,6 +940,7 @@ void Server::handleClientData(int fd) { std::string errorMessage = "Error: Command take 3 parameters\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); + client.clearCommand(); return; } start = trim(start); @@ -967,6 +977,7 @@ void Server::handleClientData(int fd) { std::string errorMessage = ":server.host NOTICE " + nick + " :Error: Channel start with #\r\n"; send(fd, errorMessage.c_str(), errorMessage.length(), 0); + client.clearCommand(); return; } @@ -1136,12 +1147,13 @@ void Server::handleClientData(int fd) } } } -//**************** STOOOOOOP HERE TOP G ... - client.clearCommand(); +//**************** STOOOOOOP HERE TOP G ... + // command.clear(); } + client.clearCommand(); } - if (bytesRead == 0) { + else if (bytesRead == 0) { std::cout << "Client <" << fd << "> Disconnected" << std::endl; clientCleanup(fd); } else if (bytesRead == -1) {