Correct bugs

This commit is contained in:
Madpeterz 2021-04-04 22:48:48 +01:00
parent cfeabd7a9e
commit 810c9fb3e7
3 changed files with 117 additions and 41 deletions

View file

@ -12,6 +12,8 @@ namespace BetterSecondBot
{
static void Main(string[] args)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(PanicMode);
if (helpers.notempty(Environment.GetEnvironmentVariable("Basic_BotUserName")) == true)
{
new CliDocker();
@ -24,6 +26,13 @@ namespace BetterSecondBot
Thread.Sleep(5000);
}
static void PanicMode(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;
Console.WriteLine("PanicMode caught : " + e.Message);
Console.WriteLine("Runtime terminating: {0}", args.IsTerminating);
}
public static class AssemblyInfo
{
/// <summary> Gets the git hash value from the assembly

View file

@ -406,6 +406,8 @@ namespace BetterSecondBot.HttpService
}
public IPAddress handleGetClientIP()
{
try
{
if (disableIPchecks == true)
{
@ -416,6 +418,12 @@ namespace BetterSecondBot.HttpService
return HttpContext.Request.RemoteEndPoint.Address;
}
}
catch (Exception e)
{
Console.WriteLine("Error getting client ip");
return IPAddress.Parse("0.0.0.0");
}
}
@ -486,7 +494,7 @@ namespace BetterSecondBot.HttpService
protected Object BasicReply(string input, string command)
{
return BasicReply(input, command);
return BasicReply(input, command, new string[] { });
}
protected Object BasicReply(string input,string command, string[] args)
@ -503,7 +511,7 @@ namespace BetterSecondBot.HttpService
protected Object Failure(string input, string command)
{
return Failure(input, command);
return Failure(input, command, new string[] { });
}

View file

@ -279,8 +279,16 @@ namespace BetterSecondBot.bottypes
}
string outputto = "none";
List<string> bigbits = message.Split(new[] { "#|#" }, StringSplitOptions.RemoveEmptyEntries).ToList();
if(bigbits.Count == 0)
{
bigbits.Add("");
}
List<string> bits = bigbits[0].Split(new[] { "|||" }, StringSplitOptions.RemoveEmptyEntries).ToList();
if (bits.Count == 1)
if(bits.Count < 1)
{
bits.Add("");
}
if (bits.Count < 2)
{
bits.Add("");
}
@ -293,7 +301,17 @@ namespace BetterSecondBot.bottypes
{
frommaster = true;
}
CoreCommandLib(sender_uuid, frommaster, bits[0], bits[1].Split("~#~", StringSplitOptions.RemoveEmptyEntries), signing_code, outputto);
try
{
string command = bits[0];
string[] args = bits[1].Split("~#~", StringSplitOptions.RemoveEmptyEntries);
CoreCommandLib(sender_uuid, frommaster, command, args, signing_code, outputto);
}
catch (Exception e)
{
LogFormater.Crit("[CoreCommandLib] exploded: " + e.Message + "");
}
}
}
@ -305,6 +323,8 @@ namespace BetterSecondBot.bottypes
string ott = Tokens.OneTimeToken();
WebApiControllerWithTokens Endpoint = commandEndpoints[endpointcommandmap[command]];
MethodInfo theMethod = Endpoint.GetType().GetMethod(command);
if (theMethod != null)
{
List<string> argsList = args.ToList();
argsList.Add(ott);
string reply = "Inconnect number of args expected: " + theMethod.GetParameters().Count().ToString() + " but got: " + argsList.Count.ToString();
@ -312,7 +332,21 @@ namespace BetterSecondBot.bottypes
if (argsList.Count == theMethod.GetParameters().Count())
{
status = true;
object processed = theMethod.Invoke(Endpoint, argsList.ToArray<object>());
try
{
if (Endpoint == null)
{
return new KeyValuePair<bool, string>(false, "Endpoint is null");
}
if (argsList.Count == 0)
{
return new KeyValuePair<bool, string>(false, "Zero args at final check require at min 1");
}
object[] argsWorker = argsList.ToArray<object>();
object processed = theMethod.Invoke(Endpoint, argsWorker);
reply = "Error";
if(processed != null)
{
reply = JsonConvert.SerializeObject(processed);
}
return new KeyValuePair<bool, string>(status, reply);
@ -322,6 +356,18 @@ namespace BetterSecondBot.bottypes
return new KeyValuePair<bool, string>(false, e.Message);
}
}
return new KeyValuePair<bool, string>(status, reply);
}
else
{
return new KeyValuePair<bool, string>(false, "theMethod is null");
}
}
catch (Exception e)
{
return new KeyValuePair<bool, string>(false, e.Message);
}
}
protected void CoreCommandLib(UUID sender, bool Master, string command, string[] args, string signingcode, string targetreply)
{
@ -349,15 +395,9 @@ namespace BetterSecondBot.bottypes
{
return CallAPI(command, args, replyvia, false);
}
public string CallAPI(string command, string[] args, string replyvia, bool customcommand)
protected string customCommand(string command, string[] args, string replyvia, bool customcommand)
{
if(commandnameLowerToReal.ContainsKey(command.ToLowerInvariant()) == false)
{
if (custom_commands.ContainsKey(command) == false)
{
SmartCommandReply(false, replyvia, "Unknown command", command);
return "{ status: \"false\", message: \"Unknown\" }";
}
if (customcommand == true)
{
SmartCommandReply(false, replyvia, "Custom command lockout", command);
@ -381,6 +421,25 @@ namespace BetterSecondBot.bottypes
}
return "{ status: \"true\", message: \"mixed reply custom command\" }";
}
public string CallAPI(string command, string[] args, string replyvia, bool customcommand)
{
if(command == null)
{
return "No command";
}
if (replyvia == null)
{
replyvia = "none";
}
if (commandnameLowerToReal.ContainsKey(command.ToLowerInvariant()) == false)
{
if (custom_commands.ContainsKey(command) == false)
{
SmartCommandReply(false, replyvia, "Unknown command", command);
return "{ status: \"false\", message: \"Unknown\" }";
}
customCommand(command, args, replyvia, customcommand);
}
KeyValuePair<bool, string> statusreply = callAPIcommand(command, args);
SmartCommandReply(statusreply.Key, replyvia, statusreply.Value, command);
return "{ status: \"" + statusreply.Key.ToString() + "\", message: \""+ statusreply.Value+"\" command: \""+command+"\", args: \""+string.Join("~#~",args)+"\" }";