From 03a39741e42ffa58ee3b8ed1aa8ed6e316105bcc Mon Sep 17 00:00:00 2001 From: Madpeterz <57852827+Madpeterz@users.noreply.github.com> Date: Thu, 24 Dec 2020 19:55:40 +0000 Subject: [PATCH] improvements to the way the bot handles logins and recovery --- BSBshared/BotTypes/BasicBot.cs | 4 + BSBshared/Json/JsonConfigApi.cs | 26 +++-- Core/bottypes/EventsBot.cs | 7 +- Core/bottypes/athome.cs | 171 ++++++++++++++++++++------------ 4 files changed, 131 insertions(+), 77 deletions(-) diff --git a/BSBshared/BotTypes/BasicBot.cs b/BSBshared/BotTypes/BasicBot.cs index 13e70c3..787d12f 100644 --- a/BSBshared/BotTypes/BasicBot.cs +++ b/BSBshared/BotTypes/BasicBot.cs @@ -17,6 +17,8 @@ namespace BetterSecondBotShared.bottypes LastStatusMessage = "No status"; } protected List SubMasters = new List(); + protected bool attempted_first_login = false; + protected bool login_failed = false; public bool Is_avatar_master(string name) { if (name == myconfig.Security_MasterUsername) @@ -208,6 +210,7 @@ namespace BetterSecondBotShared.bottypes { if (myconfig.Setting_loginURI.Length > 5) { + Info("Using custom login server"); Lp = new LoginParams(Client, bits[0], bits[1], myconfig.Basic_BotPassword, "BetterSecondBot", version, myconfig.Setting_loginURI); } else @@ -235,6 +238,7 @@ namespace BetterSecondBotShared.bottypes Info("Basic_LoginLocation: is set to a uri! if login fails try using home or last"); } Lp.Start = myconfig.Basic_LoginLocation; + Info("Basic_LoginLocation: First login using->"+Lp.Start); } else { diff --git a/BSBshared/Json/JsonConfigApi.cs b/BSBshared/Json/JsonConfigApi.cs index c1aa77c..65dcec8 100644 --- a/BSBshared/Json/JsonConfigApi.cs +++ b/BSBshared/Json/JsonConfigApi.cs @@ -84,6 +84,10 @@ namespace BetterSecondBotShared.Json { return "the URI to login to (leave as \"secondlife\" unless you know what your doing!)"; } + else if (cmd == "Setting_LogCommands") + { + return "Allow the bot to send command to console and discord full if enabled"; + } return ""; } protected string GetHelp_Discord(string cmd) @@ -174,11 +178,10 @@ namespace BetterSecondBotShared.Json + "2: +Crititcal\n" + "3: +Warnings\n" + "4: +Debug\n"; - } - else if (cmd == "Setting_LogCommands") - { - return "Allow the bot to send command to console and discord full if enabled"; - } + } + // Name2Key + else if (cmd == "Name2Key_Url") { return "url to the name to key provider"; } + else if (cmd == "Name2Key_Key") { return "access code for the provider"; } return "Unknown value:" + cmd; } #endregion @@ -189,6 +192,7 @@ namespace BetterSecondBotShared.Json else if (cmd.StartsWith("Settings") == true) { return GetHelp_Settings(cmd); } else if (cmd.StartsWith("DiscordTTS") == true) { return GetHelp_DiscordTTS(cmd); } else if (cmd.StartsWith("Discord") == true) { return GetHelp_Discord(cmd); } + else if (cmd.StartsWith("Http_") == true) { return GetHelp_HTTP(cmd); } else { return GetHelp_Logs(cmd); } } @@ -245,7 +249,8 @@ namespace BetterSecondBotShared.Json else if (cmd == "Setting_AllowFunds") { return "True"; } else if (cmd == "Setting_RelayImToAvatarUUID") { return ""; } else if (cmd == "Setting_DefaultSit_UUID") { return ""; } - else if (cmd == "Setting_loginURI") { return "secondlife"; } + else if (cmd == "Setting_loginURI") { return "secondlife"; } + else if (cmd == "Setting_LogCommands") { return "True"; } return ""; } protected string GetValueHint_Discord(string cmd) @@ -282,7 +287,9 @@ namespace BetterSecondBotShared.Json // Logs if (cmd == "Log2File_Enable") { return "False"; } else if (cmd == "Log2File_Level") { return "1"; } - else if (cmd == "Setting_LogCommands") { return "True"; } + // Name2Key + else if(cmd == "Name2Key_Url") { return ""; } + else if(cmd == "Name2Key_Key") { return ""; } return "Unknown value:" + cmd; } #endregion @@ -291,9 +298,10 @@ namespace BetterSecondBotShared.Json { if (cmd.StartsWith("Basic") == true) { return GetValueHint_Basic(cmd); } else if (cmd.StartsWith("Security") == true) { return GetValueHint_Security(cmd); } - else if (cmd.StartsWith("Settings") == true) { return GetValueHint_Settings(cmd); } - else if (cmd.StartsWith("DiscordTTS") == true) { return GetValueHint_DiscordTTS(cmd); } + else if (cmd.StartsWith("Setting_") == true) { return GetValueHint_Settings(cmd); } + else if (cmd.StartsWith("DiscordTTS_") == true) { return GetValueHint_DiscordTTS(cmd); } else if (cmd.StartsWith("Discord") == true) { return GetValueHint_Discord(cmd); } + else if (cmd.StartsWith("Http_") == true) { return GetValueHint_HTTP(cmd); } else { return GetValueHint_Logs(cmd); } } public override int GetCommandArgs(string cmd) diff --git a/Core/bottypes/EventsBot.cs b/Core/bottypes/EventsBot.cs index 18e9534..14bc7fa 100644 --- a/Core/bottypes/EventsBot.cs +++ b/Core/bottypes/EventsBot.cs @@ -155,14 +155,16 @@ namespace BSB.bottypes protected string login_status = "Waiting to login"; protected override void LoginHandler(object o, LoginProgressEventArgs e) { + attempted_first_login = true; + login_failed = false; if (e.Status == LoginStatus.Success) { login_status = "Ok"; - AfterBotLoginHandler(); } else if(e.Status == LoginStatus.Failed) { - login_status = "Login failed"; + login_failed = true; + login_status = "Login failed <"+e.Message+">"; login_auto_logout = true; if (e.FailReason == "presence") { @@ -189,6 +191,7 @@ namespace BSB.bottypes { login_status = "Redirecting"; } + Info("Login status -> " + login_status); } public virtual void GroupsHandler(object sender, CurrentGroupsEventArgs e) diff --git a/Core/bottypes/athome.cs b/Core/bottypes/athome.cs index a9e3b15..cf04754 100644 --- a/Core/bottypes/athome.cs +++ b/Core/bottypes/athome.cs @@ -9,55 +9,110 @@ namespace BSB.bottypes { public abstract class AtHome : MessageSwitcherBot { - protected string LoggedOutNextAction() + protected long ConnectionLostAt = 0; + protected override void AfterBotLoginHandler() { - string addon = ""; - long dif = helpers.UnixTimeNow() - last_reconnect_attempt; - if ((login_auto_logout == false) && (reconnect_mode == false) && (after_login_fired == true)) + base.AfterBotLoginHandler(); + last_tp_attempt_unixtime = helpers.UnixTimeNow() + 30; + if (reconnect == true) { + ResetAtHome(); + } + else + { + Client.Self.AlertMessage += AlertEvent; + Client.Network.SimChanged += ChangeSim; + } + if (Client.Network.Connected == true) + { + if (Client.Network.CurrentSim != null) + { + if (IsSimHome(Client.Network.CurrentSim.Name) == true) + { + if (UUID.TryParse(myconfig.Setting_DefaultSit_UUID, out UUID sit_UUID) == true) + { + Client.Self.RequestSit(sit_UUID, Vector3.Zero); + } + } + } + } + after_login_fired = true; + } + + protected string FirstLoginStage() + { + long dif = helpers.UnixTimeNow() - last_reconnect_attempt; + if (login_auto_logout == true) + { + if (last_reconnect_attempt == 0) + { + return login_status; + } + else + { + if (auto_logout_login_recover == false) + { + auto_logout_login_recover = true; + last_reconnect_attempt = helpers.UnixTimeNow(); + return "W4>" + login_status + " (20 secs)"; + } + else if (dif >= 20) + { + login_auto_logout = false; + auto_logout_login_recover = false; + last_reconnect_attempt = helpers.UnixTimeNow(); + Start(true); + return "Restarting first login [Using Alt locations]"; + } + return "Waiting for cooldown"; + } + } + else + { + return login_status; + } + } + + protected string LoggedInConnectionLost() + { + long dif = helpers.UnixTimeNow() - last_reconnect_attempt; + if (reconnect_mode == true) + { last_tested_home_id = -1; last_tp_attempt_unixtime = 0; after_login_fired = false; teleported = false; reconnect_mode = true; last_reconnect_attempt = helpers.UnixTimeNow(); - addon = "Connection lost - switching to recovery mode"; + return "Connection lost - switching to recovery mode"; + } + else + { + if (dif <= 60) + { + return "W4> Network reset timeout [60 secs]"; + } + else + { + last_reconnect_attempt = helpers.UnixTimeNow(); + reconnect = true; + Start(true); + return "Attempting reconnect"; + } + } + } + + protected string LoggedOutNextAction() + { + if (login_auto_logout == true) + { + return FirstLoginStage(); + } + if ((attempted_first_login == true) && (login_failed == true)) + { + return LoggedInConnectionLost(); } - else if ((login_auto_logout == false) && (reconnect_mode == false) && (after_login_fired == false)) - { - addon = login_status; - } - else if ((login_auto_logout == false) && (reconnect_mode == true) && (dif > 120)) - { - addon = "Attempting reconnect"; - last_reconnect_attempt = helpers.UnixTimeNow(); - reconnect = true; - Start(true); - } - else if ((login_auto_logout == false) && (reconnect_mode == true) && (dif <= 120)) - { - addon = "W4>Reconnect attempt timer"; - } - else if ((login_auto_logout == true) && (auto_logout_login_recover == false)) - { - auto_logout_login_recover = true; - last_reconnect_attempt = helpers.UnixTimeNow(); - addon = "W4>" + login_status + " (10 secs)"; - } - else if ((login_auto_logout == true) && (auto_logout_login_recover == true) && (dif >= 10)) - { - login_auto_logout = false; - auto_logout_login_recover = false; - last_reconnect_attempt = helpers.UnixTimeNow(); - addon = "Restarting first login"; - Start(); - } - else - { - addon = login_status; - } - Debug("Function/LoggedOutNextAction - "+ addon); - return addon; + return ""; } protected int last_tested_home_id = -1; protected long last_tp_attempt_unixtime; @@ -119,6 +174,7 @@ namespace BSB.bottypes teleported = false; reconnect_mode = false; last_tp_attempt_unixtime = 0; + avoid_sims = new Dictionary(); } @@ -134,10 +190,14 @@ namespace BSB.bottypes protected void ExpireOldAvoidSims() { - if(avoid_sims.Count() > 0) + if (avoid_sims.Count() > 0) { List remove_keys = new List(); long now = helpers.UnixTimeNow(); + if (GetClient.Network.Connected == false) + { + now -= 240; // when logged out, add 240s to all sims we are avoiding + } foreach (KeyValuePair avoids in avoid_sims) { if(avoids.Value < now) @@ -351,6 +411,10 @@ namespace BSB.bottypes { if (Client.Network.CurrentSim != null) { + if(after_login_fired == false) + { + AfterBotLoginHandler(); + } reply = AtHomeStatus(LoggedinAthome()); } else @@ -370,31 +434,6 @@ namespace BSB.bottypes return base.GetStatus(); } - protected override void AfterBotLoginHandler() - { - base.AfterBotLoginHandler(); - last_tp_attempt_unixtime = helpers.UnixTimeNow() + 30; - if (reconnect == true) - { - ResetAtHome(); - } - else - { - Client.Self.AlertMessage += AlertEvent; - Client.Network.SimChanged += ChangeSim; - } - if (IsSimHome(Client.Network.CurrentSim.Name) == true) - { - if (UUID.TryParse(myconfig.Setting_DefaultSit_UUID, out UUID sit_UUID) == true) - { - Client.Self.RequestSit(sit_UUID, Vector3.Zero); - } - } - after_login_fired = true; - } - - - public string TeleportWithSLurl(string sl_url) { string[] bits = helpers.ParseSLurl(sl_url);