Skip to content

Commit

Permalink
Basic implementation of Join command
Browse files Browse the repository at this point in the history
  • Loading branch information
jdomingu98 committed Mar 22, 2024
1 parent 0c841f1 commit e5792b7
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 6 deletions.
17 changes: 13 additions & 4 deletions includes/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class User;
*/
class Channel {
private:
std::string _name;
std::string _password;
std::vector<User> _users;
std::vector<User> _operators;
std::string _name;
std::string _password;
std::vector<User> _users;
std::vector<User> _operators;
std::vector<std::string> _inviteList;
std::vector<std::string> _banList;
std::string _topic;
std::string _modes;
int _limit;
Expand All @@ -30,6 +32,7 @@ class Channel {
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
Expand All @@ -38,6 +41,7 @@ class 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;
Expand All @@ -53,6 +57,11 @@ class Channel {

//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;
void addUser(User user);
void removeUser(std::string nickname);
void removeOper(std::string nickname);
Expand Down
1 change: 1 addition & 0 deletions includes/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Server {
void attemptUserRegistration(int clientFd);

void addChannel(Channel channel);
std::vector<Channel> getChannels() const;
void removeChannel(std::string channelName);
};

Expand Down
28 changes: 28 additions & 0 deletions includes/commands/JoinCommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef JOIN_COMMAND_HPP
# define JOIN_COMMAND_HPP

# include <map>

# include "ICommand.hpp"

# include "Server.hpp"

# include "libsUtils.hpp"
# include "utils.hpp"

/**
* An ICommand implementation that is responsible for the binding and creation of a channel.
*
*/
class JoinCommand : public ICommand {
private:
std::map<std::string, std::string> _channels; //key: channelName, value: channelPassword

public:
JoinCommand(std::string channels, std::string keys);
~JoinCommand();

void execute(Server &server, int fd);
};

#endif
40 changes: 40 additions & 0 deletions includes/exceptions/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,44 @@ class PasswordMismatchException : public IRCException {
PasswordMismatchException() : IRCException("464", ":Password incorrect") {}
};

/**
* This exception is thrown when an user attemps to join a channel that is invite-only and it was not invited.
*/
class InviteOnlyChanException : public IRCException {
public:
InviteOnlyChanException(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)") {}
};

/**
* 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)") {}
};

/**
* 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)") {}
};

/**
* 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") {}
};

#endif
4 changes: 3 additions & 1 deletion includes/parser/CommandParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# include "PassParser.hpp"
# include "NickParser.hpp"
# include "QuitParser.hpp"
# include "JoinParser.hpp"

# include "libsUtils.hpp"

Expand All @@ -26,7 +27,8 @@ enum Commands {
TOPIC,
MODE,
KICK,
INVITE
INVITE,
OPER
};

/**
Expand Down
18 changes: 18 additions & 0 deletions includes/parser/JoinParser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef JOIN_PARSER_HPP
# define JOIN_PARSER_HPP

# include "IParser.hpp"

# include "JoinCommand.hpp"

# include "libsUtils.hpp"

/**
* An IParser implementation that is responsible for parsing the JOIN command.
*/
class JoinParser : public IParser {
public:
ICommand *parse(const std::vector<std::string>& tokens);
};

#endif
2 changes: 2 additions & 0 deletions includes/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

# include <string>
# include <vector>

std::string trim(const std::string& str);
std::vector<std::string> split(const std::string &s, char delim);

#endif
79 changes: 78 additions & 1 deletion src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ std::vector<User>::iterator Channel::findOper(std::string nickname) {
return this->_operators.end();
}

/**
* This function aims to checks if the modes of the param matchs the ones of the channel.
*
* @param modesToCheck The modes to check.
*/
bool Channel::isModesSet(std::string modesToCheck) const {
for (size_t i = 0; i < modesToCheck.size(); i++) {
if (this->_modes.find(modesToCheck[i]) == std::string::npos)
return false;
}
return true;
}

/**
* This function aims to get the name of the channel.
*
Expand All @@ -77,6 +90,15 @@ std::string Channel::getName() const{
return this->_name;
}

/**
* This function aims to get the password of the channel.
*
* @return The password of the channel.
*/
std::string Channel::getPassword() const{
return this->_password;
}

/**
* This function aims to get the users of the channel.
*
Expand Down Expand Up @@ -173,6 +195,61 @@ bool Channel::checkPassword(std::string password) const {
return this->_password == password;
}

/**
* This function aims to check if the channel is invite only.
*
* @return `true` if the channel is invite only, `false` otherwise.
*/
bool Channel::isInviteOnly() const {
return isModesSet("i");
}

/**
* This function aims to check if the user is invited to the channel.
*
* @param nickname The nickname of the user.
*
* @return `true` if the user is invited, `false` otherwise.
*/
bool Channel::isUserInvited(std::string nickname) const {
return std::find(this->_inviteList.begin(), this->_inviteList.end(), nickname) != this->_inviteList.end();
}

/**
* This function aims to check if the user is banned from the channel.
*
* @param nickname The nickname of the user.
* @param username The username of the user.
* @param hostname The hostname of the user.
*
* @return `true` if the user is banned, `false` otherwise.
*/
bool Channel::isUserBanned(std::string nickname, std::string username, std::string hostname) const {
return std::find(this->_banList.begin(), this->_banList.end(), nickname) != this->_banList.end()
|| std::find(this->_banList.begin(), this->_banList.end(), username) != this->_banList.end()
|| std::find(this->_banList.begin(), this->_banList.end(), hostname) != this->_banList.end();
}

/**
* This function aims to check if the channel has a limit of users.
*
* @return `true` if the channel has a limit of users, `false` otherwise.
*/
bool Channel::hasLimit() const {
return this->_limit != NO_LIMIT;
}

/**
* This function aims to check if the channel is full.
*
* @return `true` if the channel is full, `false` otherwise.
*/
bool Channel::isFull() const {
return this->_users.size() + this->_operators.size() == this->_limit;
}



/**
* This function aims to add a user to the channel.
*
Expand Down Expand Up @@ -205,7 +282,7 @@ void Channel::removeUser(std::string nickname) {
/**
* This function aims to add an operator to the channel.
*
* @param user The operator to add.
* @param nickname The operator to add.
*
* @throw `ChannelException` If the operator is already in the channel.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ void Server::addChannel(Channel channel) {
this->_channels.push_back(channel);
}

/**
* This function aims to get the channels of the server.
*
* @return The channels of the server.
*/
std::vector<Channel> Server::getChannels() const {
return this->_channels;
}

/**
* This function aims to remove a channel from the server.
*
Expand Down
Loading

0 comments on commit e5792b7

Please sign in to comment.