Revise the message handler system, and partially the command system

This commit is contained in:
Zontreck 2020-06-28 03:27:04 -07:00
parent 57dcc685df
commit 59c62e62e2
19 changed files with 350 additions and 209 deletions

View file

@ -0,0 +1,20 @@
using OpenMetaverse;
using System;
using System.Collections.Generic;
using System.Text;
namespace Bot.CommandSystem
{
public class BaseCommands
{
public void MHE(Destinations dest, UUID client, string msg)
{
MessageFactory.Post(dest, msg, client);
}
public static void MH(Destinations dest, UUID client, string msg)
{
MessageFactory.Post(dest, msg, client);
}
}
}

View file

@ -10,7 +10,7 @@ using System.Text;
namespace Bot.CommandSystem
{
class BaseHooks
class BaseHooks : BaseCommands
{
[WebhookAttribs("/help")]
@ -94,28 +94,27 @@ namespace Bot.CommandSystem
[CommandGroup("show_level", 0, 0, "This command shows your current auth level if any.", MessageHandler.Destinations.DEST_AGENT | MessageHandler.Destinations.DEST_LOCAL | MessageHandler.Destinations.DEST_GROUP)]
[CommandGroup("show_level", 0, 0, "This command shows your current auth level if any.", Destinations.DEST_AGENT | Destinations.DEST_LOCAL | Destinations.DEST_GROUP)]
public void show_level(UUID client, int level, GridClient grid, string[] additionalArgs,
MessageHandler.MessageHandleEvent MHE, MessageHandler.Destinations source,
CommandRegistry registry, UUID agentKey, string agentName)
Destinations source,
UUID agentKey, string agentName)
{
MHE(source, client, "Hi secondlife:///app/agent/" + agentKey.ToString() + "/about !! Your authorization level is " + level.ToString());
}
[CommandGroup("show_version", 0, 0, "Outputs the bot version", MessageHandler.Destinations.DEST_AGENT | MessageHandler.Destinations.DEST_LOCAL)]
public void show_version(UUID client, int level, GridClient grid, string[] additionalArgs,
MessageHandler.MessageHandleEvent MHE, MessageHandler.Destinations source,
CommandRegistry registry, UUID agentKey, string agentName)
[CommandGroup("show_version", 0, 0, "Outputs the bot version", Destinations.DEST_AGENT | Destinations.DEST_LOCAL)]
public void show_version(UUID client, int level, string[] additionalArgs,
Destinations source,
UUID agentKey, string agentName)
{
MHE(source, client, "Version " + ASMInfo.BotVer.ToString());
}
[CommandGroup("show_admins", 4, 0, "Outputs all admin users", MessageHandler.Destinations.DEST_AGENT | MessageHandler.Destinations.DEST_LOCAL)]
public void show_admins(UUID client, int level, GridClient grid, string[] additionalArgs,
MessageHandler.MessageHandleEvent MHE, MessageHandler.Destinations source,
CommandRegistry registry, UUID agentKey, string agentName)
[CommandGroup("show_admins", 4, 0, "Outputs all admin users", Destinations.DEST_AGENT | Destinations.DEST_LOCAL)]
public void show_admins(UUID client, int level, string[] additionalArgs, Destinations source,
UUID agentKey, string agentName)
{
for (int i = 0; i < MainConfiguration.Instance.BotAdmins.Count; i++)
@ -125,16 +124,16 @@ namespace Bot.CommandSystem
}
[CommandGroup("terminate_bot", 5, 0, "", MessageHandler.Destinations.DEST_LOCAL | MessageHandler.Destinations.DEST_AGENT | MessageHandler.Destinations.DEST_CONSOLE_INFO | MessageHandler.Destinations.DEST_DISCORD)]
public void PerformExit(UUID client, int level, GridClient grid, string[] additionalArgs, MessageHandler.MessageHandleEvent MHE, MessageHandler.Destinations source, CommandRegistry registry, UUID agentKey, string agentName)
[CommandGroup("terminate_bot", 5, 0, "", Destinations.DEST_LOCAL | Destinations.DEST_AGENT | Destinations.DEST_DISCORD)]
public void PerformExit(UUID client, int level, string[] additionalArgs, Destinations source, UUID agentKey, string agentName)
{
MHE(source, client, "Bot exit initiated.");
MHE(MessageHandler.Destinations.DEST_ACTION, UUID.Zero, "{'type':'exit'}");
BotSession.Instance.LaunchTime = new DateTime(); // zero out date time to force a relog
}
// !!help
[CommandGroup("!help", 1, 0, "Prints the entire help registry", MessageHandler.Destinations.DEST_AGENT | MessageHandler.Destinations.DEST_LOCAL | MessageHandler.Destinations.DEST_GROUP | MessageHandler.Destinations.DEST_CONSOLE_INFO)]
[CommandGroup("bot.help", 1, 0, "Alias to !help", MessageHandler.Destinations.DEST_AGENT | MessageHandler.Destinations.DEST_LOCAL | MessageHandler.Destinations.DEST_GROUP)]
public void PrintAllHelp(UUID client, int level, GridClient grid, string[] additionalArgs, MessageHandler.MessageHandleEvent MHE, MessageHandler.Destinations source, CommandRegistry registry, UUID agentKey, string agentName)
[CommandGroup("!help", 1, 0, "Prints the entire help registry", Destinations.DEST_AGENT |Destinations.DEST_LOCAL | Destinations.DEST_GROUP)]
[CommandGroup("bot.help", 1, 0, "Alias to !help", Destinations.DEST_AGENT | Destinations.DEST_LOCAL | Destinations.DEST_GROUP)]
public void PrintAllHelp(UUID client, int level, string[] additionalArgs, Destinations source, UUID agentKey, string agentName)
{
if (MainConfiguration.Instance.UseSSL)
MHE(source, client, $"All commands viewable at: https://{MainConfiguration.Instance.WebServerIP}:{MainConfiguration.Instance.WebServerPort}/help");
@ -142,10 +141,10 @@ namespace Bot.CommandSystem
MHE(source, client, $"All commands viewable at: http://{MainConfiguration.Instance.WebServerIP}:{MainConfiguration.Instance.WebServerPort}/help");
}
// !help "command"
[CommandGroup("help", 0, 1, "Prints help for one command", MessageHandler.Destinations.DEST_AGENT | MessageHandler.Destinations.DEST_LOCAL | MessageHandler.Destinations.DEST_GROUP | MessageHandler.Destinations.DEST_CONSOLE_INFO)]
public void PrintHelp(UUID client, int level, GridClient grid, string[] additionalArgs, MessageHandler.MessageHandleEvent MHE, MessageHandler.Destinations source, CommandRegistry registry, UUID agentKey, string agentName)
[CommandGroup("help", 0, 1, "Prints help for one command", Destinations.DEST_AGENT | Destinations.DEST_LOCAL | Destinations.DEST_GROUP )]
public void PrintHelp(UUID client, int level, string[] additionalArgs, Destinations source, UUID agentKey, string agentName)
{
registry.PrintHelp(source, additionalArgs[0], client);
CommandRegistry.Instance.PrintHelp(source, additionalArgs[0], client);
}
}
}

