about summary refs log tree commit diff
path: root/ExampleBots/ModerationBot
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2023-12-14 07:20:46 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2023-12-14 07:20:46 +0100
commit5affd9f061e75f6575a2fe6715f9e8757cfe87e8 (patch)
tree13ea35ce981094a960746777a16dff8815c45e55 /ExampleBots/ModerationBot
parentTemp state (diff)
downloadLibMatrix-5affd9f061e75f6575a2fe6715f9e8757cfe87e8.tar.xz
Cleanup
Diffstat (limited to 'ExampleBots/ModerationBot')
-rw-r--r--ExampleBots/ModerationBot/Commands/BanMediaCommand.cs2
-rw-r--r--ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs5
-rw-r--r--ExampleBots/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs5
-rw-r--r--ExampleBots/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs4
-rw-r--r--ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs5
-rw-r--r--ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs4
-rw-r--r--ExampleBots/ModerationBot/FirstRunTasks.cs1
-rw-r--r--ExampleBots/ModerationBot/ModerationBot.cs19
-rw-r--r--ExampleBots/ModerationBot/PolicyEngine.cs15
-rw-r--r--ExampleBots/ModerationBot/PolicyList.cs1
-rw-r--r--ExampleBots/ModerationBot/StateEventTypes/Policies/BasePolicy.cs1
-rw-r--r--ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs1
-rw-r--r--ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs1
-rw-r--r--ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/UnknownPolicy.cs3
14 files changed, 16 insertions, 51 deletions
diff --git a/ExampleBots/ModerationBot/Commands/BanMediaCommand.cs b/ExampleBots/ModerationBot/Commands/BanMediaCommand.cs
index 9e49b22..535fd4f 100644
--- a/ExampleBots/ModerationBot/Commands/BanMediaCommand.cs
+++ b/ExampleBots/ModerationBot/Commands/BanMediaCommand.cs
@@ -1,4 +1,3 @@
-using System.Buffers.Text;
 using System.Security.Cryptography;
 using ArcaneLibs.Extensions;
 using LibMatrix;
@@ -7,7 +6,6 @@ using LibMatrix.Helpers;
 using LibMatrix.Services;
 using LibMatrix.Utilities.Bot.Interfaces;
 using ModerationBot.AccountData;
-using ModerationBot.StateEventTypes;
 using ModerationBot.StateEventTypes.Policies.Implementations;
 
 namespace ModerationBot.Commands;
diff --git a/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs b/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs
index 327a9a4..f578f53 100644
--- a/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs
+++ b/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs
@@ -1,14 +1,9 @@
-using System.Buffers.Text;
-using System.Security.Cryptography;
-using ArcaneLibs.Extensions;
-using LibMatrix;
 using LibMatrix.EventTypes.Spec;
 using LibMatrix.Helpers;
 using LibMatrix.RoomTypes;
 using LibMatrix.Services;
 using LibMatrix.Utilities.Bot.Interfaces;
 using ModerationBot.AccountData;
-using ModerationBot.StateEventTypes;
 
 namespace ModerationBot.Commands;
 
diff --git a/ExampleBots/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs b/ExampleBots/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs
index 35c95f8..285d792 100644
--- a/ExampleBots/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs
+++ b/ExampleBots/ModerationBot/Commands/DbgDumpActivePoliciesCommand.cs
@@ -1,14 +1,9 @@
-using System.Buffers.Text;
-using System.Security.Cryptography;
 using ArcaneLibs.Extensions;
-using LibMatrix;
 using LibMatrix.EventTypes.Spec;
-using LibMatrix.Helpers;
 using LibMatrix.RoomTypes;
 using LibMatrix.Services;
 using LibMatrix.Utilities.Bot.Interfaces;
 using ModerationBot.AccountData;
-using ModerationBot.StateEventTypes;
 
 namespace ModerationBot.Commands;
 
diff --git a/ExampleBots/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs b/ExampleBots/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs
index 0013065..3727877 100644
--- a/ExampleBots/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs
+++ b/ExampleBots/ModerationBot/Commands/DbgDumpAllStateTypesCommand.cs
@@ -1,14 +1,10 @@
-using System.Buffers.Text;
-using System.Security.Cryptography;
 using ArcaneLibs.Extensions;
 using LibMatrix;
 using LibMatrix.EventTypes.Spec;
-using LibMatrix.Helpers;
 using LibMatrix.RoomTypes;
 using LibMatrix.Services;
 using LibMatrix.Utilities.Bot.Interfaces;
 using ModerationBot.AccountData;
-using ModerationBot.StateEventTypes;
 
 namespace ModerationBot.Commands;
 
