Skip to content

Commit

Permalink
chore: merge pull request #27 from ygor-sena/7-b-implement-a-bot
Browse files Browse the repository at this point in the history
feat: refactor bot and add !quote to marvin commands
  • Loading branch information
ygor-sena authored May 30, 2024
2 parents 1147f8e + 1085685 commit a746d6d
Show file tree
Hide file tree
Showing 19 changed files with 482 additions and 230 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: caalbert <caalbert@student.42sp.org.br> +#+ +:+ +#+ #
# By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/04/23 19:40:24 by gilmar #+# #+# #
# Updated: 2024/05/27 20:14:13 by caalbert ### ########.fr #
# Updated: 2024/05/29 18:12:20 by yde-goes ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -39,13 +39,13 @@ SRC_DIRS += src
vpath %.hpp $(INC_DIRS)
vpath %.cpp $(SRC_DIRS)

HEADERS := Server.hpp Client.hpp Replies.hpp Channel.hpp Bot.hpp
HEADERS := Server.hpp Client.hpp Replies.hpp Channel.hpp MarvinBot.hpp
# If the line above is not enough, add the headers here
#HEADERS +=

SOURCES := main.cpp Client.cpp Server.cpp Channel.cpp Invite.cpp Join.cpp
SOURCES += Kick.cpp Nick.cpp Part.cpp Privmsg.cpp Quit.cpp Topic.cpp User.cpp
SOURCES += Mode.cpp Pass.cpp Bot.cpp
SOURCES += Mode.cpp Pass.cpp MarvinBot.cpp


