This commit is contained in:
Madpeter 2022-10-24 22:26:50 +01:00
parent ed64a509bf
commit e478053988
6 changed files with 276 additions and 51 deletions

View file

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace SecondBotEvents.Config
{
public class CustomCommandsConfig : Config
{
public CustomCommandsConfig(bool fromENV, string fromFolder = "") : base(fromENV, fromFolder) { }
protected override void MakeSettings()
{
filename = "customcommands";
settings.Add("count");
int loop = 1;
while(loop <= GetCount())
{
settings.Add(loop.ToString() + "_trigger");
settings.Add(loop.ToString() + "_args");
settings.Add(loop.ToString() + "_steps");
int loop2 = 1;
while(loop2 <= GetCommandSteps(loop))
{
settings.Add(loop.ToString() + "_commands_"+loop2.ToString());
loop2++;
}
loop++;
}
}
public string GetCommandStep(int commandIndex, int step)
{
return ReadSettingAsString(commandIndex.ToString() + "_commands_"+ step.ToString(), "Logoff");
}
public string GetCommandTrigger(int commandIndex)
{
return ReadSettingAsString(commandIndex.ToString() + "_trigger", "exitplease");
}
public int GetCommandSteps(int commandIndex)
{
return ReadSettingAsInt(commandIndex.ToString() + "_steps", 0);
}
public int GetCommandArgs(int commandIndex)
{
return ReadSettingAsInt(commandIndex.ToString() + "_args", 0);
}
public int GetCount()
{
return ReadSettingAsInt("count", 1);
}
}
}

View file

