Skip to content

Commit

Permalink
fix(mode): issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ruzafa8 committed Apr 28, 2024
1 parent cf908c3 commit f2eed71
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion includes/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# include "User.hpp"

# define MAX_CHANNEL_NAME_LENGTH 20
# define NO_LIMIT -1
# define NO_LIMIT 0
# define BELL_CHAR '\a'

class User;
Expand Down
2 changes: 1 addition & 1 deletion src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ bool Channel::hasLimit() const {
* @return `true` if the channel is full, `false` otherwise.
*/
bool Channel::isFull() const {
return (int) (this->_users.size() + this->_operators.size()) == this->_limit;
return (int) (this->_users.size() + this->_operators.size()) >= this->_limit && this->_limit != NO_LIMIT;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ std::vector<std::string> Utils::split(const std::string &s, char delim) {
bool Utils::isNumber(const std::string& s) {
std::string::const_iterator it = s.begin();

while (it != s.end() && std::isdigit(*it))
while (it != s.end() && (std::isdigit(*it) || (*it == '-' && it == s.begin())))
++it;
return !s.empty() && it == s.end();
}
36 changes: 16 additions & 20 deletions src/commands/ModeCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,31 @@ void ModeCommand::execute(int clientFd) {
std::string flag = _plus ? "+" : "-";
std::vector<std::string>::iterator paramIterator = _modeParams.begin();
std::string param = NONE;
std::string modeParams = NONE;
for (size_t i = 0; i < _modes.size(); i++) {
flag += _modes[i];

if (ModeCommand::modeNeedsParam(_modes[i])) {
if (paramIterator == _modeParams.end())
throw NeedMoreParamsException("MODE");
param = *(paramIterator++);
modeParams += param + " ";
}
switch (_modes[i]) {
case INVITE_ONLY:
ModeCommand::inviteOnly();
break;
case TOPIC_PROTECTED:
ModeCommand::topicProtected();
break;
case CHANNEL_KEY:
ModeCommand::channelKey(param);
break;
case CHANNEL_OPERATOR:
ModeCommand::channelOperator(param);
break;
case USER_LIMIT:
ModeCommand::userLimit(param);
break;
default:
throw UnknownModeException(std::string(1, _modes[i]));
try {
switch (_modes[i]) {
case INVITE_ONLY: ModeCommand::inviteOnly(); break;
case TOPIC_PROTECTED: ModeCommand::topicProtected(); break;
case CHANNEL_KEY: ModeCommand::channelKey(param); break;
case CHANNEL_OPERATOR: ModeCommand::channelOperator(param); break;
case USER_LIMIT: ModeCommand::userLimit(param); break;
default: throw UnknownModeException(std::string(1, _modes[i]));
}
} catch (IRCException &e) {
server.sendExceptionMessage(clientFd, e);
}
}
_channel.broadcastToChannel(
CMD_MSG(me.getNickname(), me.getUsername(), me.getHostname(), MODE_MSG(_channel.getName(), flag, param))
CMD_MSG(me.getNickname(), me.getUsername(), me.getHostname(), MODE_MSG(_channel.getName(), flag, modeParams))
);
}

Expand Down Expand Up @@ -129,6 +124,7 @@ void ModeCommand::channelKey(const std::string & param) {
* Sets the user as an operator of the channel.
*/
void ModeCommand::channelOperator(const std::string &param) {
Server::getInstance().getUserByNickname(param);
if (!_channel.isUserInChannel(param))
throw UserNotInChannelException(param, _channel.getName());
if (_plus)
Expand Down

0 comments on commit f2eed71

Please sign in to comment.