123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- /*
- * Copyright (c) 2006-2013 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.commandline;
-
- import com.dmdirc.ServerManager;
- import com.dmdirc.commandparser.commands.global.NewServer;
- import com.dmdirc.config.IdentityManager;
- import com.dmdirc.logger.ErrorLevel;
- import com.dmdirc.logger.Logger;
- import com.dmdirc.util.resourcemanager.DMDircResourceManager;
-
- import java.io.File;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.rmi.RemoteException;
- import java.util.ArrayList;
- import java.util.List;
-
- import javax.inject.Inject;
- import javax.inject.Provider;
- import javax.inject.Singleton;
-
- /**
- * Parses command line arguments for the client.
- */
- @Singleton
- public class CommandLineParser {
-
- /**
- * The arguments that the client supports, in groups of four, in the
- * following order: short option, long option, description, whether or not
- * the option takes an argument.
- */
- private final Object[][] ARGUMENTS = new Object[][]{
- {'c', "connect", "Connect to the specified server", Boolean.TRUE},
- {'d', "directory", "Use the specified configuration directory", Boolean.TRUE},
- {'e', "existing", "Try to use an existing instance of DMDirc (use with -c)", Boolean.FALSE},
- {'h', "help", "Show command line options and exit", Boolean.FALSE},
- {'l', "launcher", "Specifies the version of DMDirc's launcher", Boolean.TRUE},
- {'p', "portable", "Enable portable mode", Boolean.FALSE},
- {'r', "disable-reporting", "Disable automatic error reporting", Boolean.FALSE},
- {'v', "version", "Display client version and exit", Boolean.FALSE},
- {'k', "check", "Check if an existing instance of DMDirc exists.", Boolean.FALSE},
- };
-
- /** A list of addresses to autoconnect to. */
- private final List<URI> addresses = new ArrayList<>();
-
- /** Provider to use to get server managers. */
- private final Provider<ServerManager> serverManagerProvider;
-
- /** Whether to disable error reporting or not. */
- private boolean disablereporting;
-
- /** The version string passed for the launcher. */
- private String launcherVersion;
-
- /** The configuration directory. */
- private String configDirectory;
-
- /** The RMI server we're using. */
- private RemoteInterface server;
-
- /**
- * Creates a new instance of CommandLineParser.
- */
- @Inject
- public CommandLineParser(final Provider<ServerManager> serverManagerProvider) {
- this.serverManagerProvider = serverManagerProvider;
- }
-
- /**
- * Parses the given arguments.
- *
- * @param arguments The arguments to be parsed
- */
- public void parse(final String ... arguments) {
- boolean inArg = false;
- char previousArg = '.';
-
- for (String arg : arguments) {
- if (inArg) {
- processArgument(previousArg, arg);
- inArg = false;
- } else {
- if (arg.startsWith("--")) {
- previousArg = processLongArg(arg.substring(2));
- inArg = checkArgument(previousArg);
- } else if (arg.charAt(0) == '-') {
- previousArg = processShortArg(arg.substring(1));
- inArg = checkArgument(previousArg);
- } else {
- doUnknownArg("Unknown argument: " + arg);
- }
- }
- }
-
- if (inArg) {
- doUnknownArg("Missing parameter for argument: " + previousArg);
- }
-
- if (server != null) {
- try {
- server.connect(addresses);
- System.exit(0);
- } catch (RemoteException ex) {
- Logger.appError(ErrorLevel.MEDIUM,
- "Unable to execute remote connection", ex);
- }
- }
-
- new RemoteServer(serverManagerProvider).bind();
- }
-
- /**
- * Checks whether the specified arg type takes an argument. If it does,
- * this method returns true. If it doesn't, the method processes the
- * argument and returns false.
- *
- * @param argument The short code of the argument
- * @return True if the arg requires an argument, false otherwise
- */
- private boolean checkArgument(final char argument) {
- boolean needsArg = false;
-
- for (Object[] target : ARGUMENTS) {
- if ((Character) argument == target[0]) {
- needsArg = (Boolean) target[3];
- break;
- }
- }
-
- if (needsArg) {
- return true;
- } else {
- processArgument(argument, null);
-
- return false;
- }
- }
-
- /**
- * Processes the specified string as a single long argument.
- *
- * @param arg The string entered
- * @return The short form of the corresponding argument
- */
- private char processLongArg(final String arg) {
- for (Object[] target : ARGUMENTS) {
- if (arg.equalsIgnoreCase((String) target[1])) {
- return (Character) target[0];
- }
- }
-
- doUnknownArg("Unknown argument: " + arg);
- exit();
-
- return '.';
- }
-
- /**
- * Processes the specified string as a single short argument.
- *
- * @param arg The string entered
- * @return The short form of the corresponding argument
- */
- private char processShortArg(final String arg) {
- for (Object[] target : ARGUMENTS) {
- if (arg.equals(String.valueOf(target[0]))) {
- return (Character) target[0];
- }
- }
-
- doUnknownArg("Unknown argument: " + arg);
- exit();
-
- return '.';
- }
-
- /**
- * Processes the specified command-line argument.
- *
- * @param arg The short form of the argument used
- * @param param The (optional) string parameter for the option
- */
- private void processArgument(final char arg, final String param) {
- switch (arg) {
- case 'c':
- doConnect(param);
- break;
- case 'd':
- doDirectory(param);
- break;
- case 'e':
- doExisting();
- break;
- case 'k':
- doExistingCheck();
- break;
- case 'h':
- doHelp();
- break;
- case 'l':
- launcherVersion = param;
- break;
- case 'p':
- doDirectory(DMDircResourceManager.getCurrentWorkingDirectory());
- break;
- case 'r':
- disablereporting = true;
- break;
- case 'v':
- doVersion();
- break;
- default:
- // This really shouldn't ever happen, but we'll handle it nicely
- // anyway.
-
- doUnknownArg("Unknown argument: " + arg);
- break;
- }
- }
-
- /**
- * Informs the user that they entered an unknown argument, prints the
- * client help, and exits.
- *
- * @param message The message about the unknown argument to be displayed
- */
- private void doUnknownArg(final String message) {
- System.out.println(message);
- System.out.println();
- doHelp();
- }
-
- /**
- * Exits DMDirc.
- */
- private void exit() {
- System.exit(0);
- }
-
- /**
- * Handles the --connect argument.
- *
- * @param address The address the user told us to connect to
- */
- private void doConnect(final String address) {
- try {
- addresses.add(NewServer.getURI(address));
- } catch (URISyntaxException ex) {
- doUnknownArg("Invalid address specified: " + ex.getMessage());
- }
- }
-
- /**
- * Handles the --existing argument.
- */
- private void doExisting() {
- server = RemoteServer.getServer();
-
- if (server == null) {
- System.err.println("Unable to connect to existing instance");
- }
- }
-
- /**
- * Handles the --check argument.
- */
- private void doExistingCheck() {
- if (RemoteServer.getServer() == null) {
- System.out.println("Existing instance not found.");
- System.exit(1);
- } else {
- System.out.println("Existing instance found.");
- System.exit(0);
- }
- }
-
- /**
- * Sets the config directory to the one specified.
- *
- * @param dir The new config directory
- */
- private void doDirectory(final String dir) {
- if (dir.endsWith(File.separator)) {
- configDirectory = dir;
- } else {
- configDirectory = dir + File.separator;
- }
- }
-
- /**
- * Prints out the client version and exits.
- */
- private void doVersion() {
- System.out.println("DMDirc - a cross-platform, open-source IRC client.");
- System.out.println();
- System.out.println(" Version: " + IdentityManager.getIdentityManager()
- .getGlobalConfiguration().getOption("version", "version"));
- System.out.println(" Update channel: " + IdentityManager.getIdentityManager()
- .getGlobalConfiguration().getOption("updater", "channel"));
- exit();
- }
-
- /**
- * Prints out client help and exits.
- */
- private void doHelp() {
- System.out.println("Usage: java -jar DMDirc.jar [options]");
- System.out.println();
- System.out.println("Valid options:");
- System.out.println();
-
- int maxLength = 0;
-
- for (Object[] arg : ARGUMENTS) {
- final String needsArg = ((Boolean) arg[3]) ? " <argument>" : "";
-
- if ((arg[1] + needsArg).length() > maxLength) {
- maxLength = (arg[1] + needsArg).length();
- }
- }
-
- for (Object[] arg : ARGUMENTS) {
- final String needsArg = ((Boolean) arg[3]) ? " <argument>" : "";
- final StringBuilder desc = new StringBuilder(maxLength + 1);
-
- desc.append(arg[1]);
-
- while (desc.length() < maxLength + 1) {
- desc.append(' ');
- }
-
- System.out.print(" -" + arg[0] + needsArg);
- System.out.println(" --" + desc + needsArg + " " + arg[2]);
- System.out.println();
- }
-
- exit();
- }
-
- /**
- * Returns the user-supplied configuration directory.
- *
- * @return The user-supplied config directory, or {@code null} if none
- * was supplied.
- */
- public String getConfigDirectory() {
- return configDirectory;
- }
-
- /**
- * Indicates whether the user has requested error reporting be disabled.
- *
- * @return True if the user has disabled reporting, false otherwise.
- */
- public boolean getDisableReporting() {
- return disablereporting;
- }
-
- /**
- * Returns the provided launcher version, if any.
- *
- * @return The version supplied by the launcher, or {@code null} if no
- * launcher is identified.
- */
- public String getLauncherVersion() {
- return launcherVersion;
- }
-
- /**
- * Processes arguments once the client has been loaded properly.
- * This allows us to auto-connect to servers, etc.
- *
- * @param serverManager The server manager to use to connect servers.
- */
- public void processArguments(final ServerManager serverManager) {
- for (URI address : addresses) {
- serverManager.connectToAddress(address);
- }
- }
-
- }
|