Skip to content

Commit

Permalink
Merge pull request #44 from jdomingu98/8-comando-invite
Browse files Browse the repository at this point in the history
Comando invite
  • Loading branch information
ruzafa8 authored Mar 28, 2024
2 parents 8a65020 + 9436628 commit f11abf9
Show file tree
Hide file tree
Showing 17 changed files with 407 additions and 114 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ SRC_DIR = src
CMD_DIR = $(SRC_DIR)/commands/
PARSER_DIR = $(SRC_DIR)/parser/

CMD_PREFIXS = User Nick Pass Quit PrivateMessage Join
CMD_PREFIXS = User Nick Pass Quit PrivateMessage Join Invite
CMD_FILES = $(addsuffix Command, $(CMD_PREFIXS))
CMD_SRCS = $(addprefix $(CMD_DIR), $(CMD_FILES))

PARSER_PREFIXS = Command User Pass Nick Quit PrivateMessage Join
PARSER_PREFIXS = Command User Pass Nick Quit PrivateMessage Join Invite
PARSER_FILES = $(addsuffix Parser, $(PARSER_PREFIXS))
PARSER_SRCS = $(addprefix $(PARSER_DIR), $(PARSER_FILES))

Expand Down
58 changes: 36 additions & 22 deletions includes/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,59 @@ class Channel {
int _limit;
bool _passwordSet;

// Iterators
std::vector<User>::iterator findUser(const std::string &nickname);
std::vector<User>::const_iterator findUser(const std::string &nickname) const;
std::vector<User>::iterator findOper(const std::string &nickname);
std::vector<User>::const_iterator findOper(const std::string &nickname) const;

// Other Operations
bool checkChannelName(std::string name) const;
std::vector<User>::iterator findUser(std::string nickname);
std::vector<User>::iterator findOper(std::string nickname);
bool isModesSet(std::string modesToCheck) const;

public:
//Constructors and destructor
// Constructors and destructor
Channel(std::string name, User user);
~Channel();

//Getters
std::string getName() const;
std::string getPassword() const;
std::vector<User> getUsers() const;
std::vector<User> getOperators() const;
std::vector<User> getAllUsers() const;
std::string getTopic() const;
std::string getModes() const;
bool isPasswordSet() const;
// Getters
std::string getName() const;
std::string getPassword() const;
std::vector<User> getUsers() const;
std::vector<User> getOperators() const;
std::vector<User> getAllUsers() const;
std::string getTopic() const;
std::string getModes() const;
bool isPasswordSet() const;


//Setters
// Setters
void setPassword(std::string password);
void setTopic(std::string topic);
void changeMode(std::string modes);

//Operations
bool checkPassword(std::string password) const;
bool isInviteOnly() const;
bool isUserInvited(std::string nickname) const;
bool isUserBanned(std::string nickname, std::string username, std::string hostname) const;
bool hasLimit() const;
bool isFull() const;
// User
void addUser(User user);
void addOper(User user);
void removeUser(std::string nickname);
bool isUserBanned(std::string nickname, std::string username, std::string hostname) const;
bool isUserInChannel(const std::string &nickname) const;

// Oper
void addOper(User user);
void removeOper(std::string nickname);
void makeUserAnOper(std::string nickname);
void makeOperAnUser(std::string nickname);
bool isOper(const std::string &nickname) const;

// Invite
void inviteUser(const std::string &nickname);
bool isUserInvited(std::string nickname) const;
bool isInviteOnly() const;

// Other Operations
bool checkPassword(std::string password) const;
bool hasLimit() const;
bool isFull() const;
};

#endif
46 changes: 29 additions & 17 deletions includes/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,49 @@ class Server {
static Server *_server;
Server(const std::string port, const std::string password);

bool isValidPort(const std::string port);
// Server logic
bool isValidPort(const std::string &port) const;
void initServer();
void listenClients();
void handleNewConnection(int numFds);
void handleExistingConnection(int fd);
void closeConnections();

// User Iterators
std::vector<User>::iterator findUserByFd(int clientFd);
std::vector<User>::iterator findUserByNickname(std::string nickname);
std::vector<User>::const_iterator findUserByFd(int clientFd) const;
std::vector<User>::iterator findUserByNickname(const std::string &nickname);
std::vector<User>::const_iterator findUserByNickname(const std::string &nickname) const;

// Channel Iterators
std::vector<Channel>::iterator findChannel(const std::string &channelName);
std::vector<Channel>::const_iterator findChannel(const std::string &channelName) const;

public:
~Server();

// Singleton Pattern
static void init(std::string port, std::string password);
static Server &getInstance();

std::vector<Channel>::iterator findChannel(std::string channelName);

//Getters
User &getUserByFd(int clientFd);
User &getUserByNickname(const std::string& nickname);

//Operations
void sendMessage(int clientFd, const std::string& message) const;
bool isValidPassword(const std::string& password);
bool isNicknameInUse(const std::string& nickname);
bool userHasCheckedPassword(int clientFd);
void removeUser(int clientFd);
void attemptUserRegistration(int clientFd);
// User
User &getUserByFd(int clientFd);
const User &getUserByFd(int clientFd) const;
User &getUserByNickname(const std::string& nickname);
bool isNicknameInUse(const std::string& nickname) const;
void removeUser(int clientFd);
void attemptUserRegistration(int clientFd);

void addChannel(Channel channel);
// Channel
std::vector<Channel> getChannels() const;
void removeChannel(std::string channelName);
Channel &getChannelByName(const std::string &channelName);
bool channelExists(const std::string &channelName) const;
void addChannel(Channel channel);
void removeChannel(std::string channelName);

// Other Operations
void sendMessage(int clientFd, const std::string& message) const;
bool isValidPassword(const std::string& password) const;
};