OBJS := $(addprefix $(OBJ_DIR)/, $(SOURCES:.cpp=.o))
Expand Down
25 changes: 0 additions & 25 deletions include/Bot.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions include/Channel.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/21 08:24:04 by gilmar #+# #+# */
/* Updated: 2024/05/29 16:23:52 by yde-goes ### ########.fr */
/* Updated: 2024/05/30 13:11:15 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -43,6 +43,7 @@ class Channel
void remove_invite_only(void);
void remove_topic_restriction(void);
void remove_channel_operator(Client *client);
void remove_channel_client(Client *client);

void join(Client *client);
void kick(Client *client);
Expand All @@ -68,7 +69,6 @@ class Channel
bool _topic_restriction;
std::string _created_at;

// bool _has_password;
bool _has_key;
std::vector<Client *>_clients; // -> list of clients that are channel members
std::vector<Client *>_operator_clients; // -> list of channel operators
Expand Down
3 changes: 2 additions & 1 deletion include/Client.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/04/28 10:19:06 by gilmar #+# #+# */
/* Updated: 2024/05/29 16:23:31 by yde-goes ### ########.fr */
/* Updated: 2024/05/30 13:11:52 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -63,6 +63,7 @@ class Client //-> class for client

bool is_channel_invited(const std::string &channel);
void add_channel_invited(const std::string &channel);
void remove_channel_invited(const std::string &channel);

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

Expand Down
57 changes: 57 additions & 0 deletions include/MarvinBot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MarvinBot.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/27 11:31:32 by caalbert #+# #+# */
/* Updated: 2024/05/29 21:27:05 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef MARVINBOT_HPP
# define MARVINBOT_HPP

# include "Server.hpp"
# include "Replies.hpp"

# define SOCRATES_KNOWLEDGE "I know that I know nothing. - Socrates"
# define DESCARTES_EXISTENCE "I think, therefore I am. - René Descartes"
# define KANT_CATEGORICAL_IMPERATIVE "Act only according to that maxim whereby you can, at the same time, will that it should become a universal law. - Immanuel Kant"
# define NIETZSCHE_GOD "God is dead! - Friedrich Nietzsche"
# define PLATO_FORMS "The Forms are eternal and changeless. - Plato"
# define MARX_RELIGION "Religion is the opium of the people. - Karl Marx"
# define CONFUCIUS_WISDOM "Real knowledge is to know the extent of one's ignorance. - Confucius"
# define HUME_SCIENCE "A wise man proportions his belief to the evidence. - David Hume"
# define HEIDEGGER_BEING "Being is time, and time is finite. - Martin Heidegger"
# define ROUSSEAU_FREEDOM "Man is born free, and everywhere he is in chains. - Jean-Jacques Rousseau"

class MarvinBot
{
public:
MarvinBot();
~MarvinBot();

// Enum for quotes
enum EnumMarvinBotQuotes {
QUOTE_SOCRATES_KNOWLEDGE,
QUOTE_DESCARTES_EXISTENCE,
QUOTE_KANT_CATEGORICAL_IMPERATIVE,
QUOTE_NIETZSCHE_GOD,
QUOTE_PLATO_FORMS,
QUOTE_MARX_RELIGION,
QUOTE_CONFUCIUS_WISDOM,
QUOTE_HUME_SCIENCE,
QUOTE_HEIDEGGER_BEING,
QUOTE_ROUSSEAU_FREEDOM,
QUOTES_COUNT // To keep track of the number of quotes
};

const char* marvin_bot_quotes[QUOTES_COUNT];

private:
std::string _return_quote();
};

#endif // MARVINBOT_HPP
15 changes: 11 additions & 4 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 16:22:57 by yde-goes ### ########.fr */
/* Updated: 2024/05/29 19:19:18 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,6 +45,13 @@
# define RPL_INVITING(hostname, channelname, invitername, invitedname) (":" + hostname + " INVITE " + invitedname + " " + channelname + CRLF)
# define RPL_PRIVMSG(hostname, receiver, text) (":" + hostname + " PRIVMSG " + receiver + " " + text + CRLF)

# define BOT_CMDMARVIN(nickname) (": 4242 marvin_bot " + nickname + " :Hello, I am Marvin, the paranoid robot." + CRLF)
# define BOT_CMDTIME(nickname, time) (": 4242 marvin_bot " + nickname + " :Server time: " + time_str)
# define BOT_CMDWHOIS(nickname, username, ipaddr) (": 4242 marvin_bot " + nickname + " :Whois " + username + " " + ipaddr + " :End of WHOIS list" + CRLF)
# define BOT_CMDQUOTE(nickname, quote) (": 4242 marvin_bot " + nickname + " :" + quote + CRLF)
# define BOT_CLIENTNOTINCHANNEL(nickname) (": 2424 marvin_bot " + nickname + ": Must be part of at least one channel to use marvin commands" + CRLF)
# define BOT_WHOISDOESNTEXIST(nickname) (": 4242 marvin_bot " + nickname + " :No such user" + CRLF)

///////// ERRORS /////////
# define ERR_NEEDMODEPARM(channelname, mode) (": 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)
Expand All @@ -64,9 +71,9 @@

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

# define ERR_BADCHANNELKEY(clientnickname, channelname) (": 475 " + clientnickname + " " + channelname + " :Cannot join channel (incorrect key)" + CRLF)
# define ERR_INVITEONLYCHAN(clientnickname, channelname) (": 473 " + clientnickname + " " + channelname + " :Cannot join channel (+i)" + CRLF)
# define ERR_CHANNELISFULL(clientnickname, channelname) (": 422 " + clientnickname + " " + channelname + " :Cannot join channel (+l)" + CRLF)
# define ERR_BADCHANNELKEY(nickname, channelname) (": 475 " + nickname + " " + channelname + " :Cannot join channel (incorrect key)" + CRLF)
# 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)" + CRLF)
# define ERR_NOSUCHCHANNEL(channelname) (": 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)
Expand Down
16 changes: 8 additions & 8 deletions include/Server.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/04/28 10:23:47 by gilmar #+# #+# */
/* Updated: 2024/05/29 16:22:34 by yde-goes ### ########.fr */
/* Updated: 2024/05/29 19:17:18 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -55,8 +55,6 @@ class Server
Server(std::string password, std::vector<Client> _clients, std::vector<Channel> _channels);

void init(const std::string &port, const std::string &password);
Client* get_client(int fd);
void send_response(int fd, const std::string& message);

private:
int _port; //-> server port
Expand All @@ -73,6 +71,7 @@ class Server
void _is_valid_port(const std::string &port);
bool _is_valid_nickname(const std::string &nickname);
bool _is_nickname_in_use(const int fd, const std::string &nickname);
bool _is_client_in_any_channel(const int fd);

void _handler_client_join(const std::string &buffer, const int fd);
void _handler_client_quit(const std::string &buffer, const int fd);
Expand All @@ -85,9 +84,11 @@ class Server
void _handler_client_nickname(const std::string &nickname, const int fd);
void _handler_client_username(const std::string &username, const int fd);
void _handler_client_password(const std::string &password, const int fd);
void _handle_marvin(const std::string &buffer, int fd);
void _handle_time(const std::string &buffer, int fd);
void _handle_whois(const std::string &buffer, int fd);
void _handler_bot_marvin(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_whoami(const std::string &buffer, int fd);
void _handler_bot_quote(const std::string &buffer, int fd);

void _handler_invite_only_mode(Channel* channel, bool set);
void _handler_topic_restriction_mode(Channel* channel, bool set);
Expand All @@ -114,7 +115,7 @@ class Server
void (Server::*handler)(const std::string &, const int);
};

static const int _command_list_size = 15; //-> command list size
static const int _command_list_size = 16; //-> command list size
static const command_handler _command_list[_command_list_size]; //-> command list
void _execute_command(const std::string buffer, const int fd); //-> execute command

Expand All @@ -130,7 +131,6 @@ class Server
Channel* _get_channel(const std::string &channel_name); //-> get channel

std::string toupper(const std::string& str);
Client* get_client_by_nickname(const std::string& nickname); //-> get client by nickname
};


Expand Down
42 changes: 0 additions & 42 deletions src/Bot.cpp

This file was deleted.

55 changes: 23 additions & 32 deletions src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Channel.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gilmar <gilmar@student.42.fr> +#+ +:+ +#+ */
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:26:17 by gilmar #+# #+# */
/* Updated: 2024/05/28 21:37:58 by gilmar ### ########.fr */
/* Updated: 2024/05/30 13:13:05 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -157,6 +157,22 @@ void Channel::remove_channel_operator(Client *client)
}
}

void Channel::remove_channel_client(Client *client)
{
for (std::vector<Client *>::iterator it = this->_clients.begin(); it != this->_clients.end();)
{
if ((*it)->get_nickname() == client->get_nickname())
{
std::cout << (*it)->get_nickname() << std::endl;
it = this->_clients.erase(it);
break ;
}
else
++it;
}
return ;
}

/*
** --------------------------------- OTHERS ---------------------------------
*/
Expand Down Expand Up @@ -212,52 +228,27 @@ void Channel::join(Client *client)

