Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix register msg #135

Merged
merged 3 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions includes/libsUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# include <string>
# include <vector>
# include <unistd.h>
# include <ctime>
#include <iomanip>
Atrujillo02 marked this conversation as resolved.
Show resolved Hide resolved

# include "Logger.hpp"

Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hace falta el std::string??

Suggested change
# define RPL_WELCOME(nickname, username, hostname) std::string("Welcome to the Internet Relay Network ") + USER_ID(nickname, username, hostname)
# define RPL_WELCOME(nickname, username, hostname) "Welcome to the Internet Relay Network " + USER_ID(nickname, username, hostname)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A mi eso me pesaba con la de los rpl que tengo que hacer. Es necesario porque coge el USER_ID como un string y el mensaje del principio como un const char * o algo así. En mi tarea lo arreglaré

Damos por bueno esto, me traigo los cambios y lo arreglo con el resto

# define RPL_YOURHOST(servername)"Your host is " + servername + ", running version ft_messenger-v1.0.0"
Atrujillo02 marked this conversation as resolved.
Show resolved Hide resolved
# define RPL_CREATED(date)"This server was create: " + (date)
Atrujillo02 marked this conversation as resolved.
Show resolved Hide resolved
# define RPL_MYINFO(servername, userModes, channelModes) servername + " " + "ft_messenger-v1.0.0 " + "Available user modes: \"" + (userModes) + "\", Available channel modes: " + (channelModes)
Atrujillo02 marked this conversation as resolved.
Show resolved Hide resolved

# define ERROR_MSG(errorCode, nickname, errorMsg) ":irc.ft_messenger.net " + (errorCode) + " " + (nickname) + " " + (errorMsg) + "."

Expand All @@ -75,5 +81,6 @@

std::vector<std::string> split(const std::string &s, char delim);
bool isNumber(const std::string& s);
std::string getCurrentDate();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string getCurrentDate() const;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si lo pongo da error, según copilot dice que no tiene sentido poner const en una función no miembro, porque no hay objeto para modificar. Este es el error"a type qualifier is not allowed on a nonmember function"

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah claro, que es el libUtils. Cuando hagas el singleton añadelo a todas las funciones


#endif
15 changes: 14 additions & 1 deletion src/User.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

creo que en vez de enviar 1 mensaje deberíamos de mandar 4, porque cada uno debe de estar terminado por \r\n ... creo

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

si, lo que dice @ruzafa8

Server::getInstance().sendMessage(this->getFd(), response);

}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/libsUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Se puede hacer mucho más sencillo con la función de ctime strftime, que literalmente da formato a la fecha.
En palabras de copilot:

En C++98, puedes usar la biblioteca para trabajar con fechas y horas. Sin embargo, para formatear la fecha y hora en el formato específico que mencionaste (Sun, 14 Jan 2024 22:56:48 UTC), necesitarás usar la función strftime, que permite especificar un formato de fecha y hora personalizado.
Aquí te muestro cómo puedes hacerlo:

`
// Obtener la hora actual

std::time_t t = std::time(0);
std::tm* now = std::localtime(&t);

// Buffer para almacenar la fecha y hora formateada
char buffer[100];

// Formato de fecha y hora: Sun, 14 Jan 2024 22:56:48 UTC
strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S UTC", now);`

En este código, %a se reemplaza por el nombre abreviado del día de la semana, %d por el día del mes, %b por el nombre abreviado del mes, %Y por el año, %H por la hora, %M por el minuto y %S por el segundo.

Tendrías que hacer una conversión del buffer a string y ya

}
Loading