add support to log commands
This commit is contained in:
parent
ec11f502e6
commit
17c4bb63a9
5 changed files with 74 additions and 16 deletions
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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<BotCommandNotice> BotclientCommandEventNotices;
|
||||
public event EventHandler<BotCommandNotice> 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<bool, string> 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<bool, string>(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<BotCommandNotice> 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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<string> args = new List<string>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
Loading…
Reference in a new issue