Skip to content

Commit

Permalink
Replace the flags_required field with an enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
SadieCat committed Apr 14, 2020
1 parent bafb721 commit 9ec5126
Show file tree
Hide file tree
Showing 55 changed files with 116 additions and 83 deletions.
20 changes: 15 additions & 5 deletions include/ctables.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@

#pragma once

/** Used to indicate the type of a command. */
enum class CmdAccess : uint8_t
{
/* The command has no special attributes. */
NORMAL = 0,

/** Only server operators can use the command. */
OPERATOR = 1,

/** Only servers can use the command. */
SERVER = 2,
};

/** Used to indicate the result of trying to execute a command. */
enum CmdResult
{
Expand All @@ -39,9 +52,6 @@ enum CmdResult
CMD_INVALID = 2
};

/** Flag for commands that are only allowed from servers */
const char FLAG_SERVERONLY = 7; // technically anything nonzero below 'A' works

/** Translation types for translation of parameters to UIDs.
* This allows the core commands to not have to be aware of how UIDs
* work (making it still possible to write other linking modules which
Expand Down Expand Up @@ -208,8 +218,8 @@ class CoreExport Command : public CommandBase
/** Unregisters this command from the command parser. */
~Command() override;

/** The user modes required to be able to execute this command. */
unsigned char flags_needed = 0;
/** Who can access this command? */
CmdAccess access_needed = CmdAccess::NORMAL;

/** Whether the command will not be forwarded by the linking module even if it comes via ENCAP. */
bool force_manual_route = false;
Expand Down
61 changes: 42 additions & 19 deletions src/command_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,22 @@ CmdResult CommandParser::CallHandler(const std::string& commandname, const Comma
{
bool bOkay = false;

if (IS_LOCAL(user) && n->second->flags_needed)
if (IS_LOCAL(user))
{
/* if user is local, and flags are needed .. */

if (user->IsModeSet(n->second->flags_needed))
switch (n->second->access_needed)
{
/* if user has the flags, and now has the permissions, go ahead */
if (user->HasCommandPermission(commandname))
case CmdAccess::NORMAL: // Anyone can execute.
bOkay = true;
}
break;

case CmdAccess::OPERATOR: // Only opers can execute.
bOkay = user->HasCommandPermission(commandname);
break;

case CmdAccess::SERVER: // Only servers can execute.
bOkay = IS_SERVER(user);
break;
};
}
else
{
Expand Down Expand Up @@ -256,24 +262,41 @@ void CommandParser::ProcessCommand(LocalUser* user, std::string& command, Comman

/* activity resets the ping pending timer */
user->nextping = ServerInstance->Time() + user->MyClass->GetPingTime();

if (handler->flags_needed)
switch (handler->access_needed)
{
if (!user->IsModeSet(handler->flags_needed))
case CmdAccess::NORMAL:
break; // Nothing special too do.

case CmdAccess::OPERATOR:
{
user->CommandFloodPenalty += failpenalty;
user->WriteNumeric(ERR_NOPRIVILEGES, "Permission Denied - You do not have the required operator privileges");
FOREACH_MOD(OnCommandBlocked, (command, command_p, user));
return;
if (!user->IsOper())
{
user->CommandFloodPenalty += failpenalty;
user->WriteNumeric(ERR_NOPRIVILEGES, "Permission Denied - You do not have the required operator privileges");
FOREACH_MOD(OnCommandBlocked, (command, command_p, user));
return;
}

if (!user->HasCommandPermission(command))
{
user->CommandFloodPenalty += failpenalty;
user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - Oper type %s does not have access to command %s",
user->oper->name.c_str(), command.c_str()));
FOREACH_MOD(OnCommandBlocked, (command, command_p, user));
return;
}
break;
}

if (!user->HasCommandPermission(command))
case CmdAccess::SERVER:
{
user->CommandFloodPenalty += failpenalty;
user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - Oper type %s does not have access to command %s",
user->oper->name.c_str(), command.c_str()));
if (user->registered == REG_ALL)
user->WriteNumeric(ERR_UNKNOWNCOMMAND, command, "Unknown command");

ServerInstance->stats.Unknown++;
FOREACH_MOD(OnCommandBlocked, (command, command_p, user));
return;

break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_info/cmd_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ CmdResult CommandCommands::Handle(User* user, const Params& parameters)
for (CommandParser::CommandMap::const_iterator i = commands.begin(); i != commands.end(); ++i)
{
// Don't show S2S commands to users
if (i->second->flags_needed == FLAG_SERVERONLY)
if (i->second->access_needed == CmdAccess::SERVER)
continue;

Module* src = i->second->creator;
Expand Down
4 changes: 2 additions & 2 deletions src/coremods/core_loadmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CommandLoadmodule : public Command
CommandLoadmodule(Module* parent)
: Command(parent,"LOADMODULE", 1, 1)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<modulename>" };
}

Expand Down Expand Up @@ -75,7 +75,7 @@ class CommandUnloadmodule : public Command
CommandUnloadmodule(Module* parent)
: Command(parent, "UNLOADMODULE", 1)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<modulename>" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_oper/cmd_die.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
CommandDie::CommandDie(Module* parent)
: Command(parent, "DIE", 1, 1)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<servername>" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_oper/cmd_kill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CommandKill::CommandKill(Module* parent)
: Command(parent, "KILL", 2, 2)
, protoev(parent, name)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<nick>[,<nick>]+ :<reason>" };
translation = { TR_CUSTOM, TR_CUSTOM };
}
Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_oper/cmd_rehash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
CommandRehash::CommandRehash(Module* parent)
: Command(parent, "REHASH", 0)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
Penalty = 2;
syntax = { "[<servermask>]" };
}
Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_oper/cmd_restart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
CommandRestart::CommandRestart(Module* parent)
: Command(parent, "RESTART", 1, 1)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<servername>" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_reloadmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CommandReloadmodule : public Command
{
reloadevprov = &evprov;
dummyserializer = &dummyser;
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<modulename>" };
}

