blob: 9ea8ec0c109c9a2b58b05ef3d63c59983e2bbb2c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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());
}
}
|