void Channel::kick(Client *client)
{
for (std::vector<Client *>::iterator it = this->_clients.begin(); it != this->_clients.end(); ++it)
{
if((*it)->get_nickname() == client->get_nickname())
{
this->_clients.erase(it);
return ;
}
}
return ;
client->remove_channel_invited(this->get_name());
remove_channel_operator(client);
remove_channel_client(client);
}

void Channel::part(Client *client)
{
// Remove client from channel operators
for (std::vector<Client *>::iterator it_op = this->_operator_clients.begin(); it_op != this->_operator_clients.end(); ++it_op)
{
if((*it_op)->get_nickname() == client->get_nickname())
{
this->_operator_clients.erase(it_op);
return ;
}
}

for (std::vector<Client *>::iterator it = this->_clients.begin(); it != this->_clients.end(); ++it)
{
if((*it)->get_nickname() == client->get_nickname())
{
std::cout << (*it)->get_nickname() << std::endl;
this->_clients.erase(it);
return ;
}
}
remove_channel_operator(client);
remove_channel_client(client);
}

void Channel::broadcast(Client *sender, std::string target, std::string message)
{
for (std::vector<Client *>::iterator it = this->_clients.begin(); it != this->_clients.end(); it++) {
if (*it == sender)
continue ;
std::cout << "2) " << this->get_name() << " : " << (*it)->get_nickname() << std::endl;
(*it)->broadcast(sender, target, message);
}
for (std::vector<Client *>::iterator it = this->_operator_clients.begin(); it != this->_operator_clients.end(); it++) {
if (*it == sender)
continue ;
std::cout << "2) " << this->get_name() << " : " << (*it)->get_nickname() << std::endl;
(*it)->broadcast(sender, target, message);
}
return ;
Expand Down
Loading

0 comments on commit a746d6d

Please sign in to comment.