Skip to content

Commit

Permalink
Add globally blocked and globally allowed helper commands
Browse files Browse the repository at this point in the history
  • Loading branch information
features-not-bugs committed Mar 6, 2022
1 parent 01474db commit 3c86956
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 12 deletions.
197 changes: 189 additions & 8 deletions src/AdminRestrictions/CommandRestrictor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool IsCommandPermitted(ConsoleSystem.Arg arg)
return true;

// Is this command globally allowed?
for (int i = 0; i < _configuration.globallyAllowedCommands.Length; i++)
for (int i = 0; i < _configuration.globallyAllowedCommands.Count; i++)
{
if (string.Equals(_configuration.globallyAllowedCommands[i], arg.cmd.FullName))
{
Expand All @@ -79,7 +79,7 @@ bool IsCommandPermitted(ConsoleSystem.Arg arg)
}

// Is this command globally blocked?
for (int i = 0; i < _configuration.globallyBlockedCommands.Length; i++)
for (int i = 0; i < _configuration.globallyBlockedCommands.Count; i++)
{
if (string.Equals(_configuration.globallyBlockedCommands[i], arg.cmd.FullName))
{
Expand Down Expand Up @@ -182,7 +182,51 @@ void RegisterCommands()
Variable = false,
Call = new Action<ConsoleSystem.Arg>(ReloadCfgCommand)
};
ConsoleSystem.Index.Server.Dict[commandPrefix + "." + "reloadcfg"] = reloadCfgCommand;
ConsoleSystem.Index.Server.Dict[reloadCfgCommand.FullName] = reloadCfgCommand;

ConsoleSystem.Command addGloballyAllowedCommand = new ConsoleSystem.Command()
{
Name = "addgloballyallowedcommand",
Parent = commandPrefix,
FullName = commandPrefix + "." + "addgloballyallowedcommand",
ServerAdmin = true,
Variable = false,
Call = new Action<ConsoleSystem.Arg>(AddGloballyAllowedCommand)
};
ConsoleSystem.Index.Server.Dict[addGloballyAllowedCommand.FullName] = addGloballyAllowedCommand;

ConsoleSystem.Command removeGloballyAllowedCommand = new ConsoleSystem.Command()
{
Name = "removegloballyallowedcommand",
Parent = commandPrefix,
FullName = commandPrefix + "." + "removegloballyallowedcommand",
ServerAdmin = true,
Variable = false,
Call = new Action<ConsoleSystem.Arg>(RemoveGloballyAllowedCommand)
};
ConsoleSystem.Index.Server.Dict[removeGloballyAllowedCommand.FullName] = removeGloballyAllowedCommand;

ConsoleSystem.Command addGloballyBlockedCommand = new ConsoleSystem.Command()
{
Name = "addgloballyblockedcommand",
Parent = commandPrefix,
FullName = commandPrefix + "." + "addgloballyblockedcommand",
ServerAdmin = true,
Variable = false,
Call = new Action<ConsoleSystem.Arg>(AddGloballyBlockedCommand)
};
ConsoleSystem.Index.Server.Dict[addGloballyBlockedCommand.FullName] = addGloballyBlockedCommand;

ConsoleSystem.Command removeGloballyBlockedCommand = new ConsoleSystem.Command()
{
Name = "removegloballyblockedcommand",
Parent = commandPrefix,
FullName = commandPrefix + "." + "removegloballyblockedcommand",
ServerAdmin = true,
Variable = false,
Call = new Action<ConsoleSystem.Arg>(RemoveGloballyBlockedCommand)
};
ConsoleSystem.Index.Server.Dict[removeGloballyBlockedCommand.FullName] = removeGloballyBlockedCommand;

ConsoleSystem.Command addAdminToGroupCommand = new ConsoleSystem.Command()
{
Expand All @@ -193,7 +237,7 @@ void RegisterCommands()
Variable = false,
Call = new Action<ConsoleSystem.Arg>(AddAdminToGroupCommand)
};
ConsoleSystem.Index.Server.Dict[commandPrefix + "." + "addadmintogroup"] = addAdminToGroupCommand;
ConsoleSystem.Index.Server.Dict[addAdminToGroupCommand.FullName] = addAdminToGroupCommand;

ConsoleSystem.Command removeAdminFromGroupCommand = new ConsoleSystem.Command()
{
Expand All @@ -204,7 +248,7 @@ void RegisterCommands()
Variable = false,
Call = new Action<ConsoleSystem.Arg>(RemoveAdminFromGroupCommand)
};
ConsoleSystem.Index.Server.Dict[commandPrefix + "." + "removeadminfromgroup"] = removeAdminFromGroupCommand;
ConsoleSystem.Index.Server.Dict[removeAdminFromGroupCommand.FullName] = removeAdminFromGroupCommand;

ConsoleSystem.Command[] allCommands = ConsoleSystem.Index.All.Concat(new ConsoleSystem.Command[] { reloadCfgCommand, addAdminToGroupCommand, removeAdminFromGroupCommand }).ToArray();
// Would be nice if this had a public setter, or better yet, a register command helper
Expand All @@ -213,6 +257,108 @@ void RegisterCommands()
.SetValue(null, allCommands);
}

void AddGloballyAllowedCommand(ConsoleSystem.Arg arg)
{
if (!arg.HasArgs(1))
{
arg.ReplyWith("[AdminRestrictions]: Invalid syntax: adminrestrictions.addgloballyallowedcommand <command.name> [<command.name>...]");
return;
}

int processed = 0;
for (int i = 0; i < arg.Args.Length; i++)
{
var command = arg.Args[i];
if (string.IsNullOrWhiteSpace(command)) continue;
if (_configuration.globallyAllowedCommands.Contains(command)) continue;
_configuration.globallyAllowedCommands.Add(command);
processed++;
}

if (processed > 0)
{
SaveConfiguration();
}

arg.ReplyWith("[AdminRestrictions]: Added " + processed + " command(s) to the globally allowed list");
}

void RemoveGloballyAllowedCommand(ConsoleSystem.Arg arg)
{
if (!arg.HasArgs(1))
{
arg.ReplyWith("[AdminRestrictions]: Invalid syntax: adminrestrictions.removegloballyallowedcommand <command.name> [<command.name>...]");
return;
}

int processed = 0;
for (int i = 0; i < arg.Args.Length; i++)
{
var command = arg.Args[i];
if (string.IsNullOrWhiteSpace(command)) continue;
if (!_configuration.globallyAllowedCommands.Remove(command)) continue;
processed++;
}

if (processed > 0)
{
SaveConfiguration();
}

arg.ReplyWith("[AdminRestrictions]: Removed " + processed + " command(s) from the globally allowed list");
}

void AddGloballyBlockedCommand(ConsoleSystem.Arg arg)
{
if (!arg.HasArgs(1))
{
arg.ReplyWith("[AdminRestrictions]: Invalid syntax: adminrestrictions.addgloballyblockedcommand <command.name> [<command.name>...]");
return;
}

int processed = 0;
for (int i = 0; i < arg.Args.Length; i++)
{
var command = arg.Args[i];
if (string.IsNullOrWhiteSpace(command)) continue;
if (_configuration.globallyBlockedCommands.Contains(command)) continue;
_configuration.globallyAllowedCommands.Add(command);
processed++;
}

if (processed > 0)
{
SaveConfiguration();
}

arg.ReplyWith("[AdminRestrictions]: Added " + processed + " command(s) to the globally blocked list");
}

void RemoveGloballyBlockedCommand(ConsoleSystem.Arg arg)
{
if (!arg.HasArgs(1))
{
arg.ReplyWith("[AdminRestrictions]: Invalid syntax: adminrestrictions.removegloballyblockedcommand <command.name> [<command.name>...]");
return;
}

int processed = 0;
for (int i = 0; i < arg.Args.Length; i++)
{
var command = arg.Args[i];
if (string.IsNullOrWhiteSpace(command)) continue;
if (!_configuration.globallyBlockedCommands.Remove(command)) continue;
processed++;
}

if (processed > 0)
{
SaveConfiguration();
}

arg.ReplyWith("[AdminRestrictions]: Removed " + processed + " command(s) from the globally blocked list");
}

void AddAdminToGroupCommand(ConsoleSystem.Arg arg)
{
if (!arg.HasArgs(2))
Expand Down Expand Up @@ -415,8 +561,11 @@ GlobalConfig GenerateDefaultConfiguration()
return new GlobalConfig
{
enabled = false,
globallyBlockedCommands = new string[]
globallyBlockedCommands = new List<string>
{
"server.stop",
"spawn.fill_populations",
"spawn.fill_groups",
"global.quit",
"global.restart",
"demo.record",
Expand All @@ -425,15 +574,47 @@ GlobalConfig GenerateDefaultConfiguration()
"demo.splitseconds",
"demo.stop"
},
logToFile = true,
globallyAllowedCommands = new List<string>
{
"global.teleport2player",
"global.teleport2owneditem",
"global.teleport2marker",
"global.teleportlos",
"global.teleportpos",
"global.spectate",
"global.teaminfo",
"server.snapshot",
"server.combatlog",
"server.combatlog_outgoing",
"global.god",
"global.sleep"
},
logToFile = false,
groupConfigs = new GroupConfig[]
{
new GroupConfig
{
name = "demo-group-1",
name = "allow-all",
allowAll = true,
allowedCommands = new string[0],
steamIds = new List<ulong>
{
1234567890
}
},
new GroupConfig
{
name = "admins",
allowAll = false,
allowedCommands = new string[]
{
"global.teleport2me",
"entity.spawn",
"inventory.give",
"inventory.giveall",
"inventory.givearm",
"inventory.giveid",
"inventory.giveto",
"global.entid"
},
steamIds = new List<ulong>
Expand Down
9 changes: 5 additions & 4 deletions src/AdminRestrictions/Configuration/GlobalConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace AdminRestrictions.Configuration
{
Expand All @@ -8,10 +9,10 @@ internal class GlobalConfig
public bool enabled = false;

[JsonProperty(PropertyName = "Globally Blocked Commands")]
public string[] globallyBlockedCommands;
public List<string> globallyBlockedCommands;

[JsonProperty(PropertyName = "Globally Allowed Commands")]
public string[] globallyAllowedCommands;
public List<string> globallyAllowedCommands;

[JsonProperty(PropertyName = "Log to file")]
public bool logToFile = true;
Expand All @@ -21,8 +22,8 @@ internal class GlobalConfig

public GlobalConfig()
{
globallyBlockedCommands = globallyBlockedCommands ?? new string[0];
globallyAllowedCommands = globallyAllowedCommands ?? new string[0];
globallyBlockedCommands = globallyBlockedCommands ?? new List<string>();
globallyAllowedCommands = globallyAllowedCommands ?? new List<string>();
groupConfigs = groupConfigs ?? new GroupConfig[0];
}
}
Expand Down

0 comments on commit 3c86956

Please sign in to comment.