about summary refs log tree commit diff
path: root/MatrixContentFilter/Commands
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixContentFilter/Commands')
-rw-r--r--MatrixContentFilter/Commands/BanCommand.cs97
-rw-r--r--MatrixContentFilter/Commands/CheckHistoryCommand.cs2
-rw-r--r--MatrixContentFilter/Commands/ConfigureCommand.cs1
-rw-r--r--MatrixContentFilter/Commands/DumpEventCommand.cs6
-rw-r--r--MatrixContentFilter/Commands/GetConfigCommand.cs4
-rw-r--r--MatrixContentFilter/Commands/NewRoomCommand.cs8
-rw-r--r--MatrixContentFilter/Commands/RedactCommand.cs15
7 files changed, 103 insertions, 30 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 diff --git a/MatrixContentFilter/Commands/CheckHistoryCommand.cs b/MatrixContentFilter/Commands/CheckHistoryCommand.cs
index 1e98545..4d34ee6 100644 --- a/MatrixContentFilter/Commands/CheckHistoryCommand.cs +++ b/MatrixContentFilter/Commands/CheckHistoryCommand.cs
@@ -1,9 +1,7 @@ using LibMatrix.Helpers; -using LibMatrix.Homeservers; using LibMatrix.Utilities.Bot.Interfaces; using MatrixContentFilter.Abstractions; using MatrixContentFilter.Services; -using Microsoft.Extensions.Logging; namespace MatrixContentFilter.Commands; diff --git a/MatrixContentFilter/Commands/ConfigureCommand.cs b/MatrixContentFilter/Commands/ConfigureCommand.cs
index 756fc14..472e387 100644 --- a/MatrixContentFilter/Commands/ConfigureCommand.cs +++ b/MatrixContentFilter/Commands/ConfigureCommand.cs
@@ -1,4 +1,3 @@ -using System.Text; using LibMatrix.EventTypes.Spec; using LibMatrix.Utilities.Bot.Commands; using LibMatrix.Utilities.Bot.Interfaces; diff --git a/MatrixContentFilter/Commands/DumpEventCommand.cs b/MatrixContentFilter/Commands/DumpEventCommand.cs
index 5131e19..ebffcd4 100644 --- a/MatrixContentFilter/Commands/DumpEventCommand.cs +++ b/MatrixContentFilter/Commands/DumpEventCommand.cs
@@ -1,14 +1,8 @@ using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State; -using LibMatrix.Filters; using LibMatrix.Helpers; -using LibMatrix.Homeservers; using LibMatrix.Utilities.Bot.Interfaces; -using MatrixContentFilter.Abstractions; using MatrixContentFilter.Services; using MatrixContentFilter.Services.AsyncActionQueues; -using Microsoft.Extensions.Logging; namespace MatrixContentFilter.Commands; diff --git a/MatrixContentFilter/Commands/GetConfigCommand.cs b/MatrixContentFilter/Commands/GetConfigCommand.cs
index bac00ca..ebd059c 100644 --- a/MatrixContentFilter/Commands/GetConfigCommand.cs +++ b/MatrixContentFilter/Commands/GetConfigCommand.cs
@@ -1,12 +1,8 @@ -using System.Text; using ArcaneLibs.Attributes; using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec; using LibMatrix.Helpers; -using LibMatrix.Utilities.Bot.Commands; using LibMatrix.Utilities.Bot.Interfaces; using MatrixContentFilter.EventTypes; -using Microsoft.Extensions.DependencyInjection; namespace MatrixContentFilter.Commands; diff --git a/MatrixContentFilter/Commands/NewRoomCommand.cs b/MatrixContentFilter/Commands/NewRoomCommand.cs
index b8becd4..916649e 100644 --- a/MatrixContentFilter/Commands/NewRoomCommand.cs +++ b/MatrixContentFilter/Commands/NewRoomCommand.cs
@@ -1,12 +1,4 @@ -using System.Text; -using ArcaneLibs.Attributes; -using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec; -using LibMatrix.Helpers; -using LibMatrix.Utilities.Bot.Commands; using LibMatrix.Utilities.Bot.Interfaces; -using MatrixContentFilter.EventTypes; -using Microsoft.Extensions.DependencyInjection; namespace MatrixContentFilter.Commands; diff --git a/MatrixContentFilter/Commands/RedactCommand.cs b/MatrixContentFilter/Commands/RedactCommand.cs
index 6b2f8b6..9d45f18 100644 --- a/MatrixContentFilter/Commands/RedactCommand.cs +++ b/MatrixContentFilter/Commands/RedactCommand.cs
@@ -1,14 +1,11 @@ using ArcaneLibs.Extensions; using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State; +using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.Filters; using LibMatrix.Helpers; -using LibMatrix.Homeservers; using LibMatrix.Utilities.Bot.Interfaces; -using MatrixContentFilter.Abstractions; using MatrixContentFilter.Services; using MatrixContentFilter.Services.AsyncActionQueues; -using Microsoft.Extensions.Logging; namespace MatrixContentFilter.Commands; @@ -54,11 +51,11 @@ public class RedactCommand( Key = "\u23f3" //hour glass emoji } }); - - await foreach (var resp in ctx.Room.GetManyMessagesAsync(limit: count, chunkSize: Math.Min(count, 250) - ,filter: new SyncFilter.RoomFilter.StateFilter(types: [RoomMemberEventContent.EventId, RoomMessageEventContent.EventId], senders: [mxid]) + + await foreach (var resp in ctx.Room.GetManyMessagesAsync(limit: count, chunkSize: Math.Min(count, 250), + filter: new SyncFilter.RoomFilter.StateFilter(types: [RoomMemberEventContent.EventId, RoomMessageEventContent.EventId], senders: [mxid]) .ToJson(indent: false, ignoreNull: true).UrlEncode()) - ) { + ) { foreach (var msg in resp.Chunk) { if (msg.Sender != mxid) continue; if (msg is not { Type: RoomMemberEventContent.EventId or RoomMessageEventContent.EventId }) continue; @@ -85,7 +82,7 @@ public class RedactCommand( .Build()); }); } - + await ctx.Room.RedactEventAsync(hourglassReaction.EventId); await ctx.Room.SendTimelineEventAsync("m.reaction", new RoomMessageReactionEventContent() { RelatesTo = new() {