Skip to content

Commit

Permalink
Merge branch '12-bonus-enh-creacin-de-un-bot-sencillo'
Browse files Browse the repository at this point in the history
  • Loading branch information
ruzafa8 committed May 10, 2024
2 parents 1fe0bd0 + bfd4348 commit 73d3e6e
Show file tree
Hide file tree
Showing 15 changed files with 699 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@

# Other files
ircserv
bot
.DS_Store
.vscode
56 changes: 46 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NAME = ircserv
BOT = bot
CXX = c++
FLAGS = -Wall -Werror -Wextra -std=c++98
RM = rm -rf
Expand All @@ -9,7 +10,7 @@ PASS ?= 1111
# =================================================================================

INCLUDES_PATH = includes
INCLUDES_SUBDIRS = commands parser exceptions
INCLUDES_SUBDIRS = commands parser exceptions irc_bot
INCLUDES_DIRS = $(INCLUDES_PATH) $(addprefix $(INCLUDES_PATH)/, $(INCLUDES_SUBDIRS))

HEADERS = $(addprefix -I , $(INCLUDES_DIRS))
Expand All @@ -19,6 +20,10 @@ HEADERS = $(addprefix -I , $(INCLUDES_DIRS))
SRC_DIR = src
CMD_DIR = $(SRC_DIR)/commands/
PARSER_DIR = $(SRC_DIR)/parser/
BOT_DIR = $(SRC_DIR)/irc_bot
BOT_MSG_DIR = $(BOT_DIR)/message/
BOT_RESP_DIR = $(BOT_DIR)/response/


CMD_PREFIXS = A User Nick Pass Quit PrivateMessage Join Part Invite Mode Kick Topic Notice
CMD_FILES = $(addsuffix Command, $(CMD_PREFIXS))
Expand All @@ -30,24 +35,44 @@ PARSER_SRCS = $(addprefix $(PARSER_DIR), $(PARSER_FILES))

FILES = main Server User Channel Utils Logger Responses

SRCS_PATHS = $(addprefix $(SRC_DIR)/, $(FILES)) $(CMD_SRCS) $(PARSER_SRCS)
SRCS = $(addsuffix .cpp, $(SRCS_PATHS))
SRCS_PATHS = $(addprefix $(SRC_DIR)/, $(FILES)) $(CMD_SRCS) $(PARSER_SRCS)
SRCS = $(addsuffix .cpp, $(SRCS_PATHS))

# =================================================================================

BPREFIXS = Who Down Up
# Bonus part (file transfer)
BPREFIXS = Who Down Up
BCMD_FILES = $(addsuffix Command, $(BPREFIXS))
BCMD_SRCS = $(addprefix $(CMD_DIR), $(BCMD_FILES))

BPARSER_FILES = $(addsuffix Parser, $(BPREFIXS))
BPARSER_SRCS = $(addprefix $(PARSER_DIR), $(BPARSER_FILES))

BSRCS_PATHS = $(BCMD_SRCS) $(BPARSER_SRCS)
BSRCS_PATHS = $(BCMD_SRCS) $(BPARSER_SRCS)
BSRCS = $(addsuffix .cpp, $(BSRCS_PATHS))

OBJS = $(SRCS:.cpp=.o)
BOBJS = $(BSRCS:.cpp=.o)

# =================================================================================

# Bonus part (bot)
BOT_MSG_PREFIXS = Message SenderEntity
BOT_MSG_FILES = $(addsuffix .cpp, $(addprefix $(BOT_MSG_DIR), $(BOT_MSG_PREFIXS)))

BOT_RESP_PREFIXS = ResponseBuilder
BOT_RESP_FILES = $(addsuffix .cpp, $(addprefix $(BOT_RESP_DIR), $(BOT_RESP_PREFIXS)))

BOT_FILES = GlaskBot IRCClient bot
BOT_SRCS = $(BOT_MSG_FILES) $(BOT_RESP_FILES) $(addsuffix .cpp, $(addprefix $(BOT_DIR)/, $(BOT_FILES)))

#BOT_SRCS = src/irc_bot/GlaskBot.cpp src/irc_bot/IRCClient.cpp src/irc_bot/bot.cpp\
src/irc_bot/message/Message.cpp src/irc_bot/message/SenderEntity.cpp\
src/irc_bot/response/ResponseBuilder.cpp
BOT_OBJS = $(BOT_SRCS:.cpp=.o)

# =================================================================================

all: $(NAME)

%.o: %.cpp
Expand All @@ -56,10 +81,10 @@ all: $(NAME)
$(NAME): $(OBJS)
$(CXX) $(FLAGS) $(HEADERS) $(SRCS) -o $(NAME)

clean:
clean: bot_clean
$(RM) $(OBJS) $(BOBJS) files

fclean: clean
fclean: clean bot_fclean
$(RM) $(NAME)

re: fclean all
Expand All @@ -72,7 +97,7 @@ a: $(NAME)
clear
./$(NAME) $(PORT) $(PASS)

bonus: $(OBJS) $(BOBJS)
bonus: $(OBJS) $(BOBJS) $(BOT)
$(CXX) $(FLAGS) -D BONUS $(HEADERS) $(SRCS) $(BSRCS) -o $(NAME)
$(RM) files
mkdir files
Expand All @@ -85,6 +110,17 @@ ba: bonus
clear
./$(NAME) $(PORT) $(PASS)

bre: fclean bonus
bre: fclean bonus $(BOT)

# Bonus rules (bot)

$(BOT): $(BOT_OBJS)
$(CXX) $(FLAGS) $(HEADERS) $(BOT_OBJS) -o $(BOT)

bot_clean:
$(RM) $(BOT_OBJS)

bot_fclean: bot_clean
$(RM) $(BOT)

.PHONY: a all ba be bre bonus clean e fclean re
.PHONY: a all ba be bre bonus clean e fclean re bot_clean bot_fclean
31 changes: 31 additions & 0 deletions includes/irc_bot/BadUsageException.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef BAD_USAGE_EXCEPTION_HPP
# define BAD_USAGE_EXCEPTION_HPP

# include <exception>
# include <string>

# define USAGE_EXCEPTION_MESSAGE(message) "usage: " + (message)

/**
* Exception thrown when a command is used incorrectly
*/
class BadUsageException : public std::exception {
private:
std::string _message;
std::string _to;
public:
BadUsageException(const std::string &to, const std::string &message)
: _message(USAGE_EXCEPTION_MESSAGE(message)), _to(to) {};

virtual ~BadUsageException() throw() {}

virtual const char *what() const throw() {
return _message.c_str();
};

const std::string &getTo() const {
return _to;
}
};

#endif
38 changes: 38 additions & 0 deletions includes/irc_bot/GlaskBot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef GLASK_BOT_HPP
# define GLASK_BOT_HPP

# include <sstream>
# include <iostream>
# include <map>

# include "IRCClient.hpp"
# include "Message.hpp"
# include "ResponseBuilder.hpp"
# include "BadUsageException.hpp"

# define BOT_NAME "glask"

# define BOT_JOINNING_MESSAGE "Uoola a todos!! Soy GlaskBot, un bot creado por el grupo de desarrollo de Glask. Si necesitas ayuda, no dudes en preguntar. ¡Estoy aquí para ayudarte!"

/**
* This class aims to provide a simple interface to interact with an IRC server.
*/
class GlaskBot : public IRCClient {
private:
static std::vector<std::string> split(const std::string &response);
std::map<std::string, std::string> welcomeMessages; // <channel, message>
void onPrivateMessage(const Message &message);
void onJoin(const Message &message);

void joinChannel(const std::string &destination, const std::vector<std::string> &messageParts);
void setMessage(const std::vector<std::string> &messageParts);

bool isChannel(const std::string &receiver);

public:
GlaskBot(const std::string &address, int port, const std::string &password);
virtual ~GlaskBot();
void handleResponse(const Message &message);
};

#endif
36 changes: 36 additions & 0 deletions includes/irc_bot/IRCClient.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef IRC_CLIENT_HPP
# define IRC_CLIENT_HPP

# include <iostream>
# include <string>
# include <arpa/inet.h>
# include <cstring>
# include <unistd.h>
# include <vector>
# include "Message.hpp"

# define BUFFER_SIZE 1024

/**
* This class aims to provide a simple interface to interact with an IRC server.
*/
class IRCClient {
private:
bool _connectionEstablished;
int _sock;
const std::string &_address;
int port;

void conn();
void receive();

public:
IRCClient(const std::string &, int);
virtual ~IRCClient();
bool sendData(const std::string &);
void startLoop();

virtual void handleResponse(const Message &message) = 0;
};

#endif
25 changes: 25 additions & 0 deletions includes/irc_bot/MalformedException.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef MALFORMED_EXCEPTION_HPP
# define MALFORMED_EXCEPTION_HPP

# include <exception>
# include <string>

# define MALFORMED_EXCEPTION_MESSAGE(message) "Malformed message: " + (message)

/**
* Exception thrown when a message is malformed
*/
class MalformedException : public std::exception {
private:
std::string _message;
public:
MalformedException(const std::string &message)
: _message(MALFORMED_EXCEPTION_MESSAGE(message)) {};

virtual ~MalformedException() throw() {}

virtual const char *what() const throw() {
return _message.c_str();
};
};
#endif
28 changes: 28 additions & 0 deletions includes/irc_bot/Message.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef MESSAGE_HPP
# define MESSAGE_HPP

# include <vector>
# include <string>
# include <sstream>
# include <iostream>
# include "SenderEntity.hpp"

/**
* This class represents a message received from an IRC server.
*/
class Message {
private:
SenderEntity *_sender;
std::string _command;
std::string _params;
std::vector<std::string> splitParams(const std::string &response);

public:
Message(const std::string &senderData);
~Message();
const SenderEntity *getSender() const;
const std::string &getCommand() const;
const std::string &getParams() const;
};

#endif
24 changes: 24 additions & 0 deletions includes/irc_bot/ResponseBuilder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef RESPONSE_BUILDER_HPP
# define RESPONSE_BUILDER_HPP

# include <string>

/**
* This class is responsible for building responses to the IRC server.
*/
class ResponseBuilder {
private:
ResponseBuilder();

public:
static std::string join(const std::string &channel);
static std::string part(const std::string &channel);
static std::string privmsg(const std::string &destination, const std::string &message);
static std::string user(const std::string &username, const std::string &hostname, const std::string &serverName, const std::string &realName);
static std::string nick(const std::string &nick);
static std::string pass(const std::string &password);

~ResponseBuilder();
};

#endif
28 changes: 28 additions & 0 deletions includes/irc_bot/SenderEntity.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef SENDER_ENTITY_HPP
# define SENDER_ENTITY_HPP

# include <string>
# include "MalformedException.hpp"

/**
* This class represents the entity that sent a message.
*/
class SenderEntity {
private:
bool _isServer;
std::string _serverName;
std::string _nickname;
std::string _username;
std::string _hostname;

public:
SenderEntity(const std::string &senderData);

bool isServer() const;
const std::string &getServerName() const;
const std::string &getNickname() const;
const std::string &getHostname() const;
const std::string &getUsername() const;
};

#endif
Loading

0 comments on commit 73d3e6e

Please sign in to comment.