From 5338af5e4f16fce19bdd6aee922c0e08ee798876 Mon Sep 17 00:00:00 2001 From: Bettercallous Date: Sat, 27 Apr 2024 21:51:21 +0100 Subject: [PATCH] fixed client authentication bugs --- .vscode/settings.json | 5 ----- Client.cpp | 14 +++++++++++--- Client.hpp | 5 ++++- Makefile | 2 +- Server.cpp | 31 +++++++++++++++++-------------- Server.hpp | 5 +++-- 6 files changed, 36 insertions(+), 26 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index ce48fa6..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files.associations": { - "iosfwd": "cpp" - } -} \ No newline at end of file diff --git a/Client.cpp b/Client.cpp index 914a3ff..68ffc86 100644 --- a/Client.cpp +++ b/Client.cpp @@ -1,8 +1,8 @@ #include "Client.hpp" -Client::Client() : _isRegistered(false) {} +Client::Client() {} -Client::Client(int fd) : _fd(fd) {} +Client::Client(int fd) : _fd(fd), _authentication(0) {} Client::~Client() {} @@ -41,4 +41,12 @@ std::string Client::getUser() const { void Client::setUser(const std::string& newUser) { user = newUser; -} \ No newline at end of file +} + +int Client::getAuthentication() const { + return _authentication; +} + +void Client::setAuthentication(int auth) { + _authentication = auth; +} diff --git a/Client.hpp b/Client.hpp index 068565f..a427fb5 100644 --- a/Client.hpp +++ b/Client.hpp @@ -10,7 +10,7 @@ class Client { std::string name; std::string nick; std::string user; - bool _isRegistered; + int _authentication; public: Client(); Client(int fd); @@ -30,6 +30,9 @@ class Client { // Getter and setter for user std::string getUser() const; void setUser(const std::string& newUser); + + int getAuthentication() const; + void setAuthentication(int auth); }; diff --git a/Makefile b/Makefile index bf0747e..d21d676 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ OBJ = ${SRC:.cpp=.o} CXX = c++ -CXXFLAGS = -std=c++98 -fsanitize=address -g3 #-Wall -Wextra -Werror +CXXFLAGS = -std=c++98 -fsanitize=address -g3 -Wall -Wextra -Werror all : $(NAME) diff --git a/Server.cpp b/Server.cpp index 3a3b4fc..f857467 100644 --- a/Server.cpp +++ b/Server.cpp @@ -1,6 +1,5 @@ #include "Server.hpp" -int a = 0; int opperatorfd = 0; int issettop = 0; int isinveted = 0; @@ -347,7 +346,7 @@ int Server::findUserFdforkickregulars(const std::string& username) { // brodcasting msg to all nicks in the channel -void Server::broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg, int fd) { +void Server::broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg) { std::map::iterator it = channels.find(channel); if (it == channels.end()) { std::cerr << "Channel " << channel << " does not exist" << std::endl; @@ -559,9 +558,10 @@ void Server::handleClientData(int fd) } else { command.append(buffer, bytesRead - 1); std::cout << "Received data from client " << fd << ": " << command << std::endl; + int auth = getClientByFd(fd).getAuthentication(); //******************* FROM THERE IM STARTING TOP GGG ************ . - if (startsWith(command, "pass") || startsWith(command, "PASS")) + if ((startsWith(command, "pass") || startsWith(command, "PASS")) && auth == 0) { std::string cmd, password; std::istringstream iss(command); @@ -576,7 +576,7 @@ void Server::handleClientData(int fd) else { std::string confirmation = "Please Enter Your Nickname : \n"; send(fd, confirmation.c_str(), confirmation.length(), 0); - a = 1; + getClientByFd(fd).setAuthentication(1); } } @@ -589,7 +589,7 @@ void Server::handleClientData(int fd) std::cout << "ping was sent" << std::endl; } - else if ((startsWith(command, "nick") || startsWith(command, "NICK")) && a == 1) + else if ((startsWith(command, "nick") || startsWith(command, "NICK")) && auth == 1) { std::string cmd, nick; std::istringstream iss(command); @@ -620,13 +620,11 @@ void Server::handleClientData(int fd) } std::string confirmation = "Please Enter Your Username : \n"; send(fd, confirmation.c_str(), confirmation.length(), 0); - - a = 2; - + getClientByFd(fd).setAuthentication(2); } } - else if ((startsWith(command, "user") || startsWith(command, "USER")) && a == 2) + else if ((startsWith(command, "user") || startsWith(command, "USER")) && auth == 2) { std::istringstream iss(command); @@ -662,7 +660,6 @@ void Server::handleClientData(int fd) break; } } - a = 0; std::string one = ":irc.l9oroch 001 " + nickname + " :Welcome to the TopG Network, " + nickname + '\n'; std::string two = ":irc.l9oroch 002 " + nickname + " :Your host is TopG, running version 4.5" + '\n'; @@ -789,7 +786,7 @@ void Server::handleClientData(int fd) break; } } - broadcastMessage(recipient, niiick, message, fd); + broadcastMessage(recipient, niiick, message); } else { @@ -947,7 +944,6 @@ void Server::handleClientData(int fd) else if (startsWith(command, "MODE ") || startsWith(command, "mode ")) { std::string channelName, mode , nick; - int limit; std::istringstream iss(command.substr(5)); iss >> channelName >> mode >> nick; @@ -965,8 +961,6 @@ void Server::handleClientData(int fd) // if (mode == "+l") // what is this ? // nick = trim(nick); - std::map::iterator it = channels.find(channelName); - std::cout << "this is the mode : " << mode << std::endl; if (mode == "+o") @@ -1164,6 +1158,15 @@ void Server::clientCleanup(int fd) { } } +Client& Server::getClientByFd(int fd) { + size_t i = 0; + for (; i < _clients.size(); ++i) { + if (_clients[i].getFd() == fd) + break; + } + return _clients[i]; +} + int randomInRange(int min, int max) { // Check for invalid range if (min > max) { diff --git a/Server.hpp b/Server.hpp index 5676f8a..342fb89 100644 --- a/Server.hpp +++ b/Server.hpp @@ -64,7 +64,7 @@ class Server { void createChannel(const std::string& channel, const std::string& nickname, int fd); void handlePrivateMessage(int senderFd, const std::string& recipient, const std::string& message); void handleInvitation(int senderFd, const std::string& recipient, std::string channelName); - void broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg, int fd); + void broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg); void smallbroadcastMessagefortheckick(std::string nicknamesender , const std::string& channelname, const std::string& usertokick, const std::string& reason); void smallbroadcastMessageforjoin(std::string nicknamesender , const std::string& channelname); void smallbroadcastMessageforTopic(std::string nicknamesender, const std::string& channelname, std::string topic); @@ -89,7 +89,8 @@ class Server { void handleClientData(int fd); void clientCleanup(int fd); void closeFds(); - + + Client& getClientByFd(int fd); }; int randomInRange(int min, int max);