From 1fb709b9fc1d6eef36fc5f5af0ddb772bdc5da32 Mon Sep 17 00:00:00 2001 From: ygor-sena Date: Fri, 31 May 2024 21:08:39 -0300 Subject: [PATCH] fix!: add required format response by Hexchat IRC client --- include/Replies.hpp | 20 ++++++++++---------- include/Server.hpp | 4 +++- src/Channel.cpp | 11 +---------- src/Client.cpp | 10 ++++++---- src/Server.cpp | 16 +++++++++++++++- src/commands/Join.cpp | 21 +++++++++++++++++++-- src/commands/Mode.cpp | 8 ++++++-- src/commands/Nick.cpp | 6 +++--- src/commands/Privmsg.cpp | 5 +++-- src/commands/User.cpp | 6 +++--- 10 files changed, 69 insertions(+), 38 deletions(-) diff --git a/include/Replies.hpp b/include/Replies.hpp index e2a1d3e..efa41de 100644 --- a/include/Replies.hpp +++ b/include/Replies.hpp @@ -6,7 +6,7 @@ /* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/11 18:18:57 by gilmar #+# #+# */ -/* Updated: 2024/05/29 19:19:18 by yde-goes ### ########.fr */ +/* Updated: 2024/05/31 20:57:35 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,11 @@ // refer to https://datatracker.ietf.org/doc/html/rfc1459 -#define RPL_CONNECTED(nickname) \ - (": 001 " + nickname + " : Welcome to the IRC server!" + CRLF) +#define RPL_CONNECTED(serverhostname, nickname, targethostname) \ + (":" + serverhostname + " 001 " + nickname + " :Welcome to the IRC server! " + nickname + "!" + targethostname + CRLF) -#define RPL_UMODEIS(hostname, channelname, mode, user) \ - ":" + hostname + " MODE " + channelname + " " + mode + " " + user + CRLF +#define RPL_UMODEIS(nickname, hostname, channelname, set, mode, arg) \ + (":" + nickname + "!" + hostname + " MODE " + channelname + " " + set + mode + " " + arg + CRLF) #define RPL_CREATIONTIME(nickname, channelname, creationtime) \ ": 329 " + nickname + " " + channelname + " " + creationtime + CRLF @@ -29,15 +29,15 @@ #define RPL_CHANNELMODES(nickname, channelname, modes) \ ": 324 " + nickname + " " + channelname + " " + modes + CRLF -#define RPL_CHANGEMODE(hostname, channelname, mode, arguments) \ +#define RPL_CHANGEMODE(hostname, channelname, mode, arguments) J \ (":" + hostname + " MODE " + channelname + " " + mode + " " + arguments + \ CRLF) #define RPL_NICKCHANGE(oldnickname, nickname) \ (":" + oldnickname + " NICK " + nickname + CRLF) -#define RPL_JOINMSG(hostname, ipaddress, channelname) \ - (":" + hostname + "@" + ipaddress + " JOIN " + channelname + CRLF) +#define RPL_JOINMSG(nickname, hostname, channelname) \ + (":" + nickname + "!" + hostname + " JOIN " + channelname + CRLF) #define RPL_NAMREPLY(nickname, channelname, clientslist) \ (": 353 " + nickname + " @ " + channelname + " :" + clientslist + CRLF) @@ -61,8 +61,8 @@ kickername + " " + comment + CRLF) #define RPL_INVITING(hostname, channelname, invitername, invitedname) \ (":" + hostname + " INVITE " + invitedname + " " + channelname + CRLF) -#define RPL_PRIVMSG(hostname, receiver, text) \ - (":" + hostname + " PRIVMSG " + receiver + " " + text + CRLF) +#define RPL_PRIVMSG(nickname, hostname, receiver, text) \ + (":" + nickname + "!" + hostname + " PRIVMSG " + receiver + " " + text + CRLF) #define BOT_CMDMARVIN(nickname) \ (": 4242 marvin_bot " + nickname + \ diff --git a/include/Server.hpp b/include/Server.hpp index 6730c2b..76ba597 100644 --- a/include/Server.hpp +++ b/include/Server.hpp @@ -6,7 +6,7 @@ /* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/28 10:23:47 by gilmar #+# #+# */ -/* Updated: 2024/05/31 13:40:50 by yde-goes ### ########.fr */ +/* Updated: 2024/05/31 18:57:35 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,6 +60,7 @@ class Server private: int _port; //-> server port int _server_fdsocket; //-> server socket file descriptor + std::string _hostname; std::string _password; //-> server password std::vector _clients; //-> vector of clients std::vector _fds; //-> vector of pollfd @@ -69,6 +70,7 @@ class Server int _reply_code; // -> This a workaround to test the IRC commands without // mocking up a client-server connection + std::string _get_hostname(); //-> get hostname int get_reply_code(void); // -> Get the reply code to be checked against // criterion test results void _is_valid_port(const std::string& port); diff --git a/src/Channel.cpp b/src/Channel.cpp index d838e10..8497efa 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -6,7 +6,7 @@ /* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/21 08:26:17 by gilmar #+# #+# */ -/* Updated: 2024/05/31 13:53:16 by yde-goes ### ########.fr */ +/* Updated: 2024/05/31 20:00:22 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -436,15 +436,6 @@ void Channel::quit(Client* client) { remove_channel_operator(client); } */ void Channel::broadcast(Client* sender, std::string target, std::string message) { - for (std::vector::iterator it = this->_operator_clients.begin(); - it != this->_operator_clients.end(); - it++) - { - if (*it == sender) - continue; - (*it)->broadcast(sender, target, message); - return; - } for (std::vector::iterator it = this->_clients.begin(); it != this->_clients.end(); it++) diff --git a/src/Client.cpp b/src/Client.cpp index 247a2e9..271e313 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -6,7 +6,7 @@ /* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/28 10:20:02 by gilmar #+# #+# */ -/* Updated: 2024/05/31 12:48:20 by yde-goes ### ########.fr */ +/* Updated: 2024/05/31 19:45:35 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,7 +50,9 @@ Client::Client(const Client& other) /* ** ------------------------------- DESTRUCTOR --------------------------------- */ -Client::~Client() {} +Client::~Client() +{ +} /* ** ------------------------------- ACCESSORS ---------------------------------- @@ -216,7 +218,7 @@ std::string Client::get_password() const { return _password; } * * @return std::string The hostname of the client. */ -std::string Client::get_hostname() const { return _nickname + "@" + _ip_addr; } +std::string Client::get_hostname() const { return _username + "@" + _ip_addr; } /** * @brief Get the list of channels the client has been invited to. @@ -295,7 +297,7 @@ void Client::remove_channel_invited(const std::string& channel) */ void Client::broadcast(Client* sender, std::string target, std::string message) { - std::string response = RPL_PRIVMSG(sender->get_hostname(), target, message); + std::string response = RPL_PRIVMSG(sender->get_nickname(), sender->get_hostname(), target, message); if (send(this->get_fd(), response.c_str(), response.size(), 0) == -1) std::cerr << "Response send() failed" << std::endl; diff --git a/src/Server.cpp b/src/Server.cpp index 290af63..d8393ed 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -6,7 +6,7 @@ /* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/28 10:26:55 by gilmar #+# #+# */ -/* Updated: 2024/05/31 14:01:43 by yde-goes ### ########.fr */ +/* Updated: 2024/05/31 21:06:46 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ Server::Server() { + _hostname = "ft_irc.42.fr"; _reply_code = 0; _port = 0; _server_fdsocket = -1; @@ -34,6 +35,7 @@ Server::Server() Server::Server(std::string password, std::vector clients, std::vector channels) { + _hostname = "ft_irc.42.fr"; _reply_code = 0; _port = 0; _server_fdsocket = -1; @@ -71,6 +73,18 @@ Server::~Server() ** ------------------------------- GETTERS -------------------------------- */ +/** + * @brief Get the hostname of the server. + * + * This function returns the hostname of the server. + * + * @return The hostname of the server. + */ +std::string Server::_get_hostname() +{ + return _hostname; +} + /** * @brief Retrieves the client associated with the given file descriptor. * diff --git a/src/commands/Join.cpp b/src/commands/Join.cpp index 67f720b..0f85510 100644 --- a/src/commands/Join.cpp +++ b/src/commands/Join.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Join.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: gilmar +#+ +:+ +#+ */ +/* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/21 08:33:05 by gilmar #+# #+# */ -/* Updated: 2024/05/30 14:53:41 by gilmar ### ########.fr */ +/* Updated: 2024/05/31 19:52:35 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,6 +82,7 @@ void Server::_handler_client_join(const std::string& buffer, const int fd) _add_channel(channel); channel->join(client); channel->set_channel_operator(client); + _send_response(fd, RPL_JOINMSG(client->get_nickname(), client->get_hostname(), joining_channel)); return; } @@ -121,6 +122,8 @@ void Server::_handler_client_join(const std::string& buffer, const int fd) _reply_code = 475; return; } + + _send_response(fd, RPL_JOINMSG(client->get_nickname(), client->get_hostname(), joining_channel)); // Adicionar o cliente ao canal channel->join(client); @@ -128,4 +131,18 @@ void Server::_handler_client_join(const std::string& buffer, const int fd) // Registra o comando JOIN recebido std::cout << "JOIN command received from client " << buffer << std::endl; + +/* # Reply to a /JOIN command + :nick!user@host JOIN #channel + + # Reply to a /PRIVMSG command + :nick!user@host PRIVMSG #channel :Hello, world! + + # Reply to a /MODE command + :nick!user@host MODE #channel +o otheruser + + # Reply to a /USER command + :irc.example.com 001 nick :Welcome to the Internet Relay Network nick!user@host + +*/ } \ No newline at end of file diff --git a/src/commands/Mode.cpp b/src/commands/Mode.cpp index 45b2942..caf4a72 100644 --- a/src/commands/Mode.cpp +++ b/src/commands/Mode.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Mode.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: gilmar +#+ +:+ +#+ */ +/* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/21 08:30:34 by gilmar #+# #+# */ -/* Updated: 2024/05/30 17:00:18 by gilmar ### ########.fr */ +/* Updated: 2024/05/31 21:02:47 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -93,6 +93,7 @@ void Server::_handler_client_mode(const std::string& buffer, const int fd) _reply_code = 696; return; } + } bool Server::_parse_mode_command(const std::string& modes, Channel* channel, @@ -123,6 +124,9 @@ bool Server::_parse_mode_command(const std::string& modes, Channel* channel, } } } + + std::string signal = set ? "+" : "-"; + _send_response(fd, RPL_UMODEIS(client->get_nickname(), client->get_hostname(), channel->get_name(), signal, mode, arg)); return true; } diff --git a/src/commands/Nick.cpp b/src/commands/Nick.cpp index 6a2d3b2..a39ed70 100644 --- a/src/commands/Nick.cpp +++ b/src/commands/Nick.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Nick.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: gilmar +#+ +:+ +#+ */ +/* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/21 08:31:54 by gilmar #+# #+# */ -/* Updated: 2024/05/30 16:02:09 by gilmar ### ########.fr */ +/* Updated: 2024/05/31 19:02:11 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,7 +65,7 @@ void Server::_handler_client_nickname(const std::string& buffer, const int fd) if (_client_is_ready_to_login(fd)) { client->set_is_logged(fd); - _send_response(fd, RPL_CONNECTED(client->get_nickname())); + _send_response(fd, RPL_CONNECTED(_get_hostname(), client->get_nickname(), client->get_hostname())); _reply_code = 001; return; } diff --git a/src/commands/Privmsg.cpp b/src/commands/Privmsg.cpp index 971cf22..ad05a85 100644 --- a/src/commands/Privmsg.cpp +++ b/src/commands/Privmsg.cpp @@ -6,7 +6,7 @@ /* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/21 08:31:33 by gilmar #+# #+# */ -/* Updated: 2024/05/31 10:14:41 by yde-goes ### ########.fr */ +/* Updated: 2024/05/31 19:27:41 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -141,7 +141,8 @@ void Server::_handler_client_privmsg(const std::string& buffer, const int fd) // Send the message to the receiver _send_response(target_client->get_fd(), - RPL_PRIVMSG(client->get_hostname(), + RPL_PRIVMSG(client->get_nickname(), + client->get_hostname(), target_client->get_nickname(), params[1])); } diff --git a/src/commands/User.cpp b/src/commands/User.cpp index 576c7a4..6443a77 100644 --- a/src/commands/User.cpp +++ b/src/commands/User.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* User.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: gilmar +#+ +:+ +#+ */ +/* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/21 08:32:16 by gilmar #+# #+# */ -/* Updated: 2024/05/30 16:47:25 by gilmar ### ########.fr */ +/* Updated: 2024/05/31 18:58:03 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,7 +69,7 @@ void Server::_handler_client_username(const std::string& buffer, const int fd) if (_client_is_ready_to_login(fd)) { client->set_is_logged(fd); - _send_response(fd, RPL_CONNECTED(client->get_nickname())); + _send_response(fd, RPL_CONNECTED(_get_hostname(), client->get_nickname(), client->get_hostname())); _reply_code = 001; } else