about summary refs log tree commit diff
path: root/ExampleBots/MediaModeratorPoC/Bot/Commands
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/Bot/Commands
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/Commands/BanMediaCommand.cs102
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/Commands/CmdCommand.cs46
-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
4 files changed, 75 insertions, 82 deletions
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/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!"));
     }
 }