View file

@ -13,9 +13,9 @@ namespace Bot.CommandSystem
public MethodInfo AssignedMethod;
public int arguments = 0;
public CommandHelp cmdUsage;
public MessageHandler.Destinations CommandSource;
public Destinations CommandSource;
public CommandGroup(string Command, int minLevel, int argCount, string HelpText, MessageHandler.Destinations SourceType)
public CommandGroup(string Command, int minLevel, int argCount, string HelpText, Destinations SourceType)
{
this.Command = Command;
this.minLevel = minLevel;

View file

@ -16,7 +16,7 @@ namespace Bot.CommandSystem
public bool hasGroupFlag()
{
if ((h.dests_allowed & MessageHandler.Destinations.DEST_GROUP) == MessageHandler.Destinations.DEST_GROUP) return true;
if ((h.dests_allowed & Destinations.DEST_GROUP) == Destinations.DEST_GROUP) return true;
else return false;
}
@ -28,7 +28,7 @@ namespace Bot.CommandSystem
public int args;
public string Text;
public string sources;
public MessageHandler.Destinations dests_allowed;
public Destinations dests_allowed;
}
public string GetUsage()
{
@ -39,14 +39,14 @@ namespace Bot.CommandSystem
{
return "Usage: " + h.Text;
}
public CommandHelp(string CmdName, int minLevel, int argCount, string HelpText, MessageHandler.Destinations DESTS)
public CommandHelp(string CmdName, int minLevel, int argCount, string HelpText, Destinations DESTS)
{
h = new Help();
string Applicable = "Command can be used in [";
if ((DESTS & MessageHandler.Destinations.DEST_LOCAL) == MessageHandler.Destinations.DEST_LOCAL) Applicable += "Local, ";
if ((DESTS & MessageHandler.Destinations.DEST_AGENT) == MessageHandler.Destinations.DEST_AGENT) Applicable += "IM, ";
if ((DESTS & MessageHandler.Destinations.DEST_GROUP) == MessageHandler.Destinations.DEST_GROUP) Applicable += "Group, ";
if ((DESTS & MessageHandler.Destinations.DEST_DISCORD) == MessageHandler.Destinations.DEST_DISCORD) Applicable += "Discord, ";
if ((DESTS & Destinations.DEST_LOCAL) == Destinations.DEST_LOCAL) Applicable += "Local, ";
if ((DESTS & Destinations.DEST_AGENT) == Destinations.DEST_AGENT) Applicable += "IM, ";
if ((DESTS & Destinations.DEST_GROUP) == Destinations.DEST_GROUP) Applicable += "Group, ";
if ((DESTS & Destinations.DEST_DISCORD) == Destinations.DEST_DISCORD) Applicable += "Discord, ";
if (Applicable.Substring(Applicable.Length - 1, 1) == " ") Applicable = Applicable.Substring(0, Applicable.Length - 2) + "]";

View file

@ -22,9 +22,9 @@ namespace Bot.CommandSystem
public GridClient cl;
public Logger Log = BotSession.Instance.Logger;
public string newReply;
public unsafe bool RunChatCommand(string cmdData, GridClient client, MessageHandler.MessageHandleEvent MHE, CommandRegistry registry)
public unsafe bool RunChatCommand(string cmdData)
{
register = registry;
register = CommandRegistry.Instance;
Dictionary<UUID, int> BotAdmins = MainConfiguration.Instance.BotAdmins;
dynamic parameters = JsonConvert.DeserializeObject(cmdData);
string request = parameters.request;
@ -55,46 +55,41 @@ namespace Bot.CommandSystem
}
cl = client;
cl = BotSession.Instance.grid;
MessageHandler.Destinations sourceLoc = new MessageHandler.Destinations();
if (parameters.type == "chat") sourceLoc = MessageHandler.Destinations.DEST_LOCAL;
else if (parameters.type == "group") sourceLoc = MessageHandler.Destinations.DEST_GROUP;
else if (parameters.type == "im") sourceLoc = MessageHandler.Destinations.DEST_AGENT;
else if (parameters.type == "console")
{
userLevel = 5000;
sourceLoc = MessageHandler.Destinations.DEST_CONSOLE_INFO;
}
else sourceLoc = MessageHandler.Destinations.DEST_LOCAL;
Destinations sourceLoc = new Destinations();
if (parameters.type == "chat") sourceLoc = Destinations.DEST_LOCAL;
else if (parameters.type == "group") sourceLoc = Destinations.DEST_GROUP;
else if (parameters.type == "im") sourceLoc = Destinations.DEST_AGENT;
else sourceLoc = Destinations.DEST_LOCAL;
string agentName = parameters.fromName;
if(sourceLoc == MessageHandler.Destinations.DEST_LOCAL)
if(sourceLoc == Destinations.DEST_LOCAL)
{
GroupLog.Instance.WriteLogEntry(true, false, agentName, agentKey, request);
}else if (sourceLoc == MessageHandler.Destinations.DEST_AGENT)
}else if (sourceLoc == Destinations.DEST_AGENT)
{
GroupLog.Instance.WriteLogEntry(false, true, agentName, agentKey, request);
}
if (sourceLoc == MessageHandler.Destinations.DEST_GROUP)
if (sourceLoc == Destinations.DEST_GROUP)
{
agentKey = fromID;
fromID = sessID;
// Initiate group log saver
string GroupName = client.Groups.GroupName2KeyCache[fromID];
string GroupName = cl.Groups.GroupName2KeyCache[fromID];
GroupLog.Instance.WriteLogEntry(GroupName, "secondlife:///app/agent/" + agentKey.ToString() + "/about (" + agentName + ") : " + request);
if (agentKey == client.Self.AgentID) return false;
if (agentKey == cl.Self.AgentID) return false;
}
else {
agentKey = fromID;
if (agentKey == client.Self.AgentID) return false;
if (agentKey == cl.Self.AgentID) return false;
}
if (request.Substring(0, 1) != "!")
@ -155,7 +150,7 @@ namespace Bot.CommandSystem
try
{
register.RunCommand(request, fromID, userLevel, MHE, sourceLoc, agentKey, agentName);
register.RunCommand(request, fromID, userLevel, sourceLoc, agentKey, agentName);
}
catch (Exception e)
{
@ -164,17 +159,17 @@ namespace Bot.CommandSystem
int i;
int* ptr = &i;
IntPtr addr = (IntPtr)ptr;
MHE(MessageHandler.Destinations.DEST_LOCAL, UUID.Zero, "Exception caught: [" + Msg + "]\n \n[STACK] " + e.StackTrace.Replace("ZNI", "")+"\nMemory Position: 0x"+addr.ToString("x")+"\nCommand: "+request+$"\nMisc Details: {fromID}, {userLevel}, {sourceLoc}, {agentKey}, {agentName}");
MessageFactory.Post(Destinations.DEST_LOCAL, "Exception caught: [" + Msg + "]\n \n[STACK] " + e.StackTrace.Replace("ZNI", "") + "\nMemory Position: 0x" + addr.ToString("x") + "\nCommand: " + request + $"\nMisc Details: {fromID}, {userLevel}, {sourceLoc}, {agentKey}, {agentName}", UUID.Zero);
// do nothing here.
}
Log.info(log:"Leaving command parser");
return false;
}
public CommandManager(Logger _Log, GridClient cl, MessageHandler.MessageHandleEvent MHE)
public CommandManager()
{
this.cl = cl;
Log = _Log;
Log = BotSession.Instance.Logger;
}
}
}

