wip
This commit is contained in:
parent
ed64a509bf
commit
e478053988
6 changed files with 276 additions and 51 deletions
57
SecondBotEvents/Config/CustomCommandsConfig.cs
Normal file
57
SecondBotEvents/Config/CustomCommandsConfig.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -14,6 +14,8 @@ namespace SecondBotEvents.Config
|
||||||
protected List<string> settings = new List<string>();
|
protected List<string> settings = new List<string>();
|
||||||
protected string filename = "config";
|
protected string filename = "config";
|
||||||
protected bool loaded = false;
|
protected bool loaded = false;
|
||||||
|
protected bool tryLoadfromEnv = false;
|
||||||
|
|
||||||
public Config(bool fromENV, string fromFolder="")
|
public Config(bool fromENV, string fromFolder="")
|
||||||
{
|
{
|
||||||
MakeSettings();
|
MakeSettings();
|
||||||
|
|
@ -29,6 +31,7 @@ namespace SecondBotEvents.Config
|
||||||
{
|
{
|
||||||
if(fromENV == true)
|
if(fromENV == true)
|
||||||
{
|
{
|
||||||
|
tryLoadfromEnv = true;
|
||||||
LoadSettingsFromEnv();
|
LoadSettingsFromEnv();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -88,14 +91,7 @@ namespace SecondBotEvents.Config
|
||||||
{
|
{
|
||||||
foreach (string setting in settings)
|
foreach (string setting in settings)
|
||||||
{
|
{
|
||||||
string key = filename + "_" + setting;
|
TryLoadSetting(setting);
|
||||||
string value = Environment.GetEnvironmentVariable(key);
|
|
||||||
if (SecondbotHelpers.notempty(value) == false)
|
|
||||||
{
|
|
||||||
mysettings.Add(setting, "");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
mysettings.Add(setting, value);
|
|
||||||
}
|
}
|
||||||
loaded = true;
|
loaded = true;
|
||||||
}
|
}
|
||||||
|
|
@ -105,12 +101,32 @@ 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)
|
protected string ReadSettingAsString(string key, string defaultValue)
|
||||||
{
|
{
|
||||||
if (mysettings.ContainsKey(key) == false)
|
if (mysettings.ContainsKey(key) == false)
|
||||||
|
{
|
||||||
|
if(TryLoadSetting(key) == false)
|
||||||
{
|
{
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return mysettings[key];
|
return mysettings[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,8 @@ namespace SecondBotEvents
|
||||||
public EventsService EventsService { get { return (EventsService)GetService("EventsService"); } }
|
public EventsService EventsService { get { return (EventsService)GetService("EventsService"); } }
|
||||||
public DialogService DialogService { get { return (DialogService)GetService("DialogService"); } }
|
public DialogService DialogService { get { return (DialogService)GetService("DialogService"); } }
|
||||||
|
|
||||||
|
public CustomCommandsService CustomCommandsService { get { return (CustomCommandsService)GetService("CustomCommandsService"); } }
|
||||||
|
|
||||||
|
|
||||||
private EventHandler<SystemStatusMessage> SystemStatusMessages;
|
private EventHandler<SystemStatusMessage> SystemStatusMessages;
|
||||||
public event EventHandler<SystemStatusMessage> SystemStatusMessagesEvent
|
public event EventHandler<SystemStatusMessage> SystemStatusMessagesEvent
|
||||||
|
|
@ -46,6 +48,7 @@ namespace SecondBotEvents
|
||||||
add { lock (SystemStatusMessagesEventsLockable) { SystemStatusMessages += value; } }
|
add { lock (SystemStatusMessagesEventsLockable) { SystemStatusMessages += value; } }
|
||||||
remove { lock (SystemStatusMessagesEventsLockable) { SystemStatusMessages -= value; } }
|
remove { lock (SystemStatusMessagesEventsLockable) { SystemStatusMessages -= value; } }
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly object SystemStatusMessagesEventsLockable = new object();
|
private readonly object SystemStatusMessagesEventsLockable = new object();
|
||||||
|
|
||||||
public void TriggerSystemStatusMessageEvent(bool change, string message)
|
public void TriggerSystemStatusMessageEvent(bool change, string message)
|
||||||
|
|
@ -149,6 +152,7 @@ namespace SecondBotEvents
|
||||||
RegisterService("HomeboundService");
|
RegisterService("HomeboundService");
|
||||||
RegisterService("EventsService");
|
RegisterService("EventsService");
|
||||||
RegisterService("DialogService");
|
RegisterService("DialogService");
|
||||||
|
RegisterService("CustomCommandsService");
|
||||||
if (BotClient.IsLoaded() == false)
|
if (BotClient.IsLoaded() == false)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Config is not loaded :(");
|
Console.WriteLine("Config is not loaded :(");
|
||||||
|
|
|
||||||
|
|
@ -132,7 +132,12 @@ namespace SecondBotEvents.Services
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SignedCommand C = new SignedCommand(e.IM.Message,
|
CommandInterfaceCaller(e.IM.Message, requireSigning);
|
||||||
|
}
|
||||||
|
|
||||||
|
public KeyValuePair<bool, string> CommandInterfaceCaller(string message, bool requireSigning=false, bool viaCustomCommand=false)
|
||||||
|
{
|
||||||
|
SignedCommand C = new SignedCommand(message,
|
||||||
requireSigning,
|
requireSigning,
|
||||||
myConfig.GetEnforceTimeWindow(),
|
myConfig.GetEnforceTimeWindow(),
|
||||||
myConfig.GetTimeWindowSecs(),
|
myConfig.GetTimeWindowSecs(),
|
||||||
|
|
@ -140,13 +145,14 @@ namespace SecondBotEvents.Services
|
||||||
);
|
);
|
||||||
if (C.accepted == false)
|
if (C.accepted == false)
|
||||||
{
|
{
|
||||||
return;
|
return new KeyValuePair<bool, string>(false, "Not accepted via signing");
|
||||||
}
|
}
|
||||||
KeyValuePair<bool, string> reply = RunCommand(C);
|
KeyValuePair<bool, string> reply = RunCommand(C, viaCustomCommand);
|
||||||
if(C.replyTarget != null)
|
if (C.replyTarget != null)
|
||||||
{
|
{
|
||||||
SmartCommandReply(C.replyTarget, reply.Value, C.command);
|
SmartCommandReply(C.replyTarget, reply.Value, C.command);
|
||||||
}
|
}
|
||||||
|
return reply;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SmartCommandReply(string target, string output, string command)
|
public void SmartCommandReply(string target, string output, string command)
|
||||||
|
|
@ -210,21 +216,10 @@ namespace SecondBotEvents.Services
|
||||||
}
|
}
|
||||||
protected HttpClient HTTPclient = new HttpClient();
|
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
|
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]];
|
CommandsAPI Endpoint = commandEndpoints[endpointcommandmap[C.command]];
|
||||||
MethodInfo theMethod = Endpoint.GetType().GetMethod(C.command);
|
MethodInfo theMethod = Endpoint.GetType().GetMethod(C.command);
|
||||||
if (theMethod != null)
|
if (theMethod != null)
|
||||||
|
|
@ -264,6 +259,36 @@ namespace SecondBotEvents.Services
|
||||||
return new KeyValuePair<bool, string>(false, e.Message);
|
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()
|
public string[] GetFullListOfCommands()
|
||||||
{
|
{
|
||||||
123
SecondBotEvents/Services/CustomCommandsService.cs
Normal file
123
SecondBotEvents/Services/CustomCommandsService.cs
Normal 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>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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, ScriptDialogEventArgs> DialogWindows = new Dictionary<int, ScriptDialogEventArgs>();
|
||||||
protected Dictionary<int, long> DialogWindowsExpire = new Dictionary<int, long>();
|
protected Dictionary<int, long> DialogWindowsExpire = new Dictionary<int, long>();
|
||||||
|
|
@ -215,7 +192,30 @@ 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()
|
public override void Start()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue