diff --git a/includes/Channel.hpp b/includes/Channel.hpp index 3c47600..f9640ba 100644 --- a/includes/Channel.hpp +++ b/includes/Channel.hpp @@ -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); diff --git a/includes/Server.hpp b/includes/Server.hpp index 0ae4e8f..67c3aa0 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -46,12 +46,13 @@ class Server { void closeConnections(); std::vector::iterator findUserByFd(int clientFd); std::vector::iterator findUserByNickname(std::string nickname); - std::vector::iterator findChannel(std::string channelName); public: Server(const std::string port, const std::string password); ~Server(); + std::vector::iterator findChannel(std::string channelName); + void sendMessage(int clientFd, const std::string& message) const; bool isValidPassword(const std::string& password); User &getUserByFd(int clientFd); diff --git a/includes/User.hpp b/includes/User.hpp index fecbe77..3f33aa7 100644 --- a/includes/User.hpp +++ b/includes/User.hpp @@ -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; @@ -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); void sendPrivateMessageToUser(const Server &server, const User &destination, const std::string& message); }; diff --git a/includes/commands/JoinCommand.hpp b/includes/commands/JoinCommand.hpp index 1bfa2e8..8e2cc02 100644 --- a/includes/commands/JoinCommand.hpp +++ b/includes/commands/JoinCommand.hpp @@ -19,7 +19,7 @@ class JoinCommand : public ICommand { std::map _channels; //key: channelName, value: channelPassword public: - JoinCommand(std::string channels, std::string keys); + JoinCommand(std::map _channels); ~JoinCommand(); void execute(Server &server, int fd); diff --git a/includes/utils.hpp b/includes/utils.hpp index 0e7558d..8ed8ab0 100644 --- a/includes/utils.hpp +++ b/includes/utils.hpp @@ -7,6 +7,7 @@ # include # include +# include std::string trim(const std::string& str); std::vector split(const std::string &s, char delim); diff --git a/src/Channel.cpp b/src/Channel.cpp index 72179c6..5105b36 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -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. * diff --git a/src/User.cpp b/src/User.cpp index 2484cb8..9fb3a24 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -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. * @@ -192,34 +201,14 @@ 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::iterator it = findChannel(channelName); - if (it != this->_channels.end()) - this->_channels.erase(it); - else {} - //throw UserException(USER_CHANNEL_NOT_FOUND_ERR); -} - /** * This function aims to send a private message to a user. * diff --git a/src/commands/JoinCommand.cpp b/src/commands/JoinCommand.cpp index d7fc7d9..5fd61f0 100644 --- a/src/commands/JoinCommand.cpp +++ b/src/commands/JoinCommand.cpp @@ -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 channelsVec = split(channels, ','); - std::vector 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 channelMap) + : ICommand(true), _channels(channelMap) {} /** * Destroys the JoinCommand. @@ -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 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::iterator it = this->_channels.begin(); it != this->_channels.end(); it++) { + for (std::map::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())); } } \ No newline at end of file diff --git a/src/parser/JoinParser.cpp b/src/parser/JoinParser.cpp index 5552c3f..34a9128 100644 --- a/src/parser/JoinParser.cpp +++ b/src/parser/JoinParser.cpp @@ -14,8 +14,24 @@ * @return The parsed command. */ ICommand *JoinParser::parse(const std::vector& tokens) { - if (tokens.size() < 2) throw NeedMoreParamsException("JOIN"); - return new JoinCommand(tokens[1], tokens[2]); + + std::map channels; + std::vector channelsVec = split(tokens[1], ','); + std::vector 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); } \ No newline at end of file