diff --git a/include/server.hpp b/include/server.hpp index c07fe61..9d0a01a 100644 --- a/include/server.hpp +++ b/include/server.hpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */ -/* Updated: 2025/06/14 23:08:11 by sben-tay ### ########.fr */ +/* Updated: 2025/06/16 18:01:10 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,4 +41,5 @@ public: std::string getPassword() const; void showInfo() const; void printUsers() const; + void disconnectClient(int fd); }; diff --git a/include/user.hpp b/include/user.hpp index 13cf52b..ff77dc9 100644 --- a/include/user.hpp +++ b/include/user.hpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */ -/* Updated: 2025/06/08 21:59:53 by sben-tay ### ########.fr */ +/* Updated: 2025/06/16 18:36:01 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,9 @@ class User std::string _username; bool _hasNick; bool _hasUser; - bool _hasPass; // to check if the user has sent a PASS command + bool _hasPass; + bool _passReceived; + bool _passIsValid; public: User(short unsigned fd); @@ -46,6 +48,8 @@ class User void setHasNick(bool value); void setHasUser(bool value); void setHasPass(bool value); + void setPassReceived(bool value); + void setPassIsValid(bool value); bool getHasPass() const; std::string getNickname() const; diff --git a/sources/commands/nick.cpp b/sources/commands/nick.cpp index f125b4b..6182a58 100644 --- a/sources/commands/nick.cpp +++ b/sources/commands/nick.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/12 17:52:12 by sben-tay ### ########.fr */ +/* Updated: 2025/06/16 18:30:15 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,9 +21,8 @@ e_code cmd::Nick::checkArgs() { return ERR_NONICKNAMEGIVEN; } User* existing = searchList(_users, _args[1]); // à adapter si besoin - if (existing != NULL) { - - WARNING_MSG("Nick already in use: " << _args[1]); + if (existing != NULL) { + _sender->appendToWriteBuffer(":" + _sender->getPrefix() + " 433 * " + _args[1] + " :Nickname is already in use\r\n"); return ERR_NICKNAMEINUSE; } return _PARSING_OK; diff --git a/sources/commands/pass.cpp b/sources/commands/pass.cpp index 7777167..f16983d 100644 --- a/sources/commands/pass.cpp +++ b/sources/commands/pass.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/12 17:52:35 by sben-tay ### ########.fr */ +/* Updated: 2025/06/16 17:33:20 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,17 +29,22 @@ e_code Pass::checkArgs() { * @note To connect a user with the password */ -void Pass::execute() { - if (checkArgs() != _PARSING_OK) { - ERROR_MSG("Invalid arguments for Pass command (see warning message)"); - DEBUG_MSG("skill issues"); - return; - } - DEBUG_MSG("mais pas trop skill issues"); - if (_args.at(1) != _server->getPassword()) { - ERROR_MSG("The password is incorrect"); +void cmd::Pass::execute() { + if (_args.size() < 2) { + _sender->appendToWriteBuffer(":localhost 461 PASS :Not enough parameters\r\n"); return; } _sender->setHasPass(true); + _sender->setPassReceived(true); + if (_args[1] == _server->getPassword()) + { + DEBUG_MSG("PASSISVALID"); + _sender->setPassIsValid(true); + } + else{ + DEBUG_MSG("PASSISINVALID"); + _sender->setPassIsValid(false); + } + _sender->checkRegistration(); } diff --git a/sources/core/Server.cpp b/sources/core/Server.cpp index c465308..3f5426e 100644 --- a/sources/core/Server.cpp +++ b/sources/core/Server.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */ -/* Updated: 2025/06/14 23:16:54 by sben-tay ### ########.fr */ +/* Updated: 2025/06/16 18:35:26 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -95,8 +95,7 @@ void Server::start() // Handle disconnected clients for (size_t i = 0; i < disconnected.size(); ++i) { - delete _users[disconnected[i]]; - _users.erase(disconnected[i]); + disconnectClient(disconnected[i]); } for (size_t i = 0; i < readyClients.size(); ++i) { @@ -132,7 +131,6 @@ void Server::start() _users[fd]->clearWriteBuffer(); } } - } std::cout << "Poll loop finished" << std::endl; } @@ -203,4 +201,13 @@ std::vector splitLines(const std::string& input) { lines.push_back(line); } return lines; +} + +void Server::disconnectClient(int fd) { + _pollManager.removeClient(fd); + close(fd); + if (_users.count(fd)) { + delete _users[fd]; + _users.erase(fd); + } } \ No newline at end of file diff --git a/sources/user/user.cpp b/sources/user/user.cpp index bb8fc5f..3256bb7 100644 --- a/sources/user/user.cpp +++ b/sources/user/user.cpp @@ -6,15 +6,16 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */ -/* Updated: 2025/06/14 23:27:43 by sben-tay ### ########.fr */ +/* Updated: 2025/06/16 18:36:36 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ #include "core.hpp" +#include // Constructor User::User(short unsigned int fd) : _fd(fd), _registered(false), _hasNick(false), _hasUser(false), \ - _hasPass(false) {} + _hasPass(false), _passReceived(false), _passIsValid(false) {} /** * @brief Getter for the fd @@ -35,7 +36,6 @@ std::string User::getName() const { void User::setUsername(const std::string &username) { _username = username; _hasUser = true; - checkRegistration(); } // Setter for nickname (with basic checks) @@ -51,7 +51,6 @@ void User::setNickname(const std::string &nickname) { } else { _nickname = nickname; _hasNick = true; - checkRegistration(); } } @@ -83,14 +82,24 @@ void User::appendToWriteBuffer(const std::string &data) // Check registration void User::checkRegistration() { - if (!_registered && _hasNick && _hasUser && _hasPass) + if (_registered) + return; + if (_hasNick && _hasUser && _passReceived && _passIsValid) { - std::cout << "JE SUIS ENREGISTRE" << std::endl; _registered = true; - std::string welcome = ":localhost 001 " + _nickname + - " :Welcome to the IRC server " + getPrefix() + "\r\n"; - - appendToWriteBuffer(welcome); + std::string msg = ":localhost 001 " + _nickname + + " :Welcome to the IRC server " + getPrefix() + "\r\n"; + appendToWriteBuffer(msg); + } + else if (_hasNick && _hasUser && _passReceived && !_passIsValid) + { + std::string msg = ":localhost 464 * :Password incorrect\r\n"; + appendToWriteBuffer(msg); + } + else if (_hasNick && _hasUser && !_passReceived) + { + std::string msg = ":localhost 451 * :You must send PASS to register\r\n"; + appendToWriteBuffer(msg); } } @@ -123,6 +132,10 @@ void User::setHasUser(bool value) { _hasUser = value; } void User::setHasPass(bool value) { _hasPass = value; } +void User::setPassReceived(bool value) { _passReceived = value; } + +void User::setPassIsValid(bool value) { _passIsValid = value; } + bool User::getHasPass() const { return _hasPass; } std::string User::getNickname() const { return _nickname; }