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!: ircserv now handles Ctrl-D correctly" #50

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Client.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: caalbert <caalbert@student.42sp.org.br> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 10:19:06 by gilmar #+# #+# */
/* Updated: 2024/06/01 15:22:55 by caalbert ### ########.fr */
/* Updated: 2024/06/01 17:01:24 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -60,6 +60,7 @@ class Client
Client(const Client& other);

int get_fd() const;

bool get_is_logged() const;
bool get_is_operator() const;
bool get_is_authenticated() const;
Expand Down Expand Up @@ -88,6 +89,9 @@ class Client

void broadcast(Client* sender, std::string target, std::string message);

void append_to_buffer(const std::string& data);
void clear_buffer();

private:
int _fd;
bool _is_logged;
Expand Down
70 changes: 35 additions & 35 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/06/01 09:34:16 by yde-goes ### ########.fr */
/* Updated: 2024/06/01 17:01:11 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,10 +30,10 @@
mode + " " + arg + CRLF)

#define RPL_CREATIONTIME(nickname, channelname, creationtime) \
": 329 " + nickname + " " + channelname + " " + creationtime + CRLF
":329 " + nickname + " " + channelname + " " + creationtime + CRLF

#define RPL_CHANNELMODES(nickname, channelname, modes) \
": 324 " + nickname + " " + channelname + " " + modes + CRLF
":324 " + nickname + " " + channelname + " " + modes + CRLF

#define RPL_CHANGEMODE(hostname, channelname, mode, arguments) \
J(":" + hostname + " MODE " + channelname + " " + mode + " " + arguments + \
Expand All @@ -46,19 +46,19 @@
(":" + nickname + "!" + hostname + " JOIN " + channelname + CRLF)

#define RPL_NAMREPLY(nickname, channelname, clientslist) \
(": 353 " + nickname + " @ " + channelname + " :" + clientslist + CRLF)
(":353 " + nickname + " @ " + channelname + " :" + clientslist + CRLF)

#define RPL_ENDOFNAMES(nickname, channelname) \
(": 366 " + nickname + " " + channelname + " :END of /NAMES list" + CRLF)
(":366 " + nickname + " " + channelname + " :END of /NAMES list" + CRLF)

#define RPL_TOPIC(nickname, channelname, topic) \
(": 332 " + nickname + " " + channelname + " :" + topic + CRLF)
(":332 " + nickname + " " + channelname + " :" + topic + CRLF)

#define RPL_NOTOPIC(nickname, channelname) \
(": 331 " + nickname + " " + channelname + " : No topic is set " + CRLF)
(":331 " + nickname + " " + channelname + " : No topic is set " + CRLF)

#define RPL_QUITMESSAGE(nickname) \
(": 301 " + nickname + " :You have quit the server." + CRLF)
(":301 " + nickname + " :You have quit the server." + CRLF)

#define RPL_PART(hostname, channelname, nickname) \
(":" + hostname + " PART " + channelname + " :" + nickname + \
Expand Down Expand Up @@ -104,86 +104,86 @@
** --------------------------- ERROR REPLY MESSAGES ---------------------------
*/
#define ERR_NEEDMODEPARM(channelname, mode) \
(": 696 " + channelname + \
(":696 " + channelname + \
" * You must specify a parameter for the key mode. " + mode + CRLF)

#define ERR_INVALIDMODEPARM(channelname, mode) \
(": 696 " + channelname + " Invalid mode parameter. " + mode + CRLF)
(":696 " + channelname + " Invalid mode parameter. " + mode + CRLF)

#define ERR_KEYSET(channelname) \
": 467 " + channelname + " Channel key already set. " + CRLF
":467 " + channelname + " Channel key already set. " + CRLF

#define ERR_UNKNOWNMODE(nickname, channelname, mode) \
": 472 " + nickname + " " + channelname + " " + mode + \
#define ERR_UNKNOWNMODE(nickname, channelname, mode) \
":472 " + nickname + " " + channelname + " " + mode + \
" :is not a recognised channel mode" + CRLF

#define ERR_NEEDMOREPARAMS(nickname) \
(": 461 " + nickname + " :Not enough parameters." + CRLF)
(":461 " + nickname + " :Not enough parameters." + CRLF)

#define ERR_CHANNELNOTFOUND(nickname, channelname) \
(": 403 " + nickname + " " + channelname + " :No such channel" + CRLF)
(":403 " + nickname + " " + channelname + " :No such channel" + CRLF)

#define ERR_NOTOPERATOR(channelname) \
(": 482 " + channelname + " :You're not a channel operator" + CRLF)
(":482 " + channelname + " :You're not a channel operator" + CRLF)

#define ERR_NOSUCHNICK(channelname, name) \
(": 401 " + channelname + " " + name + " :No such nick/channel" + CRLF)
(":401 " + channelname + " " + name + " :No such nick/channel" + CRLF)

#define ERR_INCORPASS(nickname) \
(": 464 " + nickname + " :Password incorrect !" + CRLF)
(":464 " + nickname + " :Password incorrect !" + CRLF)

#define ERR_NONICKNAME(nickname) \
(": 431 " + nickname + " :No nickname given" + CRLF)
(":431 " + nickname + " :No nickname given" + CRLF)

#define ERR_NICKINUSE(nickname) \
(": 433 " + nickname + " :Nickname is already in use" + CRLF)
(":433 " + nickname + " :Nickname is already in use" + CRLF)

#define ERR_ALREADYREGISTERED(nickname) \
(": 462 " + nickname + " :You may not reregister !" + CRLF)
(":462 " + nickname + " :You may not reregister !" + CRLF)

#define ERR_NOTREGISTERED(nickname) \
(": 451 " + nickname + " :You have not registered!" + CRLF)
(":451 " + nickname + " :You have not registered!" + CRLF)

#define ERR_BADCHANNELKEY(nickname, channelname) \
(": 475 " + nickname + " " + channelname + \
(":475 " + nickname + " " + channelname + \
" :Cannot join channel (incorrect key)" + CRLF)

#define ERR_INVITEONLYCHAN(nickname, channelname) \
(": 473 " + nickname + " " + channelname + " :Cannot join channel (+i)" + \
#define ERR_INVITEONLYCHAN(nickname, channelname) \
(":473 " + nickname + " " + channelname + " :Cannot join channel (+i)" + \
CRLF)

#define ERR_CHANNELISFULL(nickname, channelname) \
(": 422 " + nickname + " " + channelname + " :Cannot join channel (+l)" + \
#define ERR_CHANNELISFULL(nickname, channelname) \
(":422 " + nickname + " " + channelname + " :Cannot join channel (+l)" + \
CRLF)

#define ERR_NOSUCHCHANNEL(channelname) \
(": 403 " + channelname + " :No such channel" + CRLF)
(":403 " + channelname + " :No such channel" + CRLF)

#define ERR_USERNOTINCHANNEL(nickname, channelname) \
("441 " + nickname + " " + channelname + " :They aren't on that channel" + \
CRLF)

#define ERR_NOTONCHANNEL(channelname) \
(": 442 " + channelname + " :You're not on that channel" + CRLF)
(":442 " + channelname + " :You're not on that channel" + CRLF)

#define ERR_USERONCHANNEL(user, channelname) \
(": 443 " + user + " " + channelname + " :is already on channel" + CRLF)
(":443 " + user + " " + channelname + " :is already on channel" + CRLF)

#define ERR_CHANOPRIVSNEEDED(channelname) \
(": 482 " + channelname + " :You're not a channel operator" + CRLF)
(":482 " + channelname + " :You're not a channel operator" + CRLF)

#define ERR_NOPRIVILEGES(nickname) \
(": 481 " + nickname + " :Permission Denied- You're not an IRC operator" + \
#define ERR_NOPRIVILEGES(nickname) \
(":481 " + nickname + " :Permission Denied- You're not an IRC operator" + \
CRLF)

#define ERR_ERRONEUSNICK(nickname) \
(": 432 " + nickname + " :Erroneus nickname" + CRLF)
(":432 " + nickname + " :Erroneus nickname" + CRLF)

#define ERR_BADCHANMASK(server, nickname, channel) \
(":" + server + " 476 " + nickname + " " + channel + \
" :Bad Channel Mask\r\n")

#define ERR_CMDNOTFOUND(command) \
(": 421 " + command + " :Unknown command" + CRLF)
(":421 " + command + " :Unknown command" + CRLF)

#endif // REPLIES_HPP
5 changes: 3 additions & 2 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: caalbert <caalbert@student.42sp.org.br> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 10:23:47 by gilmar #+# #+# */
/* Updated: 2024/06/01 15:25:49 by caalbert ### ########.fr */
/* Updated: 2024/06/01 17:03:48 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -99,6 +99,7 @@ class Server
void _handler_client_username(const std::string& username, const int fd);
void _handler_client_password(const std::string& password, const int fd);

void _handler_bot_input(const std::string& buffer, int fd);
void _handler_bot_time(const std::string& buffer, int fd);
void _handler_bot_whois(const std::string& buffer, int fd);
void _handler_bot_quote(const std::string& buffer, int fd);
Expand Down
20 changes: 18 additions & 2 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Client.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 10:20:02 by gilmar #+# #+# */
/* Updated: 2024/05/31 21:51:48 by gilmar ### ########.fr */
/* Updated: 2024/06/01 16:47:11 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -283,6 +283,22 @@ void Client::remove_channel_invited(const std::string& channel)
}
}

/**
* @brief Appends the given data to the buffer.
*
* This function appends the provided data to the internal buffer of the client.
*
* @param data The data to be appended to the buffer.
*/
void Client::append_to_buffer(const std::string& data) { _buffer += data; }

/**
* @brief Clears the buffer of the client.
*
* This function clears the buffer of the client, removing all its contents.
*/
void Client::clear_buffer(void) { _buffer.clear(); }

/**
* @brief Sends a broadcast message from the client to a target.
*
Expand Down
33 changes: 31 additions & 2 deletions src/MarvinBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* MarvinBot.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/27 11:32:28 by caalbert #+# #+# */
/* Updated: 2024/06/01 09:02:01 by gilmar ### ########.fr */
/* Updated: 2024/06/01 17:09:50 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -60,6 +60,35 @@ std::string _return_quote()
** ----------------------------- COMMAND HANDLERS -----------------------------
*/

/**
* @brief Handles the input received from the bot.
*
* This function parses the input buffer and determines the appropriate action
* based on the command. The command can be one of the following: "marvin",
* "time", "whoami", "whois", or "quote".
*
* @param buffer The input buffer containing the command and any additional
* arguments.
* @param fd The file descriptor associated with the connection.
*/
void Server::_handler_bot_input(const std::string& buffer, int fd)
{
std::istringstream iss(buffer);
std::string command;
iss >> command;

if (command == "marvin")
_handler_bot_marvin(buffer, fd);
else if (command == "time")
_handler_bot_time(buffer, fd);
else if (command == "whoami")
_handler_bot_whoami(buffer, fd);
else if (command == "whois")
_handler_bot_whois(buffer, fd);
else if (command == "quote")
_handler_bot_quote(buffer, fd);
}

/**
* Handles the bot command "marvin" for a specific client.
*
Expand Down
26 changes: 16 additions & 10 deletions 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/06/01 09:22:38 by yde-goes ### ########.fr */
/* Updated: 2024/06/01 17:02:12 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -348,10 +348,13 @@ void Server::_receive_new_data(const int fd)
}
else
{
cli->set_buffer(buffer);
if (cli->get_buffer().find_first_of(LINE_FEED) == std::string::npos)
return;
_execute_command(cli->get_buffer(), fd);
buffer[bytes] = '\0';
cli->append_to_buffer(buffer);
if (cli->get_buffer().find_first_of(CRLF) != std::string::npos)
{
_execute_command(cli->get_buffer(), fd);
cli->clear_buffer();
}
}
}

Expand Down Expand Up @@ -398,11 +401,7 @@ const Server::command_handler Server::_command_list[_command_list_size] = {
{"PASS", &Server::_handler_client_password},
{"INVITE", &Server::_handler_client_invite},
{"PRIVMSG", &Server::_handler_client_privmsg},
{"!MARVIN", &Server::_handler_bot_marvin},
{"!TIME", &Server::_handler_bot_time},
{"!WHOIS", &Server::_handler_bot_whois},
{"!WHOAMI", &Server::_handler_bot_whoami},
{"!QUOTE", &Server::_handler_bot_quote},
{"MARVINBOT", &Server::_handler_bot_input},
};

/**
Expand All @@ -427,6 +426,7 @@ void Server::_execute_command(const std::string buffer, const int fd)

if (buffer.empty())
return;

std::string clean_buffer = _cleanse_buffer(buffer, CRLF);
std::vector<std::string> splitted_buffer =
_split_buffer(clean_buffer, DELIMITER);
Expand All @@ -437,6 +437,12 @@ void Server::_execute_command(const std::string buffer, const int fd)

for (size_t i = 0; i < _command_list_size; i++)
{
if (command == "WHO")
{
cmd_executed = true;
break;
}

if (command == _command_list[i].command)
{
(this->*_command_list[i].handler)(parameters, fd);
Expand Down
10 changes: 8 additions & 2 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/06/01 09:03:41 by gilmar ### ########.fr */
/* Updated: 2024/06/01 15:54:13 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -54,6 +54,12 @@ void Server::_handler_client_mode(const std::string& buffer, const int fd)
Client* client = _get_client(fd);
Channel* channel = _get_channel(channelName);

if (modeFlags.empty())
{
_reply_code = 461;
return;
}

if (channelName.empty() || modeFlags.empty())
{
_send_response(fd, ERR_NEEDMOREPARAMS(client->get_nickname()));
Expand Down
Loading