package com.evanram.code.java.dimchat_client;
import java.util.Arrays;
public class ClientCommand
{
private String command;
private String[] args;
public ClientCommand(String command)
{
if(command.startsWith(".") && command.length() > 0)
command = command.substring(1);
this.command = command;
formatCommandString();
execute(this.command, args);
}
private void execute(String command, String[] args)
{
if("help".equals(command))
{
Message.log("Help", "These are client commands, send server commands with the \'!\' prefix.");
Message.log("Help Commands", "Help, Info, Password, Version, Disconnect, Quit");
}
else if("info".equals(command))
{
Message.logInfo("Client Version: " + Client.getVersion().toString());
Message.logInfo("Connected to " + Connections.address + ":" + Connections.port);
}
else if("password".equals(command) || "pswd".equals(command))
{
if(args.length > 0)
{
Client.getDimcrypt().setPassword(args[0]);
Message.log("Encryption", "Changed encryption password.");
}
else
{
Message.log("Encryption", "Cleared encryption password.");
}
}
else if("version".equals(command) || "ver".equals(command))
{
Message.log("Version", Client.getVersion().toString());
}
else if("disconnect".equals(command))
{
Message.logInfo("Disconnecting.");
Client.newConnection();
}
else if("quit".equals(command))
{
System.exit(0);
}
else
{
Message.logError("Bad client command! Type \'.help\' for help.");
}
}
private void formatCommandString()
{
if(command.length() > 1)
{
if(command.charAt(0) == '!')
command = command.substring(1);
}
processArguments();
command = command.toLowerCase();
}
private void processArguments()
{
command = command.replace("\\s+", "");
String[] argsWithCmd = command.split(" "); //arguments that contain command
command = argsWithCmd[0];
args = Arrays.copyOfRange(argsWithCmd, 1, argsWithCmd.length); //remove command from args
}
}