Skip to content

Commit

Permalink
fix: compile = true
Browse files Browse the repository at this point in the history
  • Loading branch information
ruzafa8 committed Mar 22, 2024
1 parent 09bc274 commit 0c06a60
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 70 deletions.
1 change: 1 addition & 0 deletions includes/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Channel {
bool hasLimit() const;
bool isFull() const;
void addUser(User user);
void addOper(User user);
void removeUser(std::string nickname);
void removeOper(std::string nickname);
void makeUserAnOper(std::string nickname);
Expand Down
3 changes: 2 additions & 1 deletion includes/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ class Server {
void closeConnections();
std::vector<User>::iterator findUserByFd(int clientFd);
std::vector<User>::iterator findUserByNickname(std::string nickname);
std::vector<Channel>::iterator findChannel(std::string channelName);

public:
Server(const std::string port, const std::string password);
~Server();

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

void sendMessage(int clientFd, const std::string& message);
bool isValidPassword(const std::string& password);
User &getUserByFd(int clientFd);
Expand Down
4 changes: 2 additions & 2 deletions includes/User.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class User {
int getFd() const;
std::string getNickname() const;
std::string getUsername() const;
std::string getHostname() const;
bool isPasswordChecked() const;
bool isUserInMaxChannels() const;
bool isAlreadyInChannel(std::string channelName) const;
Expand All @@ -55,8 +56,7 @@ class User {
void checkPassword();
void makeRegistration(Server &server);
bool canRegister();
void joinChannel(Channel channel);
void leaveChannel(std::string channelName);
void addChannel(Channel &channel);
};

#endif
2 changes: 1 addition & 1 deletion includes/commands/JoinCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JoinCommand : public ICommand {
std::map<std::string, std::string> _channels; //key: channelName, value: channelPassword

public:
JoinCommand(std::string channels, std::string keys);
JoinCommand(std::map<std::string, std::string> _channels);
~JoinCommand();

void execute(Server &server, int fd);
Expand Down
1 change: 1 addition & 0 deletions includes/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# include <string>
# include <vector>
# include <sstream>

std::string trim(const std::string& str);
std::vector<std::string> split(const std::string &s, char delim);
Expand Down
11 changes: 11 additions & 0 deletions src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ void Channel::addUser(User user) {
this->_users.push_back(user);
}

/**
* This function aims to add an operator to the channel.
*
* @param user The operator to add.
*
* @throw `ChannelException` If the operator is already in the channel.
*/
void Channel::addOper(User user) {
this->_operators.push_back(user);
}

/**
* This function aims to remove a user from the channel.
*
Expand Down
35 changes: 12 additions & 23 deletions src/User.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ void User::setNickname(const std::string& nickname) {
this->_nickname = nickname;
}

/**
* This function aims to get the hostname of the user.
*
* @return The hostname of the user.
*/
std::string User::getHostname() const {
return this->_hostname;
}

/**
* This function aims to set the password of the user.
*
Expand Down Expand Up @@ -192,30 +201,10 @@ bool User::isRegistered() const {
}

/**
* This function aims to join a channel.
* This function aims to add a channel to the user.
*
* @param channel The channel to join.
* @param channel The channel to be added.
*/
void User::joinChannel(Channel channel) {
if (isUserInMaxChannels())
//throw UserException(USER_CHANNEL_FULL_ERR);
if (isAlreadyInChannel(channel.getName()))
//throw UserException(USER_ALREADY_IN_CHANNEL_ERR);
// Comprobar que el canal es de solo invitación y puede entrar
// Comprobar que el canal tiene contraseña y la contraseña es correcta
// Comprobar que el canal tiene límite y no se ha alcanzado
void User::addChannel(Channel &channel) {
this->_channels.push_back(channel);
}

/**
* This function aims to leave a channel.
*
* @param channelName The name of the channel to leave.
*/
void User::leaveChannel(std::string channelName) {
std::vector<Channel>::iterator it = findChannel(channelName);
if (it != this->_channels.end())
this->_channels.erase(it);
else {}
//throw UserException(USER_CHANNEL_NOT_FOUND_ERR);
}
66 changes: 25 additions & 41 deletions src/commands/JoinCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,10 @@
/**
* Constructs a new JoinCommand.
*
* @param channels The channels to join.
* @param keys The passwords to join the channels.
* @param channelMap The map of channels to be joined.
*/
JoinCommand::JoinCommand(std::string channels, std::string keys) {
std::vector<std::string> channelsVec = split(channels, ',');
std::vector<std::string> keysVec = split(keys, ',');

for (size_t i = 0; i < channelsVec.size(); i++) {
if (channelsVec[i] == '' && keysVec[i] == '')
continue;
if (channelsVec[i] == '') {}
// throw IRCException();
if (i < keysVec.size() && keysVec[i] != "")
this->_channels[channelsVec[i]] = keysVec[i];
else
this->_channels[channelsVec[i]] = "";
}
channelsVec.clear();
keysVec.clear();
}
JoinCommand::JoinCommand(std::map<std::string, std::string> channelMap)
: ICommand(true), _channels(channelMap) {}

/**
* Destroys the JoinCommand.
Expand All @@ -49,65 +33,65 @@ JoinCommand::~JoinCommand() {
*
*/
void JoinCommand::execute(Server &server, int clientFd) {
User *user = server.getUserByFd(clientFd);
User user = server.getUserByFd(clientFd);
std::vector<Channel> serverChannels = server.getChannels();

bool isOperator;
std::string nickname = user->getNickname();
std::string username = user->getUsername();
std::string hostname = user->getHostname();
std::string nickname = user.getNickname();
std::string username = user.getUsername();
std::string hostname = user.getHostname();

std::string channelName;
std::string channelKey;

for (std::vector<Channel>::iterator it = this->_channels.begin(); it != this->_channels.end(); it++) {
for (std::map<std::string, std::string>::iterator it = this->_channels.begin(); it != this->_channels.end(); it++) {
channelName = it->first;
channelKey = it->second;
isOperator = false;

//0. If channel[i] does not exist, create it
if (server.findChannel(channelName) == serverChannels.end()) {
Channel newChannel(channelName, *user);
Channel newChannel(channelName, user);
server.addChannel(newChannel);
if (channelKey != "")
server.findChannel(channelName)->setPassword(channelKey);
isOperator = true;
}

Channel *channel = server.findChannel(channelName);
Channel channel = *server.findChannel(channelName);

//1. Check if channel[i] is invite-only channel and if user is invited -> ERR_INVITEONLYCHAN
/*if (channel->isInviteOnly() && !channel->isUserInvited(nickname)) {
throw InviteOnlyChanException(channel->getName());
/*if (channel.isInviteOnly() && !channel.isUserInvited(nickname)) {
throw InviteOnlyChanException(channel.getName());
}*/

//2. Check if user's nick/username/hostname is banned from channel[i] -> ERR_BANNEDFROMCHAN
/*if (channel->isUserBanned(nickname, username, hostname)) {
throw BannedFromChanException(channel->getName());
/*if (channel.isUserBanned(nickname, username, hostname)) {
throw BannedFromChanException(channel.getName());
}*/

//3. Check if password is correct if channel[i] is password-protected
if (channel->isPasswordSet() && channel->getPassword() != channelKey) {
throw BadChannelKeyException(channel->getName());
if (channel.isPasswordSet() && channel.getPassword() != channelKey) {
throw BadChannelKeyException(channel.getName());
}

//4. Check if channel[i] has limit and if its full
if (channel->hasLimit() && channel->isFull()) {
throw ChannelIsFullException(channel->getName());
if (channel.hasLimit() && channel.isFull()) {
throw ChannelIsFullException(channel.getName());
}

//5. Check if user has joined max channels
if (user->isUserInMaxChannels()) {
throw TooManyChannelsException(channel->getName());
if (user.isUserInMaxChannels()) {
throw TooManyChannelsException(channel.getName());
}

isOperator ? channel->addOper(user)
: channel->addUser(user);
isOperator ? channel.addOper(user)
: channel.addUser(user);

user->addChannel(channel);
user.addChannel(channel);

//6. Send JOIN message to all users in channel[i] ¿?
//server.sendMessage(clientFd, RPL_TOPIC(channel->getName(), channel->getTopic()));
//server.sendMessage(clientFd, RPL_NAMREPLY(channel->getName(), channel->getAllUsers()));
//server.sendMessage(clientFd, RPL_TOPIC(channel.getName(), channel.getTopic()));
//server.sendMessage(clientFd, RPL_NAMREPLY(channel.getName(), channel.getAllUsers()));
}
}
20 changes: 18 additions & 2 deletions src/parser/JoinParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,24 @@
* @return The parsed command.
*/
ICommand *JoinParser::parse(const std::vector<std::string>& tokens) {

if (tokens.size() < 2)
throw NeedMoreParamsException("JOIN");
return new JoinCommand(tokens[1], tokens[2]);

std::map<std::string, std::string> channels;
std::vector<std::string> channelsVec = split(tokens[1], ',');
std::vector<std::string> keysVec = split(tokens[2], ',');

for (size_t i = 0; i < channelsVec.size(); i++) {
if (channelsVec[i] == "" && keysVec[i] == "")
continue;
if (channelsVec[i] == "") {}
// throw IRCException();
if (i < keysVec.size() && keysVec[i] != "")
channels[channelsVec[i]] = keysVec[i];
else
channels[channelsVec[i]] = "";
}
channelsVec.clear();
keysVec.clear();
return new JoinCommand(channels);
}

0 comments on commit 0c06a60

Please sign in to comment.