#endif
17 changes: 7 additions & 10 deletions includes/User.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ class Server;
*/
class User {
private:
int _fd;
bool _passwordChecked;
bool _registered;
int _fd;
bool _registered;
std::string _username;
std::string _hostname;
std::string _serverName;
Expand All @@ -26,11 +25,11 @@ class User {
std::string _password;
std::vector<Channel> _channels;

std::vector<Channel>::const_iterator findChannel(std::string channelName) const;
std::vector<Channel>::iterator findChannel(std::string channelName);
// Iterators
std::vector<Channel>::const_iterator findChannel(const std::string &channelName) const;
std::vector<Channel>::iterator findChannel(const std::string &channelName);

public:
// Constructors and destructors
User(int fd);
~User();

Expand All @@ -39,7 +38,6 @@ class User {
std::string getNickname() const;
std::string getUsername() const;
std::string getHostname() const;
bool isPasswordChecked() const;
bool isUserInMaxChannels() const;
bool isAlreadyInChannel(std::string channelName) const;
bool isRegistered() const;
Expand All @@ -53,11 +51,10 @@ class User {
void setPassword(const std::string& password);

// Operations
void checkPassword();
void makeRegistration();
bool canRegister();
bool canRegister() const;
void addChannel(Channel &channel);
void sendPrivateMessageToUser(const User &destination, const std::string& message);
void sendPrivateMessageToUser(const User &destination, const std::string& message) const;
};

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

# include "libsUtils.hpp"
# include "ICommand.hpp"
# include "Server.hpp"

/**
* A class that represents the command INVITE.
*/
class InviteCommand : public ICommand {
private:
std::string _nickname;
std::string _channelName;

public:
InviteCommand();
InviteCommand(const std::string& nickname, const std::string& channelName);
~InviteCommand();

void execute(int clientFd);
};

#endif
65 changes: 49 additions & 16 deletions includes/exceptions/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
// # define ERR_TOO_MANY_CHANNELS(channelName) (channelName) " :You have joined too many channels"
// # define RPL_TOPIC(channel, topic) (channel) " :" (topic)

// #define ERR_NOT_ON_CHANNEL(channel) (channel) " :You're not on that channel"
# define ERR_NOT_ON_CHANNEL " :You're not on that channel"

// #define RPL_NO_TOPIC(channel) (channel) " :No topic is set"
// #define ERR_CHAN_O_PRIVS_NEEDED(channel) (channel) " :You're not channel operator"
# define ERR_CHAN_O_PRIVS_NEEDED " :You're not channel operator"

// #define ERR_NO_SUCH_NICK(nickname) (nickname) " :No such nick/channel"
// #define ERR_USER_ON_CHANNEL(nickname, channel) (nickname) " " (channel) " :is already on channel"
# define ERR_NO_SUCH_NICK " :No such nick/channel"
# define ERR_USER_ON_CHANNEL " :is already on channel"
// #define RPL_INVITING(channel, nickname) (channel) " " (nickname)
// #define RPL_AWAY(nickname, awayMessage) (nickname) " :" (awayMessage)

#define ERR_NO_RECIPIENT(command) (":No recipient given (" + command + ")")
#define ERR_NO_TEXT_TO_SEND ":No text to send"
# define ERR_NO_RECIPIENT(command) (":No recipient given (" + command + ")")
# define ERR_NO_TEXT_TO_SEND ":No text to send"
// #define ERR_CANNOT_SEND_TO_CHAN(channel) (channel) " :Cannot send to channel"
// #define ERR_NO_TOP_LEVEL(mask) (mask) " :No toplevel domain specified"
// #define ERR_WILD_TOP_LEVEL(mask) (mask) " :Wildcard in toplevel domain"
Expand Down Expand Up @@ -68,15 +68,15 @@ class AlreadyRegisteredException : public IRCException {
*/
class ErroneousNicknameException : public IRCException {
public:
ErroneousNicknameException(const std::string nickname) : IRCException("432", nickname + ERR_ERRONEOUS_NICKNAME) {}
ErroneousNicknameException(const std::string &nickname) : IRCException("432", nickname + ERR_ERRONEOUS_NICKNAME) {}
};

/**
* This exception is thrown when a command is missing parameters.
*/
class NeedMoreParamsException : public IRCException {
public:
NeedMoreParamsException(const std::string command) : IRCException("461", command + ERR_NEED_MORE_PARAMS) {}
NeedMoreParamsException(const std::string &command) : IRCException("461", command + ERR_NEED_MORE_PARAMS) {}
};

/**
Expand All @@ -85,7 +85,7 @@ class NeedMoreParamsException : public IRCException {
*/
class NickCollisionException : public IRCException {
public:
NickCollisionException(const std::string nickname) : IRCException("436", nickname + ERR_NICK_COLLISION) {}
NickCollisionException(const std::string &nickname) : IRCException("436", nickname + ERR_NICK_COLLISION) {}
};

/**
Expand All @@ -94,7 +94,7 @@ class NickCollisionException : public IRCException {
*/
class NicknameInUseException : public IRCException {
public:
NicknameInUseException(const std::string nickname) : IRCException("433", nickname + ERR_NICKNAME_IN_USE) {}
NicknameInUseException(const std::string &nickname) : IRCException("433", nickname + ERR_NICKNAME_IN_USE) {}
};

/**
Expand Down Expand Up @@ -126,47 +126,47 @@ class PasswordMismatchException : public IRCException {
*/
class InviteOnlyChanException : public IRCException {
public:
InviteOnlyChanException(std::string channelName) : IRCException("473", channelName + " :Cannot join channel (+i)") {}
InviteOnlyChanException(const std::string &channelName) : IRCException("473", channelName + " :Cannot join channel (+i)") {}
};

/**
* This exception is thrown when an user attemps to join a channel where it was banned previously.
*/
class BannedFromChanException : public IRCException {
public:
BannedFromChanException(std::string channelName) : IRCException("474", channelName + " :Cannot join channel (+b)") {}
BannedFromChanException(const std::string &channelName) : IRCException("474", channelName + " :Cannot join channel (+b)") {}
};

/**
* This exception is thrown when a channel password is incorrect.
*/
class BadChannelKeyException : public IRCException {
public:
BadChannelKeyException(std::string channelName) : IRCException("475", channelName + " :Cannot join channel (+k)") {}
BadChannelKeyException(const std::string &channelName) : IRCException("475", channelName + " :Cannot join channel (+k)") {}
};

/**
* This exception is thrown when the channel is full
*/
class ChannelIsFullException : public IRCException {
public:
ChannelIsFullException(std::string channelName) : IRCException("471", channelName + " :Cannot join channel (+l)") {}
ChannelIsFullException(const std::string &channelName) : IRCException("471", channelName + " :Cannot join channel (+l)") {}
};

/**
* This exception is thrown when a user joins too many channels.
*/
class TooManyChannelsException : public IRCException {
public:
TooManyChannelsException(std::string channelName) : IRCException("405", channelName + " :You have joined too many channels") {}
TooManyChannelsException(const std::string &channelName) : IRCException("405", channelName + " :You have joined too many channels") {}
};

/**
* This exception is thrown when the command does not provide a client or channel to send a message.
*/
class NoRecipientGivenException : public IRCException {
public:
NoRecipientGivenException(const std::string command) : IRCException("411", ERR_NO_RECIPIENT(command)) {}
NoRecipientGivenException(const std::string &command) : IRCException("411", ERR_NO_RECIPIENT(command)) {}
};

/**
Expand All @@ -177,4 +177,37 @@ class NoTextToSendException : public IRCException {
NoTextToSendException() : IRCException("412", ERR_NO_TEXT_TO_SEND) {}
};

/**
* This exception is thrown when the user or channel is not found.
*/
class NoSuchNickException : public IRCException {
public:
NoSuchNickException(const std::string &nickname) : IRCException("401", nickname + ERR_NO_SUCH_NICK) {}
};

/**
* This exception is thrown when the user is not on the channel.
*/
class NotOnChannelException : public IRCException {
public:
NotOnChannelException(const std::string &channelName) : IRCException("442", channelName + ERR_NOT_ON_CHANNEL) {}
};

/**
* This exception is thrown when the user invited is already on the channel.
*/
class UserOnChannelException : public IRCException {
public:
UserOnChannelException(const std::string &nickname, const std::string &channelName)
: IRCException("443", nickname + "" + channelName + ERR_USER_ON_CHANNEL) {}
};

/**
* This exception is thrown when the user needs to be an operator to perform the action.
*/
class ChanOPrivsNeededException : public IRCException {
public:
ChanOPrivsNeededException(const std::string &channelName) : IRCException("482", channelName + ERR_CHAN_O_PRIVS_NEEDED) {}
};

#endif
Loading

0 comments on commit f11abf9

Please sign in to comment.