about summary refs log tree commit diff
path: root/MiniUtils/Commands/KickACLedCommand.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-11-10 03:05:18 +0100
committerRory& <root@rory.gay>2025-11-10 03:05:18 +0100
commitd272c5b6f50e3f66a430fe4a6bbb3188f9adb1c8 (patch)
tree328d96076fe8c6fe6081f9083fcb3a498c41a683 /MiniUtils/Commands/KickACLedCommand.cs
parentMore stuff (diff)
downloadMiniUtils-master.tar.xz
MiniUtils changes, add uick ban sync hack bot HEAD master
Diffstat (limited to 'MiniUtils/Commands/KickACLedCommand.cs')
-rw-r--r--MiniUtils/Commands/KickACLedCommand.cs73
1 files changed, 73 insertions, 0 deletions
diff --git a/MiniUtils/Commands/KickACLedCommand.cs b/MiniUtils/Commands/KickACLedCommand.cs
new file mode 100644

index 0000000..9ea8ec0 --- /dev/null +++ b/MiniUtils/Commands/KickACLedCommand.cs
@@ -0,0 +1,73 @@ +using System.Collections.Frozen; +using ArcaneLibs.Extensions; +using LibMatrix; +using LibMatrix.EventTypes.Spec.State.RoomInfo; +using LibMatrix.Filters; +using LibMatrix.Helpers; +using LibMatrix.RoomTypes; +using LibMatrix.Utilities.Bot.Interfaces; +using MiniUtils.Classes; +using MiniUtils.Services; + +namespace MiniUtils.Commands; + +public class KickACLedCommand(IgnoreListManager ignoreListManager) : ICommand { + public string Name => "kick acled users"; + + public string[]? Aliases => []; + + public string Description => "Kick all users targetted by server ACLs"; + + public bool Unlisted => false; + + public async Task Invoke(CommandContext ctx) { + if (ctx.Args is ["banned"]) + await RedactUsers(ctx, await ctx.Room.GetMemberIdsListAsync("ban")); + else if (ctx.Args is [.. var senders]) { + var sendersSet = senders.ToFrozenSet(); + await RedactUsers(ctx, sendersSet); + } + } + + private async Task RedactUsers(CommandContext ctx, FrozenSet<string> senders) { + var count = 0; + var subCount = 0; + List<Task> tasks = []; + // await foreach (var resp in ctx.Room.GetManyMessagesAsync(filter: filter.ToJson(false, ignoreNull: true), chunkSize: 1000)) { + // foreach (var chunk in resp.Chunk.Chunk(49)) { + // foreach (var evt in chunk) { + // if (!senders.Contains(evt.Sender!)) continue; + // tasks.Add(RedactEvent(ctx.Room, evt.EventId!)); + // count++; + // subCount++; + // } + // + // if (subCount >= 40) { + // await ctx.Room.SendMessageEventAsync(new MessageBuilder() + // .WithBody( + // $"[{Emojis.Hourglass}] {Emojis.Recycle} {count} ({Emojis.Checkmark} {tasks.Count(t => t.IsCompletedSuccessfully)} {Emojis.Prohibited} {tasks.Count(t => t.IsFaulted)} {Emojis.Hourglass} {tasks.Count(t => t.Status == TaskStatus.Running)})") + // .Build()); + // // await Task.WhenAll(tasks); + // subCount = 0; + // } + // } + // } + + var acls = await ctx.Room.GetStateOrNullAsync<RoomServerAclEventContent>(RoomServerAclEventContent.EventId); + if (acls == null) { + await ctx.Room.SendMessageEventAsync(new MessageBuilder().WithBody("No ACLs found.").Build()); + return; + } + + await foreach (var resp in ctx.Room.GetMembersEnumerableAsync()) { + var serverName = resp.StateKey!.Split(':',2)[1]; + if (acls.DenyRegexes?.Any(x => x.IsMatch(serverName)) ?? false) { + Console.WriteLine("Kicking {0} from {1} due to ACL match: {2}", resp.StateKey, ctx.Room.RoomId, acls.DenyRegexes.First(x => x.IsMatch(serverName))); + } + } + + await Task.WhenAll(tasks); + + await ctx.Room.SendMessageEventAsync(new MessageBuilder().WithBody($"{Emojis.Recycle} {count}").Build()); + } +} \ No newline at end of file