about summary refs log tree commit diff
path: root/ExampleBots/MediaModeratorPoC
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-05 06:28:52 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-05 06:28:52 +0200
commitcf455ed8de20bbee011289223e7d8d5775dfd69e (patch)
treecbdfdbc207af64a105b4d21941a6f0e71ca65e9d /ExampleBots/MediaModeratorPoC
parentAdd start of Media Moderator PoC bot (diff)
downloadLibMatrix-cf455ed8de20bbee011289223e7d8d5775dfd69e.tar.xz
Media moderator PoC works, abstract command handling to library
Diffstat (limited to '')
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/AccountData/BotData.cs7
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs102
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/Commands/CmdCommand.cs46
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/Interfaces/CommandContext.cs14
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs319
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/MediaModBotConfiguration.cs9
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/StateEventTypes/MediaPolicyStateEventData.cs22
-rw-r--r--ExampleBots/MediaModeratorPoC/MediaModeratorPoC.csproj5
-rw-r--r--ExampleBots/MediaModeratorPoC/Program.cs9
-rw-r--r--ExampleBots/MediaModeratorPoC/appsettings.Development.json6
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs (renamed from ExampleBots/MediaModeratorPoC/Bot/Commands/HelpCommand.cs)5
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs (renamed from ExampleBots/MediaModeratorPoC/Bot/Commands/PingCommand.cs)4
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs (renamed from ExampleBots/MediaModeratorPoC/Bot/FileStorageProvider.cs)0
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs (renamed from ExampleBots/MediaModeratorPoC/Bot/Interfaces/ICommand.cs)0
14 files changed, 352 insertions, 196 deletions
diff --git a/ExampleBots/MediaModeratorPoC/Bot/AccountData/BotData.cs b/ExampleBots/MediaModeratorPoC/Bot/AccountData/BotData.cs
index 9b92948..b4e1167 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/AccountData/BotData.cs
+++ b/ExampleBots/MediaModeratorPoC/Bot/AccountData/BotData.cs
@@ -1,7 +1,14 @@
+using System.Text.Json.Serialization;
+
 namespace MediaModeratorPoC.Bot.AccountData;
 
 public class BotData {
+    [JsonPropertyName("control_room")]
     public string ControlRoom { get; set; } = "";
+
+    [JsonPropertyName("log_room")]
     public string? LogRoom { get; set; } = "";
+
+    [JsonPropertyName("policy_room")]
     public string? PolicyRoom { get; set; } = "";
 }
diff --git a/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs b/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs
index d0b5674..90de136 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs
+++ b/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs
@@ -1,4 +1,8 @@
+using System.Security.Cryptography;
+using ArcaneLibs.Extensions;
+using LibMatrix.Helpers;
 using LibMatrix.Responses;
+using LibMatrix.Services;
 using LibMatrix.StateEventTypes.Spec;
 using MediaModeratorPoC.Bot.AccountData;
 using MediaModeratorPoC.Bot.Interfaces;
@@ -6,7 +10,7 @@ using MediaModeratorPoC.Bot.StateEventTypes;
 
 namespace MediaModeratorPoC.Bot.Commands;
 
-public class BanMediaCommand(IServiceProvider services) : ICommand {
+public class BanMediaCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver) : ICommand {
     public string Name { get; } = "banmedia";
     public string Description { get; } = "Create a policy banning a piece of media, must be used in reply to a message";
 
@@ -14,53 +18,93 @@ public class BanMediaCommand(IServiceProvider services) : ICommand {
         //check if user is admin in control room
         var botData = await ctx.Homeserver.GetAccountData<BotData>("gay.rory.media_moderator_poc_data");
         var controlRoom = await ctx.Homeserver.GetRoom(botData.ControlRoom);
-        var powerLevels = await controlRoom.GetPowerLevelAsync();
-        var isAdmin = powerLevels.UserHasPermission(ctx.MessageEvent.Sender, "m.room.ban");
+        var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasPermission(ctx.MessageEvent.Sender, "m.room.ban");
         if (!isAdmin) {
             // await ctx.Reply("You do not have permission to use this command!");
-            var logRoom = await ctx.Homeserver.GetRoom(botData.LogRoom);
-            await logRoom.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
-                Body = $"User {ctx.MessageEvent.Sender} tried to use command {Name} but does not have permission!",
-                MessageType = "m.text"
-            });
+            await (await ctx.Homeserver.GetRoom(botData.LogRoom!)).SendMessageEventAsync("m.room.message",
+                new RoomMessageEventData(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.GetAccountData<BotData>("gay.rory.media_moderator_poc_data");
+        var policyRoom = await ctx.Homeserver.GetRoom(botData.PolicyRoom ?? botData.ControlRoom);
+        var logRoom = await ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom);
+
         //check if reply
-        if ((ctx.MessageEvent.TypedContent as RoomMessageEventData).RelatesTo is { InReplyTo: not null } ) {
-            var messageContent = ctx.MessageEvent.TypedContent as RoomMessageEventData;
+        var messageContent = ctx.MessageEvent.TypedContent as RoomMessageEventData;
+        if (messageContent?.RelatesTo is { InReplyTo: not null }) {
             try {
-                var botData = await ctx.Homeserver.GetAccountData<BotData>("gay.rory.media_moderator_poc_data");
-                var policyRoom = await ctx.Homeserver.GetRoom(botData.PolicyRoom);
-                var logRoom = await ctx.Homeserver.GetRoom(botData.LogRoom);
-                await logRoom.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
-                    Body = $"User {ctx.MessageEvent.Sender} is trying to ban media {messageContent.RelatesTo!.InReplyTo!.EventId}",
-                    MessageType = "m.text"
-                });
+                await logRoom.SendMessageEventAsync("m.room.message",
+                    new RoomMessageEventData(
+                        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.GetEvent<StateEventResponse>(messageContent.RelatesTo!.InReplyTo!.EventId);
 
-                await policyRoom.SendStateEventAsync("gay.rory.media_moderator_poc.rule.media", new MediaPolicyStateEventData() {
-                    Entity = (repliedMessage.TypedContent as RoomMessageEventData).Url!,
-                    Reason = string.Join(' ', ctx.Args),
-                    Recommendation = PolicyRecommendationTypes.Ban
+                //check if recommendation is in list
+                if (ctx.Args.Length < 2) {
+                    await ctx.Room.SendMessageEventAsync("m.room.message", 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("m.room.message", 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 RoomMessageEventData).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._httpClient.GetStreamAsync(resolvedUri));
+                }
+                catch (Exception ex) {
+                    await logRoom.SendMessageEventAsync("m.room.message",
+                        MessageFormatter.FormatException($"Error calculating file hash for {mxcUri} via {mxcUri.Split('/')[2]}, retrying via {ctx.Homeserver.HomeServerDomain}...",
+                            ex));
+                    try {
+                        resolvedUri = await hsResolver.ResolveMediaUri(ctx.Homeserver.HomeServerDomain, mxcUri);
+                        fileHash = await hashAlgo.ComputeHashAsync(await ctx.Homeserver._httpClient.GetStreamAsync(resolvedUri));
+                    }
+                    catch (Exception ex2) {
+                        await ctx.Room.SendMessageEventAsync("m.room.message", MessageFormatter.FormatException("Error calculating file hash", ex2));
+                        await logRoom.SendMessageEventAsync("m.room.message",
+                            MessageFormatter.FormatException($"Error calculating file hash via {ctx.Homeserver.HomeServerDomain}!", ex2));
+                    }
+                }
+
+                MediaPolicyStateEventData policy;
+                await policyRoom.SendStateEventAsync("gay.rory.media_moderator_poc.rule.media", Guid.NewGuid().ToString(), policy = new MediaPolicyStateEventData {
+                    Entity = uriHash,
+                    FileHash = fileHash,
+                    Reason = string.Join(' ', ctx.Args[1..]),
+                    Recommendation = recommendation,
                 });
+
+                await ctx.Room.SendMessageEventAsync("m.room.message", MessageFormatter.FormatSuccessJson("Media policy created", policy));
+                await logRoom.SendMessageEventAsync("m.room.message", MessageFormatter.FormatSuccessJson("Media policy created", policy));
             }
             catch (Exception e) {
-                await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
-                    Body = $"Error: {e.Message}",
-                    MessageType = "m.text"
-                });
+                await logRoom.SendMessageEventAsync("m.room.message", MessageFormatter.FormatException("Error creating policy", e));
+                await ctx.Room.SendMessageEventAsync("m.room.message", MessageFormatter.FormatException("Error creating policy", e));
+                await using var stream = new MemoryStream(e.ToString().AsBytes().ToArray());
+                await logRoom.SendFileAsync("m.file", "error.log.cs", stream);
             }
         }
         else {
-            await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
-                Body = "This command must be used in reply to a message!",
-                MessageType = "m.text",
-            });
+            await ctx.Room.SendMessageEventAsync("m.room.message", MessageFormatter.FormatError("This command must be used in reply to a message!"));
         }
     }
 }
