diff options
Diffstat (limited to 'ExampleBots')
23 files changed, 17 insertions, 64 deletions
diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs index ea42597..e690890 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs @@ -1,4 +1,3 @@ -using ArcaneLibs.Extensions; using ArcaneLibs.StringNormalisation; using LibMatrix.EventTypes.Spec; using LibMatrix.ExampleBot.Bot.Interfaces; diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs index 935d53f..2c014de 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs @@ -1,6 +1,5 @@ using System.Text.Json; using ArcaneLibs.Extensions; -using LibMatrix.Extensions; using LibMatrix.Interfaces.Services; using Microsoft.Extensions.Logging; diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/Interfaces/CommandContext.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/Interfaces/CommandContext.cs index 9b6ef7a..6dbb7f9 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/Interfaces/CommandContext.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/Interfaces/CommandContext.cs @@ -1,5 +1,4 @@ using LibMatrix.EventTypes.Spec; -using LibMatrix.Responses; using LibMatrix.RoomTypes; namespace LibMatrix.ExampleBot.Bot.Interfaces; diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs index 8cf4f1f..8e6cd6a 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs @@ -3,7 +3,6 @@ using ArcaneLibs.Extensions; using LibMatrix.EventTypes.Spec; using LibMatrix.EventTypes.Spec.State; using LibMatrix.ExampleBot.Bot.Interfaces; -using LibMatrix.Extensions; using LibMatrix.Helpers; using LibMatrix.Homeservers; using LibMatrix.Services; diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs index 890db85..2f6e0b0 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs @@ -1,9 +1,7 @@ using System.Diagnostics.CodeAnalysis; -using ArcaneLibs.Extensions; using LibMatrix.ExampleBot.Bot.Interfaces; using LibMatrix.Homeservers; using LibMatrix.Services; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; diff --git a/ExampleBots/LibMatrix.ExampleBot/Program.cs b/ExampleBots/LibMatrix.ExampleBot/Program.cs index 6d8775e..25ce07d 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Program.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Program.cs @@ -3,8 +3,6 @@ using ArcaneLibs; using LibMatrix.ExampleBot.Bot; using LibMatrix.ExampleBot.Bot.Interfaces; -using LibMatrix.ExampleBot.Bot.StartupTasks; -using LibMatrix.Extensions; using LibMatrix.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; 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> diff --git a/ExampleBots/PluralContactBotPoC/Bot/AccountData/BotData.cs b/ExampleBots/PluralContactBotPoC/Bot/AccountData/BotData.cs index 5d11432..2adcbb3 100644 --- a/ExampleBots/PluralContactBotPoC/Bot/AccountData/BotData.cs +++ b/ExampleBots/PluralContactBotPoC/Bot/AccountData/BotData.cs @@ -1,6 +1,5 @@ using System.Text.Json.Serialization; using LibMatrix.EventTypes; -using LibMatrix.Helpers; using LibMatrix.Interfaces; namespace PluralContactBotPoC.Bot.AccountData; diff --git a/ExampleBots/PluralContactBotPoC/Bot/AccountData/SystemData.cs b/ExampleBots/PluralContactBotPoC/Bot/AccountData/SystemData.cs index 42edd23..ad8ab1d 100644 --- a/ExampleBots/PluralContactBotPoC/Bot/AccountData/SystemData.cs +++ b/ExampleBots/PluralContactBotPoC/Bot/AccountData/SystemData.cs @@ -1,6 +1,5 @@ using System.Text.Json.Serialization; using LibMatrix.EventTypes; -using LibMatrix.Helpers; using LibMatrix.Interfaces; namespace PluralContactBotPoC.Bot.StateEventTypes; @@ -8,7 +7,7 @@ namespace PluralContactBotPoC.Bot.StateEventTypes; [MatrixEvent(EventName = "gay.rory.plural_contact_bot.system_data")] public class SystemData : EventContent { [JsonPropertyName("control_room")] - public string ControlRoom { get; set; } = null!; + public required string ControlRoom { get; set; } [JsonPropertyName("system_members")] public List<string> Members { get; set; } = new(); [JsonPropertyName("dm_space")] diff --git a/ExampleBots/PluralContactBotPoC/Program.cs b/ExampleBots/PluralContactBotPoC/Program.cs index f8d93c6..f65f2e9 100644 --- a/ExampleBots/PluralContactBotPoC/Program.cs +++ b/ExampleBots/PluralContactBotPoC/Program.cs @@ -1,13 +1,11 @@ // See https://aka.ms/new-console-template for more information using System.Text.Json; -using System.Text.Json.Serialization; using ArcaneLibs.Extensions; using LibMatrix.Services; using LibMatrix.Utilities.Bot; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using PluralContactBotPoC; using PluralContactBotPoC.Bot; Console.WriteLine("Hello, World!"); |