about summary refs log tree commit diff
path: root/ExampleBots/MediaModeratorPoC/Bot/Commands
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-04 06:29:00 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-04 06:29:00 +0200
commit9dcce18cda5317ea1150eed06d6589b6285577e6 (patch)
tree1b36a9ddffa312e58daab075c43fb482c2bae905 /ExampleBots/MediaModeratorPoC/Bot/Commands
parentToo many changes to name... (diff)
downloadLibMatrix-9dcce18cda5317ea1150eed06d6589b6285577e6.tar.xz
Add start of Media Moderator PoC bot
Diffstat (limited to 'ExampleBots/MediaModeratorPoC/Bot/Commands')
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs66
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/Commands/CmdCommand.cs46
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/Commands/HelpCommand.cs25
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/Commands/PingCommand.cs15
4 files changed, 152 insertions, 0 deletions
diff --git a/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs b/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs
new file mode 100644
index 0000000..d0b5674
--- /dev/null
+++ b/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs
@@ -0,0 +1,66 @@
+using LibMatrix.Responses;
+using LibMatrix.StateEventTypes.Spec;
+using MediaModeratorPoC.Bot.AccountData;
+using MediaModeratorPoC.Bot.Interfaces;
+using MediaModeratorPoC.Bot.StateEventTypes;
+
+namespace MediaModeratorPoC.Bot.Commands;
+
+public class BanMediaCommand(IServiceProvider services) : 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";
+
+    public async Task<bool> CanInvoke(CommandContext ctx) {
+        //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");
+        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"
+            });
+        }
+        return isAdmin;
+    }
+
+    public async Task Invoke(CommandContext ctx) {
+        //check if reply
+        if ((ctx.MessageEvent.TypedContent as RoomMessageEventData).RelatesTo is { InReplyTo: not null } ) {
+            var messageContent = ctx.MessageEvent.TypedContent as RoomMessageEventData;
+            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"
+                });
+
+                //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
+                });
+            }
+            catch (Exception e) {
+                await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
+                    Body = $"Error: {e.Message}",
+                    MessageType = "m.text"
+                });
+            }
+        }
+        else {
+            await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
+                Body = "This command must be used in reply to a message!",
+                MessageType = "m.text",
+            });
+        }
+    }
+}
diff --git a/ExampleBots/MediaModeratorPoC/Bot/Commands/CmdCommand.cs b/ExampleBots/MediaModeratorPoC/Bot/Commands/CmdCommand.cs
new file mode 100644
index 0000000..14c4334
--- /dev/null
+++ b/ExampleBots/MediaModeratorPoC/Bot/Commands/CmdCommand.cs
@@ -0,0 +1,46 @@
+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/ExampleBots/MediaModeratorPoC/Bot/Commands/HelpCommand.cs
new file mode 100644
index 0000000..8d63daa
--- /dev/null
+++ b/ExampleBots/MediaModeratorPoC/Bot/Commands/HelpCommand.cs
@@ -0,0 +1,25 @@
+using System.Text;
+using LibMatrix.StateEventTypes.Spec;
+using MediaModeratorPoC.Bot.Interfaces;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace MediaModeratorPoC.Bot.Commands;
+
+public class HelpCommand(IServiceProvider services) : ICommand {
+    public string Name { get; } = "help";
+    public string Description { get; } = "Displays this help message";
+
+    public async Task Invoke(CommandContext ctx) {
+        var sb = new StringBuilder();
+        sb.AppendLine("Available commands:");
+        var commands = services.GetServices<ICommand>().ToList();
+        foreach (var command in commands) {
+            sb.AppendLine($"- {command.Name}: {command.Description}");
+        }
+
+        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/ExampleBots/MediaModeratorPoC/Bot/Commands/PingCommand.cs
new file mode 100644
index 0000000..f9f46c2
--- /dev/null
+++ b/ExampleBots/MediaModeratorPoC/Bot/Commands/PingCommand.cs
@@ -0,0 +1,15 @@
+using LibMatrix.StateEventTypes.Spec;
+using MediaModeratorPoC.Bot.Interfaces;
+
+namespace MediaModeratorPoC.Bot.Commands;
+
+public class PingCommand : ICommand {
+    public string Name { get; } = "ping";
+    public string Description { get; } = "Pong!";
+
+    public async Task Invoke(CommandContext ctx) {
+        await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
+            Body = "pong!"
+        });
+    }
+}