about summary refs log tree commit diff
path: root/MatrixContentFilter/Commands/BanCommand.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixContentFilter/Commands/BanCommand.cs')
-rw-r--r--MatrixContentFilter/Commands/BanCommand.cs97
1 files changed, 97 insertions, 0 deletions
diff --git a/MatrixContentFilter/Commands/BanCommand.cs b/MatrixContentFilter/Commands/BanCommand.cs
new file mode 100644

index 0000000..0ae38e2 --- /dev/null +++ b/MatrixContentFilter/Commands/BanCommand.cs
@@ -0,0 +1,97 @@ +using LibMatrix.Helpers; +using LibMatrix.RoomTypes; +using LibMatrix.Utilities.Bot.Interfaces; +using MatrixContentFilter.Services; +using MatrixContentFilter.Services.AsyncActionQueues; + +namespace MatrixContentFilter.Commands; + +public class BanCommand( + ConfigurationService filterConfigService, + AsyncMessageQueue msgQueue, + InfoCacheService infoCache, + ConfigurationService cfgService, + AbstractAsyncActionQueue actionQueue +) : ICommand { + public string Name { get; } = "ban"; + public string[]? Aliases { get; } = []; + public string Description { get; } = "Ban user (see --help)"; + public bool Unlisted { get; } = false; + + public async Task Invoke(CommandContext ctx) { + bool HasMoreArgs(int index) => ctx.Args.Length > index + 1; + if (ctx.Args.Contains("--help") || ctx.Args.Length == 0) { + await SendHelp(ctx.Room); + return; + } + + BanCommandArgs args = new() { + UserId = null, + Reason = null + }; + + // if (ctx.Args.Contains("--redact")) { + // var index = Array.IndexOf(ctx.Args, "--redact"); + // if (HasMoreArgs(index)) { + // args.Redact = int.TryParse(ctx.Args[index + 1], out var redact) ? redact : 500; + // } + // } + + for (int i = 0; i < ctx.Args.Length; i++) { + if (ctx.Args[i].StartsWith("--")) { + switch (ctx.Args[i]) { + case "--redact": { + if (HasMoreArgs(i)) { + // args.Redact = int.TryParse(ctx.Args[i + 1], out var redact) ? redact : 500; + if (int.TryParse(ctx.Args[i + 1], out var redact)) { + args.Redact = redact; + i++; + } + else args.Redact = 500; + } + + break; + } + } + } + else if (args.UserId == null) { + args.UserId = ctx.Args[i]; + } + else args.Reason += ctx.Args[i] + " "; + } + } + + private async Task SendHelp(GenericRoom room) { + string[][] helpTable = [ + ["--help", "", "Show this message"], + ["--redact", "count ?? 500", "Redact last N events from user"], + ["user_id", "required", "User ID to ban"], + ["reason", "required", "Reason for ban"], + ]; + var msb = new MessageBuilder("m.notice") + .WithTable(tb => { + tb.WithTitle("Help for ban command", 3) + .WithTitle("Basic usage: ban [options] user_id reason", 3) + .WithRow(rb => + rb.WithCell("Command") + .WithCell("Arguments") + .WithCell("Description") + ); + foreach (var row in helpTable) { + tb.WithRow(rb => + rb.WithCell(row[0]) + .WithCell(row[1]) + .WithCell(row[2]) + ); + } + }); + + await room.SendMessageEventAsync(msb.Build()); + } + + private struct BanCommandArgs { + public string UserId { get; set; } + public string? Reason { get; set; } + public int? Redact { get; set; } + } +} \ No newline at end of file