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("Username");
|
||||||
settings.Add("Password");
|
settings.Add("Password");
|
||||||
settings.Add("LoginURI");
|
settings.Add("LoginURI");
|
||||||
|
settings.Add("LogCommands");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool GetLogCommands()
|
||||||
|
{
|
||||||
|
return ReadSettingAsBool("LogCommands");
|
||||||
|
}
|
||||||
public string GetUsername()
|
public string GetUsername()
|
||||||
{
|
{
|
||||||
return ReadSettingAsString("Username","Firstname Lastname");
|
return ReadSettingAsString("Username","Firstname Lastname");
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ namespace SecondBotEvents.Services
|
||||||
public class BotClientService : BotServices
|
public class BotClientService : BotServices
|
||||||
{
|
{
|
||||||
public GridClient client = null;
|
public GridClient client = null;
|
||||||
protected BasicConfig basicCfg = null;
|
public BasicConfig basicCfg = null;
|
||||||
|
|
||||||
protected bool ExitBot = false;
|
protected bool ExitBot = false;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,31 @@ using System.Net.Http;
|
||||||
|
|
||||||
namespace SecondBotEvents.Services
|
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
|
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 CommandsConfig myConfig = null;
|
||||||
public bool acceptNewCommands = false;
|
public bool acceptNewCommands = false;
|
||||||
public CommandsService(EventsSecondBot setMaster) : base(setMaster)
|
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)
|
public KeyValuePair<bool, string> CommandInterfaceCaller(string message, bool requireSigning=false, bool viaCustomCommand=false)
|
||||||
{
|
{
|
||||||
SignedCommand C = new SignedCommand(message,
|
string source = "command interface";
|
||||||
requireSigning,
|
if(viaCustomCommand == true)
|
||||||
myConfig.GetEnforceTimeWindow(),
|
{
|
||||||
myConfig.GetTimeWindowSecs(),
|
source = "customcommand";
|
||||||
myConfig.GetSharedSecret()
|
}
|
||||||
);
|
SignedCommand C = new SignedCommand(this,source,message,requireSigning,myConfig.GetEnforceTimeWindow(),myConfig.GetTimeWindowSecs(),myConfig.GetSharedSecret());
|
||||||
if (C.accepted == false)
|
if (C.accepted == false)
|
||||||
{
|
{
|
||||||
return new KeyValuePair<bool, string>(false, "Not accepted via signing");
|
return new KeyValuePair<bool, string>(false, "Not accepted via signing");
|
||||||
|
|
@ -334,6 +357,17 @@ namespace SecondBotEvents.Services
|
||||||
acceptNewCommands = true;
|
acceptNewCommands = true;
|
||||||
LogFormater.Info("Commands service [accepting IM commands]");
|
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
|
public class SignedCommand
|
||||||
|
|
@ -344,29 +378,39 @@ namespace SecondBotEvents.Services
|
||||||
public string replyTarget = null;
|
public string replyTarget = null;
|
||||||
public bool accepted = false;
|
public bool accepted = false;
|
||||||
public long unixtimeOfCommand = 0;
|
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);
|
UnpackInput(input);
|
||||||
if (requireSigning == false)
|
if (requireSigning == false)
|
||||||
{
|
{
|
||||||
accepted = true; // just accept the command
|
accepted = true; // just accept the command
|
||||||
|
master.CommandNotice(command, source, String.Join("@@@", args), accepted);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Vaildate(requireTimewindow, windowSize, secret);
|
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;
|
command = setCommand;
|
||||||
signingCode = setSigningCode;
|
signingCode = setSigningCode;
|
||||||
args = setArgs;
|
args = setArgs;
|
||||||
unixtimeOfCommand = setUnixtime;
|
unixtimeOfCommand = setUnixtime;
|
||||||
replyTarget = setReplyTarget;
|
replyTarget = setReplyTarget;
|
||||||
|
|
||||||
if (requireSigning == false)
|
if (requireSigning == false)
|
||||||
{
|
{
|
||||||
accepted = true; // just accept the command
|
accepted = true; // just accept the command
|
||||||
|
master.CommandNotice(command, source, String.Join("@@@", args), accepted);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Vaildate(requireTimewindow, windowSize, secret);
|
Vaildate(requireTimewindow, windowSize, secret);
|
||||||
|
master.CommandNotice(command, source, String.Join("@@@", args), accepted);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void Vaildate(bool requireTimewindow, int windowSize, string secret)
|
protected void Vaildate(bool requireTimewindow, int windowSize, string secret)
|
||||||
|
|
@ -388,12 +432,12 @@ namespace SecondBotEvents.Services
|
||||||
raw += unixtimeOfCommand.ToString();
|
raw += unixtimeOfCommand.ToString();
|
||||||
}
|
}
|
||||||
raw += secret;
|
raw += secret;
|
||||||
LogFormater.Info("raw="+raw);
|
|
||||||
string cooked = SecondbotHelpers.GetSHA1(raw);
|
string cooked = SecondbotHelpers.GetSHA1(raw);
|
||||||
if (cooked != signingCode)
|
if (cooked != signingCode)
|
||||||
{
|
{
|
||||||
return; // invaild signing code
|
return; // invaild signing code
|
||||||
}
|
}
|
||||||
|
|
||||||
accepted = true;
|
accepted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -224,7 +224,10 @@ namespace SecondBotEvents.Services
|
||||||
ReplyToMessage(message, "Notices require title and a message split with @@@!");
|
ReplyToMessage(message, "Notices require title and a message split with @@@!");
|
||||||
return;
|
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)
|
if (bits.Length == 3)
|
||||||
{
|
{
|
||||||
C.command = "GroupnoticeWithAttachment";
|
C.command = "GroupnoticeWithAttachment";
|
||||||
|
|
@ -239,7 +242,8 @@ namespace SecondBotEvents.Services
|
||||||
List<string> args = new List<string>();
|
List<string> args = new List<string>();
|
||||||
args.Add(groupuuid.ToString());
|
args.Add(groupuuid.ToString());
|
||||||
args.Add(cleaned);
|
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;
|
return;
|
||||||
}
|
}
|
||||||
else if(cleaned.StartsWith("!invite") == true)
|
else if(cleaned.StartsWith("!invite") == true)
|
||||||
|
|
@ -250,7 +254,8 @@ namespace SecondBotEvents.Services
|
||||||
args.Add(groupuuid.ToString());
|
args.Add(groupuuid.ToString());
|
||||||
args.Add(cleaned);
|
args.Add(cleaned);
|
||||||
args.Add("everyone");
|
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;
|
return;
|
||||||
}
|
}
|
||||||
else if(cleaned.StartsWith("!ban") == true)
|
else if(cleaned.StartsWith("!ban") == true)
|
||||||
|
|
@ -261,7 +266,8 @@ namespace SecondBotEvents.Services
|
||||||
args.Add(groupuuid.ToString());
|
args.Add(groupuuid.ToString());
|
||||||
args.Add(cleaned);
|
args.Add(cleaned);
|
||||||
args.Add("true");
|
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;
|
return;
|
||||||
}
|
}
|
||||||
GetClient().Self.InstantMessageGroup(groupuuid, message.Author.Username + ": " + message.CleanContent);
|
GetClient().Self.InstantMessageGroup(groupuuid, message.Author.Username + ": " + message.CleanContent);
|
||||||
|
|
@ -356,7 +362,8 @@ namespace SecondBotEvents.Services
|
||||||
if (message.Author.Id != DiscordClient.CurrentUser.Id)
|
if (message.Author.Id != DiscordClient.CurrentUser.Id)
|
||||||
{
|
{
|
||||||
// do command
|
// do command
|
||||||
RunCommandFromMessage(new SignedCommand(message.Content, false, false, 0, ""), message);
|
RunCommandFromMessage(new SignedCommand(master.CommandsService,
|
||||||
|
"discordapi", message.Content, false, false, 0, ""), message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -298,7 +298,9 @@ namespace SecondBotEvents.Services
|
||||||
{
|
{
|
||||||
return "No bot connected yet";
|
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)
|
if(C.accepted == false)
|
||||||
{
|
{
|
||||||
return "Command request rejected";
|
return "Command request rejected";
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue