Account list file support

Accounts can be stored in a file and used while (re)connecting
+ Check that the server IP is valid (avoid /connect <player>)
+ Fix command prompt not exiting while exiting the server
+ SendChatMessage() -> SendText() method name change
This commit is contained in:
ORelio 2014-06-19 19:24:03 +02:00
parent 36690b8b34
commit 2907b9c587
7 changed files with 108 additions and 26 deletions

View file

@ -81,7 +81,7 @@ namespace MinecraftClient
protected bool SendText(string text) protected bool SendText(string text)
{ {
ConsoleIO.WriteLineFormatted("§8BOT:" + text, false); ConsoleIO.WriteLineFormatted("§8BOT:" + text, false);
return handler.SendChatMessage(text); return handler.SendText(text);
} }
/// <summary> /// <summary>

View file

@ -8,15 +8,27 @@ namespace MinecraftClient.Commands
public class Connect : Command public class Connect : Command
{ {
public override string CMDName { get { return "connect"; } } public override string CMDName { get { return "connect"; } }
public override string CMDDesc { get { return "connect <serverip>: connect to the specified server."; } } public override string CMDDesc { get { return "connect <serverip> [account]: connect to the specified server."; } }
public override string Run(McTcpClient handler, string command) public override string Run(McTcpClient handler, string command)
{ {
if (hasArg(command)) if (hasArg(command))
{ {
Settings.setServerIP(getArgs(command)[0]); string[] args = getArgs(command);
Program.Restart(); if (args.Length > 1)
return ""; {
if (!Settings.setAccount(args[1]))
{
return "Unknown account '" + args[1] + "'.";
}
}
if (Settings.setServerIP(args[0]))
{
Program.Restart();
return "";
}
else return "Invalid server IP '" + args[0] + "'.";
} }
else return CMDDesc; else return CMDDesc;
} }

View file

@ -8,10 +8,18 @@ namespace MinecraftClient.Commands
public class Reco : Command public class Reco : Command
{ {
public override string CMDName { get { return "reco"; } } public override string CMDName { get { return "reco"; } }
public override string CMDDesc { get { return "reco: restart and reconnect to the server."; } } public override string CMDDesc { get { return "reco [account]: restart and reconnect to the server."; } }
public override string Run(McTcpClient handler, string command) public override string Run(McTcpClient handler, string command)
{ {
string[] args = getArgs(command);
if (args.Length > 0)
{
if (!Settings.setAccount(args[0]))
{
return "Unknown account '" + args[0] + "'.";
}
}
Program.Restart(); Program.Restart();
return ""; return "";
} }

View file

@ -14,7 +14,7 @@ namespace MinecraftClient.Commands
{ {
if (hasArg(command)) if (hasArg(command))
{ {
handler.SendChatMessage(getArg(command)); handler.SendText(getArg(command));
return ""; return "";
} }
else return CMDDesc; else return CMDDesc;

View file

@ -41,6 +41,7 @@ namespace MinecraftClient
TcpClient client; TcpClient client;
IMinecraftCom handler; IMinecraftCom handler;
Thread cmdprompt;
/// <summary> /// <summary>
/// Starts the main chat client /// Starts the main chat client
@ -126,10 +127,14 @@ namespace MinecraftClient
{ {
foreach (ChatBot bot in scripts_on_hold) { bots.Add(bot); } foreach (ChatBot bot in scripts_on_hold) { bots.Add(bot); }
scripts_on_hold.Clear(); scripts_on_hold.Clear();
Console.WriteLine("Server was successfully joined.\nType '" Console.WriteLine("Server was successfully joined.\nType '"
+ (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar)
+ "quit' to leave the server."); + "quit' to leave the server.");
StartTalk();
cmdprompt = new Thread(new ThreadStart(CommandPrompt));
cmdprompt.Name = "MCC Command prompt";
cmdprompt.Start();
} }
} }
} }
@ -149,7 +154,7 @@ namespace MinecraftClient
/// Allows the user to send chat messages, commands, and to leave the server. /// Allows the user to send chat messages, commands, and to leave the server.
/// </summary> /// </summary>
private void StartTalk() private void CommandPrompt()
{ {
try try
{ {
@ -183,14 +188,14 @@ namespace MinecraftClient
string command = Settings.internalCmdChar == ' ' ? text : text.Substring(1); string command = Settings.internalCmdChar == ' ' ? text : text.Substring(1);
if (!performInternalCommand(Settings.expandVars(command), ref response_msg) && Settings.internalCmdChar == '/') if (!performInternalCommand(Settings.expandVars(command), ref response_msg) && Settings.internalCmdChar == '/')
{ {
SendChatMessage(text); SendText(text);
} }
else if (response_msg.Length > 0) else if (response_msg.Length > 0)
{ {
ConsoleIO.WriteLineFormatted("§8MCC: " + response_msg); ConsoleIO.WriteLineFormatted("§8MCC: " + response_msg);
} }
} }
else SendChatMessage(text); else SendText(text);
} }
} }
} }
@ -199,7 +204,7 @@ namespace MinecraftClient
} }
/// <summary> /// <summary>
/// Perform an internal MCC command (not a server command, use SendChatMessage() instead for that!) /// Perform an internal MCC command (not a server command, use SendText() instead for that!)
/// </summary> /// </summary>
/// <param name="command">The command</param> /// <param name="command">The command</param>
/// <param name="interactive_mode">Set to true if command was sent by the user using the command prompt</param> /// <param name="interactive_mode">Set to true if command was sent by the user using the command prompt</param>
@ -277,6 +282,10 @@ namespace MinecraftClient
handler.Disconnect(); handler.Disconnect();
handler.Dispose(); handler.Dispose();
if (cmdprompt != null)
cmdprompt.Abort();
Thread.Sleep(1000); Thread.Sleep(1000);
if (client != null) { client.Close(); } if (client != null) { client.Close(); }
@ -351,7 +360,7 @@ namespace MinecraftClient
/// <param name="text">Text to send to the server</param> /// <param name="text">Text to send to the server</param>
/// <returns>True if the text was sent with no error</returns> /// <returns>True if the text was sent with no error</returns>
public bool SendChatMessage(string text) public bool SendText(string text)
{ {
if (text.Length > 100) //Message is too long? if (text.Length > 100) //Message is too long?
{ {

View file

@ -84,8 +84,9 @@ namespace MinecraftClient
//Remote Control //Remote Control
public static bool RemoteCtrl_Enabled = false; public static bool RemoteCtrl_Enabled = false;
//Custom app variables //Custom app variables and Minecraft accounts
private static Dictionary<string, string> AppVars = new Dictionary<string, string>(); private static Dictionary<string, string> AppVars = new Dictionary<string, string>();
private static Dictionary<string, KeyValuePair<string, string>> Accounts = new Dictionary<string, KeyValuePair<string, string>>();
private enum ParseMode { Default, Main, AppVars, Proxy, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl }; private enum ParseMode { Default, Main, AppVars, Proxy, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl };
@ -157,7 +158,19 @@ namespace MinecraftClient
case "backslash": internalCmdChar = '\\'; break; case "backslash": internalCmdChar = '\\'; break;
} }
break; break;
case "accountsfile":
if (File.Exists(argValue))
{
foreach (string account_line in File.ReadAllLines(argValue))
{
//Each line contains account data: 'Alias,Login,Password'
string[] account_data = account_line.Split('#')[0].Trim().Split(',');
if (account_data.Length == 3)
Accounts[account_data[0].ToLower()]
= new KeyValuePair<string, string>(account_data[1], account_data[2]);
}
}
break;
} }
break; break;
@ -283,13 +296,14 @@ namespace MinecraftClient
+ "consoletitle=%username% - Minecraft Console Client\r\n" + "consoletitle=%username% - Minecraft Console Client\r\n"
+ "internalcmdchar=slash #use 'none', 'slash' or 'backslash'\r\n" + "internalcmdchar=slash #use 'none', 'slash' or 'backslash'\r\n"
+ "mcversion=auto #use 'auto' or '1.X.X' values\r\n" + "mcversion=auto #use 'auto' or '1.X.X' values\r\n"
+ "accountsfile=accounts.txt\r\n"
+ "exitonfailure=false\r\n" + "exitonfailure=false\r\n"
+ "timestamps=false\r\n" + "timestamps=false\r\n"
+ "\r\n" + "\r\n"
+ "[AppVars]\r\n" + "[AppVars]\r\n"
+ "#yourvar=yourvalue\r\n" + "#yourvar=yourvalue\r\n"
+ "#can be used in other fields as %yourvar%\r\n" + "#can be used in some other fields as %yourvar%\r\n"
+ "#%username% and %serverip% are reserved variable names.\r\n" + "#%username% and %serverip% are reserved variables.\r\n"
+ "\r\n" + "\r\n"
+ "[Proxy]\r\n" + "[Proxy]\r\n"
+ "enabled=false\r\n" + "enabled=false\r\n"
@ -341,26 +355,51 @@ namespace MinecraftClient
public static int str2int(string str) { try { return Convert.ToInt32(str); } catch { return 0; } } public static int str2int(string str) { try { return Convert.ToInt32(str); } catch { return 0; } }
public static bool str2bool(string str) { return str == "true" || str == "1"; } public static bool str2bool(string str) { return str == "true" || str == "1"; }
/// <summary>
/// Load login/password using an account alias
/// </summary>
/// <returns>True if the account was found and loaded</returns>
public static bool setAccount(string accountAlias)
{
accountAlias = accountAlias.ToLower();
if (Accounts.ContainsKey(accountAlias))
{
Settings.Login = Accounts[accountAlias].Key;
Settings.Password = Accounts[accountAlias].Value;
return true;
}
else return false;
}
/// <summary> /// <summary>
/// Parse a "serverip:port" couple and store the values in ServerIP and ServerPort variables /// Parse a "serverip:port" couple and store the values in ServerIP and ServerPort variables
/// </summary> /// </summary>
/// <returns>True if the server IP was valid and loaded, false otherwise</returns>
public static void setServerIP(string serverIP) public static bool setServerIP(string serverIP)
{ {
string[] sip = serverIP.Split(':'); string[] sip = serverIP.Split(':');
ServerIP = sip[0]; string host = sip[0];
if (sip.Length == 1) short port = 25565;
{
ServerPort = 25565; if (sip.Length > 1)
}
else
{ {
try try
{ {
ServerPort = Convert.ToInt16(sip[1]); port = Convert.ToInt16(sip[1]);
} }
catch (FormatException) { ServerPort = 25565; } catch (FormatException) { return false; }
} }
if (host == "localhost" || host.Contains('.'))
{
ServerIP = host;
ServerPort = port;
return true;
}
return false;
} }
/// <summary> /// <summary>

View file

@ -0,0 +1,14 @@
# Minecraft Console Client
# Account list file
# Put account data as comma separated values
# Values are: Alias,Login,Password
# It allows a fast account switching
# without directly using the credentials
# Usage examples:
# /tell <mybot> reco Player2
# /connect <serverip> Player1
Player1,playerone@email.com,thepassword
Player2,TestBot,-