diff --git a/include/Client.hpp b/include/Client.hpp index beb1841..849448b 100644 --- a/include/Client.hpp +++ b/include/Client.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Client.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: caalbert +#+ +:+ +#+ */ +/* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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; @@ -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; diff --git a/include/Replies.hpp b/include/Replies.hpp index 2e5095c..d5afc7c 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/06/01 09:34:16 by yde-goes ### ########.fr */ +/* Updated: 2024/06/01 17:01:11 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -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 + \ @@ -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 + \ @@ -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 diff --git a/include/Server.hpp b/include/Server.hpp index 4484b10..600e7db 100644 --- a/include/Server.hpp +++ b/include/Server.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Server.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: caalbert +#+ +:+ +#+ */ +/* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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); diff --git a/src/Client.cpp b/src/Client.cpp index 5d1f207..a4e0853 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Client.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: gilmar +#+ +:+ +#+ */ +/* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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. * diff --git a/src/MarvinBot.cpp b/src/MarvinBot.cpp index a2d49cc..e399c3a 100644 --- a/src/MarvinBot.cpp +++ b/src/MarvinBot.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* MarvinBot.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: gilmar +#+ +:+ +#+ */ +/* By: yde-goes +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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. * diff --git a/src/Server.cpp b/src/Server.cpp index b5363b2..91f3775 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/06/01 09:22:38 by yde-goes ### ########.fr */ +/* Updated: 2024/06/01 17:02:12 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -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(); + } } } @@ -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}, }; /** @@ -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 splitted_buffer = _split_buffer(clean_buffer, DELIMITER); @@ -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); diff --git a/src/commands/Mode.cpp b/src/commands/Mode.cpp index b09dc78..490b4d4 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/06/01 09:03:41 by gilmar ### ########.fr */ +/* Updated: 2024/06/01 15:54:13 by yde-goes ### ########.fr */ /* */ /* ************************************************************************** */ @@ -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()));