about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--Commands/BanMediaCommand.cs2
-rw-r--r--Commands/DbgAllRoomsArePolicyListsCommand.cs5
-rw-r--r--Commands/DbgDumpActivePoliciesCommand.cs5
-rw-r--r--Commands/DbgDumpAllStateTypesCommand.cs4
-rw-r--r--Commands/JoinRoomCommand.cs5
-rw-r--r--Commands/JoinSpaceMembersCommand.cs4
-rw-r--r--FirstRunTasks.cs1
-rw-r--r--ModerationBot.cs19
-rw-r--r--PolicyEngine.cs15
-rw-r--r--PolicyList.cs1
-rw-r--r--StateEventTypes/Policies/BasePolicy.cs1
-rw-r--r--StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs1
-rw-r--r--StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs1
-rw-r--r--StateEventTypes/Policies/Implementations/UnknownPolicy.cs3
14 files changed, 16 insertions, 51 deletions
diff --git a/Commands/BanMediaCommand.cs b/Commands/BanMediaCommand.cs
index 9e49b22..535fd4f 100644
--- a/Commands/BanMediaCommand.cs
+++ b/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/Commands/DbgAllRoomsArePolicyListsCommand.cs b/Commands/DbgAllRoomsArePolicyListsCommand.cs
index 327a9a4..f578f53 100644
--- a/Commands/DbgAllRoomsArePolicyListsCommand.cs
+++ b/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/Commands/DbgDumpActivePoliciesCommand.cs b/Commands/DbgDumpActivePoliciesCommand.cs
index 35c95f8..285d792 100644
--- a/Commands/DbgDumpActivePoliciesCommand.cs
+++ b/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/Commands/DbgDumpAllStateTypesCommand.cs b/Commands/DbgDumpAllStateTypesCommand.cs
index 0013065..3727877 100644
--- a/Commands/DbgDumpAllStateTypesCommand.cs
+++ b/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/Commands/JoinRoomCommand.cs b/Commands/JoinRoomCommand.cs
index 7496a07..eb22a70 100644
--- a/Commands/JoinRoomCommand.cs
+++ b/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/Commands/JoinSpaceMembersCommand.cs b/Commands/JoinSpaceMembersCommand.cs
index 6e64f6f..da77b05 100644
--- a/Commands/JoinSpaceMembersCommand.cs
+++ b/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/FirstRunTasks.cs b/FirstRunTasks.cs
index 83356bf..9dece1d 100644
--- a/FirstRunTasks.cs
+++ b/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/ModerationBot.cs b/ModerationBot.cs
index 8a48b61..1be7bd5 100644
--- a/ModerationBot.cs
+++ b/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/PolicyEngine.cs b/PolicyEngine.cs
index 8bfa448..5af99ac 100644
--- a/PolicyEngine.cs
+++ b/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/PolicyList.cs b/PolicyList.cs
index a3052bd..f291c7b 100644
--- a/PolicyList.cs
+++ b/PolicyList.cs
@@ -1,7 +1,6 @@
 using System.Text.Json.Serialization;
 using LibMatrix;
 using LibMatrix.RoomTypes;
-using ModerationBot.StateEventTypes;
 
 namespace ModerationBot;
 
diff --git a/StateEventTypes/Policies/BasePolicy.cs b/StateEventTypes/Policies/BasePolicy.cs
index 21b44b2..64b0448 100644
--- a/StateEventTypes/Policies/BasePolicy.cs
+++ b/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/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs b/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs
index 3dfd937..72c9a60 100644
--- a/StateEventTypes/Policies/Implementations/MediaPolicyHomeserver.cs
+++ b/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/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs b/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs
index daac162..fa21e2d 100644
--- a/StateEventTypes/Policies/Implementations/MessagePolicyContainsText.cs
+++ b/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/StateEventTypes/Policies/Implementations/UnknownPolicy.cs b/StateEventTypes/Policies/Implementations/UnknownPolicy.cs
index 8dc8258..78860ca 100644
--- a/StateEventTypes/Policies/Implementations/UnknownPolicy.cs
+++ b/StateEventTypes/Policies/Implementations/UnknownPolicy.cs
@@ -1,6 +1,3 @@
-using System.Text.Json.Serialization;
-using LibMatrix.EventTypes;
-
 namespace ModerationBot.StateEventTypes.Policies.Implementations;
 
 /// <summary>