From 5a2eef88ddceab6c97a109bc4c7314b7d7151f83 Mon Sep 17 00:00:00 2001 From: alexLinux Date: Thu, 25 Apr 2024 16:27:18 +0200 Subject: [PATCH 1/3] fix register msg --- includes/libsUtils.hpp | 7 +++++++ src/User.cpp | 15 ++++++++++++++- src/libsUtils.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/includes/libsUtils.hpp b/includes/libsUtils.hpp index 55ed0e3..c6647a4 100644 --- a/includes/libsUtils.hpp +++ b/includes/libsUtils.hpp @@ -17,6 +17,8 @@ # include # include # include +# include +#include # include "Logger.hpp" @@ -62,6 +64,10 @@ // # define RPL_AWAY(nickname, username, hostname, awayMessage) USER_ID(nickname, username, hostname) + " " + (nickname) + " :" + (awayMessage) // # define RPL_CHANNEL_MODE_IS(nickname, username, hostname, channel, mode, modeParams) USER_ID(nickname, username, hostname) + " " + (channel) + " " + (mode) + " " + (modeParams) # define RPL_END_OF_NAMES(nickname, usermane, hostname, channel) USER_ID(nickname, username, hostname) + " " + (channel) + " :End of NAMES list." +# define RPL_WELCOME(nickname, username, hostname) std::string("Welcome to the Internet Relay Network ") + USER_ID(nickname, username, hostname) +# define RPL_YOURHOST(servername)"Your host is " + servername + ", running version ft_messenger-v1.0.0" +# define RPL_CREATED(date)"This server was create: " + (date) +# define RPL_MYINFO(servername, userModes, channelModes) servername + " " + "ft_messenger-v1.0.0 " + "Available user modes: \"" + (userModes) + "\", Available channel modes: " + (channelModes) # define ERROR_MSG(errorCode, nickname, errorMsg) ":irc.ft_messenger.net " + (errorCode) + " " + (nickname) + " " + (errorMsg) + "." @@ -75,5 +81,6 @@ std::vector split(const std::string &s, char delim); bool isNumber(const std::string& s); +std::string getCurrentDate(); #endif \ No newline at end of file diff --git a/src/User.cpp b/src/User.cpp index 9630980..9d54308 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -185,7 +185,20 @@ bool User::canRegister() const { void User::makeRegistration() { if (!Server::getInstance().isValidPassword(this->_password)) throw PasswordMismatchException(); - this->_registered = true; + this->_registered = true; + + std::string date = getCurrentDate(); + std::string userModes = " "; + std::string channelModes = "iklot"; + + std::string welcome = RPL_WELCOME(_nickname, _username, _hostname); + std::string yourHost = RPL_YOURHOST(_serverName); + std::string created = RPL_CREATED(date); + std::string myInfo = RPL_MYINFO(_serverName, userModes, channelModes); + + std::string response = welcome + "\n" + yourHost + "\n" + created + "\n" + myInfo; + Server::getInstance().sendMessage(this->getFd(), response); + } /** diff --git a/src/libsUtils.cpp b/src/libsUtils.cpp index 5dde1f9..d5b19b2 100644 --- a/src/libsUtils.cpp +++ b/src/libsUtils.cpp @@ -32,3 +32,27 @@ bool isNumber(const std::string& s) { ++it; return !s.empty() && it == s.end(); } + +/** + * Gets the current date. + * + * @return The string with the current date. + */ +std::string getCurrentDate() { + std::time_t t = std::time(0); + std::tm* now = std::localtime(&t); + std::ostringstream oss; + const char* weekDays[7] = {"Sunday", "Monday", "Tuesday", + "Wednesday", "Thursday", "Friday", "Saturday"}; + const char* months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", + "Aug", "Sep", "Oct", "Nov", "Dec"}; + + oss << weekDays[now->tm_wday] << ", " + << std::setfill('0') << std::setw(2) << now->tm_mday << ' ' + << months[now->tm_mon] << ' ' + << (now->tm_year + 1900) << ' ' + << std::setfill('0') << std::setw(2) << now->tm_hour << ':' + << std::setfill('0') << std::setw(2) << now->tm_min << ':' + << std::setfill('0') << std::setw(2) << now->tm_sec << " UTC"; + return oss.str(); +} \ No newline at end of file From 2ecd18b7dd208cbcd59aa45dbddc8424eac5ad12 Mon Sep 17 00:00:00 2001 From: alexLinux Date: Thu, 25 Apr 2024 20:05:53 +0200 Subject: [PATCH 2/3] fix new change in RPL_WELCOME --- includes/libsUtils.hpp | 8 ++++---- src/User.cpp | 17 +++++++++-------- src/libsUtils.cpp | 16 +++------------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/includes/libsUtils.hpp b/includes/libsUtils.hpp index c6647a4..7a3fd65 100644 --- a/includes/libsUtils.hpp +++ b/includes/libsUtils.hpp @@ -18,7 +18,6 @@ # include # include # include -#include # include "Logger.hpp" @@ -34,6 +33,7 @@ # define DEFAULT_PORT "6666" # define DEFAULT_PASS "1234" +# define DEFAULT_SERVER_VERSION "ft_messenger-v1.0.0" // ========================================= IRC SERVER ERROR MESSAGES ========================================= # define INVALID_ARGS "[ERROR] Invalid args.\nUsage: ./ircserv " @@ -65,9 +65,9 @@ // # define RPL_CHANNEL_MODE_IS(nickname, username, hostname, channel, mode, modeParams) USER_ID(nickname, username, hostname) + " " + (channel) + " " + (mode) + " " + (modeParams) # define RPL_END_OF_NAMES(nickname, usermane, hostname, channel) USER_ID(nickname, username, hostname) + " " + (channel) + " :End of NAMES list." # define RPL_WELCOME(nickname, username, hostname) std::string("Welcome to the Internet Relay Network ") + USER_ID(nickname, username, hostname) -# define RPL_YOURHOST(servername)"Your host is " + servername + ", running version ft_messenger-v1.0.0" -# define RPL_CREATED(date)"This server was create: " + (date) -# define RPL_MYINFO(servername, userModes, channelModes) servername + " " + "ft_messenger-v1.0.0 " + "Available user modes: \"" + (userModes) + "\", Available channel modes: " + (channelModes) +# define RPL_YOURHOST(servername) "Your host is " + (servername) + ", running version " + (DEFAULT_SERVER_VERSION) +# define RPL_CREATED(date) "This server was create: " + (date) +# define RPL_MYINFO(servername, channelModes) (servername) + " " + (DEFAULT_SERVER_VERSION) + " Available user modes: " + ", Available channel modes: " + (channelModes) # define ERROR_MSG(errorCode, nickname, errorMsg) ":irc.ft_messenger.net " + (errorCode) + " " + (nickname) + " " + (errorMsg) + "." diff --git a/src/User.cpp b/src/User.cpp index 9d54308..21cf612 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -188,17 +188,18 @@ void User::makeRegistration() { this->_registered = true; std::string date = getCurrentDate(); - std::string userModes = " "; std::string channelModes = "iklot"; - std::string welcome = RPL_WELCOME(_nickname, _username, _hostname); - std::string yourHost = RPL_YOURHOST(_serverName); - std::string created = RPL_CREATED(date); - std::string myInfo = RPL_MYINFO(_serverName, userModes, channelModes); + std::string welcome = RPL_WELCOME(_nickname, _username, _hostname) + "\r\n"; + std::string yourHost = RPL_YOURHOST(_serverName) + "\r\n"; + std::string created = RPL_CREATED(date) + "\r\n"; + std::string myInfo = RPL_MYINFO(_serverName, channelModes) + "\r\n"; - std::string response = welcome + "\n" + yourHost + "\n" + created + "\n" + myInfo; - Server::getInstance().sendMessage(this->getFd(), response); - + Server& server = Server::getInstance(); + server.sendMessage(this->getFd(), welcome); + server.sendMessage(this->getFd(), yourHost); + server.sendMessage(this->getFd(), created); + server.sendMessage(this->getFd(), myInfo); } /** diff --git a/src/libsUtils.cpp b/src/libsUtils.cpp index d5b19b2..f085ea0 100644 --- a/src/libsUtils.cpp +++ b/src/libsUtils.cpp @@ -41,18 +41,8 @@ bool isNumber(const std::string& s) { std::string getCurrentDate() { std::time_t t = std::time(0); std::tm* now = std::localtime(&t); - std::ostringstream oss; - const char* weekDays[7] = {"Sunday", "Monday", "Tuesday", - "Wednesday", "Thursday", "Friday", "Saturday"}; - const char* months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", - "Aug", "Sep", "Oct", "Nov", "Dec"}; + char buffer[100]; - oss << weekDays[now->tm_wday] << ", " - << std::setfill('0') << std::setw(2) << now->tm_mday << ' ' - << months[now->tm_mon] << ' ' - << (now->tm_year + 1900) << ' ' - << std::setfill('0') << std::setw(2) << now->tm_hour << ':' - << std::setfill('0') << std::setw(2) << now->tm_min << ':' - << std::setfill('0') << std::setw(2) << now->tm_sec << " UTC"; - return oss.str(); + strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S UTC", now); + return std::string(buffer); } \ No newline at end of file From f64d80408df94111fb504a11fa6938aaa98d01d2 Mon Sep 17 00:00:00 2001 From: alexLinux Date: Thu, 25 Apr 2024 20:39:51 +0200 Subject: [PATCH 3/3] fix clean code --- src/User.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/User.cpp b/src/User.cpp index 21cf612..106ef2d 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -190,16 +190,11 @@ void User::makeRegistration() { std::string date = getCurrentDate(); std::string channelModes = "iklot"; - std::string welcome = RPL_WELCOME(_nickname, _username, _hostname) + "\r\n"; - std::string yourHost = RPL_YOURHOST(_serverName) + "\r\n"; - std::string created = RPL_CREATED(date) + "\r\n"; - std::string myInfo = RPL_MYINFO(_serverName, channelModes) + "\r\n"; - Server& server = Server::getInstance(); - server.sendMessage(this->getFd(), welcome); - server.sendMessage(this->getFd(), yourHost); - server.sendMessage(this->getFd(), created); - server.sendMessage(this->getFd(), myInfo); + server.sendMessage(this->getFd(), RPL_WELCOME(_nickname, _username, _hostname)); + server.sendMessage(this->getFd(), RPL_YOURHOST(_serverName)); + server.sendMessage(this->getFd(), RPL_CREATED(date)); + server.sendMessage(this->getFd(), RPL_MYINFO(_serverName, channelModes)); } /**