Skip to content

Commit

Permalink
refactor part 4
Browse files Browse the repository at this point in the history
  • Loading branch information
jdomingu98 authored May 5, 2024
1 parent 4fbde19 commit 2f4e07a
Show file tree
Hide file tree
Showing 29 changed files with 170 additions and 208 deletions.
48 changes: 22 additions & 26 deletions includes/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class Channel {
private:
std::string _name;
std::string _password;
std::vector<User *> _users;
std::vector<User *> _operators;
std::vector<User *> _users;
std::vector<std::string> _inviteList;
std::string _topic;
int _limit;
bool _passwordSet;
std::map<std::string, std::vector<char> > _files;

// modes
// Modes
bool _inviteOnly;
bool _topicProtected;

Expand All @@ -45,7 +45,7 @@ class Channel {
std::vector<User *>::const_iterator findOper(const std::string &nickname) const;

// Other Operations
bool checkChannelName(std::string name) const;
bool checkName(std::string name) const;

public:
// Constructors and destructor
Expand All @@ -56,52 +56,48 @@ class Channel {

Channel &operator=(const Channel &other);

// 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;
bool isPasswordSet() const;

// Setters
void setTopic(std::string topic);
std::string getName() const;
void broadcast(const std::string &message);

// User
std::vector<User *> getUsers() const;
void addUser(User *user);
void removeUser(const std::string &nickname);
bool isUserInChannel(const std::string &nickname) const;

// Oper
void makeUserAnOper(std::string nickname);
void makeOperAnUser(std::string nickname);
bool isOper(const std::string &nickname) const;
void broadcastToChannel(const std::string &message);

// Password
std::string getPassword() const;
bool isPasswordSet() const;
void setPassword(const std::string &password);
bool checkPassword(std::string password) const;
void unsetPassword();

// Topic
std::string getTopic() const;
void setTopic(std::string topic);
bool isTopicProtected() const;
void setTopicProtected(bool topicProtected);

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

// Modes
bool isInviteOnly() const;
void setInviteOnly(bool inviteOnly);
bool isTopicProtected() const;
void setTopicProtected(bool topicProtected);

// Mode
std::string getModes() const;
std::string getModeParams() const;

// Password
void setPassword(const std::string &password);
bool checkPassword(std::string password) const;
void unsetPassword();

// Limit
void setLimit(int limit);
bool hasLimit() const;
bool isFull() const;

// Bonus
// Transfer file bonus
void uploadFile(const std::string &path);
void downloadFile(const std::string &filename);
};
Expand Down
16 changes: 16 additions & 0 deletions includes/parser/IParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@

# include "ACommand.hpp"

# define DOWN "DOWN"
# define INVITE "INVITE"
# define JOIN "JOIN"
# define KICK "KICK"
# define MODE "MODE"
# define NICK "NICK"
# define NOTICE "NOTICE"
# define PART "PART"
# define PASS "PASS"
# define PRIVMSG "PRIVMSG"
# define QUIT "QUIT"
# define TOPIC "TOPIC"
# define UP "UP"
# define USER "USER"
# define WHO "WHO"

class ACommand;

/**
Expand Down
39 changes: 5 additions & 34 deletions src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Channel::Channel()
*/
Channel::Channel(const std::string name, User *user)
: _password(NONE), _topic(NONE), _limit(NO_LIMIT), _passwordSet(false), _inviteOnly(false) {
if (!checkChannelName(name))
if (!checkName(name))
throw BadChannelMaskException(name);
this->_name = name;
this->_operators.push_back(user);
Expand Down Expand Up @@ -78,7 +78,7 @@ Channel &Channel::operator=(const Channel &other) {
*
* @return `true` if the channel name is valid, `false` otherwise.
*/
bool Channel::checkChannelName(std::string name) const {
bool Channel::checkName(std::string name) const {
if ((name[0] != '#' && name[0] != '&') || name.size() > MAX_CHANNEL_NAME_LENGTH)
return false;
for (size_t i = 1; i < name.size(); i++) {
Expand Down Expand Up @@ -166,30 +166,12 @@ std::string Channel::getPassword() const {
return this->_password;
}

/**
* This function aims to get the users of the channel.
*
* @return The users of the channel.
*/
std::vector<User *> Channel::getUsers() const {
return this->_users;
}

/**
* This function aims to get the operators of the channel.
*
* @return The operators of the channel.
*/
std::vector<User *> Channel::getOperators() const {
return this->_operators;
}

/**
* This function aims to get all the users of the channel.
*
* @return All the users of the channel.
*/
std::vector<User *> Channel::getAllUsers() const {
std::vector<User *> Channel::getUsers() const {
std::vector<User *> allUsers = this->_operators;
allUsers.insert(allUsers.end(), this->_users.begin(), this->_users.end());
return allUsers;
Expand Down Expand Up @@ -356,17 +338,6 @@ void Channel::removeUser(const std::string &nickname) {
}
}

/**
* 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(const std::string &nickname) const {
return findOper(nickname) != this->_operators.end() || findUser(nickname) != this->_users.end();
}

/**
* This function aims to make a user an operator of the channel.
*
Expand Down Expand Up @@ -473,9 +444,9 @@ std::string Channel::getModeParams() const {
*
* @param message The message to broadcast.
*/
void Channel::broadcastToChannel(const std::string &message) {
void Channel::broadcast(const std::string &message) {
Server& server = Server::getInstance();
std::vector<User *> allUsers = getAllUsers();
std::vector<User *> allUsers = getUsers();
for (std::vector<User *>::iterator it = allUsers.begin(); it != allUsers.end(); it++)
server.sendMessage((*it)->getFd(), message);
}
Expand Down
25 changes: 9 additions & 16 deletions src/Responses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,16 @@
*/
std::string NamesReplyResponse::rplNamesReply(const Channel &channel) const {
const std::vector<User *> users = channel.getUsers();
const std::vector<User *> opers = channel.getOperators();

std::string msg = "= " + channel.getName();
msg.append(" :");
if (!opers.empty()) {
msg.append("@").append(opers[0]->getNickname());

for (size_t i = 1; i < opers.size(); i++)
msg.append(" @").append(opers[i]->getNickname());

for (size_t i = 0; i < users.size(); i++)
msg.append(" ").append(users[i]->getNickname());
} else if (!users.empty()) {
msg.append(users[0]->getNickname());

for (size_t i = 1; i < users.size(); i++)
msg.append(" ").append(users[i]->getNickname());
std::string msg = "= " + channel.getName() + " : ";
std::vector<User *>::const_iterator it;
for (it = users.begin(); it != users.end(); it++) {
const std::string &nickname = (*it)->getNickname();
if (channel.isOper(nickname))
msg.append("@");
msg.append(nickname);
if (it != users.end() - 1)
msg.append(" ");
}
return msg;
}
4 changes: 2 additions & 2 deletions src/User.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ Channel *User::getChannelByName(std::string &channelName) {
void User::sendPrivateMessageToChannel(const Channel &destination, const std::string& message) const {
Logger::debug("Sending private message to channel " + destination.getName() + " from " + getNickname() + ": " + message);
std::string response = CMD_MSG(_nickname, _username, _hostname, PRIVMSG_MSG(destination.getName(), message));
std::vector<User *> users = destination.getAllUsers();
std::vector<User *> users = destination.getUsers();
for (size_t i = 0; i < users.size(); i++)
if (users[i]->getNickname() != _nickname)
Server::getInstance().sendMessage(users[i]->getFd(), response);
Expand All @@ -327,7 +327,7 @@ void User::sendPrivateMessageToChannel(const Channel &destination, const std::st
void User::sendNoticeToChannel(const Channel &destination, const std::string& message) const {
Logger::debug("Sending private message to channel " + destination.getName() + " from " + getNickname() + ": " + message);
std::string response = CMD_MSG(_nickname, _username, _hostname, NOTICE_MSG(destination.getName(), message));
std::vector<User *> users = destination.getAllUsers();
std::vector<User *> users = destination.getUsers();
for (size_t i = 0; i < users.size(); i++)
if (users[i]->getNickname() != _nickname)
Server::getInstance().sendMessage(users[i]->getFd(), response);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/InviteCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void InviteCommand::execute(int clientFd) {

Channel *channel = server.getChannelByName(_channelName);

if (channel->isUserInChannel(_nickname))
if (me->isOnChannel(_channelName))
throw UserOnChannelException(_nickname, _channelName);

if (channel->isInviteOnly() && !channel->isOper(me->getNickname()))
Expand Down
2 changes: 1 addition & 1 deletion src/commands/JoinCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void JoinCommand::sendMessages(int clientFd, const Channel &channel) const {
User *user = server.getUserByFd(clientFd);

const std::string &channelName = channel.getName();
const std::vector<User *> allUsers = channel.getAllUsers();
const std::vector<User *> allUsers = channel.getUsers();

const std::string &nickname = user->getNickname();
for (std::vector<User *>::const_iterator it = allUsers.begin(); it != allUsers.end(); ++it)
Expand Down
8 changes: 5 additions & 3 deletions src/commands/KickCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void KickCommand::execute(int clientFd) {
try {
Channel *channel = server.getChannelByName(_channels[i]);

if (!channel->isUserInChannel(nickname))
if (!user->isOnChannel(_channels[i]))
throw NotOnChannelException(_channels[i]);

if (!channel->isOper(nickname))
Expand Down Expand Up @@ -64,10 +64,12 @@ void KickCommand::execute(int clientFd) {
void KickCommand::kickUserFromChannel(Channel &channel, const User &user,
const std::string &kickedUser, const std::string &comment) {
Server &server = Server::getInstance();
if (!channel.isUserInChannel(kickedUser))
User *kicked = server.getUserByNickname(kickedUser);

if (!kicked->isOnChannel(channel.getName()))
throw UserNotInChannelException(kickedUser, channel.getName());

const std::vector<User *> allUsers = channel.getAllUsers();
const std::vector<User *> allUsers = channel.getUsers();
for (std::vector<User *>::const_iterator it = allUsers.begin(); it != allUsers.end(); ++it) {
Logger::debug("Sending KICK message of user " + kickedUser + " to user " + (*it)->getNickname().c_str());
server.sendMessage((*it)->getFd(),
Expand Down
7 changes: 4 additions & 3 deletions src/commands/ModeCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void ModeCommand::execute(int clientFd) {
return;
}
}
_channel->broadcastToChannel(
_channel->broadcast(
CMD_MSG(me->getNickname(), me->getUsername(), me->getHostname(), MODE_MSG(_channel->getName(), flag, modeParams))
);
}
Expand Down Expand Up @@ -133,8 +133,9 @@ void ModeCommand::channelKey(const std::string & param) {
* @throws `UserNotInChannelException` If the user is not in the channel
*/
void ModeCommand::channelOperator(const std::string &param) {
Server::getInstance().getUserByNickname(param); // throw NoSuchNickException if the user does not exist
if (!_channel->isUserInChannel(param))
User *user = Server::getInstance().getUserByNickname(param);

if (!user->isOnChannel(_channel->getName()))
throw UserNotInChannelException(param, _channel->getName());

_plus ? _channel->makeUserAnOper(param)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/NickCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void NickCommand::execute(int clientFd) {
if (user->isRegistered() && nickname != this->_nickname) {
users.insert(user);
for (size_t i = 0; i < channels.size(); i++) {
const std::vector<User *> allUsers = channels[i]->getAllUsers();
const std::vector<User *> allUsers = channels[i]->getUsers();
for (size_t j = 0; j < allUsers.size(); j++)
users.insert(allUsers[j]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/PartCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void PartCommand::execute(int clientFd) {
throw NotOnChannelException(name);
Logger::debug("User in channel " + name);

std::vector<User *> allUsers = channel->getAllUsers();
std::vector<User *> allUsers = channel->getUsers();
std::vector<User *>::iterator usersIt;
Logger::debug("Sending PART message");
for (usersIt = allUsers.begin(); usersIt != allUsers.end(); ++usersIt)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/QuitCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void QuitCommand::execute(int clientFd) {
std::vector<Channel *>::iterator channelIt;
for (channelIt = channels.begin(); channelIt != channels.end(); ++channelIt) {

std::vector<User *> allUsers = (*channelIt)->getAllUsers();
std::vector<User *> allUsers = (*channelIt)->getUsers();
std::vector<User *>::iterator usersIt;
for (usersIt = allUsers.begin(); usersIt != allUsers.end(); ++usersIt)
users.insert(*usersIt);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/TopicCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void TopicCommand::execute(int clientFd) {
_channel->setTopic(_topic);
Logger::debug("Sending the new topic to all its users");

_channel->broadcastToChannel(
_channel->broadcast(
CMD_MSG(nickname, user->getUsername(), user->getHostname(),
TOPIC_MSG(channelName, _topic)
)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/UpCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void UpCommand::execute(int clientFd) {
Channel *channel = server.getChannelByName(_channelName);
channel->uploadFile(_path);

std::vector<User *> users = channel->getAllUsers();
std::vector<User *> users = channel->getUsers();
std::vector<User *>::iterator userIt;
for (userIt = users.begin(); userIt != users.end(); ++userIt) {
server.sendMessage((*userIt)->getFd(),
Expand Down
2 changes: 1 addition & 1 deletion src/commands/WhoCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ WhoCommand::WhoCommand(const std::string &query, const bool hasOperatorFlag)
* @param channel The channel to get the query of
*/
void WhoCommand::getQueryOfChannel(int clientFd, const Channel &channel) {
const std::vector<User *> &users = channel.getAllUsers();
const std::vector<User *> &users = channel.getUsers();
std::vector<User *>::const_iterator it;

for (it = users.begin(); it != users.end(); ++it) {
Expand Down
Loading

0 comments on commit 2f4e07a

Please sign in to comment.