From 17c4bb63a9fd598d15cd9515caf9e202df036818 Mon Sep 17 00:00:00 2001 From: Madpeter <57852827+Madpeterz@users.noreply.github.com> Date: Fri, 1 Dec 2023 00:19:22 +0000 Subject: [PATCH] add support to log commands --- SecondBotEvents/Config/BasicConfig.cs | 5 ++ SecondBotEvents/Services/BotClientService.cs | 2 +- SecondBotEvents/Services/CommandsService.cs | 62 +++++++++++++++++--- SecondBotEvents/Services/DiscordService.cs | 17 ++++-- SecondBotEvents/Services/HttpService.cs | 4 +- 5 files changed, 74 insertions(+), 16 deletions(-) diff --git a/SecondBotEvents/Config/BasicConfig.cs b/SecondBotEvents/Config/BasicConfig.cs index b8ad30b..d5bf416 100644 --- a/SecondBotEvents/Config/BasicConfig.cs +++ b/SecondBotEvents/Config/BasicConfig.cs @@ -13,8 +13,13 @@ namespace SecondBotEvents.Config settings.Add("Username"); settings.Add("Password"); settings.Add("LoginURI"); + settings.Add("LogCommands"); } + public bool GetLogCommands() + { + return ReadSettingAsBool("LogCommands"); + } public string GetUsername() { return ReadSettingAsString("Username","Firstname Lastname"); diff --git a/SecondBotEvents/Services/BotClientService.cs b/SecondBotEvents/Services/BotClientService.cs index 0456276..aea377b 100644 --- a/SecondBotEvents/Services/BotClientService.cs +++ b/SecondBotEvents/Services/BotClientService.cs @@ -12,7 +12,7 @@ namespace SecondBotEvents.Services public class BotClientService : BotServices { public GridClient client = null; - protected BasicConfig basicCfg = null; + public BasicConfig basicCfg = null; protected bool ExitBot = false; diff --git a/SecondBotEvents/Services/CommandsService.cs b/SecondBotEvents/Services/CommandsService.cs index 61f7bf0..bee8780 100644 --- a/SecondBotEvents/Services/CommandsService.cs +++ b/SecondBotEvents/Services/CommandsService.cs @@ -11,8 +11,31 @@ using System.Net.Http; namespace SecondBotEvents.Services { + public class BotCommandNotice + { + public string command; + public string args; + public string source; + public bool accepted; + public BotCommandNotice(string Setcommand, string Setargs, string Setsource, bool Setaccepted) + { + command = Setcommand; + args = Setargs; + source = Setsource; + accepted = Setaccepted; + } + } public class CommandsService : BotServices { + private EventHandler BotclientCommandEventNotices; + public event EventHandler BotclientCommandEventNotice + { + add { lock (BotCommandNoticeLockable) { BotclientCommandEventNotices += value; } } + remove { lock (BotCommandNoticeLockable) { BotclientCommandEventNotices -= value; } } + } + private readonly object BotCommandNoticeLockable = new object(); + + public CommandsConfig myConfig = null; public bool acceptNewCommands = false; public CommandsService(EventsSecondBot setMaster) : base(setMaster) @@ -139,12 +162,12 @@ namespace SecondBotEvents.Services public KeyValuePair CommandInterfaceCaller(string message, bool requireSigning=false, bool viaCustomCommand=false) { - SignedCommand C = new SignedCommand(message, - requireSigning, - myConfig.GetEnforceTimeWindow(), - myConfig.GetTimeWindowSecs(), - myConfig.GetSharedSecret() - ); + string source = "command interface"; + if(viaCustomCommand == true) + { + source = "customcommand"; + } + SignedCommand C = new SignedCommand(this,source,message,requireSigning,myConfig.GetEnforceTimeWindow(),myConfig.GetTimeWindowSecs(),myConfig.GetSharedSecret()); if (C.accepted == false) { return new KeyValuePair(false, "Not accepted via signing"); @@ -334,6 +357,17 @@ namespace SecondBotEvents.Services acceptNewCommands = true; LogFormater.Info("Commands service [accepting IM commands]"); } + + public void CommandNotice(string command,string source, string args,bool accepted) + { + BotCommandNotice e = new BotCommandNotice(command, source, args, accepted); + EventHandler handler = BotclientCommandEventNotices; + handler?.Invoke(this, e); + if(master.BotClient.basicCfg.GetLogCommands() == true) + { + LogFormater.Info("Command log:" + JsonConvert.SerializeObject(e)); + } + } } public class SignedCommand @@ -344,29 +378,39 @@ namespace SecondBotEvents.Services public string replyTarget = null; public bool accepted = false; public long unixtimeOfCommand = 0; - public SignedCommand(string input, bool requireSigning, bool requireTimewindow, int windowSize, string secret) + protected CommandsService master; + public SignedCommand(CommandsService setMaster, string source, string input, bool requireSigning, bool requireTimewindow, int windowSize, string secret) { + master = setMaster; UnpackInput(input); if (requireSigning == false) { accepted = true; // just accept the command + master.CommandNotice(command, source, String.Join("@@@", args), accepted); return; } Vaildate(requireTimewindow, windowSize, secret); + master.CommandNotice(command, source, String.Join("@@@", args), accepted); } - public SignedCommand(string setCommand, string setSigningCode, string[] setArgs, int setUnixtime, string setReplyTarget, bool requireTimewindow, int windowSize, string secret, bool requireSigning = true) + + public SignedCommand(CommandsService setMaster, string source, string setCommand, string setSigningCode, string[] setArgs, + int setUnixtime, string setReplyTarget, bool requireTimewindow, int windowSize, string secret, bool requireSigning = true) { + master = setMaster; command = setCommand; signingCode = setSigningCode; args = setArgs; unixtimeOfCommand = setUnixtime; replyTarget = setReplyTarget; + if (requireSigning == false) { accepted = true; // just accept the command + master.CommandNotice(command, source, String.Join("@@@", args), accepted); return; } Vaildate(requireTimewindow, windowSize, secret); + master.CommandNotice(command, source, String.Join("@@@", args), accepted); } protected void Vaildate(bool requireTimewindow, int windowSize, string secret) @@ -388,12 +432,12 @@ namespace SecondBotEvents.Services raw += unixtimeOfCommand.ToString(); } raw += secret; - LogFormater.Info("raw="+raw); string cooked = SecondbotHelpers.GetSHA1(raw); if (cooked != signingCode) { return; // invaild signing code } + accepted = true; } diff --git a/SecondBotEvents/Services/DiscordService.cs b/SecondBotEvents/Services/DiscordService.cs index 63a4a6f..7d4c122 100644 --- a/SecondBotEvents/Services/DiscordService.cs +++ b/SecondBotEvents/Services/DiscordService.cs @@ -224,7 +224,10 @@ namespace SecondBotEvents.Services ReplyToMessage(message, "Notices require title and a message split with @@@!"); return; } - SignedCommand C = new SignedCommand("Groupnotice", null, args.ToArray(), 0, "none", false, 0, "", false); + SignedCommand C = new SignedCommand( + master.CommandsService, + "discordapi", + "Groupnotice", null, args.ToArray(), 0, "none", false, 0, "", false); if (bits.Length == 3) { C.command = "GroupnoticeWithAttachment"; @@ -239,7 +242,8 @@ namespace SecondBotEvents.Services List args = new List(); args.Add(groupuuid.ToString()); args.Add(cleaned); - RunCommandFromMessage(new SignedCommand("GroupEject", null, args.ToArray(), 0, "none", false, 0, "", false), message); + RunCommandFromMessage(new SignedCommand(master.CommandsService, + "discordapi", "GroupEject", null, args.ToArray(), 0, "none", false, 0, "", false), message); return; } else if(cleaned.StartsWith("!invite") == true) @@ -250,7 +254,8 @@ namespace SecondBotEvents.Services args.Add(groupuuid.ToString()); args.Add(cleaned); args.Add("everyone"); - RunCommandFromMessage(new SignedCommand("GroupInvite", null, args.ToArray(), 0, "none", false, 0, "", false), message); + RunCommandFromMessage(new SignedCommand(master.CommandsService, + "discordapi", "GroupInvite", null, args.ToArray(), 0, "none", false, 0, "", false), message); return; } else if(cleaned.StartsWith("!ban") == true) @@ -261,7 +266,8 @@ namespace SecondBotEvents.Services args.Add(groupuuid.ToString()); args.Add(cleaned); args.Add("true"); - RunCommandFromMessage(new SignedCommand("GroupBan", null, args.ToArray(), 0, "none", false, 0, "", false), message); + RunCommandFromMessage(new SignedCommand(master.CommandsService, + "discordapi", "GroupBan", null, args.ToArray(), 0, "none", false, 0, "", false), message); return; } GetClient().Self.InstantMessageGroup(groupuuid, message.Author.Username + ": " + message.CleanContent); @@ -356,7 +362,8 @@ namespace SecondBotEvents.Services if (message.Author.Id != DiscordClient.CurrentUser.Id) { // do command - RunCommandFromMessage(new SignedCommand(message.Content, false, false, 0, ""), message); + RunCommandFromMessage(new SignedCommand(master.CommandsService, + "discordapi", message.Content, false, false, 0, ""), message); return; } } diff --git a/SecondBotEvents/Services/HttpService.cs b/SecondBotEvents/Services/HttpService.cs index 4550bd6..cd0eedb 100644 --- a/SecondBotEvents/Services/HttpService.cs +++ b/SecondBotEvents/Services/HttpService.cs @@ -298,7 +298,9 @@ namespace SecondBotEvents.Services { return "No bot connected yet"; } - SignedCommand C = new(commandName, signing, myArgs, cmdUnixtime, null, true, 5, master.CommandsService.myConfig.GetSharedSecret()); + SignedCommand C = new( + master.CommandsService,"http", + commandName, signing, myArgs, cmdUnixtime, null, true, 5, master.CommandsService.myConfig.GetSharedSecret()); if(C.accepted == false) { return "Command request rejected";