1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-10 15:20:48 -05:00

add configurable command and broadcast command prefix for issue #149

This commit is contained in:
RaidMax
2020-07-31 20:40:03 -05:00
parent fc20e78066
commit 51202e7f8b
27 changed files with 403 additions and 61 deletions

View File

@ -59,7 +59,7 @@ namespace SharedLibraryCore
/// <summary>
/// Helper property to provide the syntax of the command
/// </summary>
public string Syntax => $"{_translationLookup["COMMAND_HELP_SYNTAX"]} !{Alias} {string.Join(" ", Arguments.Select(a => $"<{(a.Required ? "" : _translationLookup["COMMAND_HELP_OPTIONAL"] + " ")}{a.Name}>"))}";
public string Syntax => $"{_translationLookup["COMMAND_HELP_SYNTAX"]} {_config.CommandPrefix}{Alias} {string.Join(" ", Arguments.Select(a => $"<{(a.Required ? "" : _translationLookup["COMMAND_HELP_OPTIONAL"] + " ")}{a.Name}>"))}";
/// <summary>
/// Alternate name for this command to be executed by
@ -123,5 +123,7 @@ namespace SharedLibraryCore
/// indicates if this command allows impersonation (run as)
/// </summary>
public bool AllowImpersonation { get; set; }
public bool IsBroadcast { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Exceptions;
using System;
using System.Collections.Generic;
@ -10,12 +11,14 @@ namespace SharedLibraryCore.Commands
{
public class CommandProcessing
{
public static async Task<Command> ValidateCommand(GameEvent E)
public static async Task<Command> ValidateCommand(GameEvent E, ApplicationConfiguration appConfig)
{
var loc = Utilities.CurrentLocalization.LocalizationIndex;
var Manager = E.Owner.Manager;
bool isBroadcast = E.Data.StartsWith(appConfig.BroadcastCommandPrefix);
int prefixLength = isBroadcast ? appConfig.BroadcastCommandPrefix.Length : appConfig.CommandPrefix.Length;
string CommandString = E.Data.Substring(1, E.Data.Length - 1).Split(' ')[0];
string CommandString = E.Data.Substring(prefixLength, E.Data.Length - prefixLength).Split(' ')[0];
E.Message = E.Data;
Command C = null;
@ -34,6 +37,8 @@ namespace SharedLibraryCore.Commands
throw new CommandException($"{E.Origin} entered unknown command \"{CommandString}\"");
}
C.IsBroadcast = isBroadcast;
if (!C.AllowImpersonation && E.ImpersonationOrigin != null)
{
E.ImpersonationOrigin.Tell(loc["COMMANDS_RUN_AS_FAIL"]);

View File

@ -773,6 +773,8 @@ namespace SharedLibraryCore.Commands
/// </summary>
public class ListAdminsCommand : Command
{
private readonly CommandConfiguration _config;
public ListAdminsCommand(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, translationLookup)
{
Name = "admins";
@ -780,6 +782,8 @@ namespace SharedLibraryCore.Commands
Alias = "a";
Permission = Permission.User;
RequiresTarget = false;
_config = config;
}
public static string OnlineAdmins(Server S, ITranslationLookup lookup)
@ -798,7 +802,7 @@ namespace SharedLibraryCore.Commands
{
foreach (string line in OnlineAdmins(E.Owner, _translationLookup).Split(Environment.NewLine))
{
var _ = E.Message.IsBroadcastCommand() ? E.Owner.Broadcast(line) : E.Origin.Tell(line);
var _ = E.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix) ? E.Owner.Broadcast(line) : E.Origin.Tell(line);
}
return Task.CompletedTask;
@ -903,6 +907,8 @@ namespace SharedLibraryCore.Commands
/// </summary>
public class ListRulesCommands : Command
{
private readonly CommandConfiguration _config;
public ListRulesCommands(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, translationLookup)
{
Name = "rules";
@ -910,6 +916,8 @@ namespace SharedLibraryCore.Commands
Alias = "r";
Permission = Permission.User;
RequiresTarget = false;
_config = config;
}
public override Task ExecuteAsync(GameEvent E)
@ -917,7 +925,7 @@ namespace SharedLibraryCore.Commands
if (E.Owner.Manager.GetApplicationSettings().Configuration().GlobalRules?.Length < 1 &&
E.Owner.ServerConfig.Rules?.Length < 1)
{
var _ = E.Message.IsBroadcastCommand() ?
var _ = E.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix) ?
E.Owner.Broadcast(_translationLookup["COMMANDS_RULES_NONE"]) :
E.Origin.Tell(_translationLookup["COMMANDS_RULES_NONE"]);
}
@ -933,7 +941,7 @@ namespace SharedLibraryCore.Commands
foreach (string r in rules)
{
var _ = E.Message.IsBroadcastCommand() ? E.Owner.Broadcast($"- {r}") : E.Origin.Tell($"- {r}");
var _ = E.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix) ? E.Owner.Broadcast($"- {r}") : E.Origin.Tell($"- {r}");
}
}

View File

@ -66,6 +66,12 @@ namespace SharedLibraryCore.Configuration
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_CUSTOM_LOCALE")]
public string CustomLocale { get; set; }
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_COMMAND_PREFIX")]
public string CommandPrefix { get; set; } = "!";
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_BROADCAST_COMMAND_PREFIX")]
public string BroadcastCommandPrefix { get; set; } = "@";
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_DB_PROVIDER")]
public string DatabaseProvider { get; set; } = "sqlite";
[ConfigurationOptional]

View File

@ -1,6 +1,7 @@
using SharedLibraryCore.Interfaces;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace SharedLibraryCore.Configuration
{
@ -14,6 +15,18 @@ namespace SharedLibraryCore.Configuration
/// </summary>
public Dictionary<string, CommandProperties> Commands { get; set; } = new Dictionary<string, CommandProperties>();
/// <summary>
/// prefix indicated the chat message is a command
/// </summary>
[JsonIgnore]
public string CommandPrefix { get; set; }
/// <summary>
/// prefix indicating that the chat message is a broadcast command
/// </summary>
[JsonIgnore]
public string BroadcastCommandPrefix { get; set; }
public IBaseConfiguration Generate()
{
throw new NotImplementedException();

View File

@ -64,13 +64,19 @@ namespace SharedLibraryCore.Configuration.Validation
RuleFor(_app => _app.GlobalRules)
.NotNull();
RuleForEach(_app => _app.Servers)
.NotEmpty()
.SetValidator(new ServerConfigurationValidator());
RuleFor(_app => _app.MasterUrl)
.NotNull()
.Must(_url => _url != null && _url.Scheme == Uri.UriSchemeHttp);
RuleFor(_app => _app.CommandPrefix)
.NotEmpty();
RuleFor(_app => _app.BroadcastCommandPrefix)
.NotEmpty();
RuleForEach(_app => _app.Servers)
.NotEmpty()
.SetValidator(new ServerConfigurationValidator());
}
}
}

View File

@ -49,5 +49,10 @@ namespace SharedLibraryCore.Interfaces
/// Indicates if the commands can be run as another client
/// </summary>
bool AllowImpersonation { get; }
/// <summary>
/// Indicates if the command result should be broadcasted to all clients
/// </summary>
bool IsBroadcast { get; set; }
}
}

View File

@ -486,7 +486,7 @@ namespace SharedLibraryCore.Database.Models
.DisallowedClientNames
?.Any(_name => Regex.IsMatch(Name, _name)) ?? false)
{
CurrentServer.Logger.WriteDebug($"Kicking {this} because their name is generic");
CurrentServer.Logger.WriteDebug($"Kicking {this} because their name is not allowed");
Kick(loc["SERVER_KICK_GENERICNAME"], Utilities.IW4MAdminClient(CurrentServer));
return false;
}

View File

@ -235,9 +235,14 @@ namespace SharedLibraryCore
return str;
}
public static bool IsBroadcastCommand(this string str)
public static bool IsBroadcastCommand(this string str, string broadcastCommandPrefix)
{
return str[0] == '@';
return str.StartsWith(broadcastCommandPrefix);
}
public static IManagerCommand AsCommand(this GameEvent gameEvent)
{
return gameEvent.Extra as IManagerCommand;
}
/// <summary>