View file

@ -37,7 +37,6 @@ namespace Bot.CommandSystem
_instance.client = bs.grid;
_instance.config = bs.ConfigurationHandle;
_instance.Log = bs.Logger;
_instance.MHEx = bs.MHE;
_instance.LocateCommands();
}
return _instance;
@ -59,7 +58,6 @@ namespace Bot.CommandSystem
public GridClient client;
public Logger Log;
public IConfig config;
public MessageHandler.MessageHandleEvent MHEx;
public void LocateCommands()
{
try
@ -127,16 +125,17 @@ namespace Bot.CommandSystem
}
catch (ReflectionTypeLoadException e)
{
MHEx(MessageHandler.Destinations.DEST_LOCAL, UUID.Zero, "FAILURE!!!\n \n[Assembly load failure]");
MessageFactory.Post(Destinations.DEST_LOCAL, "FAILURE!!!\n\n[Assembly load failure]", UUID.Zero);
foreach (Exception X in e.LoaderExceptions)
{
MHEx(MessageHandler.Destinations.DEST_LOCAL, UUID.Zero, X.Message + "\n \nSTACK: " + X.StackTrace);
MessageFactory.Post(Destinations.DEST_LOCAL, X.Message + "\n \nSTACK: " + X.StackTrace, UUID.Zero);
}
}
}
public void PrintHelpAll(MessageHandler.Destinations dest, UUID uid)
public void PrintHelpAll(Destinations dest, UUID uid)
{
for (int i = 0; i < Cmds.Count; i++)
@ -146,7 +145,7 @@ namespace Bot.CommandSystem
KeyValuePair<string, CommandGroup> kvp = Cmds.ElementAt(i);
CommandHelp HE = kvp.Value.cmdUsage;
if (dest == MessageHandler.Destinations.DEST_GROUP)
if (dest == Destinations.DEST_GROUP)
{
if (!HE.hasGroupFlag())
{
@ -154,13 +153,14 @@ namespace Bot.CommandSystem
}
else
{
MHEx(dest, uid, HE.GetUsage());
MessageFactory.Post(dest, HE.GetUsage(), uid);
}
}
else
{
MessageFactory.Post(dest, HE.GetUsage(), uid);
MHEx(dest, uid, HE.GetUsage());
}
// MHEx(dest, uid, kvp.Value.cmdUsage.GetUsage());
@ -168,30 +168,31 @@ namespace Bot.CommandSystem
}
}
public void PrintHelp(MessageHandler.Destinations dest, string cmd, UUID uid)
public void PrintHelp(Destinations dest, string cmd, UUID uid)
{
try
{
CommandHelp HE = Cmds[cmd].cmdUsage;
if (dest == MessageHandler.Destinations.DEST_GROUP)
if (dest == Destinations.DEST_GROUP)
{
if (!HE.hasGroupFlag())
{
//return; // DO NOT SCHEDULE THIS HELP INFO FOR GROUP!!!
}
}
MHEx(dest, uid, Cmds[cmd].cmdUsage.GetUsage());
MessageFactory.Post(dest, Cmds[cmd].cmdUsage.GetUsage(), uid);
}
catch (Exception e)
{
MHEx(dest, uid, "Error: Unrecognized command");
MessageFactory.Post(dest, "Error: Unknown command", uid);
}
}
public void RunCommand(string cmdString, UUID user, int level, MessageHandler.MessageHandleEvent MHE, MessageHandler.Destinations source, UUID agentKey, string agentName)
public void RunCommand(string cmdString, UUID user, int level, Destinations source, UUID agentKey, string agentName)
{
MHEx = MHE;
int pos = 0;
string[] cmdStruct = cmdString.Split(' ');
int IgnoreCount = 0;
@ -213,11 +214,11 @@ namespace Bot.CommandSystem
{
// Check that the destination is allowed.
// If not then skip this command entirely
MessageHandler.Destinations dests = cgX.CommandSource;
Destinations dests = cgX.CommandSource;
bool Allowed = false;
if ((dests & MessageHandler.Destinations.DEST_AGENT) == source) Allowed = true;
if ((dests & MessageHandler.Destinations.DEST_GROUP) == source) Allowed = true;
if ((dests & MessageHandler.Destinations.DEST_LOCAL) == source) Allowed = true;
if ((dests & Destinations.DEST_AGENT) == source) Allowed = true;
if ((dests & Destinations.DEST_GROUP) == source) Allowed = true;
if ((dests & Destinations.DEST_LOCAL) == source) Allowed = true;
if (!Allowed)
{
@ -235,10 +236,10 @@ namespace Bot.CommandSystem
additionalArgs[i - 1] = cmdStruct[pos + i];
}
pos++;
//(UUID client, int level, GridClient grid, string[] additionalArgs,
//MessageHandler.MessageHandleEvent MHE, MessageHandler.Destinations source,
//CommandRegistry registry, UUID agentKey, string agentName)
Thread CommandThread = new Thread(() => cgX.AssignedMethod.Invoke(ovj, new object[] { user, level, client, additionalArgs, MHE, source, this, agentKey, agentName }));
//(UUID client, int level, string[] additionalArgs,
//Destinations source,
//UUID agentKey, string agentName)
Thread CommandThread = new Thread(() => cgX.AssignedMethod.Invoke(ovj, new object[] { user, level, additionalArgs, source, agentKey, agentName }));
CommandThread.Start();
}
}