about summary refs log tree commit diff
path: root/ExampleBots/ModerationBot/Commands
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2023-11-23 05:42:33 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2023-11-23 05:42:33 +0100
commit3e934eee892f69a8f78b94950993000522702769 (patch)
tree6aa0d3d26c9a07a7a3e097fe28abb785400bfbd6 /ExampleBots/ModerationBot/Commands
parentAdd license retroactively, matching where the code originated from (MatrixRoo... (diff)
downloadLibMatrix-3e934eee892f69a8f78b94950993000522702769.tar.xz
Moderation bot work
Diffstat (limited to '')
-rw-r--r--ExampleBots/ModerationBot/Commands/BanMediaCommand.cs (renamed from ExampleBots/MediaModeratorPoC/Commands/BanMediaCommand.cs)28
-rw-r--r--ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs63
-rw-r--r--ExampleBots/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs43
-rw-r--r--ExampleBots/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs73
-rw-r--r--ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs54
-rw-r--r--ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs75
6 files changed, 323 insertions, 13 deletions
diff --git a/ExampleBots/MediaModeratorPoC/Commands/BanMediaCommand.cs b/ExampleBots/ModerationBot/Commands/BanMediaCommand.cs
index 5dfa706..21e0a94 100644
--- a/ExampleBots/MediaModeratorPoC/Commands/BanMediaCommand.cs
+++ b/ExampleBots/ModerationBot/Commands/BanMediaCommand.cs
@@ -6,18 +6,19 @@ using LibMatrix.EventTypes.Spec;
 using LibMatrix.Helpers;
 using LibMatrix.Services;
 using LibMatrix.Utilities.Bot.Interfaces;
-using MediaModeratorPoC.AccountData;
-using MediaModeratorPoC.StateEventTypes;
+using ModerationBot.AccountData;
+using ModerationBot.StateEventTypes;
+using ModerationBot.StateEventTypes.Policies.Implementations;
 
-namespace MediaModeratorPoC.Commands;
+namespace ModerationBot.Commands;
 
-public class BanMediaCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver) : ICommand {
+public class BanMediaCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand {
     public string Name { get; } = "banmedia";
     public string Description { get; } = "Create a policy banning a piece of media, must be used in reply to a message";
 
     public async Task<bool> CanInvoke(CommandContext ctx) {
         //check if user is admin in control room
-        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.modbot_data");
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
         var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom);
         var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasPermission(ctx.MessageEvent.Sender, "m.room.ban");
         if (!isAdmin) {
@@ -30,8 +31,9 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic
     }
 
     public async Task Invoke(CommandContext ctx) {
-        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.modbot_data");
-        var policyRoom = ctx.Homeserver.GetRoom(botData.PolicyRoom ?? botData.ControlRoom);
+        
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var policyRoom = ctx.Homeserver.GetRoom(botData.DefaultPolicyRoom ?? botData.ControlRoom);
         var logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom);
 
         //check if reply
@@ -69,7 +71,7 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic
                 byte[]? fileHash = null;
 
                 try {
-                    fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver._httpClient.GetStreamAsync(resolvedUri));
+                    fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver.ClientHttpClient.GetStreamAsync(resolvedUri));
                 }
                 catch (Exception ex) {
                     await logRoom.SendMessageEventAsync(
@@ -77,7 +79,7 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic
                             ex));
                     try {
                         resolvedUri = await hsResolver.ResolveMediaUri(ctx.Homeserver.BaseUrl, mxcUri);
-                        fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver._httpClient.GetStreamAsync(resolvedUri));
+                        fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver.ClientHttpClient.GetStreamAsync(resolvedUri));
                     }
                     catch (Exception ex2) {
                         await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatException("Error calculating file hash", ex2));
@@ -86,10 +88,10 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic
                     }
                 }
 
