2014-05-31 01:59:03 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.ChatBots
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This bot automatically re-join the server if kick message contains predefined string (Server is restarting ...)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class AutoRelog : ChatBot
|
|
|
|
|
|
{
|
|
|
|
|
|
private string[] dictionary = new string[0];
|
|
|
|
|
|
private int attempts;
|
|
|
|
|
|
private int delay;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This bot automatically re-join the server if kick message contains predefined string
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="DelayBeforeRelog">Delay before re-joining the server (in seconds)</param>
|
|
|
|
|
|
/// <param name="retries">Number of retries if connection fails (-1 = infinite)</param>
|
|
|
|
|
|
public AutoRelog(int DelayBeforeRelog, int retries)
|
|
|
|
|
|
{
|
|
|
|
|
|
attempts = retries;
|
|
|
|
|
|
if (attempts == -1) { attempts = int.MaxValue; }
|
2020-06-20 15:01:16 +02:00
|
|
|
|
McClient.ReconnectionAttemptsLeft = attempts;
|
2014-05-31 01:59:03 +02:00
|
|
|
|
delay = DelayBeforeRelog;
|
|
|
|
|
|
if (delay < 1) { delay = 1; }
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogDebugToConsoleTranslated("bot.autoRelog.launch", attempts);
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
2020-06-20 15:01:16 +02:00
|
|
|
|
McClient.ReconnectionAttemptsLeft = attempts;
|
2020-04-02 18:19:37 +02:00
|
|
|
|
if (Settings.AutoRelog_IgnoreKickMessage)
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogDebugToConsoleTranslated("bot.autoRelog.no_kick_msg");
|
2020-04-02 18:19:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (System.IO.File.Exists(Settings.AutoRelog_KickMessagesFile))
|
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogDebugToConsoleTranslated("bot.autoRelog.loading", System.IO.Path.GetFullPath(Settings.AutoRelog_KickMessagesFile));
|
2019-05-30 11:45:43 +02:00
|
|
|
|
|
2020-05-29 20:23:03 +02:00
|
|
|
|
dictionary = System.IO.File.ReadAllLines(Settings.AutoRelog_KickMessagesFile, Encoding.UTF8);
|
2014-05-31 01:59:03 +02:00
|
|
|
|
|
2020-04-02 18:19:37 +02:00
|
|
|
|
for (int i = 0; i < dictionary.Length; i++)
|
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogDebugToConsoleTranslated("bot.autoRelog.loaded", dictionary[i]);
|
2020-04-02 18:19:37 +02:00
|
|
|
|
dictionary[i] = dictionary[i].ToLower();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogToConsoleTranslated("bot.autoRelog.not_found", System.IO.Path.GetFullPath(Settings.AutoRelog_KickMessagesFile));
|
2020-04-02 18:19:37 +02:00
|
|
|
|
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogDebugToConsoleTranslated("bot.autoRelog.curr_dir", System.IO.Directory.GetCurrentDirectory());
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool OnDisconnect(DisconnectReason reason, string message)
|
|
|
|
|
|
{
|
2019-12-08 22:24:20 +01:00
|
|
|
|
if (reason == DisconnectReason.UserLogout)
|
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogDebugToConsoleTranslated("bot.autoRelog.ignore");
|
2019-12-08 22:24:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
message = GetVerbatim(message);
|
|
|
|
|
|
string comp = message.ToLower();
|
2019-05-30 11:45:43 +02:00
|
|
|
|
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogDebugToConsoleTranslated("bot.autoRelog.disconnect_msg", message);
|
2019-05-30 11:45:43 +02:00
|
|
|
|
|
2020-04-02 18:19:37 +02:00
|
|
|
|
if (Settings.AutoRelog_IgnoreKickMessage)
|
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogDebugToConsoleTranslated("bot.autoRelog.reconnect_always");
|
|
|
|
|
|
LogToConsoleTranslated("bot.autoRelog.wait", delay);
|
2020-09-17 18:54:55 +08:00
|
|
|
|
System.Threading.Thread.Sleep(delay * 1000);
|
2020-04-02 18:19:37 +02:00
|
|
|
|
ReconnectToTheServer();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-08 22:24:20 +01:00
|
|
|
|
foreach (string msg in dictionary)
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
2019-12-08 22:24:20 +01:00
|
|
|
|
if (comp.Contains(msg))
|
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogDebugToConsoleTranslated("bot.autoRelog.reconnect", msg);
|
|
|
|
|
|
LogToConsoleTranslated("bot.autoRelog.wait", delay);
|
2019-12-08 22:24:20 +01:00
|
|
|
|
System.Threading.Thread.Sleep(delay * 1000);
|
2020-06-20 15:01:16 +02:00
|
|
|
|
McClient.ReconnectionAttemptsLeft = attempts;
|
2019-12-08 22:24:20 +01:00
|
|
|
|
ReconnectToTheServer();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
2019-05-30 11:45:43 +02:00
|
|
|
|
|
2020-10-17 19:41:31 +08:00
|
|
|
|
LogDebugToConsoleTranslated("bot.autoRelog.reconnect_ignore");
|
2019-12-08 22:24:20 +01:00
|
|
|
|
}
|
2019-05-30 11:45:43 +02:00
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2015-04-20 17:26:16 +02:00
|
|
|
|
|
|
|
|
|
|
public static bool OnDisconnectStatic(DisconnectReason reason, string message)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Settings.AutoRelog_Enabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
AutoRelog bot = new AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries);
|
2016-03-21 13:26:45 +01:00
|
|
|
|
bot.Initialize();
|
2015-04-20 17:26:16 +02:00
|
|
|
|
return bot.OnDisconnect(reason, message);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|