Expand Down
4 changes: 2 additions & 2 deletions src/coremods/core_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CommandConnect : public Command
CommandConnect(Module* parent)
: Command(parent, "CONNECT", 1)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<servermask>" };
}

Expand Down Expand Up @@ -125,7 +125,7 @@ class CommandSquit : public Command
CommandSquit(Module* parent)
: Command(parent, "SQUIT", 1, 2)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<servermask>" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_wallops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CommandWallops : public Command
, wallopsmode(parent, "wallops", 'w')
, protoevprov(parent, name)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { ":<message>" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_xline/cmd_eline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
CommandEline::CommandEline(Module* parent)
: Command(parent, "ELINE", 1, 3)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<user@host> [<duration> :<reason>]" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_xline/cmd_gline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
CommandGline::CommandGline(Module* parent)
: Command(parent, "GLINE", 1, 3)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<user@host> [<duration> :<reason>]" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_xline/cmd_kline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
CommandKline::CommandKline(Module* parent)
: Command(parent, "KLINE", 1, 3)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<user@host> [<duration> :<reason>]" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_xline/cmd_qline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
CommandQline::CommandQline(Module* parent)
: Command(parent, "QLINE", 1, 3)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<nickmask> [<duration> :<reason>]" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/coremods/core_xline/cmd_zline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
CommandZline::CommandZline(Module* parent)
: Command(parent, "ZLINE", 1, 3)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<ipmask> [<duration> :<reason>]" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_alltime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CommandAlltime : public Command
public:
CommandAlltime(Module* Creator) : Command(Creator, "ALLTIME", 0)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
}

CmdResult Handle(User* user, const Params& parameters) override
Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_cban.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CommandCBan : public Command
public:
CommandCBan(Module* Creator) : Command(Creator, "CBAN", 1, 3)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<channel> [<duration> [:<reason>]]" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class CommandCheck : public Command
: Command(parent,"CHECK", 1)
, snomaskmode(parent, "snomask")
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<nick>|<ipmask>|<hostmask>|<channel> [<servername>]" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_chghost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CommandChghost : public Command
: Command(Creator,"CHGHOST", 2)
{
allow_empty_last_param = false;
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<nick> <host>" };
translation = { TR_NICK, TR_TEXT };
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_chgident.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CommandChgident : public Command
CommandChgident(Module* Creator) : Command(Creator,"CHGIDENT", 2)
{
allow_empty_last_param = false;
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<nick> <ident>" };
translation = { TR_NICK, TR_TEXT };
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_chgname.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CommandChgname : public Command
CommandChgname(Module* Creator) : Command(Creator,"CHGNAME", 2, 2)
{
allow_empty_last_param = false;
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<nick> :<realname>" };
translation = { TR_NICK, TR_TEXT };
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_clearchan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CommandClearChan : public Command
: Command(Creator, "CLEARCHAN", 1, 3)
{
syntax = { "<channel> [KILL|KICK|G|Z] [:<reason>]" };
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;

// Stop the linking mod from forwarding ENCAP'd CLEARCHAN commands, see below why
force_manual_route = true;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_cloaking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class CommandCloak : public Command
public:
CommandCloak(Module* Creator) : Command(Creator, "CLOAK", 1)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<host>" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_clones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CommandClones : public SplitCommand
, batchmanager(Creator)
, batch("inspircd.org/clones")
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<limit>" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class CommandFilter : public Command
CommandFilter(Module* f)
: Command(f, "FILTER", 1, 5)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
this->syntax = { "<pattern> [<action> <flags> [<duration>] :<reason>]" };
}
CmdResult Handle(User* user, const Params& parameters) override;
Expand Down
6 changes: 3 additions & 3 deletions src/modules/m_globalload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CommandGloadmodule : public Command
public:
CommandGloadmodule(Module* Creator) : Command(Creator,"GLOADMODULE", 1)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<modulename> [<servermask>]" };
}

Expand Down Expand Up @@ -72,7 +72,7 @@ class CommandGunloadmodule : public Command
public:
CommandGunloadmodule(Module* Creator) : Command(Creator,"GUNLOADMODULE", 1)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<modulename> [<servermask>]" };
}

Expand Down Expand Up @@ -123,7 +123,7 @@ class CommandGreloadmodule : public Command
public:
CommandGreloadmodule(Module* Creator) : Command(Creator, "GRELOADMODULE", 1)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { "<modulename> [<servermask>]" };
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/m_globops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CommandGlobops : public Command
public:
CommandGlobops(Module* Creator) : Command(Creator,"GLOBOPS", 1,1)
{
flags_needed = 'o';
access_needed = CmdAccess::OPERATOR;
syntax = { ":<message>" };
}

Expand Down

0 comments on commit 9ec5126

Please sign in to comment.