diff --git a/Makefile b/Makefile index 7633a0b..23cad93 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: omoudni +#+ +:+ +#+ # +# By: omoudni +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/05/02 15:40:00 by rparodi #+# #+# # -# Updated: 2025/05/14 23:33:58 by omoudni ### ########.fr # +# Updated: 2025/05/19 15:13:05 by omoudni ### ########.fr # # # # **************************************************************************** # @@ -26,7 +26,8 @@ SESSION = test-irc # Sources SRC = sources/core/main.cpp \ sources/core/server.cpp \ - sources/core/check.cpp + sources/core/check.cpp \ + sources/core/parser.cpp \ INC_DIR = include/core \ diff --git a/diagram.puml b/diagram.puml index 1b8652d..4621615 100644 --- a/diagram.puml +++ b/diagram.puml @@ -69,6 +69,22 @@ class Commandes { + USERNAME() : void } +' ======================== +' CLASS: Parser +' ======================== +class Parser { + - _port : int + - _password : string + - _valid : bool + - _errorMsg : string + + + Parser(argc : int, argv : char**) + + isValid() : bool + + getPort() : int + + getPassword() : string + + getErrorMsg() : string +} + ' ======================== ' RELATIONS ' ======================== @@ -77,5 +93,6 @@ Server "1" o-- "*" Client : _clients Client "1" o-- "*" Channel : _channels Channel "1" *-- "1" Client : _owner Channel "1" o-- "*" Client : _operators +main ..> Parser : uses @enduml \ No newline at end of file diff --git a/include/core/core.hpp b/include/core/core.hpp index 767572b..74d6c2b 100644 --- a/include/core/core.hpp +++ b/include/core/core.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* core.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ +/* By: omoudni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/12 14:16:03 by rparodi #+# #+# */ -/* Updated: 2025/05/13 12:45:24 by rparodi ### ########.fr */ +/* Updated: 2025/05/19 15:07:26 by omoudni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,5 +23,6 @@ #include "color.hpp" #include "server.hpp" +#include "parser.hpp" unsigned short int valid_port(char *input); diff --git a/include/core/parser.hpp b/include/core/parser.hpp new file mode 100644 index 0000000..3becac5 --- /dev/null +++ b/include/core/parser.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: omoudni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/19 14:47:46 by omoudni #+# #+# */ +/* Updated: 2025/05/19 15:14:31 by omoudni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once +#include "core.hpp" +#include + +class Parser { +private: + unsigned short int _port; + std::string _password; + bool _valid; + std::string _errorMsg; + +public: + Parser(int argc, char* argv[]); + bool isValid() const; + unsigned short int getPort() const; + const std::string& getPassword() const; + const std::string& getErrorMsg() const; +}; \ No newline at end of file diff --git a/sources/core/main.cpp b/sources/core/main.cpp index c006a8c..269b649 100644 --- a/sources/core/main.cpp +++ b/sources/core/main.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: omoudni +#+ +:+ +#+ */ +/* By: omoudni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 11:03:13 by rparodi #+# #+# */ -/* Updated: 2025/05/14 23:17:03 by omoudni ### ########.fr */ +/* Updated: 2025/05/19 15:02:41 by omoudni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,16 +14,12 @@ #include int main(int argc, char *argv[]) { - if (argc != 3) { - std::cerr << CLR_RED << "Usage: " << argv[0] << " " << CLR_RESET << std::endl; - return 1; - } - unsigned short int tmp_port = valid_port(argv[1]); - if (tmp_port == 0) { - std::cerr << CLR_RED << "Error: Invalid port: " << argv[1] << CLR_RESET << std::endl; - return 1; - } - Server server(tmp_port, argv[2]); + Parser parser(argc, argv); + if (!parser.isValid()) { + std::cerr << CLR_RED << parser.getErrorMsg() << CLR_RESET << std::endl; + return 1; + } + Server server(parser.getPort(), parser.getPassword()); server.showInfo(); server.start(); return 0; diff --git a/sources/core/parser.cpp b/sources/core/parser.cpp new file mode 100644 index 0000000..362fb97 --- /dev/null +++ b/sources/core/parser.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: omoudni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/19 14:48:52 by omoudni #+# #+# */ +/* Updated: 2025/05/19 15:08:18 by omoudni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.hpp" +#include + +Parser::Parser(int argc, char* argv[]) : _port(0), _valid(false) { + if (argc != 3) { + _errorMsg = "Usage: ./ircserv "; + return; + } + _port = valid_port(argv[1]); + if (_port == 0) { + _errorMsg = "Error: Invalid port: " + std::string(argv[1]); + return; + } + _password = argv[2]; + if (_password.empty()) { + _errorMsg = "Error: Password cannot be empty"; + return; + } + for (size_t i = 0; i < _password.size(); ++i) { + if (!isprint(_password[i])) { + _errorMsg = "Error: Password contains non-printable characters"; + return; + } + } + _valid = true; +} + +bool Parser::isValid() const { return _valid; } +unsigned short int Parser::getPort() const { return _port; } +const std::string& Parser::getPassword() const { return _password; } +const std::string& Parser::getErrorMsg() const { return _errorMsg; } \ No newline at end of file