From a481bead16d904da8ad4d6de8d1a8ab006460b31 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Thu, 11 Jan 2024 18:40:42 +0000 Subject: Dev test bot --- .../Bot/Commands/CmdCommand.cs | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs (limited to 'Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs') diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs b/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs new file mode 100644 index 0000000..e690890 --- /dev/null +++ b/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs @@ -0,0 +1,72 @@ +using ArcaneLibs.StringNormalisation; +using LibMatrix.EventTypes.Spec; +using LibMatrix.ExampleBot.Bot.Interfaces; + +namespace LibMatrix.ExampleBot.Bot.Commands; + +public class CmdCommand : ICommand { + public string Name => "cmd"; + public string Description => "Runs a command on the host system"; + + public Task CanInvoke(CommandContext ctx) { + return Task.FromResult(ctx.MessageEvent.Sender.EndsWith(":rory.gay") || ctx.MessageEvent.Sender.EndsWith(":conduit.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(new RoomMessageEventContent(body: $"Command being executed: `{cmd}`")); + + var output = ArcaneLibs.Util.GetCommandOutputAsync( + Environment.OSVersion.Platform == PlatformID.Unix ? "/bin/sh" : "cmd.exe", + (Environment.OSVersion.Platform == PlatformID.Unix ? "-c " : "/c ") + cmd); + // .Replace("`", "\\`") + // .Split("\n").ToList(); + + var msg = ""; + EventIdResponse? msgId = await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { + FormattedBody = $"Waiting for command output...", + Body = msg.RemoveAnsi(), + Format = "m.notice" + }); + + var lastSendTask = Task.CompletedTask; + await foreach (var @out in output) { + Console.WriteLine($"{@out.Length:0000} {@out}"); + msg += @out + "\n"; + if (lastSendTask.IsCompleted) + lastSendTask = ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { + FormattedBody = $"
\n{msg}\n
", + Body = msg.RemoveAnsi(), + Format = "org.matrix.custom.html" + }); + if (msg.Length > 31000) { + await lastSendTask; + msgId = await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { + FormattedBody = $"Waiting for command output...", + Body = msg.RemoveAnsi(), + Format = "m.notice" + }); + 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 > 31500) || output.Count == 0) { + // await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent { + // FormattedBody = $"
\n{msg}\n
", + // // Body = Markdig.Markdown.ToHtml(msg), + // Body = msg.RemoveAnsi(), + // Format = "org.matrix.custom.html" + // }); + // msg = ""; + // } + // } + } +} -- cgit 1.4.1