-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '12-bonus-enh-creacin-de-un-bot-sencillo'
- Loading branch information
Showing
15 changed files
with
699 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,5 +33,6 @@ | |
|
||
# Other files | ||
ircserv | ||
bot | ||
.DS_Store | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.