diff --git a/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs b/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs
index 7496a07..eb22a70 100644
--- a/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs
+++ b/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs
@@ -1,13 +1,8 @@
-using System.Buffers.Text;
-using System.Security.Cryptography;
-using ArcaneLibs.Extensions;
-using LibMatrix;
 using LibMatrix.EventTypes.Spec;
 using LibMatrix.Helpers;
 using LibMatrix.Services;
 using LibMatrix.Utilities.Bot.Interfaces;
 using ModerationBot.AccountData;
-using ModerationBot.StateEventTypes;
 
 namespace ModerationBot.Commands;
 
diff --git a/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs b/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs
index 6e64f6f..da77b05 100644
--- a/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs
+++ b/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs
@@ -1,14 +1,10 @@
-using System.Buffers.Text;
-using System.Security.Cryptography;
 using ArcaneLibs.Extensions;
-using LibMatrix;
 using LibMatrix.EventTypes.Spec;
 using LibMatrix.Helpers;
 using LibMatrix.RoomTypes;
 using LibMatrix.Services;
 using LibMatrix.Utilities.Bot.Interfaces;
 using ModerationBot.AccountData;
-using ModerationBot.StateEventTypes;
 
 namespace ModerationBot.Commands;
 
diff --git a/ExampleBots/ModerationBot/FirstRunTasks.cs b/ExampleBots/ModerationBot/FirstRunTasks.cs
index 83356bf..9dece1d 100644
--- a/ExampleBots/ModerationBot/FirstRunTasks.cs
+++ b/ExampleBots/ModerationBot/FirstRunTasks.cs
@@ -1,5 +1,4 @@
 using LibMatrix;
-using LibMatrix.EventTypes.Spec.State;
 using LibMatrix.Homeservers;
 using LibMatrix.Responses;
 using ModerationBot.AccountData;
diff --git a/ExampleBots/ModerationBot/ModerationBot.cs b/ExampleBots/ModerationBot/ModerationBot.cs
index 8a48b61..1be7bd5 100644
--- a/ExampleBots/ModerationBot/ModerationBot.cs
+++ b/ExampleBots/ModerationBot/ModerationBot.cs
@@ -1,32 +1,25 @@
-using System.Security.Cryptography;
-using System.Text.RegularExpressions;
 using ArcaneLibs.Extensions;
 using LibMatrix;
 using LibMatrix.EventTypes.Spec;
 using LibMatrix.EventTypes.Spec.State;
+using LibMatrix.EventTypes.Spec.State.Policy;
 using LibMatrix.Helpers;
 using LibMatrix.Homeservers;
-using LibMatrix.Responses;
 using LibMatrix.RoomTypes;
 using LibMatrix.Services;
-using LibMatrix.Utilities.Bot.Interfaces;
 using Microsoft.Extensions.Hosting;
 using Microsoft.Extensions.Logging;
 using ModerationBot.AccountData;
-using ModerationBot.StateEventTypes;
 using ModerationBot.StateEventTypes.Policies;
 
 namespace ModerationBot;
 
-public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger<ModerationBot> logger, ModerationBotConfiguration configuration,
-    HomeserverResolverService hsResolver, PolicyEngine engine) : IHostedService {
-    private readonly IEnumerable<ICommand> _commands;
-
+public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger<ModerationBot> logger, ModerationBotConfiguration configuration, PolicyEngine engine) : IHostedService {
     private Task _listenerTask;
 
     // private GenericRoom _policyRoom;
-    private GenericRoom _logRoom;
-    private GenericRoom _controlRoom;
+    private GenericRoom? _logRoom;
+    private GenericRoom? _controlRoom;
 
     /// <summary>Triggered when the application host is ready to start the service.</summary>
     /// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
@@ -63,6 +56,10 @@ public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger<Moderation
         _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);
         }
diff --git a/ExampleBots/ModerationBot/PolicyEngine.cs b/ExampleBots/ModerationBot/PolicyEngine.cs
index 8bfa448..5af99ac 100644
--- a/ExampleBots/ModerationBot/PolicyEngine.cs
+++ b/ExampleBots/ModerationBot/PolicyEngine.cs
@@ -5,15 +5,13 @@ using System.Text.RegularExpressions;
 using ArcaneLibs.Extensions;
 using LibMatrix;
 using LibMatrix.EventTypes.Spec;
-using LibMatrix.EventTypes.Spec.State;
+using LibMatrix.EventTypes.Spec.State.Policy;
 using LibMatrix.Helpers;
 using LibMatrix.Homeservers;
-using LibMatrix.Interfaces;
 using LibMatrix.RoomTypes;
 using LibMatrix.Services;
 using Microsoft.Extensions.Logging;
 using ModerationBot.AccountData;
-using ModerationBot.StateEventTypes;
 using ModerationBot.StateEventTypes.Policies;
 using ModerationBot.StateEventTypes.Policies.Implementations;
 
