Skip to content

Commit

Permalink
feat(mode): accept alternative MODE syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mxafi committed Mar 22, 2024
1 parent 8fe1905 commit 2ffb5a1
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/command/actionMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@ void Command::actionMode(Client& client) {
// o: operator (parameter: nickname)
// l: user-limit (parameter: limit)

// We will get something like: MODE #mychannel +ik key +tl 10
// We will get something like: MODE #mychannel +ik key +tl 10
// We can also get something like: MODE #mychannel +ktl key 10 +i
// We will parse it into a vector of modestructs like this:
// vector[0]: {modifier: '+', mode: 'i', param: ""}
// vector[1]: {modifier: '+', mode: 'k', param: "key"}
// vector[2]: {modifier: '+', mode: 't', param: ""}
// vector[3]: {modifier: '+', mode: 'l', param: "10"}

std::vector<Channel::modestruct> modeRequests;
unsigned long oldModeRequestsSize = modeRequests.size();
int modeChangesWithParam = 0; // max is 3
unsigned int currentParamIndex = 1; // max is numberOfParams - 1
while (currentParamIndex < numberOfParams) {
Expand All @@ -134,8 +136,7 @@ void Command::actionMode(Client& client) {
char currentModifier = currentParamString[0];
if (currentModifier != '+' && currentModifier != '-') {
LOG_DEBUG("Command::actionMode: currentModifier != '+' && currentModifier != '-' not allowed");
client.appendToSendBuffer(
RPL_ERR_NEEDMOREPARAMS_461(serverHostname_g, client.getNickname(), "MODE"));
client.appendToSendBuffer(RPL_ERR_NEEDMOREPARAMS_461(serverHostname_g, client.getNickname(), "MODE"));
return;
}
currentParamString = currentParamString.substr(1);
Expand All @@ -150,17 +151,26 @@ void Command::actionMode(Client& client) {
modeRequests.push_back(currentMode);
}

if (isNextParamExist(currentParamIndex, numberOfParams) && isModeWithParam(modeRequests.back())) {
modeChangesWithParam++;
currentParamIndex++;
modeRequests.back().param = param_.at(currentParamIndex);
for (unsigned long i = oldModeRequestsSize; i < modeRequests.size(); i++) {
if (isModeWithParam(modeRequests.at(i))) {
if (isNextParamExist(currentParamIndex, numberOfParams)) {
modeChangesWithParam++;
currentParamIndex++;
modeRequests.at(i).param = param_.at(currentParamIndex);
} else {
LOG_DEBUG("Command::actionMode: mode with param but no param provided");
client.appendToSendBuffer(RPL_ERR_NEEDMOREPARAMS_461(serverHostname_g, client.getNickname(), "MODE"));
return;
}
}
}

oldModeRequestsSize = modeRequests.size();
currentParamIndex++;
}
if (modeChangesWithParam > 3) {
LOG_DEBUG("Command::actionMode: modeChangesWithParam > 3 not allowed");
client.appendToSendBuffer(
RPL_ERR_NEEDMOREPARAMS_461(serverHostname_g, client.getNickname(), "MODE"));
client.appendToSendBuffer(RPL_ERR_NEEDMOREPARAMS_461(serverHostname_g, client.getNickname(), "MODE"));
return;
}

Expand Down

0 comments on commit 2ffb5a1

Please sign in to comment.