diff options
-rw-r--r-- | Bot/Commands/CmdCommand.cs | 60 | ||||
-rw-r--r-- | Bot/Commands/HelpCommand.cs | 2 | ||||
-rw-r--r-- | Bot/Commands/PingCommand.cs | 2 | ||||
-rw-r--r-- | Bot/Interfaces/CommandContext.cs | 4 | ||||
-rw-r--r-- | Bot/MRUBot.cs | 8 | ||||
-rw-r--r-- | LibMatrix.ExampleBot.csproj | 4 | ||||
-rw-r--r-- | Program.cs | 2 |
7 files changed, 55 insertions, 27 deletions
diff --git a/Bot/Commands/CmdCommand.cs b/Bot/Commands/CmdCommand.cs index efedbba..5b2828e 100644 --- a/Bot/Commands/CmdCommand.cs +++ b/Bot/Commands/CmdCommand.cs @@ -1,3 +1,4 @@ +using ArcaneLibs.Extensions; using LibMatrix.ExampleBot.Bot.Interfaces; using LibMatrix.StateEventTypes.Spec; @@ -8,7 +9,7 @@ public class CmdCommand : ICommand { 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")); + return Task.FromResult(ctx.MessageEvent.Sender.EndsWith(":rory.gay") || ctx.MessageEvent.Sender.EndsWith(":conduit.rory.gay")); } public async Task Invoke(CommandContext ctx) { @@ -17,28 +18,55 @@ public class CmdCommand : ICommand { cmd = cmd.Trim(); cmd += "\""; - await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData(body: $"Command being executed: `{cmd}`")); + await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent(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 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 = ""; - 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), + EventIdResponse? msgId = await ctx.Room.SendMessageEventAsync("m.room.message", 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("m.room.message", new RoomMessageEventContent { + FormattedBody = $"<pre class=\"language-csharp\">\n{msg}\n</pre>", + Body = msg.RemoveAnsi(), Format = "org.matrix.custom.html" }); + if (msg.Length > 31000) { + await lastSendTask; + msgId = await ctx.Room.SendMessageEventAsync("m.room.message", 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 = $"<pre class=\"language-csharp\">\n{msg}\n</pre>", + // // Body = Markdig.Markdown.ToHtml(msg), + // Body = msg.RemoveAnsi(), + // Format = "org.matrix.custom.html" + // }); + // msg = ""; + // } + // } } } diff --git a/Bot/Commands/HelpCommand.cs b/Bot/Commands/HelpCommand.cs index 09c4e3f..c750130 100644 --- a/Bot/Commands/HelpCommand.cs +++ b/Bot/Commands/HelpCommand.cs @@ -17,6 +17,6 @@ public class HelpCommand(IServiceProvider services) : ICommand { sb.AppendLine($"- {command.Name}: {command.Description}"); } - await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData(body: sb.ToString())); + await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventContent(body: sb.ToString())); } } diff --git a/Bot/Commands/PingCommand.cs b/Bot/Commands/PingCommand.cs index f70cd78..a261a59 100644 --- a/Bot/Commands/PingCommand.cs +++ b/Bot/Commands/PingCommand.cs @@ -8,6 +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 RoomMessageEventContent(body: "pong!")); } } diff --git a/Bot/Interfaces/CommandContext.cs b/Bot/Interfaces/CommandContext.cs index ec61a1e..3715cb6 100644 --- a/Bot/Interfaces/CommandContext.cs +++ b/Bot/Interfaces/CommandContext.cs @@ -7,6 +7,6 @@ namespace LibMatrix.ExampleBot.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 string CommandName => (MessageEvent.TypedContent as RoomMessageEventContent).Body.Split(' ')[0][1..]; + public string[] Args => (MessageEvent.TypedContent as RoomMessageEventContent).Body.Split(' ')[1..]; } diff --git a/Bot/MRUBot.cs b/Bot/MRUBot.cs index 3f69d90..0b4e2ba 100644 --- a/Bot/MRUBot.cs +++ b/Bot/MRUBot.cs @@ -58,7 +58,7 @@ public class MRUBot : IHostedService { args.Value.InviteState.Events.FirstOrDefault(x => 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}"); + $"Got invite to {args.Key} by {inviteEvent.Sender} with reason: {(inviteEvent.TypedContent as RoomMemberEventContent).Reason}"); if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent.Sender == "@mxidupwitch:the-apothecary.club") { try { var senderProfile = await hs.GetProfile(inviteEvent.Sender); @@ -76,12 +76,12 @@ public class MRUBot : IHostedService { var room = await hs.GetRoom(@event.RoomId); // _logger.LogInformation(eventResponse.ToJson(indent: false)); - if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventData message }) { + if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent 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.text", body: "Command not found!")); + new RoomMessageEventContent(messageType: "m.text", body: "Command not found!")); return; } @@ -94,7 +94,7 @@ public class MRUBot : IHostedService { } else { await room.SendMessageEventAsync("m.room.message", - new RoomMessageEventData(messageType: "m.text", body: "You do not have permission to run this command!")); + new RoomMessageEventContent(messageType: "m.text", body: "You do not have permission to run this command!")); } } } diff --git a/LibMatrix.ExampleBot.csproj b/LibMatrix.ExampleBot.csproj index 13cbb15..30ea1e5 100644 --- a/LibMatrix.ExampleBot.csproj +++ b/LibMatrix.ExampleBot.csproj @@ -2,7 +2,7 @@ <PropertyGroup> <OutputType>Exe</OutputType> - <TargetFramework>net8.0</TargetFramework> + <TargetFramework>net7.0</TargetFramework> <LangVersion>preview</LangVersion> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> @@ -22,7 +22,7 @@ </ItemGroup> <ItemGroup> - <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0-preview.7.23375.6" /> + <PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" /> </ItemGroup> <ItemGroup> <Content Include="appsettings*.json"> diff --git a/Program.cs b/Program.cs index 0378ec9..ef40ecb 100644 --- a/Program.cs +++ b/Program.cs @@ -25,7 +25,7 @@ var host = Host.CreateDefaultBuilder(args).ConfigureServices((_, services) => { services.AddScoped(typeof(ICommand), commandClass); } - services.AddHostedService<ServerRoomSizeCalulator>(); + // services.AddHostedService<ServerRoomSizeCalulator>(); services.AddHostedService<MRUBot>(); }).UseConsoleLifetime().Build(); |