@@ -21,6 +19,7 @@ namespace ModerationBot;
 
 public class PolicyEngine(AuthenticatedHomeserverGeneric hs, ILogger<ModerationBot> logger, ModerationBotConfiguration configuration, HomeserverResolverService hsResolver) {
     private Dictionary<string, PolicyList> PolicyListAccountData { get; set; } = new();
+    // ReSharper disable once MemberCanBePrivate.Global
     public List<PolicyList> ActivePolicyLists { get; set; } = new();
     public List<BasePolicy> ActivePolicies { get; set; } = new();
     public Dictionary<string, List<BasePolicy>> ActivePoliciesByType { get; set; } = new();
@@ -47,8 +46,8 @@ public class PolicyEngine(AuthenticatedHomeserverGeneric hs, ILogger<ModerationB
             if (e is not { ErrorCode: "M_NOT_FOUND" }) throw;
         }
 
-        if (!PolicyListAccountData.ContainsKey(botData.DefaultPolicyRoom)) {
-            PolicyListAccountData.Add(botData.DefaultPolicyRoom, new PolicyList() {
+        if (!PolicyListAccountData.ContainsKey(botData.DefaultPolicyRoom!)) {
+            PolicyListAccountData.Add(botData.DefaultPolicyRoom!, new PolicyList() {
                 Trusted = true
             });
             await hs.SetAccountDataAsync("gay.rory.moderation_bot.policy_lists", PolicyListAccountData);
@@ -114,8 +113,8 @@ public class PolicyEngine(AuthenticatedHomeserverGeneric hs, ILogger<ModerationB
         foreach (var activePolicyList in ActivePolicyLists) {
             foreach (var policyEntry in activePolicyList.Policies) {
                 // TODO: implement rule translation
-                BasePolicy policy = policyEntry.TypedContent is BasePolicy ? policyEntry.TypedContent as BasePolicy : policyEntry.RawContent.Deserialize<UnknownPolicy>();
-                if (policy.Entity is null) continue;
+                var policy = policyEntry.TypedContent is BasePolicy ? policyEntry.TypedContent as BasePolicy : policyEntry.RawContent.Deserialize<UnknownPolicy>();
+                if (policy?.Entity is null) continue;
                 policy.PolicyList = activePolicyList;
                 policy.OriginalEvent = policyEntry;
                 activePolicies.Add(policy);
@@ -152,7 +151,7 @@ public class PolicyEngine(AuthenticatedHomeserverGeneric hs, ILogger<ModerationB
 
         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.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));
         }
diff --git a/ExampleBots/ModerationBot/PolicyList.cs b/ExampleBots/ModerationBot/PolicyList.cs
index a3052bd..f291c7b 100644
--- a/ExampleBots/ModerationBot/PolicyList.cs
+++ b/ExampleBots/ModerationBot/PolicyList.cs
@@ -1,7 +1,6 @@
 using System.Text.Json.Serialization;
 using LibMatrix;
 using LibMatrix.RoomTypes;
-using ModerationBot.StateEventTypes;
 
 namespace ModerationBot;
 
diff --git a/ExampleBots/ModerationBot/StateEventTypes/Policies/BasePolicy.cs b/ExampleBots/ModerationBot/StateEventTypes/Policies/BasePolicy.cs
index 21b44b2..64b0448 100644
--- a/ExampleBots/ModerationBot/StateEventTypes/Policies/BasePolicy.cs
+++ b/ExampleBots/ModerationBot/StateEventTypes/Policies/BasePolicy.cs
@@ -1,6 +1,7 @@
 using System.ComponentModel.DataAnnotations;
 using System.Text.Json.Serialization;
 using LibMatrix;
+using LibMatrix.EventTypes;
 using LibMatrix.Interfaces;
 
 namespace ModerationBot.StateEventTypes.Policies;
diff --git a/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs b/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs
index 3dfd937..72c9a60 100644
--- a/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs
+++ b/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs
@@ -1,4 +1,3 @@
-using System.Text.Json.Serialization;
 using LibMatrix.EventTypes;
 
 namespace ModerationBot.StateEventTypes.Policies.Implementations;
diff --git a/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs b/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs
index daac162..fa21e2d 100644
--- a/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs
+++ b/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs
@@ -1,4 +1,3 @@
-using System.Text.Json.Serialization;
 using LibMatrix.EventTypes;
 
 namespace ModerationBot.StateEventTypes.Policies.Implementations;
diff --git a/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/UnknownPolicy.cs b/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/UnknownPolicy.cs
index 8dc8258..78860ca 100644
--- a/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/UnknownPolicy.cs
+++ b/ExampleBots/ModerationBot/StateEventTypes/Policies/Implementations/UnknownPolicy.cs
@@ -1,6 +1,3 @@
-using System.Text.Json.Serialization;
-using LibMatrix.EventTypes;
-
 namespace ModerationBot.StateEventTypes.Policies.Implementations;
 
 /// <summary>