-                MediaPolicyEventContent policy;
-                await policyRoom.SendStateEventAsync("gay.rory.media_moderator_poc.rule.media", Guid.NewGuid().ToString(), policy = new MediaPolicyEventContent {
-                    // Entity = uriHash,
-                    FileHash = fileHash,
+                MediaPolicyFile policy;
+                await policyRoom.SendStateEventAsync("gay.rory.moderation.rule.media", Guid.NewGuid().ToString(), policy = new MediaPolicyFile {
+                    Entity = Convert.ToBase64String(uriHash),
+                    FileHash = Convert.ToBase64String(fileHash),
                     Reason = string.Join(' ', ctx.Args[1..]),
                     Recommendation = recommendation,
                 });
diff --git a/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs b/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs
new file mode 100644
index 0000000..09d3caf
--- /dev/null
+++ b/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs
@@ -0,0 +1,63 @@
+using System.Buffers.Text;
+using System.Security.Cryptography;
+using ArcaneLibs.Extensions;
+using LibMatrix;
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.Helpers;
+using LibMatrix.RoomTypes;
+using LibMatrix.Services;
+using LibMatrix.Utilities.Bot.Interfaces;
+using ModerationBot.AccountData;
+using ModerationBot.StateEventTypes;
+
+namespace ModerationBot.Commands;
+
+public class DbgAllRoomsArePolicyListsCommand
+    (IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand {
+    public string Name { get; } = "dbg-allroomsarepolicy";
+    public string Description { get; } = "[Debug] mark all rooms as trusted policy rooms";
+    private GenericRoom logRoom { get; set; }
+
+    public async Task<bool> CanInvoke(CommandContext ctx) {
+#if !DEBUG
+        return false;
+#endif
+
+        //check if user is admin in control room
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom);
+        var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasPermission(ctx.MessageEvent.Sender, "m.room.ban");
+        if (!isAdmin) {
+            // await ctx.Reply("You do not have permission to use this command!");
+            await ctx.Homeserver.GetRoom(botData.LogRoom!).SendMessageEventAsync(
+                new RoomMessageEventContent(body: $"User {ctx.MessageEvent.Sender} tried to use command {Name} but does not have permission!", messageType: "m.text"));
+        }
+
+        return isAdmin;
+    }
+
+    public async Task Invoke(CommandContext ctx) {
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom);
+        
+        var joinedRooms = await ctx.Homeserver.GetJoinedRooms();
+        
+        await ctx.Homeserver.SetAccountDataAsync("gay.rory.moderation_bot.policy_lists", joinedRooms.ToDictionary(x => x.RoomId, x => new PolicyList() {
+            Trusted = true
+        }));
+
+        await engine.ReloadActivePolicyLists();
+    }
+
+    private async Task<bool> JoinRoom(GenericRoom memberRoom, string reason, List<string> servers) {
+        try {
+            await memberRoom.JoinAsync(servers.ToArray(), reason);
+            await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joined room {memberRoom.RoomId}"));
+        }
+        catch (Exception e) {
+            await logRoom.SendMessageEventAsync(MessageFormatter.FormatException($"Failed to join {memberRoom.RoomId}", e));
+        }
+
+        return true;
+    }
+}
\ No newline at end of file
diff --git a/ExampleBots/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs b/ExampleBots/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs
new file mode 100644
index 0000000..395c87c
--- /dev/null
+++ b/ExampleBots/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs
@@ -0,0 +1,43 @@
+using System.Buffers.Text;
+using System.Security.Cryptography;
+using ArcaneLibs.Extensions;
+using LibMatrix;
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.Helpers;
+using LibMatrix.RoomTypes;
+using LibMatrix.Services;
+using LibMatrix.Utilities.Bot.Interfaces;
+using ModerationBot.AccountData;
+using ModerationBot.StateEventTypes;
+
+namespace ModerationBot.Commands;
+
+public class DbgDumpActivePoliciesCommand
+    (IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand {
+    public string Name { get; } = "dbg-dumppolicies";
+    public string Description { get; } = "[Debug] Dump all active policies";
+    private GenericRoom logRoom { get; set; }
+
+    public async Task<bool> CanInvoke(CommandContext ctx) {
+#if !DEBUG
+        return false;
+#endif
+
+        //check if user is admin in control room
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom);
+        var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasPermission(ctx.MessageEvent.Sender, "m.room.ban");
+        if (!isAdmin) {
+            // await ctx.Reply("You do not have permission to use this command!");
+            await ctx.Homeserver.GetRoom(botData.LogRoom!).SendMessageEventAsync(
+                new RoomMessageEventContent(body: $"User {ctx.MessageEvent.Sender} tried to use command {Name} but does not have permission!", messageType: "m.text"));
+        }
+
+        return isAdmin;
+    }
+
+    public async Task Invoke(CommandContext ctx) {
+        await ctx.Room.SendFileAsync("all.json", new MemoryStream(engine.ActivePolicies.ToJson().AsBytes().ToArray()), contentType: "application/json");
+        await ctx.Room.SendFileAsync("by-type.json", new MemoryStream(engine.ActivePoliciesByType.ToJson().AsBytes().ToArray()), contentType: "application/json");
+    }
+}
\ No newline at end of file
diff --git a/ExampleBots/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs b/ExampleBots/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs
new file mode 100644
index 0000000..e9a645e
--- /dev/null
+++ b/ExampleBots/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs
@@ -0,0 +1,73 @@
+using System.Buffers.Text;
+using System.Security.Cryptography;
+using ArcaneLibs.Extensions;
+using LibMatrix;
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.Helpers;
+using LibMatrix.RoomTypes;
+using LibMatrix.Services;
+using LibMatrix.Utilities.Bot.Interfaces;
+using ModerationBot.AccountData;
+using ModerationBot.StateEventTypes;
+
+namespace ModerationBot.Commands;
+
+public class DbgDumpAllStateTypesCommand
+    (IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand {
+    public string Name { get; } = "dbg-dumpstatetypes";
+    public string Description { get; } = "[Debug] Dump all state types we can find";
+    private GenericRoom logRoom { get; set; }
+
+    public async Task<bool> CanInvoke(CommandContext ctx) {
+#if !DEBUG
+        return false;
+#endif
+
+        //check if user is admin in control room
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom);
+        var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasPermission(ctx.MessageEvent.Sender, "m.room.ban");
+        if (!isAdmin) {
+            // await ctx.Reply("You do not have permission to use this command!");
+            await ctx.Homeserver.GetRoom(botData.LogRoom!).SendMessageEventAsync(
+                new RoomMessageEventContent(body: $"User {ctx.MessageEvent.Sender} tried to use command {Name} but does not have permission!", messageType: "m.text"));
+        }
+
+        return isAdmin;
+    }
+
+    public async Task Invoke(CommandContext ctx) {
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom);
+
+
+        var joinedRooms = await ctx.Homeserver.GetJoinedRooms();
+
+        var tasks = joinedRooms.Select(GetStateTypes).ToAsyncEnumerable();
+        await foreach (var (room, (raw, html)) in tasks) {
+            await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent("m.text") {
+                Body = $"States for {room.RoomId}:\n{raw}",
+                FormattedBody = $"States for {room.RoomId}:\n{html}",
+                Format = "org.matrix.custom.html"
+            });
+        }
+    }
+
+    private async Task<(GenericRoom room, (string raw, string html))> GetStateTypes(GenericRoom memberRoom) {
+        var states = await memberRoom.GetFullStateAsListAsync();
+
+        return (memberRoom, SummariseStateTypeCounts(states));
+    }
+    
+    private static (string Raw, string Html) SummariseStateTypeCounts(IList<StateEventResponse> states) {
+        string raw = "Count | State type | Mapped type", html = "<table><tr><th>Count</th><th>State type</th><th>Mapped type</th></tr>";
+        var groupedStates = states.GroupBy(x => x.Type).ToDictionary(x => x.Key, x => x.ToList()).OrderByDescending(x => x.Value.Count);
+        foreach (var (type, stateGroup) in groupedStates) {
+            raw += $"{stateGroup.Count} | {type} | {stateGroup[0].GetType.Name}";
+            html += $"<tr><td>{stateGroup.Count}</td><td>{type}</td><td>{stateGroup[0].GetType.Name}</td></tr>";
+        }
+
+        html += "</table>";
+        return (raw, html);
+    }
+}
\ No newline at end of file
diff --git a/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs b/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs
new file mode 100644
index 0000000..19a2c54
--- /dev/null
+++ b/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs
@@ -0,0 +1,54 @@
+using System.Buffers.Text;
+using System.Security.Cryptography;
+using ArcaneLibs.Extensions;
+using LibMatrix;
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.Helpers;
+using LibMatrix.Services;
+using LibMatrix.Utilities.Bot.Interfaces;
+using ModerationBot.AccountData;
+using ModerationBot.StateEventTypes;
+
+namespace ModerationBot.Commands;
+
+public class JoinRoomCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand {
+    public string Name { get; } = "join";
+    public string Description { get; } = "Join arbitrary rooms";
+
+    public async Task<bool> CanInvoke(CommandContext ctx) {
+        //check if user is admin in control room
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom);
+        var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasPermission(ctx.MessageEvent.Sender, "m.room.ban");
+        if (!isAdmin) {
+            // await ctx.Reply("You do not have permission to use this command!");
+            await ctx.Homeserver.GetRoom(botData.LogRoom!).SendMessageEventAsync(
+                new RoomMessageEventContent(body: $"User {ctx.MessageEvent.Sender} tried to use command {Name} but does not have permission!", messageType: "m.text"));
+        }
+
+        return isAdmin;
+    }
+
+    public async Task Invoke(CommandContext ctx) {
+        
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var policyRoom = ctx.Homeserver.GetRoom(botData.DefaultPolicyRoom ?? botData.ControlRoom);
+        var logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom);
+
+        await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joining room {ctx.Args[0]} with reason: {string.Join(' ', ctx.Args[1..])}"));
+        var roomId = ctx.Args[0];
+        var servers = new List<string>() {ctx.Homeserver.ServerName};
+        if (roomId.StartsWith('[')) {
+            
+        }
+
+        if (roomId.StartsWith('#')) {
+            var res = await ctx.Homeserver.ResolveRoomAliasAsync(roomId);
+            roomId = res.RoomId;
+            servers.AddRange(servers);
+        }
+
+        await ctx.Homeserver.JoinRoomAsync(roomId, servers, string.Join(' ', ctx.Args[1..]));
+        await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Resolved room {ctx.Args[0]} to {roomId} with servers: {string.Join(", ", servers)}"));
+    }
+}
diff --git a/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs b/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs
new file mode 100644
index 0000000..c3b7d12
--- /dev/null
+++ b/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs
@@ -0,0 +1,75 @@
+using System.Buffers.Text;
+using System.Security.Cryptography;
+using ArcaneLibs.Extensions;
+using LibMatrix;
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.Helpers;
+using LibMatrix.RoomTypes;
+using LibMatrix.Services;
+using LibMatrix.Utilities.Bot.Interfaces;
+using ModerationBot.AccountData;
+using ModerationBot.StateEventTypes;
+
+namespace ModerationBot.Commands;
+
+public class JoinSpaceMembersCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand {
+    public string Name { get; } = "joinspacemembers";
+    public string Description { get; } = "Join all rooms in space";
+    private GenericRoom logRoom { get; set; }
+
+    public async Task<bool> CanInvoke(CommandContext ctx) {
+        //check if user is admin in control room
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom);
+        var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasPermission(ctx.MessageEvent.Sender, "m.room.ban");
+        if (!isAdmin) {
+            // await ctx.Reply("You do not have permission to use this command!");
+            await ctx.Homeserver.GetRoom(botData.LogRoom!).SendMessageEventAsync(
+                new RoomMessageEventContent(body: $"User {ctx.MessageEvent.Sender} tried to use command {Name} but does not have permission!", messageType: "m.text"));
+        }
+
+        return isAdmin;
+    }
+
+    public async Task Invoke(CommandContext ctx) {
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom);
+
+        await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joining space children of {ctx.Args[0]} with reason: {string.Join(' ', ctx.Args[1..])}"));
+        var roomId = ctx.Args[0];
+        var servers = new List<string>() {ctx.Homeserver.ServerName};
+        if (roomId.StartsWith('[')) {
+            
+        }
+
+        if (roomId.StartsWith('#')) {
+            var res = await ctx.Homeserver.ResolveRoomAliasAsync(roomId);
+            roomId = res.RoomId;
+            servers.AddRange(servers);
+        }
+
+        var room = ctx.Homeserver.GetRoom(roomId);
+        var tasks = new List<Task<bool>>();
+        await foreach (var memberRoom in room.AsSpace.GetChildrenAsync()) {
+            servers.Add(room.RoomId.Split(':', 2)[1]);
+            servers = servers.Distinct().ToList();
+            tasks.Add(JoinRoom(memberRoom, string.Join(' ', ctx.Args[1..]), servers));
+        }
+
+        await foreach (var b in tasks.ToAsyncEnumerable()) {
+            await Task.Delay(50);
+        }
+    }
+
+    private async Task<bool> JoinRoom(GenericRoom memberRoom, string reason, List<string> servers) {
+        try {
+            await memberRoom.JoinAsync(servers.ToArray(), reason);
+            await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joined room {memberRoom.RoomId}"));
+        }
+        catch (Exception e) {
+            await logRoom.SendMessageEventAsync(MessageFormatter.FormatException($"Failed to join {memberRoom.RoomId}", e));
+        }
+
+        return true;
+    }
+}