Skip to content

Commit

Permalink
Merge pull request #39 from ygor-sena/7-b-implement-a-bot
Browse files Browse the repository at this point in the history
fix!: add required format response by Hexchat IRC client
  • Loading branch information
gialexan authored Jun 1, 2024
2 parents ece0ba7 + f95a21a commit ca6802c
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 39 deletions.
20 changes: 10 additions & 10 deletions include/Replies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -17,27 +17,27 @@

// 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

#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)
Expand All @@ -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 + \
Expand Down
4 changes: 3 additions & 1 deletion include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 10:23:47 by gilmar #+# #+# */
/* Updated: 2024/05/31 16:45:14 by gilmar ### ########.fr */
/* Updated: 2024/05/31 18:57:35 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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<Client*> _clients; //-> vector of clients
std::vector<struct pollfd> _fds; //-> vector of pollfd
Expand All @@ -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);
Expand Down
11 changes: 1 addition & 10 deletions src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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<Client*>::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<Client*>::iterator it = this->_clients.begin();
it != this->_clients.end();
it++)
Expand Down
10 changes: 6 additions & 4 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -50,7 +50,9 @@ Client::Client(const Client& other)
/*
** ------------------------------- DESTRUCTOR ---------------------------------
*/
Client::~Client() {}
Client::~Client()
{
}

/*
** ------------------------------- ACCESSORS ----------------------------------
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 15 additions & 1 deletion src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -18,6 +18,7 @@

Server::Server()
{
_hostname = "ft_irc.42.fr";
_reply_code = 0;
_port = 0;
_server_fdsocket = -1;
Expand All @@ -34,6 +35,7 @@ Server::Server()
Server::Server(std::string password, std::vector<Client*> clients,
std::vector<Channel*> channels)
{
_hostname = "ft_irc.42.fr";
_reply_code = 0;
_port = 0;
_server_fdsocket = -1;
Expand Down Expand Up @@ -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.
*
Expand Down
21 changes: 19 additions & 2 deletions src/commands/Join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Join.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -121,11 +122,27 @@ 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);
_reply_code = 200;

// 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
*/
}
9 changes: 6 additions & 3 deletions src/commands/Mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Mode.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:30:34 by gilmar #+# #+# */
/* Updated: 2024/05/31 16:53:26 by gilmar ### ########.fr */
/* Updated: 2024/05/31 21:02:47 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -96,7 +96,7 @@ void Server::_handler_client_mode(const std::string& buffer, const int fd)
return;
}

// TODO: VERIFICAR SE EXISTE UMA RESPOSTA PARA ESTA SITUAÇÃO, NO MOMENTO
// TODO: VERIFICAR SE EXISTE UMA RESPOSTA PARA ESTA SITUAÇÃO, NO MOMENTO
// ESTÁ COMO 200 MAS DEVE HAVER OUTRO REPLY.

// Enviar resposta de sucesso ao cliente
Expand Down Expand Up @@ -133,6 +133,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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/commands/Nick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Nick.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/commands/Privmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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]));
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/User.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* User.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ca6802c

Please sign in to comment.