@ -14,6 +14,8 @@ namespace SecondBotEvents.Config
protected List<string> settings = new List<string>();
protected string filename = "config";
protected bool loaded = false;
protected bool tryLoadfromEnv = false;
public Config(bool fromENV, string fromFolder="")
{
MakeSettings();
@ -29,6 +31,7 @@ namespace SecondBotEvents.Config
{
if(fromENV == true)
{
tryLoadfromEnv = true;
LoadSettingsFromEnv();
return;
}
@ -88,14 +91,7 @@ namespace SecondBotEvents.Config
{
foreach (string setting in settings)
{
string key = filename + "_" + setting;
string value = Environment.GetEnvironmentVariable(key);
if (SecondbotHelpers.notempty(value) == false)
{
mysettings.Add(setting, "");
continue;
}
mysettings.Add(setting, value);
TryLoadSetting(setting);
}
loaded = true;
}
@ -105,11 +101,31 @@ namespace SecondBotEvents.Config
}
protected bool TryLoadSetting(string setting)
{
if(tryLoadfromEnv == false)
{
return false;
}
string key = filename + "_" + setting;
string value = Environment.GetEnvironmentVariable(key);
if(value == null)
{
return false;
}
mysettings.Add(setting, value);
return true;
}
protected string ReadSettingAsString(string key, string defaultValue)
{
if (mysettings.ContainsKey(key) == false)
{
return defaultValue;
if(TryLoadSetting(key) == false)
{
return defaultValue;
}
}
return mysettings[key];
}

View file

@ -39,6 +39,8 @@ namespace SecondBotEvents
public EventsService EventsService { get { return (EventsService)GetService("EventsService"); } }
public DialogService DialogService { get { return (DialogService)GetService("DialogService"); } }
public CustomCommandsService CustomCommandsService { get { return (CustomCommandsService)GetService("CustomCommandsService"); } }
private EventHandler<SystemStatusMessage> SystemStatusMessages;
public event EventHandler<SystemStatusMessage> SystemStatusMessagesEvent
@ -46,6 +48,7 @@ namespace SecondBotEvents
add { lock (SystemStatusMessagesEventsLockable) { SystemStatusMessages += value; } }
remove { lock (SystemStatusMessagesEventsLockable) { SystemStatusMessages -= value; } }
}
private readonly object SystemStatusMessagesEventsLockable = new object();
public void TriggerSystemStatusMessageEvent(bool change, string message)
@ -149,6 +152,7 @@ namespace SecondBotEvents
RegisterService("HomeboundService");
RegisterService("EventsService");
RegisterService("DialogService");
RegisterService("CustomCommandsService");
if (BotClient.IsLoaded() == false)
{
Console.WriteLine("Config is not loaded :(");

View file

@ -132,21 +132,27 @@ namespace SecondBotEvents.Services
{
return;
}
SignedCommand C = new SignedCommand(e.IM.Message,
requireSigning,
myConfig.GetEnforceTimeWindow(),
CommandInterfaceCaller(e.IM.Message, requireSigning);
}
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()
);
if (C.accepted == false)
{
return;
return new KeyValuePair<bool, string>(false, "Not accepted via signing");
}
KeyValuePair<bool, string> reply = RunCommand(C);
if(C.replyTarget != null)
KeyValuePair<bool, string> reply = RunCommand(C, viaCustomCommand);
if (C.replyTarget != null)
{
SmartCommandReply(C.replyTarget, reply.Value, C.command);
}
return reply;
}
public void SmartCommandReply(string target, string output, string command)
@ -210,21 +216,10 @@ namespace SecondBotEvents.Services
}
protected HttpClient HTTPclient = new HttpClient();
public KeyValuePair<bool, string> RunCommand(SignedCommand C)
protected KeyValuePair<bool, string> RunBaseCommand(SignedCommand C)
{
if(acceptNewCommands == false)
{
return new KeyValuePair<bool, string>(false, "Not accepting commands");
}
try
{
string lowerName = C.command.ToLower();
if(commandnameLowerToReal.ContainsKey(lowerName) == false)
{
return new KeyValuePair<bool, string>(false, "Unknown command");
}
C.command = commandnameLowerToReal[lowerName];
CommandsAPI Endpoint = commandEndpoints[endpointcommandmap[C.command]];
MethodInfo theMethod = Endpoint.GetType().GetMethod(C.command);
if (theMethod != null)
@ -264,6 +259,36 @@ namespace SecondBotEvents.Services
return new KeyValuePair<bool, string>(false, e.Message);
}
}
public KeyValuePair<bool, string> RunCommand(SignedCommand C, bool inCustomCommand=false)
{
if(acceptNewCommands == false)
{
return new KeyValuePair<bool, string>(false, "Not accepting commands");
}
try
{
string lowerName = C.command.ToLower();
if(commandnameLowerToReal.ContainsKey(lowerName) == false)
{
return new KeyValuePair<bool, string>(false, "Unknown command");
}
C.command = commandnameLowerToReal[lowerName];
if(master.CustomCommandsService.HasCommand(C.command) == false)
{
if(inCustomCommand == true)
{
return new KeyValuePair<bool, string>(false, "Custom command chaining is not allowed");
}
return master.CustomCommandsService.RunCommand(C);
}
return RunBaseCommand(C);
}
catch (Exception e)
{
return new KeyValuePair<bool, string>(false, e.Message);
}
}
public string[] GetFullListOfCommands()
{

View file

@ -0,0 +1,123 @@
using OpenMetaverse;
using SecondBotEvents.Config;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace SecondBotEvents.Services
{
public class CustomCommandsService: BotServices
{
protected CustomCommandsConfig myConfig;
protected bool botConnected = false;
protected Dictionary<string, CustomCommand> commands = new Dictionary<string, CustomCommand>();
public bool HasCommand(string C)
{
return commands.ContainsKey(C);
}
public KeyValuePair<bool, string> RunCommand(SignedCommand C)
{
if(commands.ContainsKey(C.command) == false)
{
return new KeyValuePair<bool, string>(false, "Unknown custom command");
}
CustomCommand Cc = commands[C.command];
if(C.args.Count() < Cc.args)
{
return new KeyValuePair<bool, string>(false, "Incorrect number of args");
}
int commandsRun = 0;
foreach(string A in Cc.steps)
{
string Av = A;
int loop = 1;
while(loop <= Cc.args)
{
Av = Av.Replace("[C_ARG_"+ loop.ToString()+"]", C.args[loop - 1]);
loop++;
}
KeyValuePair<bool,string> reply = master.CommandsService.CommandInterfaceCaller(Av, false, true);
if(reply.Key == true)
{
commandsRun++;
}
}
if(commandsRun == 0)
{
return new KeyValuePair<bool, string>(false, "all custom commands have failed for: "+C.command);
}
return new KeyValuePair<bool, string>(false, C.command+": ran with "+commandsRun.ToString()+" commands issued");
}
public CustomCommandsService(EventsSecondBot setMaster) : base(setMaster)
{
myConfig = new CustomCommandsConfig(master.fromEnv, master.fromFolder);
int loop = 1;
while(loop <= myConfig.GetCount())
{
CustomCommand C = new CustomCommand();
C.trigger = myConfig.GetCommandTrigger(loop);
C.args = myConfig.GetCommandArgs(loop);
int loop2 = 1;
while(loop2 <= myConfig.GetCommandSteps(loop))
{
C.steps.Add(myConfig.GetCommandStep(loop, loop2));
loop2++;
}
commands.Add(C.trigger, C);
loop++;
}
}
public override string Status()
{
if (myConfig == null)
{
return "Config broken";
}
else if (botConnected == false)
{
return "Waiting for client";
}
return "Active "+myConfig.GetCount().ToString()+" custom commands";
}
protected void BotClientRestart(object o, BotClientNotice e)
{
Console.WriteLine("CustomCommands service [Attached to new client]");
GetClient().Network.LoggedOut += BotLoggedOut;
GetClient().Network.SimConnected += BotLoggedIn;
}
protected void BotLoggedOut(object o, LoggedOutEventArgs e)
{
GetClient().Network.SimConnected += BotLoggedIn;
Console.WriteLine("CustomCommands service [waiting for new client]");
}
protected void BotLoggedIn(object o, SimConnectedEventArgs e)
{
GetClient().Network.SimConnected -= BotLoggedIn;
botConnected = true;
}
public override void Start()
{
Stop();
master.BotClientNoticeEvent += BotClientRestart;
}
public override void Stop()
{
master.BotClientNoticeEvent -= BotClientRestart;
}
}
public class CustomCommand
{
public string trigger = "";
public int args = 0;
public List<string> steps = new List<string>();
}
}

View file

@ -144,30 +144,7 @@ namespace SecondBotEvents.Services
}
}
protected void BotClientRestart(object o, BotClientNotice e)
{
Console.WriteLine("Dialog service [Attached to new client]");
GetClient().Network.LoggedOut += BotLoggedOut;
GetClient().Network.SimConnected += BotLoggedIn;
}
protected void BotLoggedOut(object o, LoggedOutEventArgs e)
{
GetClient().Network.SimConnected += BotLoggedIn;
Console.WriteLine("Dialog service [waiting for new client]");
if (GetClient() != null)
{
GetClient().Self.ScriptDialog -= DialogWindowEvent;
}
}
protected void BotLoggedIn(object o, SimConnectedEventArgs e)
{
GetClient().Network.SimConnected -= BotLoggedIn;
botConnected = true;
// attach events
GetClient().Self.ScriptDialog += DialogWindowEvent;
}
protected Dictionary<int, ScriptDialogEventArgs> DialogWindows = new Dictionary<int, ScriptDialogEventArgs>();
protected Dictionary<int, long> DialogWindowsExpire = new Dictionary<int, long>();
@ -214,8 +191,31 @@ namespace SecondBotEvents.Services
}
}
protected void BotClientRestart(object o, BotClientNotice e)
{
Console.WriteLine("Dialog service [Attached to new client]");
GetClient().Network.LoggedOut += BotLoggedOut;
GetClient().Network.SimConnected += BotLoggedIn;
}
protected void BotLoggedOut(object o, LoggedOutEventArgs e)
{
GetClient().Network.SimConnected += BotLoggedIn;
Console.WriteLine("Dialog service [waiting for new client]");
if (GetClient() != null)
{
GetClient().Self.ScriptDialog -= DialogWindowEvent;
}
}
protected void BotLoggedIn(object o, SimConnectedEventArgs e)
{
GetClient().Network.SimConnected -= BotLoggedIn;
botConnected = true;
// attach events
GetClient().Self.ScriptDialog += DialogWindowEvent;
}
public override void Start()
{