diff --git a/ExampleBots/MediaModeratorPoC/Bot/Commands/CmdCommand.cs b/ExampleBots/MediaModeratorPoC/Bot/Commands/CmdCommand.cs
deleted file mode 100644
index 14c4334..0000000
--- a/ExampleBots/MediaModeratorPoC/Bot/Commands/CmdCommand.cs
+++ /dev/null
@@ -1,46 +0,0 @@
-using LibMatrix.StateEventTypes.Spec;
-using MediaModeratorPoC.Bot.Interfaces;
-
-namespace MediaModeratorPoC.Bot.Commands;
-
-public class CmdCommand : ICommand {
-    public string Name => "cmd";
-    public string Description => "Runs a command on the host system";
-
-    public Task<bool> CanInvoke(CommandContext ctx) {
-        return Task.FromResult(ctx.MessageEvent.Sender.EndsWith(":rory.gay"));
-    }
-
-    public async Task Invoke(CommandContext ctx) {
-        var cmd = ctx.Args.Aggregate("\"", (current, arg) => current + arg + " ");
-
-        cmd = cmd.Trim();
-        cmd += "\"";
-
-        await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
-            Body = $"Command being executed: `{cmd}`"
-        });
-
-        var output = ArcaneLibs.Util.GetCommandOutputSync(
-                Environment.OSVersion.Platform == PlatformID.Unix ? "/bin/sh" : "cmd.exe",
-                (Environment.OSVersion.Platform == PlatformID.Unix ? "-c " : "/c ") + cmd)
-            .Replace("`", "\\`")
-            .Split("\n").ToList();
-        foreach (var _out in output) Console.WriteLine($"{_out.Length:0000} {_out}");
-
-        var msg = "";
-        while (output.Count > 0) {
-            Console.WriteLine("Adding: " + output[0]);
-            msg += output[0] + "\n";
-            output.RemoveAt(0);
-            if ((output.Count > 0 && (msg + output[0]).Length > 64000) || output.Count == 0) {
-                await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
-                    FormattedBody = $"```ansi\n{msg}\n```",
-                    // Body = Markdig.Markdown.ToHtml(msg),
-                    Format = "org.matrix.custom.html"
-                });
-                msg = "";
-            }
-        }
-    }
-}
diff --git a/ExampleBots/MediaModeratorPoC/Bot/Interfaces/CommandContext.cs b/ExampleBots/MediaModeratorPoC/Bot/Interfaces/CommandContext.cs
deleted file mode 100644
index 29a5a3f..0000000
--- a/ExampleBots/MediaModeratorPoC/Bot/Interfaces/CommandContext.cs
+++ /dev/null
@@ -1,14 +0,0 @@
-using LibMatrix.Homeservers;
-using LibMatrix.Responses;
-using LibMatrix.RoomTypes;
-using LibMatrix.StateEventTypes.Spec;
-
-namespace MediaModeratorPoC.Bot.Interfaces;
-
-public class CommandContext {
-    public GenericRoom Room { get; set; }
-    public StateEventResponse MessageEvent { get; set; }
-    public string CommandName => (MessageEvent.TypedContent as RoomMessageEventData).Body.Split(' ')[0][1..];
-    public string[] Args => (MessageEvent.TypedContent as RoomMessageEventData).Body.Split(' ')[1..];
-    public AuthenticatedHomeserverGeneric Homeserver { get; set; }
-}
diff --git a/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs b/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs
index 1b379c7..81aecc7 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs
+++ b/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs
@@ -1,7 +1,12 @@
-using System.Diagnostics.CodeAnalysis;
+using System.Buffers.Text;
+using System.Data;
+using System.Security.Cryptography;
+using System.Text;
+using System.Text.Encodings.Web;
 using System.Text.RegularExpressions;
 using ArcaneLibs.Extensions;
 using LibMatrix;
+using LibMatrix.Helpers;
 using LibMatrix.Homeservers;
 using LibMatrix.Responses;
 using LibMatrix.RoomTypes;
@@ -10,50 +15,47 @@ using LibMatrix.StateEventTypes.Spec;
 using MediaModeratorPoC.Bot.AccountData;
 using MediaModeratorPoC.Bot.Interfaces;
 using MediaModeratorPoC.Bot.StateEventTypes;
-using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Hosting;
 using Microsoft.Extensions.Logging;
 
 namespace MediaModeratorPoC.Bot;
 
 public class MediaModBot : IHostedService {
-    private readonly HomeserverProviderService _homeserverProviderService;
+    private readonly AuthenticatedHomeserverGeneric _hs;
     private readonly ILogger<MediaModBot> _logger;
     private readonly MediaModBotConfiguration _configuration;
+    private readonly HomeserverResolverService _hsResolver;
     private readonly IEnumerable<ICommand> _commands;
 
-    private GenericRoom PolicyRoom;
+    private Task _listenerTask;
 
-    public MediaModBot(HomeserverProviderService homeserverProviderService, ILogger<MediaModBot> logger,
-        MediaModBotConfiguration configuration, IServiceProvider services) {
-        logger.LogInformation("MRUBot hosted service instantiated!");
-        _homeserverProviderService = homeserverProviderService;
+    private GenericRoom _policyRoom;
+    private GenericRoom _logRoom;
+    private GenericRoom _controlRoom;
+
+    public MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger<MediaModBot> logger,
+        MediaModBotConfiguration configuration, HomeserverResolverService hsResolver) {
+        logger.LogInformation("{} instantiated!", this.GetType().Name);
+        _hs = hs;
         _logger = logger;
         _configuration = configuration;
-        _logger.LogInformation("Getting commands...");
-        _commands = services.GetServices<ICommand>();
-        _logger.LogInformation("Got {} commands!", _commands.Count());
+        _hsResolver = hsResolver;
     }
 
     /// <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>
-    [SuppressMessage("ReSharper", "FunctionNeverReturns")]
     public async Task StartAsync(CancellationToken cancellationToken) {
+        _listenerTask = Run(cancellationToken);
+        _logger.LogInformation("Bot started!");
+    }
+
+    private async Task Run(CancellationToken cancellationToken) {
         Directory.GetFiles("bot_data/cache").ToList().ForEach(File.Delete);
-        AuthenticatedHomeserverGeneric hs;
-        try {
-            hs = await _homeserverProviderService.GetAuthenticatedWithToken(_configuration.Homeserver,
-                _configuration.AccessToken);
-        }
-        catch (Exception e) {
-            _logger.LogError("{}", e.Message);
-            throw;
-        }
 
         BotData botData;
 
         try {
-            botData = await hs.GetAccountData<BotData>("gay.rory.media_moderator_poc_data");
+            botData = await _hs.GetAccountData<BotData>("gay.rory.media_moderator_poc_data");
         }
         catch (Exception e) {
             if (e is not MatrixException { ErrorCode: "M_NOT_FOUND" }) {
@@ -62,20 +64,20 @@ public class MediaModBot : IHostedService {
             }
 
             botData = new BotData();
-            var creationContent = CreateRoomRequest.CreatePrivate(hs, name: "Media Moderator PoC - Control room", roomAliasName: "media-moderator-poc-control-room");
+            var creationContent = CreateRoomRequest.CreatePrivate(_hs, name: "Media Moderator PoC - Control room", roomAliasName: "media-moderator-poc-control-room");
             creationContent.Invite = _configuration.Admins;
             creationContent.CreationContent["type"] = "gay.rory.media_moderator_poc.control_room";
 
-            botData.ControlRoom = (await hs.CreateRoom(creationContent)).RoomId;
+            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 JoinRulesEventData() {
+                TypedContent = new JoinRulesEventData {
                     JoinRule = "knock_restricted",
                     Allow = new() {
-                        new JoinRulesEventData.AllowEntry() {
+                        new JoinRulesEventData.AllowEntry {
                             Type = "m.room_membership",
                             RoomId = botData.ControlRoom
                         }
@@ -86,94 +88,257 @@ public class MediaModBot : IHostedService {
             creationContent.Name = "Media Moderator PoC - Log room";
             creationContent.RoomAliasName = "media-moderator-poc-log-room";
             creationContent.CreationContent["type"] = "gay.rory.media_moderator_poc.log_room";
-            botData.LogRoom = (await hs.CreateRoom(creationContent)).RoomId;
+            botData.LogRoom = (await _hs.CreateRoom(creationContent)).RoomId;
 
             creationContent.Name = "Media Moderator PoC - Policy room";
             creationContent.RoomAliasName = "media-moderator-poc-policy-room";
             creationContent.CreationContent["type"] = "gay.rory.media_moderator_poc.policy_room";
-            botData.PolicyRoom = (await hs.CreateRoom(creationContent)).RoomId;
+            botData.PolicyRoom = (await _hs.CreateRoom(creationContent)).RoomId;
 
-            await hs.SetAccountData("gay.rory.media_moderator_poc_data", botData);
+            await _hs.SetAccountData("gay.rory.media_moderator_poc_data", botData);
         }
 
-        PolicyRoom = await hs.GetRoom(botData.PolicyRoom);
+        _policyRoom = await _hs.GetRoom(botData.PolicyRoom ?? botData.ControlRoom);
+        _logRoom = await _hs.GetRoom(botData.LogRoom ?? botData.ControlRoom);
+        _controlRoom = await _hs.GetRoom(botData.ControlRoom);
+
+        List<string> admins = new();
 
-        hs.SyncHelper.InviteReceivedHandlers.Add(async Task (args) => {
+#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.GetMembersAsync();
+                await foreach (var member in controlRoomMembers) {
+                    if ((member.TypedContent as RoomMemberEventData).Membership == "join") admins.Add(member.UserId);
+                }
+
+                await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
+            }
+        }, cancellationToken);
+#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
+
+        _hs.SyncHelper.InviteReceivedHandlers.Add(async Task (args) => {
             var inviteEvent =
                 args.Value.InviteState.Events.FirstOrDefault(x =>
-                    x.Type == "m.room.member" && x.StateKey == hs.WhoAmI.UserId);
+                    x.Type == "m.room.member" && x.StateKey == _hs.WhoAmI.UserId);
             _logger.LogInformation(
                 $"Got invite to {args.Key} by {inviteEvent.Sender} with reason: {(inviteEvent.TypedContent as RoomMemberEventData).Reason}");
             if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent.Sender.EndsWith(":conduit.rory.gay")) {
                 try {
-                    var senderProfile = await hs.GetProfile(inviteEvent.Sender);
-                    await (await hs.GetRoom(args.Key)).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!");
+                    var senderProfile = await _hs.GetProfile(inviteEvent.Sender);
+                    await (await _hs.GetRoom(args.Key)).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!");
                 }
                 catch (Exception e) {
                     _logger.LogError("{}", e.ToString());
-                    await (await hs.GetRoom(args.Key)).LeaveAsync(reason: "I was unable to join the room: " + e);
+                    await (await _hs.GetRoom(args.Key)).LeaveAsync(reason: "I was unable to join the room: " + e);
                 }
             }
         });
-        hs.SyncHelper.TimelineEventHandlers.Add(async @event => {
-            _logger.LogInformation(
-                "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: true, ignoreNull: true));
-
-            var room = await hs.GetRoom(@event.RoomId);
-            // _logger.LogInformation(eventResponse.ToJson(indent: false));
-            if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventData message }) {
-                if (message is { MessageType: "m.text" } && message.Body.StartsWith(_configuration.Prefix)) {
-                    var command = _commands.FirstOrDefault(x => x.Name == message.Body.Split(' ')[0][_configuration.Prefix.Length..]);
-                    if (command == null) {
-                        await room.SendMessageEventAsync("m.room.message",
-                            new RoomMessageEventData {
-                                MessageType = "m.notice",
-                                Body = "Command not found!"
-                            });
-                        return;
-                    }
 
-                    var ctx = new CommandContext {
-                        Room = room,
-                        MessageEvent = @event,
-                        Homeserver = hs
-                    };
-                    if (await command.CanInvoke(ctx)) {
-                        await command.Invoke(ctx);
-                    }
-                    else {
-                        await room.SendMessageEventAsync("m.room.message",
-                            new RoomMessageEventData {
-                                MessageType = "m.notice",
-                                Body = "You do not have permission to run this command!"
+        _hs.SyncHelper.TimelineEventHandlers.Add(async @event => {
+            var room = await _hs.GetRoom(@event.RoomId);
+            try {
+                _logger.LogInformation(
+                    "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: true, ignoreNull: true));
+
+                if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventData message }) {
+                    if (message is { MessageType: "m.image" }) {
+                        //check media
+                        var matchedPolicy = await CheckMedia(@event);
+                        if (matchedPolicy is null) return;
+                        var matchedpolicyData = matchedPolicy.TypedContent as MediaPolicyStateEventData;
+                        var recommendation = matchedpolicyData.Recommendation;
+                        await _logRoom.SendMessageEventAsync("m.room.message",
+                            new RoomMessageEventData(
+                                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 =
+                                    $"<font color=\"#FFFF00\">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: <pre>{matchedPolicy.RawContent!.ToJson(ignoreNull: true)}</pre></font>"
                             });
+                        switch (recommendation) {
+                            case "warn_admins": {
+                                await _controlRoom.SendMessageEventAsync("m.room.message",
+                                    new RoomMessageEventData(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" +
+                                                        $"<font color=\"#FF0000\">User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image <a href=\"{message.Url}\">{message.Url}</a></font>"
+                                    });
+                                break;
+                            }
+                            case "warn": {
+                                await room.SendMessageEventAsync("m.room.message",
+                                    new RoomMessageEventData(
+                                        body: $"Please be careful when posting this image: {matchedpolicyData.Reason}",
+                                        messageType: "m.text") {
+                                        Format = "org.matrix.custom.html",
+                                        FormattedBody =
+                                            $"<font color=\"#FFFF00\">Please be careful when posting this image: {matchedpolicyData.Reason}</a></font>"
+                                    });
+                                break;
+                            }
+                            case "redact": {
+                                await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason);
+                                break;
+                            }
+                            case "spoiler": {
+                                // <blockquote>
+                                //  <a href=\"https://matrix.to/#/@emma:rory.gay\">@emma:rory.gay</a><br>
+                                //  <a href=\"https://codeberg.org/crimsonfork/CN\"></a>
+                                //  <font color=\"#dc143c\" data-mx-color=\"#dc143c\">
+                                //      <b>CN</b>
+                                //  </font>:
+                                //  <a href=\"https://the-apothecary.club/_matrix/media/v3/download/rory.gay/sLkdxUhipiQaFwRkXcPSRwdg\">test</a><br>
+                                //  <span data-mx-spoiler=\"\"><a href=\"https://the-apothecary.club/_matrix/media/v3/download/rory.gay/sLkdxUhipiQaFwRkXcPSRwdg\">
+                                //      <img src=\"mxc://rory.gay/sLkdxUhipiQaFwRkXcPSRwdg\" height=\"69\"></a>
+                                //  </span>
+                                // </blockquote>
+                                await room.SendMessageEventAsync("m.room.message",
+                                    new RoomMessageEventData(
+                                        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 =
+                                            $"<font color=\"#FFFF00\">Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:</a></font>"
+                                    });
+                                var imageUrl = message.Url;
+                                await room.SendMessageEventAsync("m.room.message",
+                                    new RoomMessageEventData(body: $"CN: {imageUrl}",
+                                        messageType: "m.text") {
+                                        Format = "org.matrix.custom.html",
+                                        FormattedBody = $"""
+                                                             <blockquote>
+                                                                <font color=\"#dc143c\" data-mx-color=\"#dc143c\">
+                                                                    <b>CN</b>
+                                                                </font>:
+                                                                <a href=\"{imageUrl}\">{matchedpolicyData.Reason}</a><br>
+                                                                <span data-mx-spoiler=\"\">
+                                                                    <a href=\"{imageUrl}\">
+                                                                        <img src=\"{imageUrl}\" height=\"69\">
+                                                                    </a>
+                                                                </span>
+                                                             </blockquote>
+                                                         """
+                                    });
+                                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();
+                                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("m.room.message",
+                    MessageFormatter.FormatException($"Unable to ban user in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e));
+                await _logRoom.SendMessageEventAsync("m.room.message",
+                    MessageFormatter.FormatException($"Unable to ban user in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e));
+                await using var stream = new MemoryStream(e.ToString().AsBytes().ToArray());
+                await _controlRoom.SendFileAsync("m.file", "error.log.cs", stream);
+                await _logRoom.SendFileAsync("m.file", "error.log.cs", stream);
+            }
         });
-        await hs.SyncHelper.RunSyncLoop(cancellationToken: cancellationToken);
     }
 
     /// <summary>Triggered when the application host is performing a graceful shutdown.</summary>
     /// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
-    public Task StopAsync(CancellationToken cancellationToken) {
+    public async Task StopAsync(CancellationToken cancellationToken) {
         _logger.LogInformation("Shutting down bot!");
-        return Task.CompletedTask;
     }
 
+    private async Task<StateEventResponse?> CheckMedia(StateEventResponse @event) {
+        var stateList = _policyRoom.GetFullStateAsync();
+        var hashAlgo = SHA3_256.Create();
+
+        var mxcUri = @event.RawContent["url"].GetValue<string>();
+        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._httpClient.GetStreamAsync(resolvedUri));
+        }
+        catch (Exception ex) {
+            await _logRoom.SendMessageEventAsync("m.room.message",
+                MessageFormatter.FormatException($"Error calculating file hash for {mxcUri} via {mxcUri.Split('/')[2]} ({resolvedUri}), retrying via {_hs.HomeServerDomain}...",
+                    ex));
+            try {
+                resolvedUri = await _hsResolver.ResolveMediaUri(_hs.HomeServerDomain, mxcUri);
+                fileHash = await hashAlgo.ComputeHashAsync(await _hs._httpClient.GetStreamAsync(resolvedUri));
+            }
+            catch (Exception ex2) {
+                await _logRoom.SendMessageEventAsync("m.room.message",
+                    MessageFormatter.FormatException($"Error calculating file hash via {_hs.HomeServerDomain} ({resolvedUri})!", ex2));
+            }
+        }
+
+        _logger.LogInformation("Checking media {url} with hash {hash}", resolvedUri, fileHash);
 
-    private async Task<bool> CheckMedia(StateEventResponse @event) {
-        var stateList = PolicyRoom.GetFullStateAsync();
         await foreach (var state in stateList) {
-            if(state.Type != "gay.rory.media_moderator_poc.rule.media") continue;
+            if (state.Type != "gay.rory.media_moderator_poc.rule.media" && state.Type != "gay.rory.media_moderator_poc.rule.server") continue;
+            if (!state.RawContent.ContainsKey("entity")) {
+                _logger.LogWarning("Rule {rule} has no entity, this event was probably redacted!", state.StateKey);
+                continue;
+            }
+            _logger.LogInformation("Checking rule {rule}: {data}", state.StateKey, state.TypedContent.ToJson(ignoreNull: true, indent: false));
             var rule = state.TypedContent as MediaPolicyStateEventData;
-            rule.Entity = rule.Entity.Replace("\\*", ".*").Replace("\\?", ".");
-            var regex = new Regex(rule.Entity);
-            if (regex.IsMatch(@event.RawContent["url"].GetValue<string>())) {
-                _logger.LogInformation("{url} matched rule {rule}", @event.RawContent["url"], rule.ToJson(ignoreNull: true));
-                return true;
+            if (state.Type == "gay.rory.media_moderator_poc.rule.server" && rule.ServerEntity is not null) {
+                rule.ServerEntity = rule.ServerEntity.Replace("\\*", ".*").Replace("\\?", ".");
+                var regex = new Regex($"mxc://({rule.ServerEntity})/.*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
+                if (regex.IsMatch(@event.RawContent["url"].GetValue<string>())) {
+                    _logger.LogInformation("{url} matched rule {rule}", @event.RawContent["url"], rule.ToJson(ignoreNull: true));
+                    return state;
+                }
             }
+
+            if (rule.Entity is not null && uriHash.SequenceEqual(rule.Entity)) {
+                _logger.LogInformation("{url} matched rule {rule} by uri hash", @event.RawContent["url"], rule.ToJson(ignoreNull: true));
+                return state;
+            }
+
+            _logger.LogInformation("uri hash {uriHash} did not match rule's {ruleUriHash}",  Convert.ToBase64String(uriHash), Convert.ToBase64String(rule.Entity));
+
+            if (rule.FileHash is not null && fileHash is not null && rule.FileHash.SequenceEqual(fileHash)) {
+                _logger.LogInformation("{url} matched rule {rule} by file hash", @event.RawContent["url"], rule.ToJson(ignoreNull: true));
+                return state;
+            }
+
+            _logger.LogInformation("file hash {fileHash} did not match rule's {ruleFileHash}", Convert.ToBase64String(fileHash), Convert.ToBase64String(rule.FileHash));
+
+
+            //check pixels every 10% of the way through the image using ImageSharp
+            // var image = Image.Load(await _hs._httpClient.GetStreamAsync(resolvedUri));
         }
-        return false;
+
+
+        _logger.LogInformation("{url} did not match any rules", @event.RawContent["url"]);
+
+        return null;
     }
 }
diff --git a/ExampleBots/MediaModeratorPoC/Bot/MediaModBotConfiguration.cs b/ExampleBots/MediaModeratorPoC/Bot/MediaModBotConfiguration.cs
index c441e2a..d848abe 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/MediaModBotConfiguration.cs
+++ b/ExampleBots/MediaModeratorPoC/Bot/MediaModBotConfiguration.cs
@@ -3,11 +3,8 @@ using Microsoft.Extensions.Configuration;
 namespace MediaModeratorPoC.Bot;
 
 public class MediaModBotConfiguration {
-    public MediaModBotConfiguration(IConfiguration config) {
-        config.GetRequiredSection("MediaMod").Bind(this);
-    }
-    public string Homeserver { get; set; } = "";
-    public string AccessToken { get; set; } = "";
-    public string Prefix { get; set; }
+    public MediaModBotConfiguration(IConfiguration config) => config.GetRequiredSection("MediaMod").Bind(this);
+
     public List<string> Admins { get; set; } = new();
+    public bool DemoMode { get; set; } = false;
 }
diff --git a/ExampleBots/MediaModeratorPoC/Bot/StateEventTypes/MediaPolicyStateEventData.cs b/ExampleBots/MediaModeratorPoC/Bot/StateEventTypes/MediaPolicyStateEventData.cs
index 812ccf2..f37d33c 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/StateEventTypes/MediaPolicyStateEventData.cs
+++ b/ExampleBots/MediaModeratorPoC/Bot/StateEventTypes/MediaPolicyStateEventData.cs
@@ -4,15 +4,20 @@ using LibMatrix.Interfaces;
 
 namespace MediaModeratorPoC.Bot.StateEventTypes;
 
-
+[MatrixEvent(EventName = "gay.rory.media_moderator_poc.rule.homeserver")]
 [MatrixEvent(EventName = "gay.rory.media_moderator_poc.rule.media")]
 public class MediaPolicyStateEventData : IStateEventType {
     /// <summary>
-    ///     Entity this ban applies to, can use * and ? as globs.
-    ///     This is an MXC URI.
+    ///     This is an MXC URI, hashed with SHA3-256.
     /// </summary>
     [JsonPropertyName("entity")]
-    public string Entity { get; set; }
+    public byte[] Entity { get; set; }
+
+    /// <summary>
+    /// Server this ban applies to, can use * and ? as globs.
+    /// </summary>
+    [JsonPropertyName("server_entity")]
+    public string? ServerEntity { get; set; }
 
     /// <summary>
     ///     Reason this user is banned
@@ -21,10 +26,10 @@ public class MediaPolicyStateEventData : IStateEventType {
     public string? Reason { get; set; }
 
     /// <summary>
-    ///     Suggested action to take
+    ///     Suggested action to take, one of `ban`, `kick`, `mute`, `redact`, `spoiler`, `warn` or `warn_admins`
     /// </summary>
     [JsonPropertyName("recommendation")]
-    public string? Recommendation { get; set; }
+    public string Recommendation { get; set; } = "warn";
 
     /// <summary>
     ///     Expiry time in milliseconds since the unix epoch, or null if the ban has no expiry.
@@ -39,6 +44,9 @@ public class MediaPolicyStateEventData : IStateEventType {
     [JsonPropertyName("gay.rory.matrix_room_utils.readable_expiry_time_utc")]
     public DateTime? ExpiryDateTime {
         get => Expiry == null ? null : DateTimeOffset.FromUnixTimeMilliseconds(Expiry.Value).DateTime;
-        set => Expiry = ((DateTimeOffset)value).ToUnixTimeMilliseconds();
+        set => Expiry = value is null ? null : ((DateTimeOffset)value).ToUnixTimeMilliseconds();
     }
+
+    [JsonPropertyName("file_hash")]
+    public byte[]? FileHash { get; set; }
 }
diff --git a/ExampleBots/MediaModeratorPoC/MediaModeratorPoC.csproj b/ExampleBots/MediaModeratorPoC/MediaModeratorPoC.csproj
index 1fc421a..c3a5d87 100644
--- a/ExampleBots/MediaModeratorPoC/MediaModeratorPoC.csproj
+++ b/ExampleBots/MediaModeratorPoC/MediaModeratorPoC.csproj
@@ -17,12 +17,13 @@
   </PropertyGroup>

 

   <ItemGroup>

-      <ProjectReference Include="..\..\..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj" />

+<!--      <ProjectReference Include="..\..\..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj" />-->

       <ProjectReference Include="..\..\LibMatrix\LibMatrix.csproj" />

+      <ProjectReference Include="..\..\Utilities\LibMatrix.Utilities.Bot\LibMatrix.Utilities.Bot.csproj" />

   </ItemGroup>

 

   <ItemGroup>

-    <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0-preview.5.23280.8" />

+    <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0-preview.7.23375.6" />

   </ItemGroup>

   <ItemGroup>

     <Content Include="appsettings*.json">

diff --git a/ExampleBots/MediaModeratorPoC/Program.cs b/ExampleBots/MediaModeratorPoC/Program.cs
index 1bc0c62..413d91d 100644
--- a/ExampleBots/MediaModeratorPoC/Program.cs
+++ b/ExampleBots/MediaModeratorPoC/Program.cs
@@ -1,9 +1,8 @@
 // See https://aka.ms/new-console-template for more information

 

-using ArcaneLibs;

 using LibMatrix.Services;

+using LibMatrix.Utilities.Bot;

 using MediaModeratorPoC.Bot;

-using MediaModeratorPoC.Bot.Interfaces;

 using Microsoft.Extensions.DependencyInjection;

 using Microsoft.Extensions.Hosting;

 

@@ -17,11 +16,9 @@ var host = Host.CreateDefaultBuilder(args).ConfigureServices((_, services) => {
         )

     );

     services.AddSingleton<MediaModBotConfiguration>();

+

     services.AddRoryLibMatrixServices();

-    foreach (var commandClass in new ClassCollector<ICommand>().ResolveFromAllAccessibleAssemblies()) {

-        Console.WriteLine($"Adding command {commandClass.Name}");

-        services.AddScoped(typeof(ICommand), commandClass);

-    }

+    services.AddBot(withCommands: true);

 

     services.AddHostedService<MediaModBot>();

 }).UseConsoleLifetime().Build();

diff --git a/ExampleBots/MediaModeratorPoC/appsettings.Development.json b/ExampleBots/MediaModeratorPoC/appsettings.Development.json
index a895170..600efc3 100644
--- a/ExampleBots/MediaModeratorPoC/appsettings.Development.json
+++ b/ExampleBots/MediaModeratorPoC/appsettings.Development.json
@@ -6,13 +6,15 @@
       "Microsoft": "Information"
     }
   },
-  "MediaMod": {
+  "LibMatrixBot": {
     // The homeserver to connect to
     "Homeserver": "rory.gay",
     // The access token to use
     "AccessToken": "syt_xxxxxxxxxxxxxxxxx",
     // The command prefix
-    "Prefix": "?",
+    "Prefix": "?"
+  },
+  "MediaMod": {
     // List of people who should be invited to the control room
     "Admins": [
       "@emma:conduit.rory.gay",
diff --git a/ExampleBots/MediaModeratorPoC/Bot/Commands/HelpCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs
index 8d63daa..c975c8b 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/Commands/HelpCommand.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs
@@ -17,9 +17,6 @@ public class HelpCommand(IServiceProvider services) : ICommand {
             sb.AppendLine($"- {command.Name}: {command.Description}");
         }
 
-        await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
-            MessageType = "m.notice",
-            Body = sb.ToString()
-        });
+        await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData(messageType: "m.notice", body: sb.ToString()));
     }
 }
diff --git a/ExampleBots/MediaModeratorPoC/Bot/Commands/PingCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs
index f9f46c2..e7f3b10 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/Commands/PingCommand.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs
@@ -8,8 +8,6 @@ public class PingCommand : ICommand {
     public string Description { get; } = "Pong!";
 
     public async Task Invoke(CommandContext ctx) {
-        await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
-            Body = "pong!"
-        });
+        await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData(body: "pong!"));
     }
 }
diff --git a/ExampleBots/MediaModeratorPoC/Bot/FileStorageProvider.cs b/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs
index d5b991a..d5b991a 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/FileStorageProvider.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs
diff --git a/ExampleBots/MediaModeratorPoC/Bot/Interfaces/ICommand.cs b/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs
index a8fce94..a8fce94 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/Interfaces/ICommand.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs