From af6d1c7e11b2e9b4108ae8b693650b2a18cd2001 Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 30 Oct 2025 02:49:12 +0100 Subject: Old work --- .gitmodules | 3 + .idea/.idea.ModerationBot.dir/.idea/.gitignore | 13 + .idea/.idea.ModerationBot.dir/.idea/encodings.xml | 4 + .../.idea.ModerationBot.dir/.idea/indexLayout.xml | 8 + .idea/.idea.ModerationBot.dir/.idea/vcs.xml | 6 + .idea/.idea.ModerationBot/.idea/.gitignore | 13 + .idea/.idea.ModerationBot/.idea/encodings.xml | 4 + .idea/.idea.ModerationBot/.idea/indexLayout.xml | 8 + .idea/.idea.ModerationBot/.idea/vcs.xml | 6 + AccountData/BotData.cs | 18 -- Commands/BanMediaCommand.cs | 112 -------- Commands/DbgAllRoomsArePolicyListsCommand.cs | 60 ---- Commands/DbgAniRainbowTest.cs | 50 ---- Commands/DbgDumpActivePoliciesCommand.cs | 39 --- Commands/DbgDumpAllStateTypesCommand.cs | 69 ----- Commands/JoinRoomCommand.cs | 48 ---- Commands/JoinSpaceMembersCommand.cs | 73 ----- Commands/ReloadPoliciesCommand.cs | 38 --- FirstRunTasks.cs | 83 ------ LibMatrix | 1 + ModerationBot.cs | 302 --------------------- ModerationBot.csproj | 32 --- ModerationBot.sln | 161 +++++++++++ ModerationBot/AccountData/BotData.cs | 18 ++ ModerationBot/Commands/BanMediaCommand.cs | 112 ++++++++ .../Commands/DbgAllRoomsArePolicyListsCommand.cs | 60 ++++ ModerationBot/Commands/DbgAniRainbowTest.cs | 50 ++++ .../Commands/DbgDumpActivePoliciesCommand.cs | 39 +++ .../Commands/DbgDumpAllStateTypesCommand.cs | 69 +++++ ModerationBot/Commands/JoinRoomCommand.cs | 48 ++++ ModerationBot/Commands/JoinSpaceMembersCommand.cs | 73 +++++ ModerationBot/Commands/ReloadPoliciesCommand.cs | 38 +++ ModerationBot/FirstRunTasks.cs | 83 ++++++ ModerationBot/ModerationBot.cs | 302 +++++++++++++++++++++ ModerationBot/ModerationBot.csproj | 32 +++ ModerationBot/ModerationBotConfiguration.cs | 10 + ModerationBot/PolicyEngine.cs | 268 ++++++++++++++++++ ModerationBot/PolicyList.cs | 16 ++ ModerationBot/Program.cs | 42 +++ ModerationBot/Properties/launchSettings.json | 26 ++ .../Services/ModerationBotRoomProvider.cs | 76 ++++++ .../StateEventTypes/Policies/BasePolicy.cs | 53 ++++ .../Policies/Implementations/MediaPolicyFile.cs | 16 ++ .../Implementations/MediaPolicyHomeserver.cs | 10 + .../Implementations/MessagePolicyContainsText.cs | 10 + .../Policies/Implementations/UnknownPolicy.cs | 7 + ModerationBot/appsettings.Development.json | 24 ++ ModerationBot/appsettings.json | 9 + ModerationBotConfiguration.cs | 10 - PolicyEngine.cs | 268 ------------------ PolicyList.cs | 16 -- Program.cs | 42 --- Properties/launchSettings.json | 26 -- Services/ModerationBotRoomProvider.cs | 76 ------ StateEventTypes/Policies/BasePolicy.cs | 53 ---- .../Policies/Implementations/MediaPolicyFile.cs | 16 -- .../Implementations/MediaPolicyHomeserver.cs | 10 - .../Implementations/MessagePolicyContainsText.cs | 10 - .../Policies/Implementations/UnknownPolicy.cs | 7 - appsettings.Development.json | 24 -- appsettings.json | 9 - 61 files changed, 1718 insertions(+), 1491 deletions(-) create mode 100644 .gitmodules create mode 100644 .idea/.idea.ModerationBot.dir/.idea/.gitignore create mode 100644 .idea/.idea.ModerationBot.dir/.idea/encodings.xml create mode 100644 .idea/.idea.ModerationBot.dir/.idea/indexLayout.xml create mode 100644 .idea/.idea.ModerationBot.dir/.idea/vcs.xml create mode 100644 .idea/.idea.ModerationBot/.idea/.gitignore create mode 100644 .idea/.idea.ModerationBot/.idea/encodings.xml create mode 100644 .idea/.idea.ModerationBot/.idea/indexLayout.xml create mode 100644 .idea/.idea.ModerationBot/.idea/vcs.xml delete mode 100644 AccountData/BotData.cs delete mode 100644 Commands/BanMediaCommand.cs delete mode 100644 Commands/DbgAllRoomsArePolicyListsCommand.cs delete mode 100644 Commands/DbgAniRainbowTest.cs delete mode 100644 Commands/DbgDumpActivePoliciesCommand.cs delete mode 100644 Commands/DbgDumpAllStateTypesCommand.cs delete mode 100644 Commands/JoinRoomCommand.cs delete mode 100644 Commands/JoinSpaceMembersCommand.cs delete mode 100644 Commands/ReloadPoliciesCommand.cs delete mode 100644 FirstRunTasks.cs create mode 160000 LibMatrix delete mode 100644 ModerationBot.cs delete mode 100644 ModerationBot.csproj create mode 100644 ModerationBot.sln create mode 100644 ModerationBot/AccountData/BotData.cs create mode 100644 ModerationBot/Commands/BanMediaCommand.cs create mode 100644 ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs create mode 100644 ModerationBot/Commands/DbgAniRainbowTest.cs create mode 100644 ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs create mode 100644 ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs create mode 100644 ModerationBot/Commands/JoinRoomCommand.cs create mode 100644 ModerationBot/Commands/JoinSpaceMembersCommand.cs create mode 100644 ModerationBot/Commands/ReloadPoliciesCommand.cs create mode 100644 ModerationBot/FirstRunTasks.cs create mode 100644 ModerationBot/ModerationBot.cs create mode 100644 ModerationBot/ModerationBot.csproj create mode 100644 ModerationBot/ModerationBotConfiguration.cs create mode 100644 ModerationBot/PolicyEngine.cs create mode 100644 ModerationBot/PolicyList.cs create mode 100644 ModerationBot/Program.cs create mode 100644 ModerationBot/Properties/launchSettings.json create mode 100644 ModerationBot/Services/ModerationBotRoomProvider.cs create mode 100644 ModerationBot/StateEventTypes/Policies/BasePolicy.cs create mode 100644 ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyFile.cs create mode 100644 ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs create mode 100644 ModerationBot/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs create mode 100644 ModerationBot/StateEventTypes/Policies/Implementations/UnknownPolicy.cs create mode 100644 ModerationBot/appsettings.Development.json create mode 100644 ModerationBot/appsettings.json delete mode 100644 ModerationBotConfiguration.cs delete mode 100644 PolicyEngine.cs delete mode 100644 PolicyList.cs delete mode 100644 Program.cs delete mode 100644 Properties/launchSettings.json delete mode 100644 Services/ModerationBotRoomProvider.cs delete mode 100644 StateEventTypes/Policies/BasePolicy.cs delete mode 100644 StateEventTypes/Policies/Implementations/MediaPolicyFile.cs delete mode 100644 StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs delete mode 100644 StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs delete mode 100644 StateEventTypes/Policies/Implementations/UnknownPolicy.cs delete mode 100644 appsettings.Development.json delete mode 100644 appsettings.json diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1b22344 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "LibMatrix"] + path = LibMatrix + url = rory.gay:git/matrix/LibMatrix.git diff --git a/.idea/.idea.ModerationBot.dir/.idea/.gitignore b/.idea/.idea.ModerationBot.dir/.idea/.gitignore new file mode 100644 index 0000000..7d79e1c --- /dev/null +++ b/.idea/.idea.ModerationBot.dir/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/contentModel.xml +/projectSettingsUpdater.xml +/.idea.ModerationBot.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.ModerationBot.dir/.idea/encodings.xml b/.idea/.idea.ModerationBot.dir/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.ModerationBot.dir/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.ModerationBot.dir/.idea/indexLayout.xml b/.idea/.idea.ModerationBot.dir/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.ModerationBot.dir/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.ModerationBot.dir/.idea/vcs.xml b/.idea/.idea.ModerationBot.dir/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/.idea.ModerationBot.dir/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/.idea.ModerationBot/.idea/.gitignore b/.idea/.idea.ModerationBot/.idea/.gitignore new file mode 100644 index 0000000..40a1d1e --- /dev/null +++ b/.idea/.idea.ModerationBot/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/.idea.ModerationBot.iml +/projectSettingsUpdater.xml +/modules.xml +/contentModel.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.ModerationBot/.idea/encodings.xml b/.idea/.idea.ModerationBot/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.ModerationBot/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.ModerationBot/.idea/indexLayout.xml b/.idea/.idea.ModerationBot/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.ModerationBot/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.ModerationBot/.idea/vcs.xml b/.idea/.idea.ModerationBot/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/.idea.ModerationBot/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AccountData/BotData.cs b/AccountData/BotData.cs deleted file mode 100644 index de52679..0000000 --- a/AccountData/BotData.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Text.Json.Serialization; -using LibMatrix.EventTypes; - -namespace ModerationBot.AccountData; - -[MatrixEvent(EventName = EventId)] -public class BotData { - public const string EventId = "gay.rory.moderation_bot_data"; - - [JsonPropertyName("control_room")] - public string? ControlRoom { get; set; } = ""; - - [JsonPropertyName("log_room")] - public string? LogRoom { get; set; } = ""; - - [JsonPropertyName("default_policy_room")] - public string? DefaultPolicyRoom { get; set; } -} \ No newline at end of file diff --git a/Commands/BanMediaCommand.cs b/Commands/BanMediaCommand.cs deleted file mode 100644 index 07c9858..0000000 --- a/Commands/BanMediaCommand.cs +++ /dev/null @@ -1,112 +0,0 @@ -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.Services; -using ModerationBot.StateEventTypes.Policies.Implementations; - -namespace ModerationBot.Commands; - -public class BanMediaCommand(HomeserverResolverService hsResolver, PolicyEngine engine, ModerationBotRoomProvider roomProvider) : ICommand { - public string Name { get; } = "banmedia"; - public string[]? Aliases { get; } - public string Description { get; } = "Create a policy banning a piece of media, must be used in reply to a message"; - public bool Unlisted { get; } - - public async Task CanInvoke(CommandContext ctx) { - //check if user is admin in control room - var controlRoom = await roomProvider.GetControlRoomAsync(); - var logRoom = await roomProvider.GetLogRoomAsync(); - var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(ctx.MessageEvent.Sender, "m.room.ban"); - if (!isAdmin) { - // await ctx.Reply("You do not have permission to use this command!"); - await 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 policyRoom = await roomProvider.GetDefaultPolicyRoomAsync(); - var logRoom = await roomProvider.GetLogRoomAsync(); - - //check if reply - var messageContent = ctx.MessageEvent.TypedContent as RoomMessageEventContent; - if (messageContent?.RelatesTo is { InReplyTo: not null }) { - try { - await logRoom.SendMessageEventAsync( - new RoomMessageEventContent( - body: $"User {MessageFormatter.HtmlFormatMention(ctx.MessageEvent.Sender)} is trying to ban media {messageContent!.RelatesTo!.InReplyTo!.EventId}", - messageType: "m.text")); - - //get replied message - var repliedMessage = await ctx.Room.GetEventAsync(messageContent.RelatesTo!.InReplyTo!.EventId); - - //check if recommendation is in list - if (ctx.Args.Length < 2) { - await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatError("You must specify a recommendation type and reason!")); - return; - } - - var recommendation = ctx.Args[0]; - - if (recommendation is not ("ban" or "kick" or "mute" or "redact" or "spoiler" or "warn" or "warn_admins")) { - await ctx.Room.SendMessageEventAsync( - MessageFormatter.FormatError( - $"Invalid recommendation type {recommendation}, must be `warn_admins`, `warn`, `spoiler`, `redact`, `mute`, `kick` or `ban`!")); - return; - } - - //hash file - var mxcUri = (repliedMessage.TypedContent as RoomMessageEventContent).Url!; - var resolvedUri = await hsResolver.ResolveMediaUri(mxcUri.Split('/')[2], mxcUri); - var hashAlgo = SHA3_256.Create(); - var uriHash = hashAlgo.ComputeHash(mxcUri.AsBytes().ToArray()); - byte[]? fileHash = null; - - try { - fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver.ClientHttpClient.GetStreamAsync(resolvedUri)); - } - catch (Exception ex) { - await logRoom.SendMessageEventAsync( - MessageFormatter.FormatException($"Error calculating file hash for {mxcUri} via {mxcUri.Split('/')[2]}, retrying via {ctx.Homeserver.BaseUrl}...", - ex)); - try { - resolvedUri = await hsResolver.ResolveMediaUri(ctx.Homeserver.BaseUrl, mxcUri); - fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver.ClientHttpClient.GetStreamAsync(resolvedUri)); - } - catch (Exception ex2) { - await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatException("Error calculating file hash", ex2)); - await logRoom.SendMessageEventAsync( - MessageFormatter.FormatException($"Error calculating file hash via {ctx.Homeserver.BaseUrl}!", ex2)); - } - } - - 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, - }); - - await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatSuccessJson("Media policy created", policy)); - await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccessJson("Media policy created", policy)); - } - catch (Exception e) { - await logRoom.SendMessageEventAsync(MessageFormatter.FormatException("Error creating policy", e)); - await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatException("Error creating policy", e)); - await using var stream = new MemoryStream(e.ToString().AsBytes().ToArray()); - await logRoom.SendFileAsync("error.log.cs", stream); - } - } - else { - await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatError("This command must be used in reply to a message!")); - } - } -} diff --git a/Commands/DbgAllRoomsArePolicyListsCommand.cs b/Commands/DbgAllRoomsArePolicyListsCommand.cs deleted file mode 100644 index 78e1979..0000000 --- a/Commands/DbgAllRoomsArePolicyListsCommand.cs +++ /dev/null @@ -1,60 +0,0 @@ -using LibMatrix.EventTypes.Spec; -using LibMatrix.Helpers; -using LibMatrix.RoomTypes; -using LibMatrix.Services; -using LibMatrix.Utilities.Bot.Interfaces; -using ModerationBot.AccountData; - -namespace ModerationBot.Commands; - -public class DbgAllRoomsArePolicyListsCommand - (IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { - public string Name { get; } = "dbg-allroomsarepolicy"; - public string[]? Aliases { get; } - public string Description { get; } = "[Debug] mark all rooms as trusted policy rooms"; - public bool Unlisted { get; } - private GenericRoom logRoom { get; set; } - - public async Task CanInvoke(CommandContext ctx) { -// #if !DEBUG -// return false; -// #endif - - //check if user is admin in control room - var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); - var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); - var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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("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 JoinRoom(GenericRoom memberRoom, string reason, List 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; - } -} diff --git a/Commands/DbgAniRainbowTest.cs b/Commands/DbgAniRainbowTest.cs deleted file mode 100644 index eed6fa5..0000000 --- a/Commands/DbgAniRainbowTest.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System.Diagnostics; -using LibMatrix.EventTypes.Spec; -using LibMatrix.Helpers; -using LibMatrix.RoomTypes; -using LibMatrix.Services; -using LibMatrix.Utilities.Bot.Interfaces; -using ModerationBot.AccountData; - -namespace ModerationBot.Commands; - -public class DbgAniRainbowTest(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { - public string Name { get; } = "dbg-ani-rainbow"; - public string[]? Aliases { get; } - public string Description { get; } = "[Debug] animated rainbow :)"; - public bool Unlisted { get; } - private GenericRoom logRoom { get; set; } - - public async Task CanInvoke(CommandContext ctx) { - return ctx.Room.RoomId == "!DoHEdFablOLjddKWIp:rory.gay"; - } - - public async Task Invoke(CommandContext ctx) { - //255 long string - // var rainbow = "🟥🟧🟨🟩🟦🟪"; - var rainbow = "M"; - var chars = rainbow; - for (var i = 0; i < 76; i++) { - chars += rainbow[i%rainbow.Length]; - } - - var msg = new MessageBuilder(msgType: "m.notice").WithRainbowString(chars).Build(); - var msgEvent = await ctx.Room.SendMessageEventAsync(msg); - - Task.Run(async () => { - - int i = 0; - while (true) { - msg = new MessageBuilder(msgType: "m.notice").WithRainbowString(chars, offset: i+=5).Build(); - // .SetReplaceRelation(msgEvent.EventId); - // msg.Body = ""; - // msg.FormattedBody = ""; - var sw = Stopwatch.StartNew(); - await ctx.Room.SendMessageEventAsync(msg); - await Task.Delay(sw.Elapsed); - } - - }); - - } -} \ No newline at end of file diff --git a/Commands/DbgDumpActivePoliciesCommand.cs b/Commands/DbgDumpActivePoliciesCommand.cs deleted file mode 100644 index 81e81a0..0000000 --- a/Commands/DbgDumpActivePoliciesCommand.cs +++ /dev/null @@ -1,39 +0,0 @@ -using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec; -using LibMatrix.RoomTypes; -using LibMatrix.Services; -using LibMatrix.Utilities.Bot.Interfaces; -using ModerationBot.AccountData; - -namespace ModerationBot.Commands; - -public class DbgDumpActivePoliciesCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { - public string Name { get; } = "dbg-dumppolicies"; - public string[]? Aliases { get; } - public string Description { get; } = "[Debug] Dump all active policies"; - public bool Unlisted { get; } - private GenericRoom logRoom { get; set; } - - public async Task CanInvoke(CommandContext ctx) { -#if !DEBUG - return false; -#endif - - //check if user is admin in control room - var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); - var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); - var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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/Commands/DbgDumpAllStateTypesCommand.cs b/Commands/DbgDumpAllStateTypesCommand.cs deleted file mode 100644 index ac2036a..0000000 --- a/Commands/DbgDumpAllStateTypesCommand.cs +++ /dev/null @@ -1,69 +0,0 @@ -using ArcaneLibs.Extensions; -using LibMatrix; -using LibMatrix.EventTypes.Spec; -using LibMatrix.RoomTypes; -using LibMatrix.Services; -using LibMatrix.Utilities.Bot.Interfaces; -using ModerationBot.AccountData; - -namespace ModerationBot.Commands; - -public class DbgDumpAllStateTypesCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { - public string Name { get; } = "dbg-dumpstatetypes"; - public string[]? Aliases { get; } - public string Description { get; } = "[Debug] Dump all state types we can find"; - public bool Unlisted { get; } - private GenericRoom logRoom { get; set; } - - public async Task CanInvoke(CommandContext ctx) { -#if !DEBUG - return false; -#endif - - //check if user is admin in control room - var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); - var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); - var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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("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 states) { - string raw = "Count | State type | Mapped type", html = ""; - 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} | {StateEvent.GetStateEventType(stateGroup[0].Type).Name}"; - html += $""; - } - - html += "
CountState typeMapped type
{stateGroup.Count}{type}{StateEvent.GetStateEventType(stateGroup[0].Type).Name}
"; - return (raw, html); - } -} \ No newline at end of file diff --git a/Commands/JoinRoomCommand.cs b/Commands/JoinRoomCommand.cs deleted file mode 100644 index e604a4e..0000000 --- a/Commands/JoinRoomCommand.cs +++ /dev/null @@ -1,48 +0,0 @@ -using LibMatrix.EventTypes.Spec; -using LibMatrix.Helpers; -using LibMatrix.Services; -using LibMatrix.Utilities.Bot.Interfaces; -using ModerationBot.AccountData; - -namespace ModerationBot.Commands; - -public class JoinRoomCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { - public string Name { get; } = "join"; - public string[]? Aliases { get; } - public string Description { get; } = "Join arbitrary rooms"; - public bool Unlisted { get; } - - public async Task CanInvoke(CommandContext ctx) { - //check if user is admin in control room - var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); - var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); - var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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("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() { 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)}")); - } -} \ No newline at end of file diff --git a/Commands/JoinSpaceMembersCommand.cs b/Commands/JoinSpaceMembersCommand.cs deleted file mode 100644 index 86ecf7e..0000000 --- a/Commands/JoinSpaceMembersCommand.cs +++ /dev/null @@ -1,73 +0,0 @@ -using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec; -using LibMatrix.Helpers; -using LibMatrix.RoomTypes; -using LibMatrix.Services; -using LibMatrix.Utilities.Bot.Interfaces; -using ModerationBot.AccountData; - -namespace ModerationBot.Commands; - -public class JoinSpaceMembersCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { - public string Name { get; } = "joinspacemembers"; - public string[]? Aliases { get; } - public string Description { get; } = "Join all rooms in space"; - public bool Unlisted { get; } - private GenericRoom logRoom { get; set; } - - public async Task CanInvoke(CommandContext ctx) { - //check if user is admin in control room - var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); - var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); - var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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("gay.rory.moderation_bot_data"); - logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom); - var currentRooms = (await ctx.Homeserver.GetJoinedRooms()).Select(x => x.RoomId).ToList(); - - 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() { 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>(); - await foreach (var memberRoom in room.AsSpace.GetChildrenAsync()) { - if (currentRooms.Contains(memberRoom.RoomId)) continue; - 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 JoinRoom(GenericRoom memberRoom, string reason, List servers) { - try { - var resp = await memberRoom.JoinAsync(servers.ToArray(), reason, checkIfAlreadyMember: false); - await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joined room {memberRoom.RoomId} (resp={resp.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/Commands/ReloadPoliciesCommand.cs b/Commands/ReloadPoliciesCommand.cs deleted file mode 100644 index 2934185..0000000 --- a/Commands/ReloadPoliciesCommand.cs +++ /dev/null @@ -1,38 +0,0 @@ -using LibMatrix.EventTypes.Spec; -using LibMatrix.Helpers; -using LibMatrix.Services; -using LibMatrix.Utilities.Bot.Interfaces; -using ModerationBot.AccountData; - -namespace ModerationBot.Commands; - -public class ReloadPoliciesCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { - public string Name { get; } = "reloadpolicies"; - public string[]? Aliases { get; } - public string Description { get; } = "Reload policies"; - public bool Unlisted { get; } - - public async Task CanInvoke(CommandContext ctx) { - if (ctx.MessageEvent.Sender == "@cadence:cadence.moe") return true; - //check if user is admin in control room - var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); - var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); - var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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("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($"Reloading policy lists due to manual invocation!!!!")); - await engine.ReloadActivePolicyLists(); - } -} \ No newline at end of file diff --git a/FirstRunTasks.cs b/FirstRunTasks.cs deleted file mode 100644 index 9dece1d..0000000 --- a/FirstRunTasks.cs +++ /dev/null @@ -1,83 +0,0 @@ -using LibMatrix; -using LibMatrix.Homeservers; -using LibMatrix.Responses; -using ModerationBot.AccountData; - -namespace ModerationBot; - -public class FirstRunTasks { - public static async Task ConstructBotData(AuthenticatedHomeserverGeneric hs, ModerationBotConfiguration configuration, BotData? botdata) { - botdata ??= new BotData(); - var creationContent = CreateRoomRequest.CreatePrivate(hs, name: "Rory&::ModerationBot - Control room", roomAliasName: "moderation-bot-control-room"); - creationContent.Invite = configuration.Admins; - creationContent.CreationContent["type"] = "gay.rory.moderation_bot.control_room"; - - if (botdata.ControlRoom is null) - try { - botdata.ControlRoom = (await hs.CreateRoom(creationContent)).RoomId; - } - catch (Exception e) { - if (e is not MatrixException { ErrorCode: "M_ROOM_IN_USE" }) { - Console.WriteLine(e); - throw; - } - - creationContent.RoomAliasName += $"-{Guid.NewGuid()}"; - botdata.ControlRoom = (await hs.CreateRoom(creationContent)).RoomId; - } - //set access rules to allow joining via control room - // creationContent.InitialState.Add(new StateEvent { - // Type = "m.room.join_rules", - // StateKey = "", - // TypedContent = new RoomJoinRulesEventContent { - // JoinRule = "knock_restricted", - // Allow = new() { - // new RoomJoinRulesEventContent.AllowEntry { - // Type = "m.room_membership", - // RoomId = botdata.ControlRoom - // } - // } - // } - // }); - - creationContent.Name = "Rory&::ModerationBot - Log room"; - creationContent.RoomAliasName = "moderation-bot-log-room"; - creationContent.CreationContent["type"] = "gay.rory.moderation_bot.log_room"; - - if (botdata.LogRoom is null) - try { - botdata.LogRoom = (await hs.CreateRoom(creationContent)).RoomId; - } - catch (Exception e) { - if (e is not MatrixException { ErrorCode: "M_ROOM_IN_USE" }) { - Console.WriteLine(e); - throw; - } - - creationContent.RoomAliasName += $"-{Guid.NewGuid()}"; - botdata.LogRoom = (await hs.CreateRoom(creationContent)).RoomId; - } - - creationContent.Name = "Rory&::ModerationBot - Policy room"; - creationContent.RoomAliasName = "moderation-bot-policy-room"; - creationContent.CreationContent["type"] = "gay.rory.moderation_bot.policy_room"; - - if (botdata.DefaultPolicyRoom is null) - try { - botdata.DefaultPolicyRoom = (await hs.CreateRoom(creationContent)).RoomId; - } - catch (Exception e) { - if (e is not MatrixException { ErrorCode: "M_ROOM_IN_USE" }) { - Console.WriteLine(e); - throw; - } - - creationContent.RoomAliasName += $"-{Guid.NewGuid()}"; - botdata.DefaultPolicyRoom = (await hs.CreateRoom(creationContent)).RoomId; - } - - await hs.SetAccountDataAsync("gay.rory.moderation_bot_data", botdata); - - return botdata; - } -} diff --git a/LibMatrix b/LibMatrix new file mode 160000 index 0000000..6e1402b --- /dev/null +++ b/LibMatrix @@ -0,0 +1 @@ +Subproject commit 6e1402baa6ad8517a8a8446832fed1d4b48cee51 diff --git a/ModerationBot.cs b/ModerationBot.cs deleted file mode 100644 index 791d3b5..0000000 --- a/ModerationBot.cs +++ /dev/null @@ -1,302 +0,0 @@ -using ArcaneLibs.Extensions; -using LibMatrix; -using LibMatrix.EventTypes; -using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State; -using LibMatrix.EventTypes.Spec.State.Policy; -using LibMatrix.Helpers; -using LibMatrix.Homeservers; -using LibMatrix.RoomTypes; -using LibMatrix.Services; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using ModerationBot.AccountData; -using ModerationBot.StateEventTypes.Policies; - -namespace ModerationBot; - -public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger logger, ModerationBotConfiguration configuration, PolicyEngine engine) : IHostedService { - private Task _listenerTask; - - // private GenericRoom _policyRoom; - private GenericRoom? _logRoom; - private GenericRoom? _controlRoom; - - /// Triggered when the application host is ready to start the service. - /// Indicates that the start process has been aborted. - public async Task StartAsync(CancellationToken cancellationToken) { - _listenerTask = Run(cancellationToken); - logger.LogInformation("Bot started!"); - } - - private async Task Run(CancellationToken cancellationToken) { - if (Directory.Exists("bot_data/cache")) - Directory.GetFiles("bot_data/cache").ToList().ForEach(File.Delete); - - BotData botData; - - try { - logger.LogInformation("Fetching bot account data..."); - botData = await hs.GetAccountDataAsync("gay.rory.moderation_bot_data"); - logger.LogInformation("Got bot account data..."); - } - catch (Exception e) { - logger.LogInformation("Could not fetch bot account data... {}", e.Message); - if (e is not MatrixException { ErrorCode: "M_NOT_FOUND" }) { - logger.LogError("{}", e.ToString()); - throw; - } - - botData = null; - } - - botData = await FirstRunTasks.ConstructBotData(hs, configuration, botData); - - // _policyRoom = hs.GetRoom(botData.PolicyRoom ?? botData.ControlRoom); - _logRoom = hs.GetRoom(botData.LogRoom ?? botData.ControlRoom); - _controlRoom = hs.GetRoom(botData.ControlRoom); - foreach (var configurationAdmin in configuration.Admins) { - var pls = await _controlRoom.GetPowerLevelsAsync(); - if (pls is null) { - await _logRoom?.SendMessageEventAsync(MessageFormatter.FormatWarning($"Control room has no m.room.power_levels?")); - continue; - } - pls.SetUserPowerLevel(configurationAdmin, pls.GetUserPowerLevel(hs.UserId)); - await _controlRoom.SendStateEventAsync(RoomPowerLevelEventContent.EventId, pls); - } - var syncHelper = new SyncHelper(hs); - - List admins = new(); - -#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed - Task.Run(async () => { - while (!cancellationToken.IsCancellationRequested) { - var controlRoomMembers = _controlRoom.GetMembersEnumerableAsync(); - var pls = await _controlRoom.GetPowerLevelsAsync(); - await foreach (var member in controlRoomMembers) { - if ((member.TypedContent as RoomMemberEventContent)? - .Membership == "join" && pls.UserHasTimelinePermission(member.Sender, RoomMessageEventContent.EventId)) admins.Add(member.StateKey); - } - - await Task.Delay(TimeSpan.FromMinutes(5), cancellationToken); - } - }, cancellationToken); -#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed - - syncHelper.InviteReceivedHandlers.Add(async Task (args) => { - var inviteEvent = - args.Value.InviteState.Events.FirstOrDefault(x => - x.Type == "m.room.member" && x.StateKey == hs.UserId); - logger.LogInformation("Got invite to {RoomId} by {Sender} with reason: {Reason}", args.Key, inviteEvent!.Sender, - (inviteEvent.TypedContent as RoomMemberEventContent)!.Reason); - await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Bot invited to {MessageFormatter.HtmlFormatMention(args.Key)} by {MessageFormatter.HtmlFormatMention(inviteEvent.Sender)}")); - if (admins.Contains(inviteEvent.Sender)) { - try { - await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joining {MessageFormatter.HtmlFormatMention(args.Key)}...")); - var senderProfile = await hs.GetProfileAsync(inviteEvent.Sender); - await hs.GetRoom(args.Key).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!"); - } - catch (Exception e) { - logger.LogError("{}", e.ToString()); - await _logRoom.SendMessageEventAsync(MessageFormatter.FormatException("Could not join room", e)); - await hs.GetRoom(args.Key).LeaveAsync(reason: "I was unable to join the room: " + e); - } - } - }); - - syncHelper.TimelineEventHandlers.Add(async @event => { - var room = hs.GetRoom(@event.RoomId); - try { - logger.LogInformation( - "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: true, ignoreNull: true)); - - if (@event != null && ( - @event.MappedType.IsAssignableTo(typeof(BasePolicy)) - || @event.MappedType.IsAssignableTo(typeof(PolicyRuleEventContent)) - )) { - await LogPolicyChange(@event); - await engine.ReloadActivePolicyListById(@event.RoomId); - } - - var rules = await engine.GetMatchingPolicies(@event); - foreach (var matchedRule in rules) { - await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccessJson( - $"{MessageFormatter.HtmlFormatMessageLink(eventId: @event.EventId, roomId: room.RoomId, displayName: "Event")} matched {MessageFormatter.HtmlFormatMessageLink(eventId: @matchedRule.OriginalEvent.EventId, roomId: matchedRule.PolicyList.Room.RoomId, displayName: "rule")}", @matchedRule.OriginalEvent.RawContent)); - } - - if (configuration.DemoMode) { - // foreach (var matchedRule in rules) { - // await room.SendMessageEventAsync(MessageFormatter.FormatSuccessJson( - // $"{MessageFormatter.HtmlFormatMessageLink(eventId: @event.EventId, roomId: room.RoomId, displayName: "Event")} matched {MessageFormatter.HtmlFormatMessageLink(eventId: @matchedRule.EventId, roomId: matchedRule.RoomId, displayName: "rule")}", @matchedRule.RawContent)); - // } - return; - } - // - // if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) { - // if (message is { MessageType: "m.image" }) { - // //check media - // // var matchedPolicy = await CheckMedia(@event); - // var matchedPolicy = rules.FirstOrDefault(); - // if (matchedPolicy is null) return; - // var matchedpolicyData = matchedPolicy.TypedContent as MediaPolicyEventContent; - // await _logRoom.SendMessageEventAsync( - // new RoomMessageEventContent( - // body: - // $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted an image in {MessageFormatter.HtmlFormatMention(room.RoomId)} that matched rule {matchedPolicy.StateKey}, applying action {matchedpolicyData.Recommendation}, as described in rule: {matchedPolicy.RawContent!.ToJson(ignoreNull: true)}", - // messageType: "m.text") { - // Format = "org.matrix.custom.html", - // FormattedBody = - // $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted an image in {MessageFormatter.HtmlFormatMention(room.RoomId)} that matched rule {matchedPolicy.StateKey}, applying action {matchedpolicyData.Recommendation}, as described in rule:
{matchedPolicy.RawContent!.ToJson(ignoreNull: true)}
" - // }); - // switch (matchedpolicyData.Recommendation) { - // case "warn_admins": { - // await _controlRoom.SendMessageEventAsync( - // new RoomMessageEventContent( - // body: $"{string.Join(' ', admins)}\nUser {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image {message.Url}", - // messageType: "m.text") { - // Format = "org.matrix.custom.html", - // FormattedBody = $"{string.Join(' ', admins.Select(u => MessageFormatter.HtmlFormatMention(u)))}\n" + - // $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image {message.Url}" - // }); - // break; - // } - // case "warn": { - // await room.SendMessageEventAsync( - // new RoomMessageEventContent( - // body: $"Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}", - // messageType: "m.text") { - // Format = "org.matrix.custom.html", - // FormattedBody = - // $"Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}" - // }); - // break; - // } - // case "redact": { - // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason ?? "No reason specified"); - // break; - // } - // case "spoiler": { - // //
- // // @emma:rory.gay
- // // - // // - // // CN - // // : - // // test
- // // - // // - // // - // //
- // await room.SendMessageEventAsync( - // new RoomMessageEventContent( - // body: - // $"Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:", - // messageType: "m.text") { - // Format = "org.matrix.custom.html", - // FormattedBody = - // $"Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:" - // }); - // var imageUrl = message.Url; - // await room.SendMessageEventAsync( - // new RoomMessageEventContent(body: $"CN: {imageUrl}", - // messageType: "m.text") { - // Format = "org.matrix.custom.html", - // FormattedBody = $""" - //
- // - // CN - // : - // {matchedpolicyData.Reason}
- // - // - // - // - // - //
- // """ - // }); - // await room.RedactEventAsync(@event.EventId, "Automatically spoilered: " + matchedpolicyData.Reason); - // break; - // } - // case "mute": { - // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); - // //change powerlevel to -1 - // var currentPls = await room.GetPowerLevelsAsync(); - // if (currentPls is null) { - // logger.LogWarning("Unable to get power levels for {room}", room.RoomId); - // await _logRoom.SendMessageEventAsync( - // MessageFormatter.FormatError($"Unable to get power levels for {MessageFormatter.HtmlFormatMention(room.RoomId)}")); - // return; - // } - // - // currentPls.Users ??= new(); - // currentPls.Users[@event.Sender] = -1; - // await room.SendStateEventAsync("m.room.power_levels", currentPls); - // break; - // } - // case "kick": { - // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); - // await room.KickAsync(@event.Sender, matchedpolicyData.Reason); - // break; - // } - // case "ban": { - // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); - // await room.BanAsync(@event.Sender, matchedpolicyData.Reason); - // break; - // } - // default: { - // throw new ArgumentOutOfRangeException("recommendation", - // $"Unknown response type {matchedpolicyData.Recommendation}!"); - // } - // } - // } - // } - } - catch (Exception e) { - logger.LogError("{}", e.ToString()); - await _controlRoom.SendMessageEventAsync( - MessageFormatter.FormatException($"Unable to process event in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e)); - await _logRoom.SendMessageEventAsync( - MessageFormatter.FormatException($"Unable to process event in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e)); - await using var stream = new MemoryStream(e.ToString().AsBytes().ToArray()); - await _controlRoom.SendFileAsync("error.log.cs", stream); - await _logRoom.SendFileAsync("error.log.cs", stream); - } - }); - await engine.ReloadActivePolicyLists(); - await syncHelper.RunSyncLoopAsync(); - } - - private async Task LogPolicyChange(StateEventResponse changeEvent) { - var room = hs.GetRoom(changeEvent.RoomId!); - var message = MessageFormatter.FormatWarning($"Policy change detected in {MessageFormatter.HtmlFormatMessageLink(changeEvent.RoomId, changeEvent.EventId, [hs.ServerName], await room.GetNameOrFallbackAsync())}!"); - message = message.ConcatLine(new RoomMessageEventContent(body: $"Policy type: {changeEvent.Type} -> {changeEvent.MappedType.Name}") { - FormattedBody = $"Policy type: {changeEvent.Type} -> {changeEvent.MappedType.Name}" - }); - var isUpdated = changeEvent.Unsigned.PrevContent is { Count: > 0 }; - var isRemoved = changeEvent.RawContent is not { Count: > 0 }; - // if (isUpdated) { - // message = message.ConcatLine(MessageFormatter.FormatSuccess("Rule updated!")); - // message = message.ConcatLine(MessageFormatter.FormatSuccessJson("Old rule:", changeEvent.Unsigned.PrevContent!)); - // } - // else if (isRemoved) { - // message = message.ConcatLine(MessageFormatter.FormatWarningJson("Rule removed!", changeEvent.Unsigned.PrevContent!)); - // } - // else { - // message = message.ConcatLine(MessageFormatter.FormatSuccess("New rule added!")); - // } - message = message.ConcatLine(MessageFormatter.FormatSuccessJson($"{(isUpdated ? "Updated" : isRemoved ? "Removed" : "New")} rule: {changeEvent.StateKey}", changeEvent.RawContent!)); - if (isRemoved || isUpdated) { - message = message.ConcatLine(MessageFormatter.FormatSuccessJson("Old content: ", changeEvent.Unsigned.PrevContent!)); - } - - await _logRoom.SendMessageEventAsync(message); - } - - /// Triggered when the application host is performing a graceful shutdown. - /// Indicates that the shutdown process should no longer be graceful. - public async Task StopAsync(CancellationToken cancellationToken) { - logger.LogInformation("Shutting down bot!"); - } - -} diff --git a/ModerationBot.csproj b/ModerationBot.csproj deleted file mode 100644 index 99eb0b9..0000000 --- a/ModerationBot.csproj +++ /dev/null @@ -1,32 +0,0 @@ - - - - Exe - net8.0 - preview - enable - enable - false - true - - - - - - - - - - - - - - - - - - - Always - - - diff --git a/ModerationBot.sln b/ModerationBot.sln new file mode 100644 index 0000000..1891670 --- /dev/null +++ b/ModerationBot.sln @@ -0,0 +1,161 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LibMatrix", "LibMatrix", "{936A3DB8-59EE-4F00-AF73-F677F47B8217}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ArcaneLibs", "ArcaneLibs", "{2FF6B89E-4B6A-409F-84FA-EEC0D0E30095}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs", "LibMatrix\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj", "{3093BA83-1A30-4462-B5E2-42BBAA3416FF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.Blazor.Components", "LibMatrix\ArcaneLibs\ArcaneLibs.Blazor.Components\ArcaneLibs.Blazor.Components.csproj", "{19AEA6EC-6918-462D-8448-2E15032C6BFD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.Legacy", "LibMatrix\ArcaneLibs\ArcaneLibs.Legacy\ArcaneLibs.Legacy.csproj", "{A97C3494-C68A-4B36-A64A-1567D0313E92}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.Logging", "LibMatrix\ArcaneLibs\ArcaneLibs.Logging\ArcaneLibs.Logging.csproj", "{DDAE66B2-31B0-4332-B9E9-3F28B4336B0D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.StringNormalisation", "LibMatrix\ArcaneLibs\ArcaneLibs.StringNormalisation\ArcaneLibs.StringNormalisation.csproj", "{6042C750-8881-4AAE-8F1E-5FAEB730300D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.Timings", "LibMatrix\ArcaneLibs\ArcaneLibs.Timings\ArcaneLibs.Timings.csproj", "{51D305FB-362A-4038-8ED6-91DD2D3D4551}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs.UsageTest", "LibMatrix\ArcaneLibs\ArcaneLibs.UsageTest\ArcaneLibs.UsageTest.csproj", "{6B61BDF3-FAB2-4859-9D0E-99E2BACB1735}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLib.Tests", "LibMatrix\ArcaneLibs\ArcaneLib.Tests\ArcaneLib.Tests.csproj", "{02174E9A-6500-4A32-8BE6-3749DE188BFC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.EventTypes", "LibMatrix\LibMatrix.EventTypes\LibMatrix.EventTypes.csproj", "{B0FB923E-DA7B-49B6-B34B-B6730A52754B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix", "LibMatrix\LibMatrix\LibMatrix.csproj", "{A4FD5687-B963-49ED-ABC1-030A64347FE3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.MxApiExtensions", "LibMatrix\LibMatrix.MxApiExtensions\LibMatrix.MxApiExtensions.csproj", "{B4010E66-35D0-4060-8265-E4BA81825A5F}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{25D6A95A-856B-4C76-A144-8E7F718139F9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.HomeserverEmulator", "LibMatrix\Tests\LibMatrix.HomeserverEmulator\LibMatrix.HomeserverEmulator.csproj", "{DB0A52A1-8B7D-4230-832A-1F9E7C0BB5D6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.Tests", "LibMatrix\Tests\LibMatrix.Tests\LibMatrix.Tests.csproj", "{4EFA7ECF-19D1-4054-AFCE-1E8F6CEC7AB0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestDataGenerator", "LibMatrix\Tests\TestDataGenerator\TestDataGenerator.csproj", "{AFD7C054-B2ED-4465-BA2F-8BC6B0E90D85}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Utilities", "Utilities", "{4BBABE0C-2F0B-4174-BD45-043F66AAA205}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.DebugDataValidationApi", "LibMatrix\Utilities\LibMatrix.DebugDataValidationApi\LibMatrix.DebugDataValidationApi.csproj", "{92373993-AE27-4321-B7E3-7597F0B16461}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.DevTestBot", "LibMatrix\Utilities\LibMatrix.DevTestBot\LibMatrix.DevTestBot.csproj", "{2E39F6B2-A861-4230-A18E-E6586AF86CC4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.JsonSerializerContextGenerator", "LibMatrix\Utilities\LibMatrix.JsonSerializerContextGenerator\LibMatrix.JsonSerializerContextGenerator.csproj", "{6F2D0A13-CBB6-4186-9C83-538486963985}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.Utilities.Bot", "LibMatrix\Utilities\LibMatrix.Utilities.Bot\LibMatrix.Utilities.Bot.csproj", "{E75F7B39-3A15-401E-8400-548C77A5FDEE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModerationBot", "ModerationBot\ModerationBot.csproj", "{81AC8293-A9CB-4466-84E2-C04BEB2AC852}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3093BA83-1A30-4462-B5E2-42BBAA3416FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3093BA83-1A30-4462-B5E2-42BBAA3416FF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3093BA83-1A30-4462-B5E2-42BBAA3416FF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3093BA83-1A30-4462-B5E2-42BBAA3416FF}.Release|Any CPU.Build.0 = Release|Any CPU + {19AEA6EC-6918-462D-8448-2E15032C6BFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {19AEA6EC-6918-462D-8448-2E15032C6BFD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {19AEA6EC-6918-462D-8448-2E15032C6BFD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {19AEA6EC-6918-462D-8448-2E15032C6BFD}.Release|Any CPU.Build.0 = Release|Any CPU + {A97C3494-C68A-4B36-A64A-1567D0313E92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A97C3494-C68A-4B36-A64A-1567D0313E92}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A97C3494-C68A-4B36-A64A-1567D0313E92}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A97C3494-C68A-4B36-A64A-1567D0313E92}.Release|Any CPU.Build.0 = Release|Any CPU + {DDAE66B2-31B0-4332-B9E9-3F28B4336B0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DDAE66B2-31B0-4332-B9E9-3F28B4336B0D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DDAE66B2-31B0-4332-B9E9-3F28B4336B0D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DDAE66B2-31B0-4332-B9E9-3F28B4336B0D}.Release|Any CPU.Build.0 = Release|Any CPU + {6042C750-8881-4AAE-8F1E-5FAEB730300D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6042C750-8881-4AAE-8F1E-5FAEB730300D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6042C750-8881-4AAE-8F1E-5FAEB730300D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6042C750-8881-4AAE-8F1E-5FAEB730300D}.Release|Any CPU.Build.0 = Release|Any CPU + {51D305FB-362A-4038-8ED6-91DD2D3D4551}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {51D305FB-362A-4038-8ED6-91DD2D3D4551}.Debug|Any CPU.Build.0 = Debug|Any CPU + {51D305FB-362A-4038-8ED6-91DD2D3D4551}.Release|Any CPU.ActiveCfg = Release|Any CPU + {51D305FB-362A-4038-8ED6-91DD2D3D4551}.Release|Any CPU.Build.0 = Release|Any CPU + {6B61BDF3-FAB2-4859-9D0E-99E2BACB1735}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6B61BDF3-FAB2-4859-9D0E-99E2BACB1735}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6B61BDF3-FAB2-4859-9D0E-99E2BACB1735}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6B61BDF3-FAB2-4859-9D0E-99E2BACB1735}.Release|Any CPU.Build.0 = Release|Any CPU + {02174E9A-6500-4A32-8BE6-3749DE188BFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {02174E9A-6500-4A32-8BE6-3749DE188BFC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {02174E9A-6500-4A32-8BE6-3749DE188BFC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {02174E9A-6500-4A32-8BE6-3749DE188BFC}.Release|Any CPU.Build.0 = Release|Any CPU + {B0FB923E-DA7B-49B6-B34B-B6730A52754B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B0FB923E-DA7B-49B6-B34B-B6730A52754B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B0FB923E-DA7B-49B6-B34B-B6730A52754B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B0FB923E-DA7B-49B6-B34B-B6730A52754B}.Release|Any CPU.Build.0 = Release|Any CPU + {A4FD5687-B963-49ED-ABC1-030A64347FE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A4FD5687-B963-49ED-ABC1-030A64347FE3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4FD5687-B963-49ED-ABC1-030A64347FE3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4FD5687-B963-49ED-ABC1-030A64347FE3}.Release|Any CPU.Build.0 = Release|Any CPU + {B4010E66-35D0-4060-8265-E4BA81825A5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B4010E66-35D0-4060-8265-E4BA81825A5F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B4010E66-35D0-4060-8265-E4BA81825A5F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B4010E66-35D0-4060-8265-E4BA81825A5F}.Release|Any CPU.Build.0 = Release|Any CPU + {DB0A52A1-8B7D-4230-832A-1F9E7C0BB5D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DB0A52A1-8B7D-4230-832A-1F9E7C0BB5D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DB0A52A1-8B7D-4230-832A-1F9E7C0BB5D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DB0A52A1-8B7D-4230-832A-1F9E7C0BB5D6}.Release|Any CPU.Build.0 = Release|Any CPU + {4EFA7ECF-19D1-4054-AFCE-1E8F6CEC7AB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4EFA7ECF-19D1-4054-AFCE-1E8F6CEC7AB0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4EFA7ECF-19D1-4054-AFCE-1E8F6CEC7AB0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4EFA7ECF-19D1-4054-AFCE-1E8F6CEC7AB0}.Release|Any CPU.Build.0 = Release|Any CPU + {AFD7C054-B2ED-4465-BA2F-8BC6B0E90D85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AFD7C054-B2ED-4465-BA2F-8BC6B0E90D85}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AFD7C054-B2ED-4465-BA2F-8BC6B0E90D85}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AFD7C054-B2ED-4465-BA2F-8BC6B0E90D85}.Release|Any CPU.Build.0 = Release|Any CPU + {92373993-AE27-4321-B7E3-7597F0B16461}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {92373993-AE27-4321-B7E3-7597F0B16461}.Debug|Any CPU.Build.0 = Debug|Any CPU + {92373993-AE27-4321-B7E3-7597F0B16461}.Release|Any CPU.ActiveCfg = Release|Any CPU + {92373993-AE27-4321-B7E3-7597F0B16461}.Release|Any CPU.Build.0 = Release|Any CPU + {2E39F6B2-A861-4230-A18E-E6586AF86CC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2E39F6B2-A861-4230-A18E-E6586AF86CC4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2E39F6B2-A861-4230-A18E-E6586AF86CC4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2E39F6B2-A861-4230-A18E-E6586AF86CC4}.Release|Any CPU.Build.0 = Release|Any CPU + {6F2D0A13-CBB6-4186-9C83-538486963985}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6F2D0A13-CBB6-4186-9C83-538486963985}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6F2D0A13-CBB6-4186-9C83-538486963985}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6F2D0A13-CBB6-4186-9C83-538486963985}.Release|Any CPU.Build.0 = Release|Any CPU + {E75F7B39-3A15-401E-8400-548C77A5FDEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E75F7B39-3A15-401E-8400-548C77A5FDEE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E75F7B39-3A15-401E-8400-548C77A5FDEE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E75F7B39-3A15-401E-8400-548C77A5FDEE}.Release|Any CPU.Build.0 = Release|Any CPU + {81AC8293-A9CB-4466-84E2-C04BEB2AC852}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {81AC8293-A9CB-4466-84E2-C04BEB2AC852}.Debug|Any CPU.Build.0 = Debug|Any CPU + {81AC8293-A9CB-4466-84E2-C04BEB2AC852}.Release|Any CPU.ActiveCfg = Release|Any CPU + {81AC8293-A9CB-4466-84E2-C04BEB2AC852}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {2FF6B89E-4B6A-409F-84FA-EEC0D0E30095} = {936A3DB8-59EE-4F00-AF73-F677F47B8217} + {3093BA83-1A30-4462-B5E2-42BBAA3416FF} = {2FF6B89E-4B6A-409F-84FA-EEC0D0E30095} + {19AEA6EC-6918-462D-8448-2E15032C6BFD} = {2FF6B89E-4B6A-409F-84FA-EEC0D0E30095} + {A97C3494-C68A-4B36-A64A-1567D0313E92} = {2FF6B89E-4B6A-409F-84FA-EEC0D0E30095} + {DDAE66B2-31B0-4332-B9E9-3F28B4336B0D} = {2FF6B89E-4B6A-409F-84FA-EEC0D0E30095} + {6042C750-8881-4AAE-8F1E-5FAEB730300D} = {2FF6B89E-4B6A-409F-84FA-EEC0D0E30095} + {51D305FB-362A-4038-8ED6-91DD2D3D4551} = {2FF6B89E-4B6A-409F-84FA-EEC0D0E30095} + {6B61BDF3-FAB2-4859-9D0E-99E2BACB1735} = {2FF6B89E-4B6A-409F-84FA-EEC0D0E30095} + {02174E9A-6500-4A32-8BE6-3749DE188BFC} = {2FF6B89E-4B6A-409F-84FA-EEC0D0E30095} + {B0FB923E-DA7B-49B6-B34B-B6730A52754B} = {936A3DB8-59EE-4F00-AF73-F677F47B8217} + {A4FD5687-B963-49ED-ABC1-030A64347FE3} = {936A3DB8-59EE-4F00-AF73-F677F47B8217} + {B4010E66-35D0-4060-8265-E4BA81825A5F} = {936A3DB8-59EE-4F00-AF73-F677F47B8217} + {25D6A95A-856B-4C76-A144-8E7F718139F9} = {936A3DB8-59EE-4F00-AF73-F677F47B8217} + {DB0A52A1-8B7D-4230-832A-1F9E7C0BB5D6} = {25D6A95A-856B-4C76-A144-8E7F718139F9} + {4EFA7ECF-19D1-4054-AFCE-1E8F6CEC7AB0} = {25D6A95A-856B-4C76-A144-8E7F718139F9} + {AFD7C054-B2ED-4465-BA2F-8BC6B0E90D85} = {25D6A95A-856B-4C76-A144-8E7F718139F9} + {4BBABE0C-2F0B-4174-BD45-043F66AAA205} = {936A3DB8-59EE-4F00-AF73-F677F47B8217} + {92373993-AE27-4321-B7E3-7597F0B16461} = {4BBABE0C-2F0B-4174-BD45-043F66AAA205} + {2E39F6B2-A861-4230-A18E-E6586AF86CC4} = {4BBABE0C-2F0B-4174-BD45-043F66AAA205} + {6F2D0A13-CBB6-4186-9C83-538486963985} = {4BBABE0C-2F0B-4174-BD45-043F66AAA205} + {E75F7B39-3A15-401E-8400-548C77A5FDEE} = {4BBABE0C-2F0B-4174-BD45-043F66AAA205} + EndGlobalSection +EndGlobal diff --git a/ModerationBot/AccountData/BotData.cs b/ModerationBot/AccountData/BotData.cs new file mode 100644 index 0000000..de52679 --- /dev/null +++ b/ModerationBot/AccountData/BotData.cs @@ -0,0 +1,18 @@ +using System.Text.Json.Serialization; +using LibMatrix.EventTypes; + +namespace ModerationBot.AccountData; + +[MatrixEvent(EventName = EventId)] +public class BotData { + public const string EventId = "gay.rory.moderation_bot_data"; + + [JsonPropertyName("control_room")] + public string? ControlRoom { get; set; } = ""; + + [JsonPropertyName("log_room")] + public string? LogRoom { get; set; } = ""; + + [JsonPropertyName("default_policy_room")] + public string? DefaultPolicyRoom { get; set; } +} \ No newline at end of file diff --git a/ModerationBot/Commands/BanMediaCommand.cs b/ModerationBot/Commands/BanMediaCommand.cs new file mode 100644 index 0000000..07c9858 --- /dev/null +++ b/ModerationBot/Commands/BanMediaCommand.cs @@ -0,0 +1,112 @@ +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.Services; +using ModerationBot.StateEventTypes.Policies.Implementations; + +namespace ModerationBot.Commands; + +public class BanMediaCommand(HomeserverResolverService hsResolver, PolicyEngine engine, ModerationBotRoomProvider roomProvider) : ICommand { + public string Name { get; } = "banmedia"; + public string[]? Aliases { get; } + public string Description { get; } = "Create a policy banning a piece of media, must be used in reply to a message"; + public bool Unlisted { get; } + + public async Task CanInvoke(CommandContext ctx) { + //check if user is admin in control room + var controlRoom = await roomProvider.GetControlRoomAsync(); + var logRoom = await roomProvider.GetLogRoomAsync(); + var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(ctx.MessageEvent.Sender, "m.room.ban"); + if (!isAdmin) { + // await ctx.Reply("You do not have permission to use this command!"); + await 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 policyRoom = await roomProvider.GetDefaultPolicyRoomAsync(); + var logRoom = await roomProvider.GetLogRoomAsync(); + + //check if reply + var messageContent = ctx.MessageEvent.TypedContent as RoomMessageEventContent; + if (messageContent?.RelatesTo is { InReplyTo: not null }) { + try { + await logRoom.SendMessageEventAsync( + new RoomMessageEventContent( + body: $"User {MessageFormatter.HtmlFormatMention(ctx.MessageEvent.Sender)} is trying to ban media {messageContent!.RelatesTo!.InReplyTo!.EventId}", + messageType: "m.text")); + + //get replied message + var repliedMessage = await ctx.Room.GetEventAsync(messageContent.RelatesTo!.InReplyTo!.EventId); + + //check if recommendation is in list + if (ctx.Args.Length < 2) { + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatError("You must specify a recommendation type and reason!")); + return; + } + + var recommendation = ctx.Args[0]; + + if (recommendation is not ("ban" or "kick" or "mute" or "redact" or "spoiler" or "warn" or "warn_admins")) { + await ctx.Room.SendMessageEventAsync( + MessageFormatter.FormatError( + $"Invalid recommendation type {recommendation}, must be `warn_admins`, `warn`, `spoiler`, `redact`, `mute`, `kick` or `ban`!")); + return; + } + + //hash file + var mxcUri = (repliedMessage.TypedContent as RoomMessageEventContent).Url!; + var resolvedUri = await hsResolver.ResolveMediaUri(mxcUri.Split('/')[2], mxcUri); + var hashAlgo = SHA3_256.Create(); + var uriHash = hashAlgo.ComputeHash(mxcUri.AsBytes().ToArray()); + byte[]? fileHash = null; + + try { + fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver.ClientHttpClient.GetStreamAsync(resolvedUri)); + } + catch (Exception ex) { + await logRoom.SendMessageEventAsync( + MessageFormatter.FormatException($"Error calculating file hash for {mxcUri} via {mxcUri.Split('/')[2]}, retrying via {ctx.Homeserver.BaseUrl}...", + ex)); + try { + resolvedUri = await hsResolver.ResolveMediaUri(ctx.Homeserver.BaseUrl, mxcUri); + fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver.ClientHttpClient.GetStreamAsync(resolvedUri)); + } + catch (Exception ex2) { + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatException("Error calculating file hash", ex2)); + await logRoom.SendMessageEventAsync( + MessageFormatter.FormatException($"Error calculating file hash via {ctx.Homeserver.BaseUrl}!", ex2)); + } + } + + 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, + }); + + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatSuccessJson("Media policy created", policy)); + await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccessJson("Media policy created", policy)); + } + catch (Exception e) { + await logRoom.SendMessageEventAsync(MessageFormatter.FormatException("Error creating policy", e)); + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatException("Error creating policy", e)); + await using var stream = new MemoryStream(e.ToString().AsBytes().ToArray()); + await logRoom.SendFileAsync("error.log.cs", stream); + } + } + else { + await ctx.Room.SendMessageEventAsync(MessageFormatter.FormatError("This command must be used in reply to a message!")); + } + } +} diff --git a/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs b/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs new file mode 100644 index 0000000..78e1979 --- /dev/null +++ b/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs @@ -0,0 +1,60 @@ +using LibMatrix.EventTypes.Spec; +using LibMatrix.Helpers; +using LibMatrix.RoomTypes; +using LibMatrix.Services; +using LibMatrix.Utilities.Bot.Interfaces; +using ModerationBot.AccountData; + +namespace ModerationBot.Commands; + +public class DbgAllRoomsArePolicyListsCommand + (IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { + public string Name { get; } = "dbg-allroomsarepolicy"; + public string[]? Aliases { get; } + public string Description { get; } = "[Debug] mark all rooms as trusted policy rooms"; + public bool Unlisted { get; } + private GenericRoom logRoom { get; set; } + + public async Task CanInvoke(CommandContext ctx) { +// #if !DEBUG +// return false; +// #endif + + //check if user is admin in control room + var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); + var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); + var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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("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 JoinRoom(GenericRoom memberRoom, string reason, List 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; + } +} diff --git a/ModerationBot/Commands/DbgAniRainbowTest.cs b/ModerationBot/Commands/DbgAniRainbowTest.cs new file mode 100644 index 0000000..eed6fa5 --- /dev/null +++ b/ModerationBot/Commands/DbgAniRainbowTest.cs @@ -0,0 +1,50 @@ +using System.Diagnostics; +using LibMatrix.EventTypes.Spec; +using LibMatrix.Helpers; +using LibMatrix.RoomTypes; +using LibMatrix.Services; +using LibMatrix.Utilities.Bot.Interfaces; +using ModerationBot.AccountData; + +namespace ModerationBot.Commands; + +public class DbgAniRainbowTest(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { + public string Name { get; } = "dbg-ani-rainbow"; + public string[]? Aliases { get; } + public string Description { get; } = "[Debug] animated rainbow :)"; + public bool Unlisted { get; } + private GenericRoom logRoom { get; set; } + + public async Task CanInvoke(CommandContext ctx) { + return ctx.Room.RoomId == "!DoHEdFablOLjddKWIp:rory.gay"; + } + + public async Task Invoke(CommandContext ctx) { + //255 long string + // var rainbow = "🟥🟧🟨🟩🟦🟪"; + var rainbow = "M"; + var chars = rainbow; + for (var i = 0; i < 76; i++) { + chars += rainbow[i%rainbow.Length]; + } + + var msg = new MessageBuilder(msgType: "m.notice").WithRainbowString(chars).Build(); + var msgEvent = await ctx.Room.SendMessageEventAsync(msg); + + Task.Run(async () => { + + int i = 0; + while (true) { + msg = new MessageBuilder(msgType: "m.notice").WithRainbowString(chars, offset: i+=5).Build(); + // .SetReplaceRelation(msgEvent.EventId); + // msg.Body = ""; + // msg.FormattedBody = ""; + var sw = Stopwatch.StartNew(); + await ctx.Room.SendMessageEventAsync(msg); + await Task.Delay(sw.Elapsed); + } + + }); + + } +} \ No newline at end of file diff --git a/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs b/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs new file mode 100644 index 0000000..81e81a0 --- /dev/null +++ b/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs @@ -0,0 +1,39 @@ +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; +using LibMatrix.RoomTypes; +using LibMatrix.Services; +using LibMatrix.Utilities.Bot.Interfaces; +using ModerationBot.AccountData; + +namespace ModerationBot.Commands; + +public class DbgDumpActivePoliciesCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { + public string Name { get; } = "dbg-dumppolicies"; + public string[]? Aliases { get; } + public string Description { get; } = "[Debug] Dump all active policies"; + public bool Unlisted { get; } + private GenericRoom logRoom { get; set; } + + public async Task CanInvoke(CommandContext ctx) { +#if !DEBUG + return false; +#endif + + //check if user is admin in control room + var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); + var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); + var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs b/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs new file mode 100644 index 0000000..ac2036a --- /dev/null +++ b/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs @@ -0,0 +1,69 @@ +using ArcaneLibs.Extensions; +using LibMatrix; +using LibMatrix.EventTypes.Spec; +using LibMatrix.RoomTypes; +using LibMatrix.Services; +using LibMatrix.Utilities.Bot.Interfaces; +using ModerationBot.AccountData; + +namespace ModerationBot.Commands; + +public class DbgDumpAllStateTypesCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { + public string Name { get; } = "dbg-dumpstatetypes"; + public string[]? Aliases { get; } + public string Description { get; } = "[Debug] Dump all state types we can find"; + public bool Unlisted { get; } + private GenericRoom logRoom { get; set; } + + public async Task CanInvoke(CommandContext ctx) { +#if !DEBUG + return false; +#endif + + //check if user is admin in control room + var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); + var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); + var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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("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 states) { + string raw = "Count | State type | Mapped type", html = ""; + 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} | {StateEvent.GetStateEventType(stateGroup[0].Type).Name}"; + html += $""; + } + + html += "
CountState typeMapped type
{stateGroup.Count}{type}{StateEvent.GetStateEventType(stateGroup[0].Type).Name}
"; + return (raw, html); + } +} \ No newline at end of file diff --git a/ModerationBot/Commands/JoinRoomCommand.cs b/ModerationBot/Commands/JoinRoomCommand.cs new file mode 100644 index 0000000..e604a4e --- /dev/null +++ b/ModerationBot/Commands/JoinRoomCommand.cs @@ -0,0 +1,48 @@ +using LibMatrix.EventTypes.Spec; +using LibMatrix.Helpers; +using LibMatrix.Services; +using LibMatrix.Utilities.Bot.Interfaces; +using ModerationBot.AccountData; + +namespace ModerationBot.Commands; + +public class JoinRoomCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { + public string Name { get; } = "join"; + public string[]? Aliases { get; } + public string Description { get; } = "Join arbitrary rooms"; + public bool Unlisted { get; } + + public async Task CanInvoke(CommandContext ctx) { + //check if user is admin in control room + var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); + var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); + var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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("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() { 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)}")); + } +} \ No newline at end of file diff --git a/ModerationBot/Commands/JoinSpaceMembersCommand.cs b/ModerationBot/Commands/JoinSpaceMembersCommand.cs new file mode 100644 index 0000000..86ecf7e --- /dev/null +++ b/ModerationBot/Commands/JoinSpaceMembersCommand.cs @@ -0,0 +1,73 @@ +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; +using LibMatrix.Helpers; +using LibMatrix.RoomTypes; +using LibMatrix.Services; +using LibMatrix.Utilities.Bot.Interfaces; +using ModerationBot.AccountData; + +namespace ModerationBot.Commands; + +public class JoinSpaceMembersCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { + public string Name { get; } = "joinspacemembers"; + public string[]? Aliases { get; } + public string Description { get; } = "Join all rooms in space"; + public bool Unlisted { get; } + private GenericRoom logRoom { get; set; } + + public async Task CanInvoke(CommandContext ctx) { + //check if user is admin in control room + var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); + var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); + var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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("gay.rory.moderation_bot_data"); + logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom); + var currentRooms = (await ctx.Homeserver.GetJoinedRooms()).Select(x => x.RoomId).ToList(); + + 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() { 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>(); + await foreach (var memberRoom in room.AsSpace.GetChildrenAsync()) { + if (currentRooms.Contains(memberRoom.RoomId)) continue; + 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 JoinRoom(GenericRoom memberRoom, string reason, List servers) { + try { + var resp = await memberRoom.JoinAsync(servers.ToArray(), reason, checkIfAlreadyMember: false); + await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joined room {memberRoom.RoomId} (resp={resp.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/ModerationBot/Commands/ReloadPoliciesCommand.cs b/ModerationBot/Commands/ReloadPoliciesCommand.cs new file mode 100644 index 0000000..2934185 --- /dev/null +++ b/ModerationBot/Commands/ReloadPoliciesCommand.cs @@ -0,0 +1,38 @@ +using LibMatrix.EventTypes.Spec; +using LibMatrix.Helpers; +using LibMatrix.Services; +using LibMatrix.Utilities.Bot.Interfaces; +using ModerationBot.AccountData; + +namespace ModerationBot.Commands; + +public class ReloadPoliciesCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand { + public string Name { get; } = "reloadpolicies"; + public string[]? Aliases { get; } + public string Description { get; } = "Reload policies"; + public bool Unlisted { get; } + + public async Task CanInvoke(CommandContext ctx) { + if (ctx.MessageEvent.Sender == "@cadence:cadence.moe") return true; + //check if user is admin in control room + var botData = await ctx.Homeserver.GetAccountDataAsync("gay.rory.moderation_bot_data"); + var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom); + var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(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("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($"Reloading policy lists due to manual invocation!!!!")); + await engine.ReloadActivePolicyLists(); + } +} \ No newline at end of file diff --git a/ModerationBot/FirstRunTasks.cs b/ModerationBot/FirstRunTasks.cs new file mode 100644 index 0000000..9dece1d --- /dev/null +++ b/ModerationBot/FirstRunTasks.cs @@ -0,0 +1,83 @@ +using LibMatrix; +using LibMatrix.Homeservers; +using LibMatrix.Responses; +using ModerationBot.AccountData; + +namespace ModerationBot; + +public class FirstRunTasks { + public static async Task ConstructBotData(AuthenticatedHomeserverGeneric hs, ModerationBotConfiguration configuration, BotData? botdata) { + botdata ??= new BotData(); + var creationContent = CreateRoomRequest.CreatePrivate(hs, name: "Rory&::ModerationBot - Control room", roomAliasName: "moderation-bot-control-room"); + creationContent.Invite = configuration.Admins; + creationContent.CreationContent["type"] = "gay.rory.moderation_bot.control_room"; + + if (botdata.ControlRoom is null) + try { + botdata.ControlRoom = (await hs.CreateRoom(creationContent)).RoomId; + } + catch (Exception e) { + if (e is not MatrixException { ErrorCode: "M_ROOM_IN_USE" }) { + Console.WriteLine(e); + throw; + } + + creationContent.RoomAliasName += $"-{Guid.NewGuid()}"; + botdata.ControlRoom = (await hs.CreateRoom(creationContent)).RoomId; + } + //set access rules to allow joining via control room + // creationContent.InitialState.Add(new StateEvent { + // Type = "m.room.join_rules", + // StateKey = "", + // TypedContent = new RoomJoinRulesEventContent { + // JoinRule = "knock_restricted", + // Allow = new() { + // new RoomJoinRulesEventContent.AllowEntry { + // Type = "m.room_membership", + // RoomId = botdata.ControlRoom + // } + // } + // } + // }); + + creationContent.Name = "Rory&::ModerationBot - Log room"; + creationContent.RoomAliasName = "moderation-bot-log-room"; + creationContent.CreationContent["type"] = "gay.rory.moderation_bot.log_room"; + + if (botdata.LogRoom is null) + try { + botdata.LogRoom = (await hs.CreateRoom(creationContent)).RoomId; + } + catch (Exception e) { + if (e is not MatrixException { ErrorCode: "M_ROOM_IN_USE" }) { + Console.WriteLine(e); + throw; + } + + creationContent.RoomAliasName += $"-{Guid.NewGuid()}"; + botdata.LogRoom = (await hs.CreateRoom(creationContent)).RoomId; + } + + creationContent.Name = "Rory&::ModerationBot - Policy room"; + creationContent.RoomAliasName = "moderation-bot-policy-room"; + creationContent.CreationContent["type"] = "gay.rory.moderation_bot.policy_room"; + + if (botdata.DefaultPolicyRoom is null) + try { + botdata.DefaultPolicyRoom = (await hs.CreateRoom(creationContent)).RoomId; + } + catch (Exception e) { + if (e is not MatrixException { ErrorCode: "M_ROOM_IN_USE" }) { + Console.WriteLine(e); + throw; + } + + creationContent.RoomAliasName += $"-{Guid.NewGuid()}"; + botdata.DefaultPolicyRoom = (await hs.CreateRoom(creationContent)).RoomId; + } + + await hs.SetAccountDataAsync("gay.rory.moderation_bot_data", botdata); + + return botdata; + } +} diff --git a/ModerationBot/ModerationBot.cs b/ModerationBot/ModerationBot.cs new file mode 100644 index 0000000..791d3b5 --- /dev/null +++ b/ModerationBot/ModerationBot.cs @@ -0,0 +1,302 @@ +using ArcaneLibs.Extensions; +using LibMatrix; +using LibMatrix.EventTypes; +using LibMatrix.EventTypes.Spec; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.EventTypes.Spec.State.Policy; +using LibMatrix.Helpers; +using LibMatrix.Homeservers; +using LibMatrix.RoomTypes; +using LibMatrix.Services; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using ModerationBot.AccountData; +using ModerationBot.StateEventTypes.Policies; + +namespace ModerationBot; + +public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger logger, ModerationBotConfiguration configuration, PolicyEngine engine) : IHostedService { + private Task _listenerTask; + + // private GenericRoom _policyRoom; + private GenericRoom? _logRoom; + private GenericRoom? _controlRoom; + + /// Triggered when the application host is ready to start the service. + /// Indicates that the start process has been aborted. + public async Task StartAsync(CancellationToken cancellationToken) { + _listenerTask = Run(cancellationToken); + logger.LogInformation("Bot started!"); + } + + private async Task Run(CancellationToken cancellationToken) { + if (Directory.Exists("bot_data/cache")) + Directory.GetFiles("bot_data/cache").ToList().ForEach(File.Delete); + + BotData botData; + + try { + logger.LogInformation("Fetching bot account data..."); + botData = await hs.GetAccountDataAsync("gay.rory.moderation_bot_data"); + logger.LogInformation("Got bot account data..."); + } + catch (Exception e) { + logger.LogInformation("Could not fetch bot account data... {}", e.Message); + if (e is not MatrixException { ErrorCode: "M_NOT_FOUND" }) { + logger.LogError("{}", e.ToString()); + throw; + } + + botData = null; + } + + botData = await FirstRunTasks.ConstructBotData(hs, configuration, botData); + + // _policyRoom = hs.GetRoom(botData.PolicyRoom ?? botData.ControlRoom); + _logRoom = hs.GetRoom(botData.LogRoom ?? botData.ControlRoom); + _controlRoom = hs.GetRoom(botData.ControlRoom); + foreach (var configurationAdmin in configuration.Admins) { + var pls = await _controlRoom.GetPowerLevelsAsync(); + if (pls is null) { + await _logRoom?.SendMessageEventAsync(MessageFormatter.FormatWarning($"Control room has no m.room.power_levels?")); + continue; + } + pls.SetUserPowerLevel(configurationAdmin, pls.GetUserPowerLevel(hs.UserId)); + await _controlRoom.SendStateEventAsync(RoomPowerLevelEventContent.EventId, pls); + } + var syncHelper = new SyncHelper(hs); + + List admins = new(); + +#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + Task.Run(async () => { + while (!cancellationToken.IsCancellationRequested) { + var controlRoomMembers = _controlRoom.GetMembersEnumerableAsync(); + var pls = await _controlRoom.GetPowerLevelsAsync(); + await foreach (var member in controlRoomMembers) { + if ((member.TypedContent as RoomMemberEventContent)? + .Membership == "join" && pls.UserHasTimelinePermission(member.Sender, RoomMessageEventContent.EventId)) admins.Add(member.StateKey); + } + + await Task.Delay(TimeSpan.FromMinutes(5), cancellationToken); + } + }, cancellationToken); +#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + + syncHelper.InviteReceivedHandlers.Add(async Task (args) => { + var inviteEvent = + args.Value.InviteState.Events.FirstOrDefault(x => + x.Type == "m.room.member" && x.StateKey == hs.UserId); + logger.LogInformation("Got invite to {RoomId} by {Sender} with reason: {Reason}", args.Key, inviteEvent!.Sender, + (inviteEvent.TypedContent as RoomMemberEventContent)!.Reason); + await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Bot invited to {MessageFormatter.HtmlFormatMention(args.Key)} by {MessageFormatter.HtmlFormatMention(inviteEvent.Sender)}")); + if (admins.Contains(inviteEvent.Sender)) { + try { + await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joining {MessageFormatter.HtmlFormatMention(args.Key)}...")); + var senderProfile = await hs.GetProfileAsync(inviteEvent.Sender); + await hs.GetRoom(args.Key).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!"); + } + catch (Exception e) { + logger.LogError("{}", e.ToString()); + await _logRoom.SendMessageEventAsync(MessageFormatter.FormatException("Could not join room", e)); + await hs.GetRoom(args.Key).LeaveAsync(reason: "I was unable to join the room: " + e); + } + } + }); + + syncHelper.TimelineEventHandlers.Add(async @event => { + var room = hs.GetRoom(@event.RoomId); + try { + logger.LogInformation( + "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: true, ignoreNull: true)); + + if (@event != null && ( + @event.MappedType.IsAssignableTo(typeof(BasePolicy)) + || @event.MappedType.IsAssignableTo(typeof(PolicyRuleEventContent)) + )) { + await LogPolicyChange(@event); + await engine.ReloadActivePolicyListById(@event.RoomId); + } + + var rules = await engine.GetMatchingPolicies(@event); + foreach (var matchedRule in rules) { + await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccessJson( + $"{MessageFormatter.HtmlFormatMessageLink(eventId: @event.EventId, roomId: room.RoomId, displayName: "Event")} matched {MessageFormatter.HtmlFormatMessageLink(eventId: @matchedRule.OriginalEvent.EventId, roomId: matchedRule.PolicyList.Room.RoomId, displayName: "rule")}", @matchedRule.OriginalEvent.RawContent)); + } + + if (configuration.DemoMode) { + // foreach (var matchedRule in rules) { + // await room.SendMessageEventAsync(MessageFormatter.FormatSuccessJson( + // $"{MessageFormatter.HtmlFormatMessageLink(eventId: @event.EventId, roomId: room.RoomId, displayName: "Event")} matched {MessageFormatter.HtmlFormatMessageLink(eventId: @matchedRule.EventId, roomId: matchedRule.RoomId, displayName: "rule")}", @matchedRule.RawContent)); + // } + return; + } + // + // if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) { + // if (message is { MessageType: "m.image" }) { + // //check media + // // var matchedPolicy = await CheckMedia(@event); + // var matchedPolicy = rules.FirstOrDefault(); + // if (matchedPolicy is null) return; + // var matchedpolicyData = matchedPolicy.TypedContent as MediaPolicyEventContent; + // await _logRoom.SendMessageEventAsync( + // new RoomMessageEventContent( + // body: + // $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted an image in {MessageFormatter.HtmlFormatMention(room.RoomId)} that matched rule {matchedPolicy.StateKey}, applying action {matchedpolicyData.Recommendation}, as described in rule: {matchedPolicy.RawContent!.ToJson(ignoreNull: true)}", + // messageType: "m.text") { + // Format = "org.matrix.custom.html", + // FormattedBody = + // $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted an image in {MessageFormatter.HtmlFormatMention(room.RoomId)} that matched rule {matchedPolicy.StateKey}, applying action {matchedpolicyData.Recommendation}, as described in rule:
{matchedPolicy.RawContent!.ToJson(ignoreNull: true)}
" + // }); + // switch (matchedpolicyData.Recommendation) { + // case "warn_admins": { + // await _controlRoom.SendMessageEventAsync( + // new RoomMessageEventContent( + // body: $"{string.Join(' ', admins)}\nUser {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image {message.Url}", + // messageType: "m.text") { + // Format = "org.matrix.custom.html", + // FormattedBody = $"{string.Join(' ', admins.Select(u => MessageFormatter.HtmlFormatMention(u)))}\n" + + // $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image {message.Url}" + // }); + // break; + // } + // case "warn": { + // await room.SendMessageEventAsync( + // new RoomMessageEventContent( + // body: $"Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}", + // messageType: "m.text") { + // Format = "org.matrix.custom.html", + // FormattedBody = + // $"Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}" + // }); + // break; + // } + // case "redact": { + // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason ?? "No reason specified"); + // break; + // } + // case "spoiler": { + // //
+ // // @emma:rory.gay
+ // // + // // + // // CN + // // : + // // test
+ // // + // // + // // + // //
+ // await room.SendMessageEventAsync( + // new RoomMessageEventContent( + // body: + // $"Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:", + // messageType: "m.text") { + // Format = "org.matrix.custom.html", + // FormattedBody = + // $"Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:" + // }); + // var imageUrl = message.Url; + // await room.SendMessageEventAsync( + // new RoomMessageEventContent(body: $"CN: {imageUrl}", + // messageType: "m.text") { + // Format = "org.matrix.custom.html", + // FormattedBody = $""" + //
+ // + // CN + // : + // {matchedpolicyData.Reason}
+ // + // + // + // + // + //
+ // """ + // }); + // await room.RedactEventAsync(@event.EventId, "Automatically spoilered: " + matchedpolicyData.Reason); + // break; + // } + // case "mute": { + // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); + // //change powerlevel to -1 + // var currentPls = await room.GetPowerLevelsAsync(); + // if (currentPls is null) { + // logger.LogWarning("Unable to get power levels for {room}", room.RoomId); + // await _logRoom.SendMessageEventAsync( + // MessageFormatter.FormatError($"Unable to get power levels for {MessageFormatter.HtmlFormatMention(room.RoomId)}")); + // return; + // } + // + // currentPls.Users ??= new(); + // currentPls.Users[@event.Sender] = -1; + // await room.SendStateEventAsync("m.room.power_levels", currentPls); + // break; + // } + // case "kick": { + // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); + // await room.KickAsync(@event.Sender, matchedpolicyData.Reason); + // break; + // } + // case "ban": { + // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); + // await room.BanAsync(@event.Sender, matchedpolicyData.Reason); + // break; + // } + // default: { + // throw new ArgumentOutOfRangeException("recommendation", + // $"Unknown response type {matchedpolicyData.Recommendation}!"); + // } + // } + // } + // } + } + catch (Exception e) { + logger.LogError("{}", e.ToString()); + await _controlRoom.SendMessageEventAsync( + MessageFormatter.FormatException($"Unable to process event in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e)); + await _logRoom.SendMessageEventAsync( + MessageFormatter.FormatException($"Unable to process event in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e)); + await using var stream = new MemoryStream(e.ToString().AsBytes().ToArray()); + await _controlRoom.SendFileAsync("error.log.cs", stream); + await _logRoom.SendFileAsync("error.log.cs", stream); + } + }); + await engine.ReloadActivePolicyLists(); + await syncHelper.RunSyncLoopAsync(); + } + + private async Task LogPolicyChange(StateEventResponse changeEvent) { + var room = hs.GetRoom(changeEvent.RoomId!); + var message = MessageFormatter.FormatWarning($"Policy change detected in {MessageFormatter.HtmlFormatMessageLink(changeEvent.RoomId, changeEvent.EventId, [hs.ServerName], await room.GetNameOrFallbackAsync())}!"); + message = message.ConcatLine(new RoomMessageEventContent(body: $"Policy type: {changeEvent.Type} -> {changeEvent.MappedType.Name}") { + FormattedBody = $"Policy type: {changeEvent.Type} -> {changeEvent.MappedType.Name}" + }); + var isUpdated = changeEvent.Unsigned.PrevContent is { Count: > 0 }; + var isRemoved = changeEvent.RawContent is not { Count: > 0 }; + // if (isUpdated) { + // message = message.ConcatLine(MessageFormatter.FormatSuccess("Rule updated!")); + // message = message.ConcatLine(MessageFormatter.FormatSuccessJson("Old rule:", changeEvent.Unsigned.PrevContent!)); + // } + // else if (isRemoved) { + // message = message.ConcatLine(MessageFormatter.FormatWarningJson("Rule removed!", changeEvent.Unsigned.PrevContent!)); + // } + // else { + // message = message.ConcatLine(MessageFormatter.FormatSuccess("New rule added!")); + // } + message = message.ConcatLine(MessageFormatter.FormatSuccessJson($"{(isUpdated ? "Updated" : isRemoved ? "Removed" : "New")} rule: {changeEvent.StateKey}", changeEvent.RawContent!)); + if (isRemoved || isUpdated) { + message = message.ConcatLine(MessageFormatter.FormatSuccessJson("Old content: ", changeEvent.Unsigned.PrevContent!)); + } + + await _logRoom.SendMessageEventAsync(message); + } + + /// Triggered when the application host is performing a graceful shutdown. + /// Indicates that the shutdown process should no longer be graceful. + public async Task StopAsync(CancellationToken cancellationToken) { + logger.LogInformation("Shutting down bot!"); + } + +} diff --git a/ModerationBot/ModerationBot.csproj b/ModerationBot/ModerationBot.csproj new file mode 100644 index 0000000..009ace0 --- /dev/null +++ b/ModerationBot/ModerationBot.csproj @@ -0,0 +1,32 @@ + + + + Exe + net8.0 + preview + enable + enable + false + true + + + + + + + + + + + + + + + + + + + Always + + + diff --git a/ModerationBot/ModerationBotConfiguration.cs b/ModerationBot/ModerationBotConfiguration.cs new file mode 100644 index 0000000..415b581 --- /dev/null +++ b/ModerationBot/ModerationBotConfiguration.cs @@ -0,0 +1,10 @@ +using Microsoft.Extensions.Configuration; + +namespace ModerationBot; + +public class ModerationBotConfiguration { + public ModerationBotConfiguration(IConfiguration config) => config.GetRequiredSection("ModerationBot").Bind(this); + + public List Admins { get; set; } = new(); + public bool DemoMode { get; set; } = false; +} diff --git a/ModerationBot/PolicyEngine.cs b/ModerationBot/PolicyEngine.cs new file mode 100644 index 0000000..7556fc5 --- /dev/null +++ b/ModerationBot/PolicyEngine.cs @@ -0,0 +1,268 @@ +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Security.Cryptography; +using System.Text.Json; +using System.Text.RegularExpressions; +using ArcaneLibs.Extensions; +using LibMatrix; +using LibMatrix.EventTypes.Spec; +using LibMatrix.EventTypes.Spec.State.Policy; +using LibMatrix.Helpers; +using LibMatrix.Homeservers; +using LibMatrix.RoomTypes; +using LibMatrix.Services; +using Microsoft.Extensions.Logging; +using ModerationBot.AccountData; +using ModerationBot.Services; +using ModerationBot.StateEventTypes.Policies; +using ModerationBot.StateEventTypes.Policies.Implementations; + +namespace ModerationBot; + +public class PolicyEngine(AuthenticatedHomeserverGeneric hs, ILogger logger, ModerationBotConfiguration configuration, HomeserverResolverService hsResolver, ModerationBotRoomProvider roomProvider) { + private Dictionary PolicyListAccountData { get; set; } = new(); + + // ReSharper disable once MemberCanBePrivate.Global + public List ActivePolicyLists { get; set; } = new(); + public List ActivePolicies { get; set; } = new(); + public Dictionary> ActivePoliciesByType { get; set; } = new(); + private GenericRoom? _logRoom; + private GenericRoom? _controlRoom; + + public async Task ReloadActivePolicyLists() { + var sw = Stopwatch.StartNew(); + + _logRoom = await roomProvider.GetLogRoomAsync(); + _controlRoom = await roomProvider.GetControlRoomAsync(); + + await _controlRoom?.SendMessageEventAsync(MessageFormatter.FormatSuccess("Reloading policy lists!"))!; + await _logRoom?.SendMessageEventAsync(MessageFormatter.FormatSuccess("Reloading policy lists!"))!; + + var progressMessage = await _logRoom?.SendMessageEventAsync(MessageFormatter.FormatSuccess("0/? policy lists loaded"))!; + + var policyLists = new List(); + try { + PolicyListAccountData = await hs.GetAccountDataAsync>("gay.rory.moderation_bot.policy_lists"); + } + catch (MatrixException e) { + if (e is not { ErrorCode: "M_NOT_FOUND" }) throw; + } + + // if (!PolicyListAccountData.ContainsKey(botData.DefaultPolicyRoom!)) { + // PolicyListAccountData.Add(botData.DefaultPolicyRoom!, new PolicyList() { + // Trusted = true + // }); + // await hs.SetAccountDataAsync("gay.rory.moderation_bot.policy_lists", PolicyListAccountData); + // } + + var loadTasks = new List>(); + foreach (var (roomId, policyList) in PolicyListAccountData) { + var room = hs.GetRoom(roomId); + loadTasks.Add(LoadPolicyListAsync(room, policyList)); + } + + await foreach (var policyList in loadTasks.ToAsyncEnumerable()) { + policyLists.Add(policyList); + + if (true || policyList.Policies.Count >= 256 || policyLists.Count == PolicyListAccountData.Count) { + var progressMsgContent = MessageFormatter.FormatSuccess($"{policyLists.Count}/{PolicyListAccountData.Count} policy lists loaded, " + + $"{policyLists.Sum(x => x.Policies.Count)} policies total, {sw.Elapsed} elapsed.") + .SetReplaceRelation(progressMessage.EventId); + + await _logRoom?.SendMessageEventAsync(progressMsgContent); + } + } + + // Console.WriteLine($"Reloaded policy list data in {sw.Elapsed}"); + // await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Done fetching {policyLists.Count} policy lists in {sw.Elapsed}!")); + + ActivePolicyLists = policyLists; + ActivePolicies = await GetActivePolicies(); + } + + private async Task LoadPolicyListAsync(GenericRoom room, PolicyList policyList) { + policyList.Room = room; + policyList.Policies.Clear(); + + var stateEvents = room.GetFullStateAsync(); + await foreach (var stateEvent in stateEvents) { + if (stateEvent != null && ( + stateEvent.MappedType.IsAssignableTo(typeof(BasePolicy)) + || stateEvent.MappedType.IsAssignableTo(typeof(PolicyRuleEventContent)) + )) { + policyList.Policies.Add(stateEvent); + } + } + + // if (policyList.Policies.Count >= 1) + // await _logRoom?.SendMessageEventAsync( + // MessageFormatter.FormatSuccess($"Loaded {policyList.Policies.Count} policies for {MessageFormatter.HtmlFormatMention(room.RoomId)}!"))!; + + return policyList; + } + + public async Task ReloadActivePolicyListById(string roomId) { + if (!ActivePolicyLists.Any(x => x.Room.RoomId == roomId)) return; + await LoadPolicyListAsync(hs.GetRoom(roomId), ActivePolicyLists.Single(x => x.Room.RoomId == roomId)); + ActivePolicies = await GetActivePolicies(); + } + + public async Task> GetActivePolicies() { + var sw = Stopwatch.StartNew(); + List activePolicies = new(); + + foreach (var activePolicyList in ActivePolicyLists) { + foreach (var policyEntry in activePolicyList.Policies) { + // TODO: implement rule translation + var policy = policyEntry.TypedContent is BasePolicy ? policyEntry.TypedContent as BasePolicy : policyEntry.RawContent.Deserialize(); + if (policy?.Entity is null) continue; + policy.PolicyList = activePolicyList; + policy.OriginalEvent = policyEntry; + activePolicies.Add(policy); + } + } + + Console.WriteLine($"Translated policy list data in {sw.Elapsed}"); + ActivePoliciesByType = activePolicies.GroupBy(x => x.GetType().Name).ToDictionary(x => x.Key, x => x.ToList()); + await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Translated policy list data in {sw.GetElapsedAndRestart()}")); + // await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Built policy type map in {sw.GetElapsedAndRestart()}")); + + var summary = SummariseStateTypeCounts(activePolicies.Select(x => x.OriginalEvent).ToList()); + await _logRoom?.SendMessageEventAsync(new RoomMessageEventContent() { + Body = summary.Raw, + FormattedBody = summary.Html, + Format = "org.matrix.custom.html" + })!; + + return activePolicies; + } + + public async Task> GetMatchingPolicies(StateEventResponse @event) { + List matchingPolicies = new(); + if (@event.Sender == @hs.UserId) return matchingPolicies; //ignore self at all costs + + if (ActivePoliciesByType.TryGetValue(nameof(ServerPolicyRuleEventContent), out var serverPolicies)) { + var userServer = @event.Sender.Split(':', 2)[1]; + matchingPolicies.AddRange(serverPolicies.Where(x => x.Entity == userServer)); + } + + if (ActivePoliciesByType.TryGetValue(nameof(UserPolicyRuleEventContent), out var userPolicies)) { + matchingPolicies.AddRange(userPolicies.Where(x => x.Entity == @event.Sender)); + } + + if (@event.TypedContent is RoomMessageEventContent msgContent) { + matchingPolicies.AddRange(await CheckMessageContent(@event)); + // if (msgContent.MessageType == "m.text" || msgContent.MessageType == "m.notice") ; //TODO: implement word etc. filters + if (msgContent.MessageType == "m.image" || msgContent.MessageType == "m.file" || msgContent.MessageType == "m.audio" || msgContent.MessageType == "m.video") + matchingPolicies.AddRange(await CheckMedia(@event)); + } + + return matchingPolicies; + } + +#region Policy matching + + private async Task> CheckMessageContent(StateEventResponse @event) { + var matchedRules = new List(); + var msgContent = @event.TypedContent as RoomMessageEventContent; + + if (ActivePoliciesByType.TryGetValue(nameof(MessagePolicyContainsText), out var messageContainsPolicies)) + foreach (var policy in messageContainsPolicies) { + if ((@msgContent?.Body?.ToLowerInvariant().Contains(policy.Entity.ToLowerInvariant()) ?? false) || + (@msgContent?.FormattedBody?.ToLowerInvariant().Contains(policy.Entity.ToLowerInvariant()) ?? false)) + matchedRules.Add(policy); + } + + return matchedRules; + } + + private async Task> CheckMedia(StateEventResponse @event) { + var matchedRules = new List(); + var hashAlgo = SHA3_256.Create(); + + var mxcUri = @event.RawContent["url"].GetValue(); + + //check server policies before bothering with hashes + if (ActivePoliciesByType.TryGetValue(nameof(MediaPolicyHomeserver), out var mediaHomeserverPolicies)) + foreach (var policy in mediaHomeserverPolicies) { + logger.LogInformation("Checking rule {rule}: {data}", policy.OriginalEvent.StateKey, policy.OriginalEvent.TypedContent.ToJson(ignoreNull: true, indent: false)); + policy.Entity = policy.Entity.Replace("\\*", ".*").Replace("\\?", "."); + var regex = new Regex($"mxc://({policy.Entity})/.*", RegexOptions.Compiled | RegexOptions.IgnoreCase); + if (regex.IsMatch(@event.RawContent["url"]!.GetValue())) { + logger.LogInformation("{url} matched rule {rule}", @event.RawContent["url"], policy.ToJson(ignoreNull: true)); + matchedRules.Add(policy); + // continue; + } + } + + var resolvedUri = await hsResolver.ResolveMediaUri(mxcUri.Split('/')[2], mxcUri); + var uriHash = hashAlgo.ComputeHash(mxcUri.AsBytes().ToArray()); + byte[]? fileHash = null; + + try { + fileHash = await hashAlgo.ComputeHashAsync(await hs.ClientHttpClient.GetStreamAsync(resolvedUri)); + } + catch (Exception ex) { + await _logRoom.SendMessageEventAsync( + MessageFormatter.FormatException($"Error calculating file hash for {mxcUri} via {mxcUri.Split('/')[2]} ({resolvedUri}), retrying via {hs.BaseUrl}...", + ex)); + try { + resolvedUri = await hsResolver.ResolveMediaUri(hs.BaseUrl, mxcUri); + fileHash = await hashAlgo.ComputeHashAsync(await hs.ClientHttpClient.GetStreamAsync(resolvedUri)); + } + catch (Exception ex2) { + await _logRoom.SendMessageEventAsync( + MessageFormatter.FormatException($"Error calculating file hash via {hs.BaseUrl} ({resolvedUri})!", ex2)); + } + } + + logger.LogInformation("Checking media {url} with hash {hash}", resolvedUri, fileHash); + + if (ActivePoliciesByType.ContainsKey(nameof(MediaPolicyFile))) + foreach (MediaPolicyFile policy in ActivePoliciesByType[nameof(MediaPolicyFile)]) { + logger.LogInformation("Checking rule {rule}: {data}", policy.OriginalEvent.StateKey, policy.OriginalEvent.TypedContent.ToJson(ignoreNull: true, indent: false)); + if (policy.Entity is not null && Convert.ToBase64String(uriHash).SequenceEqual(policy.Entity)) { + logger.LogInformation("{url} matched rule {rule} by uri hash", @event.RawContent["url"], policy.ToJson(ignoreNull: true)); + matchedRules.Add(policy); + // continue; + } + else logger.LogInformation("uri hash {uriHash} did not match rule's {ruleUriHash}", Convert.ToHexString(uriHash), policy.Entity); + + if (policy.FileHash is not null && fileHash is not null && policy.FileHash == Convert.ToBase64String(fileHash)) { + logger.LogInformation("{url} matched rule {rule} by file hash", @event.RawContent["url"], policy.ToJson(ignoreNull: true)); + matchedRules.Add(policy); + // continue; + } + else logger.LogInformation("file hash {fileHash} did not match rule's {ruleFileHash}", Convert.ToBase64String(fileHash), policy.FileHash); + + //check pixels every 10% of the way through the image using ImageSharp + // var image = Image.Load(await _hs._httpClient.GetStreamAsync(resolvedUri)); + } + else logger.LogInformation("No active media file policies"); + // logger.LogInformation("{url} did not match any rules", @event.RawContent["url"]); + + return matchedRules; + } + +#endregion + +#region Internal code + +#region Summarisation + + private static (string Raw, string Html) SummariseStateTypeCounts(IList states) { + string raw = "Count | State type | Mapped type", html = ""; + 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 += $"\n{stateGroup.Count} | {type} | {stateGroup[0].MappedType.Name}"; + html += $""; + } + + html += "
CountState typeMapped type
{stateGroup.Count}{type}{stateGroup[0].MappedType.Name}
"; + return (raw, html); + } + +#endregion + +#endregion +} \ No newline at end of file diff --git a/ModerationBot/PolicyList.cs b/ModerationBot/PolicyList.cs new file mode 100644 index 0000000..f291c7b --- /dev/null +++ b/ModerationBot/PolicyList.cs @@ -0,0 +1,16 @@ +using System.Text.Json.Serialization; +using LibMatrix; +using LibMatrix.RoomTypes; + +namespace ModerationBot; + +public class PolicyList { + [JsonIgnore] + public GenericRoom Room { get; set; } + + [JsonPropertyName("trusted")] + public bool Trusted { get; set; } = false; + + [JsonIgnore] + public List Policies { get; set; } = new(); +} diff --git a/ModerationBot/Program.cs b/ModerationBot/Program.cs new file mode 100644 index 0000000..67403c1 --- /dev/null +++ b/ModerationBot/Program.cs @@ -0,0 +1,42 @@ +// See https://aka.ms/new-console-template for more information + +using LibMatrix.Services; +using LibMatrix.Utilities.Bot; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using ModerationBot; +using ModerationBot.Services; + +Console.WriteLine("Hello, World!"); + +var builder = Host.CreateDefaultBuilder(args); + +builder.ConfigureHostOptions(host => { + host.ServicesStartConcurrently = true; + host.ServicesStopConcurrently = true; + host.ShutdownTimeout = TimeSpan.FromSeconds(5); +}); + +if (Environment.GetEnvironmentVariable("MODERATIONBOT_APPSETTINGS_PATH") is string path) + builder.ConfigureAppConfiguration(x => x.AddJsonFile(path)); + +var host = builder.ConfigureServices((_, services) => { + services.AddScoped(x => + new TieredStorageService( + cacheStorageProvider: new FileStorageProvider("bot_data/cache/"), + dataStorageProvider: new FileStorageProvider("bot_data/data/") + ) + ); + services.AddSingleton(); + + services.AddRoryLibMatrixServices(); + services.AddMatrixBot().AddCommandHandler().DiscoverAllCommands(); + + services.AddSingleton(); + services.AddSingleton(); + + services.AddHostedService(); +}).UseConsoleLifetime().Build(); + +await host.RunAsync(); \ No newline at end of file diff --git a/ModerationBot/Properties/launchSettings.json b/ModerationBot/Properties/launchSettings.json new file mode 100644 index 0000000..997e294 --- /dev/null +++ b/ModerationBot/Properties/launchSettings.json @@ -0,0 +1,26 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "Default": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + + } + }, + "Development": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + }, + "Local config": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Local" + } + } + } +} diff --git a/ModerationBot/Services/ModerationBotRoomProvider.cs b/ModerationBot/Services/ModerationBotRoomProvider.cs new file mode 100644 index 0000000..d4a200f --- /dev/null +++ b/ModerationBot/Services/ModerationBotRoomProvider.cs @@ -0,0 +1,76 @@ +using System.Diagnostics.CodeAnalysis; +using LibMatrix.Homeservers; +using LibMatrix.Responses; +using LibMatrix.RoomTypes; +using ModerationBot.AccountData; + +namespace ModerationBot.Services; + +public class ModerationBotRoomProvider(AuthenticatedHomeserverGeneric hs, ModerationBotConfiguration cfg) { + private BotData? _botData; + + public BotData? BotData { + get { + if (BotDataExpiry >= DateTime.UtcNow) return _botData; + Console.WriteLine("BotData expired!"); + return null; + } + set { + _botData = value; + Console.WriteLine("BotData updated!"); + BotDataExpiry = DateTime.UtcNow.AddMinutes(5); + } + } + + private DateTime BotDataExpiry { get; set; } + + [MemberNotNull(nameof(BotData))] + private async Task GetBotDataAsync() { + try { + BotData ??= await hs.GetAccountDataAsync(BotData.EventId); + } + catch (Exception e) { + Console.WriteLine(e); + await hs.SetAccountDataAsync(BotData.EventId, new BotData()); + return await GetBotDataAsync(); + } + + if (BotData == null) + throw new NullReferenceException("BotData is null!"); + + return BotData; + } + + public async Task GetControlRoomAsync() { + var botData = await GetBotDataAsync(); + if (botData.ControlRoom == null) { + var createRoomRequest = CreateRoomRequest.CreatePrivate(hs, "Rory&::ModerationBot - Control Room"); + createRoomRequest.Invite = cfg.Admins; + var newRoom = await hs.CreateRoom(createRoomRequest, true, true, true); + BotData.ControlRoom = newRoom.RoomId; + await hs.SetAccountDataAsync(BotData.EventId, BotData); + } + + return hs.GetRoom(BotData.ControlRoom!); + } + + public async Task GetLogRoomAsync() { + var botData = await GetBotDataAsync(); + if (botData.LogRoom == null) { + var controlRoom = await GetControlRoomAsync(); + var createRoomRequest = CreateRoomRequest.CreatePrivate(hs, "Rory&::ModerationBot - Log Room"); + createRoomRequest.Invite = (await controlRoom.GetMembersListAsync()).Select(x=>x.StateKey).ToList(); + var newRoom = await hs.CreateRoom(createRoomRequest, true, true, true); + BotData.LogRoom = newRoom.RoomId; + await hs.SetAccountDataAsync(BotData.EventId, BotData); + } + + return hs.GetRoom(BotData.LogRoom!); + } + + public async Task GetDefaultPolicyRoomAsync() { + var botData = await GetBotDataAsync(); + + return string.IsNullOrWhiteSpace(botData.DefaultPolicyRoom) ? null : hs.GetRoom(BotData.DefaultPolicyRoom!); + } +} \ No newline at end of file diff --git a/ModerationBot/StateEventTypes/Policies/BasePolicy.cs b/ModerationBot/StateEventTypes/Policies/BasePolicy.cs new file mode 100644 index 0000000..64b0448 --- /dev/null +++ b/ModerationBot/StateEventTypes/Policies/BasePolicy.cs @@ -0,0 +1,53 @@ +using System.ComponentModel.DataAnnotations; +using System.Text.Json.Serialization; +using LibMatrix; +using LibMatrix.EventTypes; +using LibMatrix.Interfaces; + +namespace ModerationBot.StateEventTypes.Policies; + +public abstract class BasePolicy : EventContent { + /// + /// Entity this policy applies to, null if event was redacted + /// + [JsonPropertyName("entity")] + public string? Entity { get; set; } + + /// + /// Reason this policy exists + /// + [JsonPropertyName("reason")] + public string? Reason { get; set; } + + /// + /// Suggested action to take, one of `ban`, `kick`, `mute`, `redact`, `spoiler`, `warn` or `warn_admins` + /// + [JsonPropertyName("recommendation")] + [AllowedValues("ban", "kick", "mute", "redact", "spoiler", "warn", "warn_admins")] + public string Recommendation { get; set; } = "warn"; + + /// + /// Expiry time in milliseconds since the unix epoch, or null if the ban has no expiry. + /// + [JsonPropertyName("support.feline.policy.expiry.rev.2")] //stable prefix: expiry, msc pending + public long? Expiry { get; set; } + + //utils + /// + /// Readable expiry time, provided for easy interaction + /// + [JsonPropertyName("gay.rory.matrix_room_utils.readable_expiry_time_utc")] + public DateTime? ExpiryDateTime { + get => Expiry == null ? null : DateTimeOffset.FromUnixTimeMilliseconds(Expiry.Value).DateTime; + set => Expiry = value is null ? null : ((DateTimeOffset)value).ToUnixTimeMilliseconds(); + } + + #region Internal metadata + + [JsonIgnore] + public PolicyList PolicyList { get; set; } + + public StateEventResponse OriginalEvent { get; set; } + + #endregion +} diff --git a/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyFile.cs b/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyFile.cs new file mode 100644 index 0000000..f0fef63 --- /dev/null +++ b/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyFile.cs @@ -0,0 +1,16 @@ +using System.Text.Json.Serialization; +using LibMatrix.EventTypes; + +namespace ModerationBot.StateEventTypes.Policies.Implementations; + +/// +/// File policy event, entity is the MXC URI of the file, hashed with SHA3-256. +/// +[MatrixEvent(EventName = "gay.rory.moderation.rule.media")] +public class MediaPolicyFile : BasePolicy { + /// + /// Hash of the file, hashed with SHA3-256. + /// + [JsonPropertyName("file_hash")] + public string? FileHash { get; set; } +} diff --git a/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs b/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs new file mode 100644 index 0000000..72c9a60 --- /dev/null +++ b/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs @@ -0,0 +1,10 @@ +using LibMatrix.EventTypes; + +namespace ModerationBot.StateEventTypes.Policies.Implementations; + +/// +/// Homeserver media policy event, entity is the MXC URI of the file, hashed with SHA3-256. +/// +[MatrixEvent(EventName = "gay.rory.moderation.rule.media.homeserver")] +public class MediaPolicyHomeserver : BasePolicy { +} diff --git a/ModerationBot/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs b/ModerationBot/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs new file mode 100644 index 0000000..fa21e2d --- /dev/null +++ b/ModerationBot/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs @@ -0,0 +1,10 @@ +using LibMatrix.EventTypes; + +namespace ModerationBot.StateEventTypes.Policies.Implementations; + +/// +/// Text contains policy event, entity is the text to contain. +/// +[MatrixEvent(EventName = "gay.rory.moderation.rule.text.contains")] +public class MessagePolicyContainsText : BasePolicy { +} diff --git a/ModerationBot/StateEventTypes/Policies/Implementations/UnknownPolicy.cs b/ModerationBot/StateEventTypes/Policies/Implementations/UnknownPolicy.cs new file mode 100644 index 0000000..78860ca --- /dev/null +++ b/ModerationBot/StateEventTypes/Policies/Implementations/UnknownPolicy.cs @@ -0,0 +1,7 @@ +namespace ModerationBot.StateEventTypes.Policies.Implementations; + +/// +/// Unknown policy event, usually used for handling unknown cases +/// +public class UnknownPolicy : BasePolicy { +} diff --git a/ModerationBot/appsettings.Development.json b/ModerationBot/appsettings.Development.json new file mode 100644 index 0000000..224d0da --- /dev/null +++ b/ModerationBot/appsettings.Development.json @@ -0,0 +1,24 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + }, + "LibMatrixBot": { + // The homeserver to connect to + "Homeserver": "rory.gay", + // The access token to use + "AccessToken": "syt_xxxxxxxxxxxxxxxxx", + // The command prefix + "Prefix": "?" + }, + "ModerationBot": { + // List of people who should be invited to the control room + "Admins": [ + "@emma:conduit.rory.gay", + "@emma:rory.gay" + ] + } +} diff --git a/ModerationBot/appsettings.json b/ModerationBot/appsettings.json new file mode 100644 index 0000000..6ba02f3 --- /dev/null +++ b/ModerationBot/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/ModerationBotConfiguration.cs b/ModerationBotConfiguration.cs deleted file mode 100644 index 415b581..0000000 --- a/ModerationBotConfiguration.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Microsoft.Extensions.Configuration; - -namespace ModerationBot; - -public class ModerationBotConfiguration { - public ModerationBotConfiguration(IConfiguration config) => config.GetRequiredSection("ModerationBot").Bind(this); - - public List Admins { get; set; } = new(); - public bool DemoMode { get; set; } = false; -} diff --git a/PolicyEngine.cs b/PolicyEngine.cs deleted file mode 100644 index 7556fc5..0000000 --- a/PolicyEngine.cs +++ /dev/null @@ -1,268 +0,0 @@ -using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; -using System.Security.Cryptography; -using System.Text.Json; -using System.Text.RegularExpressions; -using ArcaneLibs.Extensions; -using LibMatrix; -using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State.Policy; -using LibMatrix.Helpers; -using LibMatrix.Homeservers; -using LibMatrix.RoomTypes; -using LibMatrix.Services; -using Microsoft.Extensions.Logging; -using ModerationBot.AccountData; -using ModerationBot.Services; -using ModerationBot.StateEventTypes.Policies; -using ModerationBot.StateEventTypes.Policies.Implementations; - -namespace ModerationBot; - -public class PolicyEngine(AuthenticatedHomeserverGeneric hs, ILogger logger, ModerationBotConfiguration configuration, HomeserverResolverService hsResolver, ModerationBotRoomProvider roomProvider) { - private Dictionary PolicyListAccountData { get; set; } = new(); - - // ReSharper disable once MemberCanBePrivate.Global - public List ActivePolicyLists { get; set; } = new(); - public List ActivePolicies { get; set; } = new(); - public Dictionary> ActivePoliciesByType { get; set; } = new(); - private GenericRoom? _logRoom; - private GenericRoom? _controlRoom; - - public async Task ReloadActivePolicyLists() { - var sw = Stopwatch.StartNew(); - - _logRoom = await roomProvider.GetLogRoomAsync(); - _controlRoom = await roomProvider.GetControlRoomAsync(); - - await _controlRoom?.SendMessageEventAsync(MessageFormatter.FormatSuccess("Reloading policy lists!"))!; - await _logRoom?.SendMessageEventAsync(MessageFormatter.FormatSuccess("Reloading policy lists!"))!; - - var progressMessage = await _logRoom?.SendMessageEventAsync(MessageFormatter.FormatSuccess("0/? policy lists loaded"))!; - - var policyLists = new List(); - try { - PolicyListAccountData = await hs.GetAccountDataAsync>("gay.rory.moderation_bot.policy_lists"); - } - catch (MatrixException e) { - if (e is not { ErrorCode: "M_NOT_FOUND" }) throw; - } - - // if (!PolicyListAccountData.ContainsKey(botData.DefaultPolicyRoom!)) { - // PolicyListAccountData.Add(botData.DefaultPolicyRoom!, new PolicyList() { - // Trusted = true - // }); - // await hs.SetAccountDataAsync("gay.rory.moderation_bot.policy_lists", PolicyListAccountData); - // } - - var loadTasks = new List>(); - foreach (var (roomId, policyList) in PolicyListAccountData) { - var room = hs.GetRoom(roomId); - loadTasks.Add(LoadPolicyListAsync(room, policyList)); - } - - await foreach (var policyList in loadTasks.ToAsyncEnumerable()) { - policyLists.Add(policyList); - - if (true || policyList.Policies.Count >= 256 || policyLists.Count == PolicyListAccountData.Count) { - var progressMsgContent = MessageFormatter.FormatSuccess($"{policyLists.Count}/{PolicyListAccountData.Count} policy lists loaded, " + - $"{policyLists.Sum(x => x.Policies.Count)} policies total, {sw.Elapsed} elapsed.") - .SetReplaceRelation(progressMessage.EventId); - - await _logRoom?.SendMessageEventAsync(progressMsgContent); - } - } - - // Console.WriteLine($"Reloaded policy list data in {sw.Elapsed}"); - // await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Done fetching {policyLists.Count} policy lists in {sw.Elapsed}!")); - - ActivePolicyLists = policyLists; - ActivePolicies = await GetActivePolicies(); - } - - private async Task LoadPolicyListAsync(GenericRoom room, PolicyList policyList) { - policyList.Room = room; - policyList.Policies.Clear(); - - var stateEvents = room.GetFullStateAsync(); - await foreach (var stateEvent in stateEvents) { - if (stateEvent != null && ( - stateEvent.MappedType.IsAssignableTo(typeof(BasePolicy)) - || stateEvent.MappedType.IsAssignableTo(typeof(PolicyRuleEventContent)) - )) { - policyList.Policies.Add(stateEvent); - } - } - - // if (policyList.Policies.Count >= 1) - // await _logRoom?.SendMessageEventAsync( - // MessageFormatter.FormatSuccess($"Loaded {policyList.Policies.Count} policies for {MessageFormatter.HtmlFormatMention(room.RoomId)}!"))!; - - return policyList; - } - - public async Task ReloadActivePolicyListById(string roomId) { - if (!ActivePolicyLists.Any(x => x.Room.RoomId == roomId)) return; - await LoadPolicyListAsync(hs.GetRoom(roomId), ActivePolicyLists.Single(x => x.Room.RoomId == roomId)); - ActivePolicies = await GetActivePolicies(); - } - - public async Task> GetActivePolicies() { - var sw = Stopwatch.StartNew(); - List activePolicies = new(); - - foreach (var activePolicyList in ActivePolicyLists) { - foreach (var policyEntry in activePolicyList.Policies) { - // TODO: implement rule translation - var policy = policyEntry.TypedContent is BasePolicy ? policyEntry.TypedContent as BasePolicy : policyEntry.RawContent.Deserialize(); - if (policy?.Entity is null) continue; - policy.PolicyList = activePolicyList; - policy.OriginalEvent = policyEntry; - activePolicies.Add(policy); - } - } - - Console.WriteLine($"Translated policy list data in {sw.Elapsed}"); - ActivePoliciesByType = activePolicies.GroupBy(x => x.GetType().Name).ToDictionary(x => x.Key, x => x.ToList()); - await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Translated policy list data in {sw.GetElapsedAndRestart()}")); - // await _logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Built policy type map in {sw.GetElapsedAndRestart()}")); - - var summary = SummariseStateTypeCounts(activePolicies.Select(x => x.OriginalEvent).ToList()); - await _logRoom?.SendMessageEventAsync(new RoomMessageEventContent() { - Body = summary.Raw, - FormattedBody = summary.Html, - Format = "org.matrix.custom.html" - })!; - - return activePolicies; - } - - public async Task> GetMatchingPolicies(StateEventResponse @event) { - List matchingPolicies = new(); - if (@event.Sender == @hs.UserId) return matchingPolicies; //ignore self at all costs - - if (ActivePoliciesByType.TryGetValue(nameof(ServerPolicyRuleEventContent), out var serverPolicies)) { - var userServer = @event.Sender.Split(':', 2)[1]; - matchingPolicies.AddRange(serverPolicies.Where(x => x.Entity == userServer)); - } - - if (ActivePoliciesByType.TryGetValue(nameof(UserPolicyRuleEventContent), out var userPolicies)) { - matchingPolicies.AddRange(userPolicies.Where(x => x.Entity == @event.Sender)); - } - - if (@event.TypedContent is RoomMessageEventContent msgContent) { - matchingPolicies.AddRange(await CheckMessageContent(@event)); - // if (msgContent.MessageType == "m.text" || msgContent.MessageType == "m.notice") ; //TODO: implement word etc. filters - if (msgContent.MessageType == "m.image" || msgContent.MessageType == "m.file" || msgContent.MessageType == "m.audio" || msgContent.MessageType == "m.video") - matchingPolicies.AddRange(await CheckMedia(@event)); - } - - return matchingPolicies; - } - -#region Policy matching - - private async Task> CheckMessageContent(StateEventResponse @event) { - var matchedRules = new List(); - var msgContent = @event.TypedContent as RoomMessageEventContent; - - if (ActivePoliciesByType.TryGetValue(nameof(MessagePolicyContainsText), out var messageContainsPolicies)) - foreach (var policy in messageContainsPolicies) { - if ((@msgContent?.Body?.ToLowerInvariant().Contains(policy.Entity.ToLowerInvariant()) ?? false) || - (@msgContent?.FormattedBody?.ToLowerInvariant().Contains(policy.Entity.ToLowerInvariant()) ?? false)) - matchedRules.Add(policy); - } - - return matchedRules; - } - - private async Task> CheckMedia(StateEventResponse @event) { - var matchedRules = new List(); - var hashAlgo = SHA3_256.Create(); - - var mxcUri = @event.RawContent["url"].GetValue(); - - //check server policies before bothering with hashes - if (ActivePoliciesByType.TryGetValue(nameof(MediaPolicyHomeserver), out var mediaHomeserverPolicies)) - foreach (var policy in mediaHomeserverPolicies) { - logger.LogInformation("Checking rule {rule}: {data}", policy.OriginalEvent.StateKey, policy.OriginalEvent.TypedContent.ToJson(ignoreNull: true, indent: false)); - policy.Entity = policy.Entity.Replace("\\*", ".*").Replace("\\?", "."); - var regex = new Regex($"mxc://({policy.Entity})/.*", RegexOptions.Compiled | RegexOptions.IgnoreCase); - if (regex.IsMatch(@event.RawContent["url"]!.GetValue())) { - logger.LogInformation("{url} matched rule {rule}", @event.RawContent["url"], policy.ToJson(ignoreNull: true)); - matchedRules.Add(policy); - // continue; - } - } - - var resolvedUri = await hsResolver.ResolveMediaUri(mxcUri.Split('/')[2], mxcUri); - var uriHash = hashAlgo.ComputeHash(mxcUri.AsBytes().ToArray()); - byte[]? fileHash = null; - - try { - fileHash = await hashAlgo.ComputeHashAsync(await hs.ClientHttpClient.GetStreamAsync(resolvedUri)); - } - catch (Exception ex) { - await _logRoom.SendMessageEventAsync( - MessageFormatter.FormatException($"Error calculating file hash for {mxcUri} via {mxcUri.Split('/')[2]} ({resolvedUri}), retrying via {hs.BaseUrl}...", - ex)); - try { - resolvedUri = await hsResolver.ResolveMediaUri(hs.BaseUrl, mxcUri); - fileHash = await hashAlgo.ComputeHashAsync(await hs.ClientHttpClient.GetStreamAsync(resolvedUri)); - } - catch (Exception ex2) { - await _logRoom.SendMessageEventAsync( - MessageFormatter.FormatException($"Error calculating file hash via {hs.BaseUrl} ({resolvedUri})!", ex2)); - } - } - - logger.LogInformation("Checking media {url} with hash {hash}", resolvedUri, fileHash); - - if (ActivePoliciesByType.ContainsKey(nameof(MediaPolicyFile))) - foreach (MediaPolicyFile policy in ActivePoliciesByType[nameof(MediaPolicyFile)]) { - logger.LogInformation("Checking rule {rule}: {data}", policy.OriginalEvent.StateKey, policy.OriginalEvent.TypedContent.ToJson(ignoreNull: true, indent: false)); - if (policy.Entity is not null && Convert.ToBase64String(uriHash).SequenceEqual(policy.Entity)) { - logger.LogInformation("{url} matched rule {rule} by uri hash", @event.RawContent["url"], policy.ToJson(ignoreNull: true)); - matchedRules.Add(policy); - // continue; - } - else logger.LogInformation("uri hash {uriHash} did not match rule's {ruleUriHash}", Convert.ToHexString(uriHash), policy.Entity); - - if (policy.FileHash is not null && fileHash is not null && policy.FileHash == Convert.ToBase64String(fileHash)) { - logger.LogInformation("{url} matched rule {rule} by file hash", @event.RawContent["url"], policy.ToJson(ignoreNull: true)); - matchedRules.Add(policy); - // continue; - } - else logger.LogInformation("file hash {fileHash} did not match rule's {ruleFileHash}", Convert.ToBase64String(fileHash), policy.FileHash); - - //check pixels every 10% of the way through the image using ImageSharp - // var image = Image.Load(await _hs._httpClient.GetStreamAsync(resolvedUri)); - } - else logger.LogInformation("No active media file policies"); - // logger.LogInformation("{url} did not match any rules", @event.RawContent["url"]); - - return matchedRules; - } - -#endregion - -#region Internal code - -#region Summarisation - - private static (string Raw, string Html) SummariseStateTypeCounts(IList states) { - string raw = "Count | State type | Mapped type", html = ""; - 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 += $"\n{stateGroup.Count} | {type} | {stateGroup[0].MappedType.Name}"; - html += $""; - } - - html += "
CountState typeMapped type
{stateGroup.Count}{type}{stateGroup[0].MappedType.Name}
"; - return (raw, html); - } - -#endregion - -#endregion -} \ No newline at end of file diff --git a/PolicyList.cs b/PolicyList.cs deleted file mode 100644 index f291c7b..0000000 --- a/PolicyList.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Text.Json.Serialization; -using LibMatrix; -using LibMatrix.RoomTypes; - -namespace ModerationBot; - -public class PolicyList { - [JsonIgnore] - public GenericRoom Room { get; set; } - - [JsonPropertyName("trusted")] - public bool Trusted { get; set; } = false; - - [JsonIgnore] - public List Policies { get; set; } = new(); -} diff --git a/Program.cs b/Program.cs deleted file mode 100644 index 67403c1..0000000 --- a/Program.cs +++ /dev/null @@ -1,42 +0,0 @@ -// See https://aka.ms/new-console-template for more information - -using LibMatrix.Services; -using LibMatrix.Utilities.Bot; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using ModerationBot; -using ModerationBot.Services; - -Console.WriteLine("Hello, World!"); - -var builder = Host.CreateDefaultBuilder(args); - -builder.ConfigureHostOptions(host => { - host.ServicesStartConcurrently = true; - host.ServicesStopConcurrently = true; - host.ShutdownTimeout = TimeSpan.FromSeconds(5); -}); - -if (Environment.GetEnvironmentVariable("MODERATIONBOT_APPSETTINGS_PATH") is string path) - builder.ConfigureAppConfiguration(x => x.AddJsonFile(path)); - -var host = builder.ConfigureServices((_, services) => { - services.AddScoped(x => - new TieredStorageService( - cacheStorageProvider: new FileStorageProvider("bot_data/cache/"), - dataStorageProvider: new FileStorageProvider("bot_data/data/") - ) - ); - services.AddSingleton(); - - services.AddRoryLibMatrixServices(); - services.AddMatrixBot().AddCommandHandler().DiscoverAllCommands(); - - services.AddSingleton(); - services.AddSingleton(); - - services.AddHostedService(); -}).UseConsoleLifetime().Build(); - -await host.RunAsync(); \ No newline at end of file diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json deleted file mode 100644 index 997e294..0000000 --- a/Properties/launchSettings.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "$schema": "http://json.schemastore.org/launchsettings.json", - "profiles": { - "Default": { - "commandName": "Project", - "dotnetRunMessages": true, - "environmentVariables": { - - } - }, - "Development": { - "commandName": "Project", - "dotnetRunMessages": true, - "environmentVariables": { - "DOTNET_ENVIRONMENT": "Development" - } - }, - "Local config": { - "commandName": "Project", - "dotnetRunMessages": true, - "environmentVariables": { - "DOTNET_ENVIRONMENT": "Local" - } - } - } -} diff --git a/Services/ModerationBotRoomProvider.cs b/Services/ModerationBotRoomProvider.cs deleted file mode 100644 index d4a200f..0000000 --- a/Services/ModerationBotRoomProvider.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using LibMatrix.Homeservers; -using LibMatrix.Responses; -using LibMatrix.RoomTypes; -using ModerationBot.AccountData; - -namespace ModerationBot.Services; - -public class ModerationBotRoomProvider(AuthenticatedHomeserverGeneric hs, ModerationBotConfiguration cfg) { - private BotData? _botData; - - public BotData? BotData { - get { - if (BotDataExpiry >= DateTime.UtcNow) return _botData; - Console.WriteLine("BotData expired!"); - return null; - } - set { - _botData = value; - Console.WriteLine("BotData updated!"); - BotDataExpiry = DateTime.UtcNow.AddMinutes(5); - } - } - - private DateTime BotDataExpiry { get; set; } - - [MemberNotNull(nameof(BotData))] - private async Task GetBotDataAsync() { - try { - BotData ??= await hs.GetAccountDataAsync(BotData.EventId); - } - catch (Exception e) { - Console.WriteLine(e); - await hs.SetAccountDataAsync(BotData.EventId, new BotData()); - return await GetBotDataAsync(); - } - - if (BotData == null) - throw new NullReferenceException("BotData is null!"); - - return BotData; - } - - public async Task GetControlRoomAsync() { - var botData = await GetBotDataAsync(); - if (botData.ControlRoom == null) { - var createRoomRequest = CreateRoomRequest.CreatePrivate(hs, "Rory&::ModerationBot - Control Room"); - createRoomRequest.Invite = cfg.Admins; - var newRoom = await hs.CreateRoom(createRoomRequest, true, true, true); - BotData.ControlRoom = newRoom.RoomId; - await hs.SetAccountDataAsync(BotData.EventId, BotData); - } - - return hs.GetRoom(BotData.ControlRoom!); - } - - public async Task GetLogRoomAsync() { - var botData = await GetBotDataAsync(); - if (botData.LogRoom == null) { - var controlRoom = await GetControlRoomAsync(); - var createRoomRequest = CreateRoomRequest.CreatePrivate(hs, "Rory&::ModerationBot - Log Room"); - createRoomRequest.Invite = (await controlRoom.GetMembersListAsync()).Select(x=>x.StateKey).ToList(); - var newRoom = await hs.CreateRoom(createRoomRequest, true, true, true); - BotData.LogRoom = newRoom.RoomId; - await hs.SetAccountDataAsync(BotData.EventId, BotData); - } - - return hs.GetRoom(BotData.LogRoom!); - } - - public async Task GetDefaultPolicyRoomAsync() { - var botData = await GetBotDataAsync(); - - return string.IsNullOrWhiteSpace(botData.DefaultPolicyRoom) ? null : hs.GetRoom(BotData.DefaultPolicyRoom!); - } -} \ No newline at end of file diff --git a/StateEventTypes/Policies/BasePolicy.cs b/StateEventTypes/Policies/BasePolicy.cs deleted file mode 100644 index 64b0448..0000000 --- a/StateEventTypes/Policies/BasePolicy.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using System.Text.Json.Serialization; -using LibMatrix; -using LibMatrix.EventTypes; -using LibMatrix.Interfaces; - -namespace ModerationBot.StateEventTypes.Policies; - -public abstract class BasePolicy : EventContent { - /// - /// Entity this policy applies to, null if event was redacted - /// - [JsonPropertyName("entity")] - public string? Entity { get; set; } - - /// - /// Reason this policy exists - /// - [JsonPropertyName("reason")] - public string? Reason { get; set; } - - /// - /// Suggested action to take, one of `ban`, `kick`, `mute`, `redact`, `spoiler`, `warn` or `warn_admins` - /// - [JsonPropertyName("recommendation")] - [AllowedValues("ban", "kick", "mute", "redact", "spoiler", "warn", "warn_admins")] - public string Recommendation { get; set; } = "warn"; - - /// - /// Expiry time in milliseconds since the unix epoch, or null if the ban has no expiry. - /// - [JsonPropertyName("support.feline.policy.expiry.rev.2")] //stable prefix: expiry, msc pending - public long? Expiry { get; set; } - - //utils - /// - /// Readable expiry time, provided for easy interaction - /// - [JsonPropertyName("gay.rory.matrix_room_utils.readable_expiry_time_utc")] - public DateTime? ExpiryDateTime { - get => Expiry == null ? null : DateTimeOffset.FromUnixTimeMilliseconds(Expiry.Value).DateTime; - set => Expiry = value is null ? null : ((DateTimeOffset)value).ToUnixTimeMilliseconds(); - } - - #region Internal metadata - - [JsonIgnore] - public PolicyList PolicyList { get; set; } - - public StateEventResponse OriginalEvent { get; set; } - - #endregion -} diff --git a/StateEventTypes/Policies/Implementations/MediaPolicyFile.cs b/StateEventTypes/Policies/Implementations/MediaPolicyFile.cs deleted file mode 100644 index f0fef63..0000000 --- a/StateEventTypes/Policies/Implementations/MediaPolicyFile.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Text.Json.Serialization; -using LibMatrix.EventTypes; - -namespace ModerationBot.StateEventTypes.Policies.Implementations; - -/// -/// File policy event, entity is the MXC URI of the file, hashed with SHA3-256. -/// -[MatrixEvent(EventName = "gay.rory.moderation.rule.media")] -public class MediaPolicyFile : BasePolicy { - /// - /// Hash of the file, hashed with SHA3-256. - /// - [JsonPropertyName("file_hash")] - public string? FileHash { get; set; } -} diff --git a/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs b/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs deleted file mode 100644 index 72c9a60..0000000 --- a/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs +++ /dev/null @@ -1,10 +0,0 @@ -using LibMatrix.EventTypes; - -namespace ModerationBot.StateEventTypes.Policies.Implementations; - -/// -/// Homeserver media policy event, entity is the MXC URI of the file, hashed with SHA3-256. -/// -[MatrixEvent(EventName = "gay.rory.moderation.rule.media.homeserver")] -public class MediaPolicyHomeserver : BasePolicy { -} diff --git a/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs b/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs deleted file mode 100644 index fa21e2d..0000000 --- a/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs +++ /dev/null @@ -1,10 +0,0 @@ -using LibMatrix.EventTypes; - -namespace ModerationBot.StateEventTypes.Policies.Implementations; - -/// -/// Text contains policy event, entity is the text to contain. -/// -[MatrixEvent(EventName = "gay.rory.moderation.rule.text.contains")] -public class MessagePolicyContainsText : BasePolicy { -} diff --git a/StateEventTypes/Policies/Implementations/UnknownPolicy.cs b/StateEventTypes/Policies/Implementations/UnknownPolicy.cs deleted file mode 100644 index 78860ca..0000000 --- a/StateEventTypes/Policies/Implementations/UnknownPolicy.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ModerationBot.StateEventTypes.Policies.Implementations; - -/// -/// Unknown policy event, usually used for handling unknown cases -/// -public class UnknownPolicy : BasePolicy { -} diff --git a/appsettings.Development.json b/appsettings.Development.json deleted file mode 100644 index 224d0da..0000000 --- a/appsettings.Development.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - }, - "LibMatrixBot": { - // The homeserver to connect to - "Homeserver": "rory.gay", - // The access token to use - "AccessToken": "syt_xxxxxxxxxxxxxxxxx", - // The command prefix - "Prefix": "?" - }, - "ModerationBot": { - // List of people who should be invited to the control room - "Admins": [ - "@emma:conduit.rory.gay", - "@emma:rory.gay" - ] - } -} diff --git a/appsettings.json b/appsettings.json deleted file mode 100644 index 6ba02f3..0000000 --- a/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - } -} -- cgit 1.5.1