From 269ca9cc3e20be07e428977916caef2c5b996084 Mon Sep 17 00:00:00 2001 From: Alejandro Ruzafa Date: Tue, 26 Mar 2024 20:36:52 +0100 Subject: [PATCH 1/5] feat: start invite command --- Makefile | 4 +-- includes/Channel.hpp | 31 ++++++++++++------- includes/Server.hpp | 22 +++++++++----- includes/commands/InviteCommand.hpp | 24 +++++++++++++++ includes/exceptions/exceptions.hpp | 41 ++++++++++++++++++++++--- includes/parser/CommandParser.hpp | 1 + includes/parser/InviteParser.hpp | 16 ++++++++++ src/Channel.cpp | 35 ++++++++++++++++++++++ src/Server.cpp | 32 +++++++++++++++++++- src/commands/InviteCommand.cpp | 46 +++++++++++++++++++++++++++++ src/commands/JoinCommand.cpp | 7 ++--- src/parser/CommandParser.cpp | 2 ++ src/parser/InviteParser.cpp | 20 +++++++++++++ 13 files changed, 251 insertions(+), 30 deletions(-) create mode 100644 includes/commands/InviteCommand.hpp create mode 100644 includes/parser/InviteParser.hpp create mode 100644 src/commands/InviteCommand.cpp create mode 100644 src/parser/InviteParser.cpp diff --git a/Makefile b/Makefile index 0c18194..7244201 100644 --- a/Makefile +++ b/Makefile @@ -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)) diff --git a/includes/Channel.hpp b/includes/Channel.hpp index e3d4c13..b2a0854 100644 --- a/includes/Channel.hpp +++ b/includes/Channel.hpp @@ -33,11 +33,11 @@ class Channel { bool isModesSet(std::string modesToCheck) const; public: - //Constructors and destructor + // Constructors and destructor Channel(std::string name, User user); ~Channel(); - //Getters + // Getters std::string getName() const; std::string getPassword() const; std::vector getUsers() const; @@ -48,24 +48,33 @@ class Channel { 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(std::string nickname); + + // Oper + void addOper(User user); void removeOper(std::string nickname); void makeUserAnOper(std::string nickname); void makeOperAnUser(std::string nickname); + bool isOper(std::string nickname); + + // Invite + void inviteUser(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 \ No newline at end of file diff --git a/includes/Server.hpp b/includes/Server.hpp index 7fff2fb..1ed41f0 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -48,31 +48,37 @@ class Server { void handleNewConnection(int numFds); void handleExistingConnection(int fd); void closeConnections(); + + // User operations std::vector::iterator findUserByFd(int clientFd); std::vector::iterator findUserByNickname(std::string nickname); + // Channel operations + std::vector::iterator findChannel(std::string channelName); + public: ~Server(); static void init(std::string port, std::string password); static Server &getInstance(); - std::vector::iterator findChannel(std::string channelName); - - //Getters + // User 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); - void addChannel(Channel channel); + // Channel std::vector getChannels() const; + Channel &getChannelByName(std::string channelName); + bool channelExists(std::string channelName); + 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); }; #endif \ No newline at end of file diff --git a/includes/commands/InviteCommand.hpp b/includes/commands/InviteCommand.hpp new file mode 100644 index 0000000..8b4b566 --- /dev/null +++ b/includes/commands/InviteCommand.hpp @@ -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 \ No newline at end of file diff --git a/includes/exceptions/exceptions.hpp b/includes/exceptions/exceptions.hpp index d989a9c..46aadc0 100644 --- a/includes/exceptions/exceptions.hpp +++ b/includes/exceptions/exceptions.hpp @@ -26,13 +26,13 @@ // # 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) @@ -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 \ No newline at end of file diff --git a/includes/parser/CommandParser.hpp b/includes/parser/CommandParser.hpp index 8b97a95..fc92ce8 100644 --- a/includes/parser/CommandParser.hpp +++ b/includes/parser/CommandParser.hpp @@ -11,6 +11,7 @@ # include "QuitParser.hpp" # include "PrivateMessageParser.hpp" # include "JoinParser.hpp" +# include "InviteParser.hpp" # include "libsUtils.hpp" diff --git a/includes/parser/InviteParser.hpp b/includes/parser/InviteParser.hpp new file mode 100644 index 0000000..fda9ed5 --- /dev/null +++ b/includes/parser/InviteParser.hpp @@ -0,0 +1,16 @@ +#ifndef INVITE_PARSER_HPP +# define INVITE_PARSER_HPP + +# include "libsUtils.hpp" +# include "IParser.hpp" +# include "InviteCommand.hpp" + +/** + * An IParser implementation that is responsible for parsing the INVITE command. + */ +class InviteParser : public IParser { + public: + ICommand *parse(const std::vector& tokens); +}; + +#endif \ No newline at end of file diff --git a/src/Channel.cpp b/src/Channel.cpp index 5105b36..d8d2e0a 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -215,6 +215,19 @@ bool Channel::isUserInvited(std::string nickname) const { return std::find(this->_inviteList.begin(), this->_inviteList.end(), nickname) != this->_inviteList.end(); } +/** + * This function aims to invite a user to the channel. + * + * @param nickname The nickname of the user. + * + * @throw `ChannelException` If the user is already invited. + * + */ +void Channel::inviteUser(std::string nickname) { + if (!isUserInvited(nickname)) + this->_inviteList.push_back(nickname); +} + /** * This function aims to check if the user is banned from the channel. * @@ -288,6 +301,17 @@ void Channel::removeUser(std::string nickname) { //throw ChannelException(USER_NOT_FOUND_ERR); } +/** + * This function tells if a user is in the channel. + * + * @param nickname The nickname of the user to check. + * + * @return `true` if the user is in the channel, `false` otherwise. + */ +bool Channel::isUserInChannel(std::string nickname) { + return findUser(nickname) != this->_users.end(); +} + /** * This function aims to add an operator to the channel. * @@ -340,3 +364,14 @@ void Channel::makeOperAnUser(std::string nickname) { else {} //throw ChannelException(USER_NOT_FOUND_ERR); } + +/** + * This function aims to check if a user is an operator of the channel. + * + * @param nickname The nickname of the user to check. + * + * @return `true` if the user is an operator, `false` otherwise. + */ +bool Channel::isOper(std::string nickname) { + return findOper(nickname) != this->_operators.end(); +} diff --git a/src/Server.cpp b/src/Server.cpp index 91f15e8..50eff42 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -247,6 +247,8 @@ bool Server::isNicknameInUse(const std::string& nickname) { * * @param nickname The nickname of the user. * @return The user object with all its information. + * + * @throws `ServerException` if the user is not found. */ User &Server::getUserByNickname(const std::string &nickname) { std::vector::iterator it = this->findUserByNickname(nickname); @@ -377,4 +379,32 @@ void Server::removeChannel(std::string channelName) { std::vector::iterator it = findChannel(channelName); if (it != this->_channels.end()) this->_channels.erase(it); -} \ No newline at end of file +} + +/** + * This function aims to get a channel by the name. + * + * @param channelName The name of the channel. + * + * @return The channel with the name. + * + * @throws `ServerException` if the channel is not found. + */ +Channel &Server::getChannelByName(std::string channelName) { + std::vector::iterator it = findChannel(channelName); + if (it == this->_channels.end()) + throw ServerException("CHANNEL_NOT_FOUND_ERR"); + return *it; +} + +/** + * This function aims to check if a channel exists. + * + * @param channelName The name of the channel. + * + * @return `true` if the channel exists, `false` otherwise. + */ +bool Server::channelExists(std::string channelName) { + return findChannel(channelName) != this->_channels.end(); +} + diff --git a/src/commands/InviteCommand.cpp b/src/commands/InviteCommand.cpp new file mode 100644 index 0000000..0fb8d0b --- /dev/null +++ b/src/commands/InviteCommand.cpp @@ -0,0 +1,46 @@ +#include "InviteCommand.hpp" + +/** + * InviteCommand default constructor. + */ +InviteCommand::InviteCommand() : ICommand(true), _nickname(""), _channelName("") {} + +/** + * InviteCommand nickname and channel name constructor. + * + * @param nickname The nickname + * @param channelName The channel name + */ +InviteCommand::InviteCommand(const std::string& nickname, const std::string& channelName) + : ICommand(true), _nickname(nickname), _channelName(channelName) {} + +/** + * InviteCommand destructor. + */ +InviteCommand::~InviteCommand() {} + +/** + * Execute the command INVITE. + * + * @param clientFd The socket file descriptor of the client + * + * @throws `NoSuchNickException` If the nickname is not in use + * @throws `NotOnChannelException` If the user which invites is not on the channel + * @throws `UserOnChannelException` If the user invited is already on the channel + * @throws `ChanOPrivsNeededException` If the user which invites is not an operator of the channel and the channel is invite-only + */ +void InviteCommand::execute(int clientFd) { + Server& server = Server::getInstance(); + User me = server.getUserByFd(clientFd); + if (!server.isNicknameInUse(this->_nickname)) + throw NoSuchNickException(this->_nickname); + if (!me.isAlreadyInChannel(this->_channelName)) + throw NotOnChannelException(this->_channelName); + Channel channel = server.getChannelByName(this->_channelName); + if (channel.isUserInChannel(this->_nickname)) + throw UserOnChannelException(this->_nickname, this->_channelName); + if (channel.getModes() == "i" && !channel.isOper(me.getNickname())) + throw ChanOPrivsNeededException(this->_channelName); + channel.inviteUser(this->_nickname); + server.sendMessage(clientFd, this->_nickname + " " + this->_channelName); +} \ No newline at end of file diff --git a/src/commands/JoinCommand.cpp b/src/commands/JoinCommand.cpp index 0dfed20..7575064 100644 --- a/src/commands/JoinCommand.cpp +++ b/src/commands/JoinCommand.cpp @@ -34,7 +34,6 @@ JoinCommand::~JoinCommand() { void JoinCommand::execute(int clientFd) { Server& server = Server::getInstance(); User user = server.getUserByFd(clientFd); - std::vector serverChannels = server.getChannels(); bool isOperator; std::string nickname = user.getNickname(); @@ -50,15 +49,15 @@ void JoinCommand::execute(int clientFd) { isOperator = false; //0. If channel[i] does not exist, create it - if (server.findChannel(channelName) == serverChannels.end()) { + if (!server.channelExists(channelName)) { Channel newChannel(channelName, user); server.addChannel(newChannel); if (channelKey != "") - server.findChannel(channelName)->setPassword(channelKey); + server.getChannelByName(channelName).setPassword(channelKey); isOperator = true; } - Channel channel = *server.findChannel(channelName); + Channel channel = server.getChannelByName(channelName); //1. Check if channel[i] is invite-only channel and if user is invited -> ERR_INVITEONLYCHAN /*if (channel.isInviteOnly() && !channel.isUserInvited(nickname)) { diff --git a/src/parser/CommandParser.cpp b/src/parser/CommandParser.cpp index 6d097d4..b59bb69 100644 --- a/src/parser/CommandParser.cpp +++ b/src/parser/CommandParser.cpp @@ -41,6 +41,8 @@ IParser* CommandParser::getParser(std::string command) { return new PrivateMessageParser(); if (command == "JOIN") return new JoinParser(); + if (command == "INVITE") + return new InviteParser(); throw CommandNotFoundException(); } diff --git a/src/parser/InviteParser.cpp b/src/parser/InviteParser.cpp new file mode 100644 index 0000000..c17359a --- /dev/null +++ b/src/parser/InviteParser.cpp @@ -0,0 +1,20 @@ +#include "InviteParser.hpp" + +/** + * Parses the INVITE command. + * + * The format of the INVITE command is as follows: + * + * Command: INVITE + * Parameters: + * + * @param tokens The parameters of the command. + * + * @throws `NeedMoreParamsException` if the number of arguments is less than the expected. + * @return The parsed command. + */ +ICommand *InviteParser::parse(const std::vector& tokens) { + if (tokens.size() < 3) + throw NeedMoreParamsException("INVITE"); + return new InviteCommand(tokens[1], tokens[2]); +} \ No newline at end of file From 6a2da7d1b399e70ab3e60de912b1a57c880575a5 Mon Sep 17 00:00:00 2001 From: Alejandro Ruzafa Date: Tue, 26 Mar 2024 21:59:09 +0100 Subject: [PATCH 2/5] refactor: improve code quality adding consts --- includes/Channel.hpp | 31 ++++++++------ includes/Server.hpp | 34 +++++++++------- includes/User.hpp | 13 +++--- src/Channel.cpp | 34 +++++++++++++++- src/Server.cpp | 79 ++++++++++++++++++++++++++++-------- src/User.cpp | 24 ++--------- src/commands/UserCommand.cpp | 2 +- 7 files changed, 143 insertions(+), 74 deletions(-) diff --git a/includes/Channel.hpp b/includes/Channel.hpp index b2a0854..a40deba 100644 --- a/includes/Channel.hpp +++ b/includes/Channel.hpp @@ -27,25 +27,30 @@ class Channel { int _limit; bool _passwordSet; + // Iterators + std::vector::iterator findUser(std::string nickname); + std::vector::const_iterator findUser(std::string nickname) const; + std::vector::iterator findOper(std::string nickname); + std::vector::const_iterator findOper(std::string nickname) const; + + // Other Operations bool checkChannelName(std::string name) const; - std::vector::iterator findUser(std::string nickname); - std::vector::iterator findOper(std::string nickname); bool isModesSet(std::string modesToCheck) const; - + public: // Constructors and destructor Channel(std::string name, User user); ~Channel(); // Getters - std::string getName() const; - std::string getPassword() const; - std::vector getUsers() const; - std::vector getOperators() const; - std::vector getAllUsers() const; - std::string getTopic() const; - std::string getModes() const; - bool isPasswordSet() const; + std::string getName() const; + std::string getPassword() const; + std::vector getUsers() const; + std::vector getOperators() const; + std::vector getAllUsers() const; + std::string getTopic() const; + std::string getModes() const; + bool isPasswordSet() const; // Setters @@ -57,14 +62,14 @@ class Channel { void addUser(User user); void removeUser(std::string nickname); bool isUserBanned(std::string nickname, std::string username, std::string hostname) const; - bool isUserInChannel(std::string nickname); + bool isUserInChannel(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(std::string nickname); + bool isOper(std::string nickname) const; // Invite void inviteUser(std::string nickname); diff --git a/includes/Server.hpp b/includes/Server.hpp index 1ed41f0..cf0926a 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -42,43 +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 operations + // User Iterators std::vector::iterator findUserByFd(int clientFd); + std::vector::const_iterator findUserByFd(int clientFd) const; std::vector::iterator findUserByNickname(std::string nickname); + std::vector::const_iterator findUserByNickname(std::string nickname) const; - // Channel operations + // Channel Iterators std::vector::iterator findChannel(std::string channelName); + std::vector::const_iterator findChannel(std::string channelName) const; public: ~Server(); + + // Singleton Pattern static void init(std::string port, std::string password); static Server &getInstance(); // User - User &getUserByFd(int clientFd); - User &getUserByNickname(const std::string& nickname); - bool isNicknameInUse(const std::string& nickname); - bool userHasCheckedPassword(int clientFd); - void removeUser(int clientFd); - void attemptUserRegistration(int clientFd); + 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); // Channel std::vector getChannels() const; Channel &getChannelByName(std::string channelName); - bool channelExists(std::string channelName); - void addChannel(Channel channel); - void removeChannel(std::string channelName); + bool channelExists(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); + void sendMessage(int clientFd, const std::string& message) const; + bool isValidPassword(const std::string& password) const; }; #endif \ No newline at end of file diff --git a/includes/User.hpp b/includes/User.hpp index b7737e4..1c9ef43 100644 --- a/includes/User.hpp +++ b/includes/User.hpp @@ -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; @@ -26,11 +25,11 @@ class User { std::string _password; std::vector _channels; + // Iterators std::vector::const_iterator findChannel(std::string channelName) const; std::vector::iterator findChannel(std::string channelName); public: - // Constructors and destructors User(int fd); ~User(); @@ -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; @@ -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 diff --git a/src/Channel.cpp b/src/Channel.cpp index d8d2e0a..8bc4a03 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -53,6 +53,21 @@ std::vector::iterator Channel::findUser(std::string nickname) { return this->_users.end(); } +/** + * This function aims to find a user by the nickname. + * + * @param nickname The nickname of the user. + * + * @return The iterator to the user with the nickname. + */ +std::vector::const_iterator Channel::findUser(std::string nickname) const { + for (size_t i = 0; i < this->_users.size(); i++) { + if (this->_users[i].getNickname() == nickname) + return this->_users.begin() + i; + } + return this->_users.end(); +} + /** * This function aims to find a oper by the nickname. * @@ -68,6 +83,21 @@ std::vector::iterator Channel::findOper(std::string nickname) { return this->_operators.end(); } +/** + * This function aims to find a oper by the nickname. + * + * @param nickname The nickname of the user. + * + * @return The iterator to the user with the nickname. + */ +std::vector::const_iterator Channel::findOper(std::string nickname) const { + for (size_t i = 0; i < this->_operators.size(); i++) { + if (this->_operators[i].getNickname() == nickname) + return this->_operators.begin() + i; + } + return this->_operators.end(); +} + /** * This function aims to checks if the modes of the param matchs the ones of the channel. * @@ -308,7 +338,7 @@ void Channel::removeUser(std::string nickname) { * * @return `true` if the user is in the channel, `false` otherwise. */ -bool Channel::isUserInChannel(std::string nickname) { +bool Channel::isUserInChannel(std::string nickname) const { return findUser(nickname) != this->_users.end(); } @@ -372,6 +402,6 @@ void Channel::makeOperAnUser(std::string nickname) { * * @return `true` if the user is an operator, `false` otherwise. */ -bool Channel::isOper(std::string nickname) { +bool Channel::isOper(std::string nickname) const { return findOper(nickname) != this->_operators.end(); } diff --git a/src/Server.cpp b/src/Server.cpp index 50eff42..a7a6f12 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -9,7 +9,7 @@ Server *Server::_server = NULL; * @param port The port number to validate. * @return `true` if the port is valid, `false` otherwise. */ -bool Server::isValidPort(const std::string port) { +bool Server::isValidPort(const std::string port) const { for (size_t i = 0; i < port.length(); i++) { if (!std::isdigit(port[i])) return false; @@ -214,7 +214,7 @@ void Server::handleExistingConnection(int clientFd) { * @param password The password provided by the client. * @return `true` if the password is valid, `false` otherwise. */ -bool Server::isValidPassword(const std::string& password) { +bool Server::isValidPassword(const std::string& password) const { return password == this->_password; } @@ -231,14 +231,27 @@ User &Server::getUserByFd(int clientFd) { return *it; } +/** + * This function aims to get the user by the file descriptor. + * + * @param clientFd The file descriptor of the user. + * @return The user with the file descriptor. + */ +const User &Server::getUserByFd(int clientFd) const { + std::vector::const_iterator it = findUserByFd(clientFd); + if (it == this->_users.end()) + throw ServerException(USER_NOT_FOUND_ERR); + return *it; +} + /** * This function aims to check if a nickname is already in use. * * @param nickname The nickname to check. * @return `true` if the nickname is already in use, `false` otherwise. */ -bool Server::isNicknameInUse(const std::string& nickname) { - std::vector::iterator it = findUserByNickname(nickname); +bool Server::isNicknameInUse(const std::string& nickname) const { + std::vector::const_iterator it = findUserByNickname(nickname); return it != this->_users.end(); } @@ -257,17 +270,6 @@ User &Server::getUserByNickname(const std::string &nickname) { return *it; } -/** - * This function aims to check if the user has already checked the password. - * - * @param clientFd The file descriptor of the user. - * - * @return `true` if the user has already checked the password, `false` otherwise. - */ -bool Server::userHasCheckedPassword(int clientFd) { - return this->getUserByFd(clientFd).isPasswordChecked(); -} - /** * This function aims to send a message to a client. * @@ -319,6 +321,21 @@ std::vector::iterator Server::findUserByFd(int clientFd) { return this->_users.end(); } +/** + * This function aims to find a user by the file descriptor. + * + * @param clientFd The file descriptor of the user. + * + * @return The iterator to the user with the file descriptor. + */ +std::vector::const_iterator Server::findUserByFd(int clientFd) const { + for (size_t i = 0; i < this->_users.size(); i++) { + if (this->_users[i].getFd() == clientFd) + return this->_users.begin() + i; + } + return this->_users.end(); +} + /** * This function aims to find a user by the nickname. * @@ -334,6 +351,21 @@ std::vector::iterator Server::findUserByNickname(std::string nickname) { return this->_users.end(); } +/** + * This function aims to find a user by the nickname. + * + * @param nickname The nickname of the user. + * + * @return The iterator to the user with the nickname. + */ +std::vector::const_iterator Server::findUserByNickname(std::string nickname) const { + for (size_t i = 0; i < this->_users.size(); i++) { + if (this->_users[i].getNickname() == nickname) + return this->_users.begin() + i; + } + return this->_users.end(); +} + /** * This function aims to find a channel by the name. * @@ -349,6 +381,21 @@ std::vector::iterator Server::findChannel(std::string channelName) { return this->_channels.end(); } +/** + * This function aims to find a channel by the name. + * + * @param channelName The name of the channel. + * + * @return The iterator to the channel with the name. + */ +std::vector::const_iterator Server::findChannel(std::string channelName) const { + for (size_t i = 0; i < this->_channels.size(); i++) { + if (this->_channels[i].getName() == channelName) + return this->_channels.begin() + i; + } + return this->_channels.end(); +} + /** * This function aims to add a channel to the server. * @@ -404,7 +451,7 @@ Channel &Server::getChannelByName(std::string channelName) { * * @return `true` if the channel exists, `false` otherwise. */ -bool Server::channelExists(std::string channelName) { +bool Server::channelExists(std::string channelName) const { return findChannel(channelName) != this->_channels.end(); } diff --git a/src/User.cpp b/src/User.cpp index 57868b7..71b3282 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -5,7 +5,7 @@ * * @param fd The file descriptor of the user */ -User::User(int fd) : _fd(fd), _passwordChecked(false) {} +User::User(int fd) : _fd(fd) {} /** * User destructor @@ -42,22 +42,6 @@ std::vector::iterator User::findChannel(std::string channelName) { return this->_channels.end(); } -/** - * This function aims to check the password of the user, setting is as `true`. - */ -void User::checkPassword() { - this->_passwordChecked = true; -} - -/** - * This function aims to check if the password is already checked. - * - * @return `true` if the password is already checked, `false` otherwise. - */ -bool User::isPasswordChecked() const { - return this->_passwordChecked; -} - /** * This function aims to check if the user is already in the maximum number of channels. * @@ -175,7 +159,7 @@ void User::setPassword(const std::string& password) { * * @return `true` if the user can register, `false` otherwise. */ -bool User::canRegister() { +bool User::canRegister() const { return !(this->_username.empty() || this->_hostname.empty() || this->_serverName.empty() || this->_realName.empty() || this->_nickname.empty()); } @@ -214,8 +198,8 @@ void User::addChannel(Channel &channel) { * @param destination The user who will receive the message. * @param message The message to send. */ -void User::sendPrivateMessageToUser(const User &destination, const std::string& message) { +void User::sendPrivateMessageToUser(const User &destination, const std::string& message) const { Logger::debug("Sending private message to " + destination.getNickname() + " from " + this->getNickname() + ": " + message); std::string response = ":" + this->_nickname + " PRIVMSG " + destination.getNickname() + " :" + message; Server::getInstance().sendMessage(destination.getFd(), response); -} \ No newline at end of file +} diff --git a/src/commands/UserCommand.cpp b/src/commands/UserCommand.cpp index d3555aa..37120b4 100644 --- a/src/commands/UserCommand.cpp +++ b/src/commands/UserCommand.cpp @@ -40,6 +40,6 @@ void UserCommand::execute(int clientFd) { user.setHostname(this->_hostname); user.setServerName(this->_serverName); user.setRealName(this->_realName); - if (server.getUserByFd(clientFd).canRegister()) + if (user.canRegister()) server.attemptUserRegistration(clientFd); } From e4ad1bde2c07c4007049756410f5fa41539ac8d1 Mon Sep 17 00:00:00 2001 From: Alejandro Ruzafa Date: Wed, 27 Mar 2024 23:34:02 +0100 Subject: [PATCH 3/5] perf: pass objects by reference --- includes/Channel.hpp | 14 +++++------ includes/Server.hpp | 14 +++++------ includes/User.hpp | 4 +-- includes/exceptions/exceptions.hpp | 40 +++++++++++++++--------------- src/Channel.cpp | 14 +++++------ src/Server.cpp | 16 ++++++------ src/User.cpp | 4 +-- 7 files changed, 53 insertions(+), 53 deletions(-) diff --git a/includes/Channel.hpp b/includes/Channel.hpp index a40deba..cfd9275 100644 --- a/includes/Channel.hpp +++ b/includes/Channel.hpp @@ -28,10 +28,10 @@ class Channel { bool _passwordSet; // Iterators - std::vector::iterator findUser(std::string nickname); - std::vector::const_iterator findUser(std::string nickname) const; - std::vector::iterator findOper(std::string nickname); - std::vector::const_iterator findOper(std::string nickname) const; + std::vector::iterator findUser(const std::string &nickname); + std::vector::const_iterator findUser(const std::string &nickname) const; + std::vector::iterator findOper(const std::string &nickname); + std::vector::const_iterator findOper(const std::string &nickname) const; // Other Operations bool checkChannelName(std::string name) const; @@ -62,17 +62,17 @@ class Channel { void addUser(User user); void removeUser(std::string nickname); bool isUserBanned(std::string nickname, std::string username, std::string hostname) const; - bool isUserInChannel(std::string nickname) 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(std::string nickname) const; + bool isOper(const std::string &nickname) const; // Invite - void inviteUser(std::string nickname); + void inviteUser(const std::string &nickname); bool isUserInvited(std::string nickname) const; bool isInviteOnly() const; diff --git a/includes/Server.hpp b/includes/Server.hpp index cf0926a..49f020b 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -43,7 +43,7 @@ class Server { Server(const std::string port, const std::string password); // Server logic - bool isValidPort(const std::string port) const; + bool isValidPort(const std::string &port) const; void initServer(); void listenClients(); void handleNewConnection(int numFds); @@ -53,12 +53,12 @@ class Server { // User Iterators std::vector::iterator findUserByFd(int clientFd); std::vector::const_iterator findUserByFd(int clientFd) const; - std::vector::iterator findUserByNickname(std::string nickname); - std::vector::const_iterator findUserByNickname(std::string nickname) const; + std::vector::iterator findUserByNickname(const std::string &nickname); + std::vector::const_iterator findUserByNickname(const std::string &nickname) const; // Channel Iterators - std::vector::iterator findChannel(std::string channelName); - std::vector::const_iterator findChannel(std::string channelName) const; + std::vector::iterator findChannel(const std::string &channelName); + std::vector::const_iterator findChannel(const std::string &channelName) const; public: ~Server(); @@ -77,8 +77,8 @@ class Server { // Channel std::vector getChannels() const; - Channel &getChannelByName(std::string channelName); - bool channelExists(std::string channelName) const; + Channel &getChannelByName(const std::string &channelName); + bool channelExists(const std::string &channelName) const; void addChannel(Channel channel); void removeChannel(std::string channelName); diff --git a/includes/User.hpp b/includes/User.hpp index 1c9ef43..d5ee955 100644 --- a/includes/User.hpp +++ b/includes/User.hpp @@ -26,8 +26,8 @@ class User { std::vector _channels; // Iterators - std::vector::const_iterator findChannel(std::string channelName) const; - std::vector::iterator findChannel(std::string channelName); + std::vector::const_iterator findChannel(const std::string &channelName) const; + std::vector::iterator findChannel(const std::string &channelName); public: User(int fd); diff --git a/includes/exceptions/exceptions.hpp b/includes/exceptions/exceptions.hpp index 46aadc0..20b4185 100644 --- a/includes/exceptions/exceptions.hpp +++ b/includes/exceptions/exceptions.hpp @@ -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 " :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 " :You're not channel operator" +# define ERR_CHAN_O_PRIVS_NEEDED " :You're not channel operator" -#define ERR_NO_SUCH_NICK " :No such nick/channel" -#define ERR_USER_ON_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" @@ -68,7 +68,7 @@ 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) {} }; /** @@ -76,7 +76,7 @@ class ErroneousNicknameException : public IRCException { */ 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) {} }; /** @@ -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) {} }; /** @@ -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) {} }; /** @@ -126,7 +126,7 @@ class PasswordMismatchException : public IRCException { */ class InviteOnlyChanException : public IRCException { public: - InviteOnlyChanException(std::string channelName) : IRCException("473", channelName + " :Cannot join channel (+i)") {} + InviteOnlyChanException(std::string &channelName) : IRCException("473", channelName + " :Cannot join channel (+i)") {} }; /** @@ -134,7 +134,7 @@ class InviteOnlyChanException : public IRCException { */ class BannedFromChanException : public IRCException { public: - BannedFromChanException(std::string channelName) : IRCException("474", channelName + " :Cannot join channel (+b)") {} + BannedFromChanException(std::string &channelName) : IRCException("474", channelName + " :Cannot join channel (+b)") {} }; /** @@ -142,7 +142,7 @@ class BannedFromChanException : public IRCException { */ class BadChannelKeyException : public IRCException { public: - BadChannelKeyException(std::string channelName) : IRCException("475", channelName + " :Cannot join channel (+k)") {} + BadChannelKeyException(std::string &channelName) : IRCException("475", channelName + " :Cannot join channel (+k)") {} }; /** @@ -150,7 +150,7 @@ class BadChannelKeyException : public IRCException { */ class ChannelIsFullException : public IRCException { public: - ChannelIsFullException(std::string channelName) : IRCException("471", channelName + " :Cannot join channel (+l)") {} + ChannelIsFullException(std::string &channelName) : IRCException("471", channelName + " :Cannot join channel (+l)") {} }; /** @@ -158,7 +158,7 @@ class ChannelIsFullException : public IRCException { */ class TooManyChannelsException : public IRCException { public: - TooManyChannelsException(std::string channelName) : IRCException("405", channelName + " :You have joined too many channels") {} + TooManyChannelsException(std::string &channelName) : IRCException("405", channelName + " :You have joined too many channels") {} }; /** @@ -166,7 +166,7 @@ class TooManyChannelsException : public IRCException { */ 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)) {} }; /** @@ -182,7 +182,7 @@ class NoTextToSendException : public IRCException { */ class NoSuchNickException : public IRCException { public: - NoSuchNickException(const std::string nickname) : IRCException("401", nickname + ERR_NO_SUCH_NICK) {} + NoSuchNickException(const std::string &nickname) : IRCException("401", nickname + ERR_NO_SUCH_NICK) {} }; /** @@ -190,7 +190,7 @@ class NoSuchNickException : public IRCException { */ class NotOnChannelException : public IRCException { public: - NotOnChannelException(const std::string channelName) : IRCException("442", channelName + ERR_NOT_ON_CHANNEL) {} + NotOnChannelException(const std::string &channelName) : IRCException("442", channelName + ERR_NOT_ON_CHANNEL) {} }; /** @@ -198,7 +198,7 @@ class NotOnChannelException : public IRCException { */ class UserOnChannelException : public IRCException { public: - UserOnChannelException(const std::string nickname, const std::string channelName) + UserOnChannelException(const std::string &nickname, const std::string &channelName) : IRCException("443", nickname + "" + channelName + ERR_USER_ON_CHANNEL) {} }; @@ -207,7 +207,7 @@ class UserOnChannelException : public IRCException { */ class ChanOPrivsNeededException : public IRCException { public: - ChanOPrivsNeededException(const std::string channelName) : IRCException("482", channelName + ERR_CHAN_O_PRIVS_NEEDED) {} + ChanOPrivsNeededException(const std::string &channelName) : IRCException("482", channelName + ERR_CHAN_O_PRIVS_NEEDED) {} }; #endif \ No newline at end of file diff --git a/src/Channel.cpp b/src/Channel.cpp index 8bc4a03..49a5957 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -45,7 +45,7 @@ bool Channel::checkChannelName(std::string name) const { * * @return The iterator to the user with the nickname. */ -std::vector::iterator Channel::findUser(std::string nickname) { +std::vector::iterator Channel::findUser(const std::string &nickname) { for (size_t i = 0; i < this->_users.size(); i++) { if (this->_users[i].getNickname() == nickname) return this->_users.begin() + i; @@ -60,7 +60,7 @@ std::vector::iterator Channel::findUser(std::string nickname) { * * @return The iterator to the user with the nickname. */ -std::vector::const_iterator Channel::findUser(std::string nickname) const { +std::vector::const_iterator Channel::findUser(const std::string &nickname) const { for (size_t i = 0; i < this->_users.size(); i++) { if (this->_users[i].getNickname() == nickname) return this->_users.begin() + i; @@ -75,7 +75,7 @@ std::vector::const_iterator Channel::findUser(std::string nickname) const * * @return The iterator to the user with the nickname. */ -std::vector::iterator Channel::findOper(std::string nickname) { +std::vector::iterator Channel::findOper(const std::string &nickname) { for (size_t i = 0; i < this->_operators.size(); i++) { if (this->_operators[i].getNickname() == nickname) return this->_operators.begin() + i; @@ -90,7 +90,7 @@ std::vector::iterator Channel::findOper(std::string nickname) { * * @return The iterator to the user with the nickname. */ -std::vector::const_iterator Channel::findOper(std::string nickname) const { +std::vector::const_iterator Channel::findOper(const std::string &nickname) const { for (size_t i = 0; i < this->_operators.size(); i++) { if (this->_operators[i].getNickname() == nickname) return this->_operators.begin() + i; @@ -253,7 +253,7 @@ bool Channel::isUserInvited(std::string nickname) const { * @throw `ChannelException` If the user is already invited. * */ -void Channel::inviteUser(std::string nickname) { +void Channel::inviteUser(const std::string &nickname) { if (!isUserInvited(nickname)) this->_inviteList.push_back(nickname); } @@ -338,7 +338,7 @@ void Channel::removeUser(std::string nickname) { * * @return `true` if the user is in the channel, `false` otherwise. */ -bool Channel::isUserInChannel(std::string nickname) const { +bool Channel::isUserInChannel(const std::string &nickname) const { return findUser(nickname) != this->_users.end(); } @@ -402,6 +402,6 @@ void Channel::makeOperAnUser(std::string nickname) { * * @return `true` if the user is an operator, `false` otherwise. */ -bool Channel::isOper(std::string nickname) const { +bool Channel::isOper(const std::string &nickname) const { return findOper(nickname) != this->_operators.end(); } diff --git a/src/Server.cpp b/src/Server.cpp index a7a6f12..9caf9f2 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -9,7 +9,7 @@ Server *Server::_server = NULL; * @param port The port number to validate. * @return `true` if the port is valid, `false` otherwise. */ -bool Server::isValidPort(const std::string port) const { +bool Server::isValidPort(const std::string &port) const { for (size_t i = 0; i < port.length(); i++) { if (!std::isdigit(port[i])) return false; @@ -214,7 +214,7 @@ void Server::handleExistingConnection(int clientFd) { * @param password The password provided by the client. * @return `true` if the password is valid, `false` otherwise. */ -bool Server::isValidPassword(const std::string& password) const { +bool Server::isValidPassword(const std::string &password) const { return password == this->_password; } @@ -343,7 +343,7 @@ std::vector::const_iterator Server::findUserByFd(int clientFd) const { * * @return The iterator to the user with the nickname. */ -std::vector::iterator Server::findUserByNickname(std::string nickname) { +std::vector::iterator Server::findUserByNickname(const std::string &nickname) { for (size_t i = 0; i < this->_users.size(); i++) { if (this->_users[i].getNickname() == nickname) return this->_users.begin() + i; @@ -358,7 +358,7 @@ std::vector::iterator Server::findUserByNickname(std::string nickname) { * * @return The iterator to the user with the nickname. */ -std::vector::const_iterator Server::findUserByNickname(std::string nickname) const { +std::vector::const_iterator Server::findUserByNickname(const std::string &nickname) const { for (size_t i = 0; i < this->_users.size(); i++) { if (this->_users[i].getNickname() == nickname) return this->_users.begin() + i; @@ -373,7 +373,7 @@ std::vector::const_iterator Server::findUserByNickname(std::string nicknam * * @return The iterator to the channel with the name. */ -std::vector::iterator Server::findChannel(std::string channelName) { +std::vector::iterator Server::findChannel(const std::string &channelName) { for (size_t i = 0; i < this->_channels.size(); i++) { if (this->_channels[i].getName() == channelName) return this->_channels.begin() + i; @@ -388,7 +388,7 @@ std::vector::iterator Server::findChannel(std::string channelName) { * * @return The iterator to the channel with the name. */ -std::vector::const_iterator Server::findChannel(std::string channelName) const { +std::vector::const_iterator Server::findChannel(const std::string &channelName) const { for (size_t i = 0; i < this->_channels.size(); i++) { if (this->_channels[i].getName() == channelName) return this->_channels.begin() + i; @@ -437,7 +437,7 @@ void Server::removeChannel(std::string channelName) { * * @throws `ServerException` if the channel is not found. */ -Channel &Server::getChannelByName(std::string channelName) { +Channel &Server::getChannelByName(const std::string &channelName) { std::vector::iterator it = findChannel(channelName); if (it == this->_channels.end()) throw ServerException("CHANNEL_NOT_FOUND_ERR"); @@ -451,7 +451,7 @@ Channel &Server::getChannelByName(std::string channelName) { * * @return `true` if the channel exists, `false` otherwise. */ -bool Server::channelExists(std::string channelName) const { +bool Server::channelExists(const std::string &channelName) const { return findChannel(channelName) != this->_channels.end(); } diff --git a/src/User.cpp b/src/User.cpp index 71b3282..5e85de0 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -19,7 +19,7 @@ User::~User() {} * * @return The const iterator to the channel with the name. */ -std::vector::const_iterator User::findChannel(std::string channelName) const { +std::vector::const_iterator User::findChannel(const std::string &channelName) const { for (size_t i = 0; i < this->_channels.size(); i++) { if (this->_channels[i].getName() == channelName) return this->_channels.begin() + i; @@ -34,7 +34,7 @@ std::vector::const_iterator User::findChannel(std::string channelName) * * @return The iterator to the channel with the name. */ -std::vector::iterator User::findChannel(std::string channelName) { +std::vector::iterator User::findChannel(const std::string &channelName) { for (size_t i = 0; i < this->_channels.size(); i++) { if (this->_channels[i].getName() == channelName) return this->_channels.begin() + i; From a5f698b3c92e3b83c47e80479378212b231767cf Mon Sep 17 00:00:00 2001 From: Alejandro Ruzafa Date: Wed, 27 Mar 2024 23:36:07 +0100 Subject: [PATCH 4/5] fix: compile --- includes/exceptions/exceptions.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/exceptions/exceptions.hpp b/includes/exceptions/exceptions.hpp index 20b4185..15563c9 100644 --- a/includes/exceptions/exceptions.hpp +++ b/includes/exceptions/exceptions.hpp @@ -126,7 +126,7 @@ 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)") {} }; /** @@ -134,7 +134,7 @@ class InviteOnlyChanException : public IRCException { */ 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)") {} }; /** @@ -142,7 +142,7 @@ class BannedFromChanException : public IRCException { */ 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)") {} }; /** @@ -150,7 +150,7 @@ class BadChannelKeyException : public IRCException { */ 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)") {} }; /** @@ -158,7 +158,7 @@ class ChannelIsFullException : public IRCException { */ 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") {} }; /** From 94366285f279b064f3d237f7080068add82a0900 Mon Sep 17 00:00:00 2001 From: Alejandro Ruzafa Date: Thu, 28 Mar 2024 11:09:46 +0100 Subject: [PATCH 5/5] refactor: apply PR suggestions --- includes/libsUtils.hpp | 2 ++ src/Server.cpp | 7 ++++--- src/commands/InviteCommand.cpp | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/includes/libsUtils.hpp b/includes/libsUtils.hpp index df52849..4650ca0 100644 --- a/includes/libsUtils.hpp +++ b/includes/libsUtils.hpp @@ -42,6 +42,8 @@ # define USER_NOT_FOUND_ERR "[ERROR] User not found in list." # define CHANNEL_ALREADY_ADDED_ERR "[ERROR] Channel already added." +# define INVITE_CMD_RESPONSE(nickname, channelName) nickname + " " + channelName + std::string trim(const std::string& str); std::vector split(const std::string &s, char delim); diff --git a/src/Server.cpp b/src/Server.cpp index 9caf9f2..14194c5 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -259,9 +259,10 @@ bool Server::isNicknameInUse(const std::string& nickname) const { * This function aims to get all the user information searching by the nickname. * * @param nickname The nickname of the user. - * @return The user object with all its information. * * @throws `ServerException` if the user is not found. + * + * @return The user object with all its information. */ User &Server::getUserByNickname(const std::string &nickname) { std::vector::iterator it = this->findUserByNickname(nickname); @@ -433,9 +434,9 @@ void Server::removeChannel(std::string channelName) { * * @param channelName The name of the channel. * - * @return The channel with the name. - * * @throws `ServerException` if the channel is not found. + * + * @return The channel with the name. */ Channel &Server::getChannelByName(const std::string &channelName) { std::vector::iterator it = findChannel(channelName); diff --git a/src/commands/InviteCommand.cpp b/src/commands/InviteCommand.cpp index 0fb8d0b..05717b3 100644 --- a/src/commands/InviteCommand.cpp +++ b/src/commands/InviteCommand.cpp @@ -42,5 +42,5 @@ void InviteCommand::execute(int clientFd) { if (channel.getModes() == "i" && !channel.isOper(me.getNickname())) throw ChanOPrivsNeededException(this->_channelName); channel.inviteUser(this->_nickname); - server.sendMessage(clientFd, this->_nickname + " " + this->_channelName); + server.sendMessage(clientFd, INVITE_CMD_RESPONSE(this->_nickname, this->_channelName)); } \ No newline at end of file