]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/App.java
Handle queued actions also when thread is interrupted
[signal-cli] / src / main / java / org / asamk / signal / App.java
index 54c475b163305a578e7335c7bae8e64b96a3b04a..6b31e2bccfc5bedda76697faef402c081c5079b5 100644 (file)
@@ -14,6 +14,7 @@ import org.asamk.signal.commands.LocalCommand;
 import org.asamk.signal.commands.MultiLocalCommand;
 import org.asamk.signal.commands.ProvisioningCommand;
 import org.asamk.signal.commands.RegistrationCommand;
+import org.asamk.signal.commands.SignalCreator;
 import org.asamk.signal.commands.exceptions.CommandException;
 import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
 import org.asamk.signal.commands.exceptions.UserErrorException;
@@ -66,21 +67,19 @@ public class App {
 
         parser.addArgument("-o", "--output")
                 .help("Choose to output in plain text or JSON")
-                .type(Arguments.enumStringType(OutputType.class))
-                .setDefault(OutputType.PLAIN_TEXT);
+                .type(Arguments.enumStringType(OutputType.class));
 
         parser.addArgument("--service-environment")
-                .help("Choose the server environment to use, SANDBOX or LIVE.")
+                .help("Choose the server environment to use.")
                 .type(Arguments.enumStringType(ServiceEnvironmentCli.class))
                 .setDefault(ServiceEnvironmentCli.LIVE);
 
         var subparsers = parser.addSubparsers().title("subcommands").dest("command");
 
-        final var commands = Commands.getCommands();
-        for (var entry : commands.entrySet()) {
-            var subparser = subparsers.addParser(entry.getKey());
-            entry.getValue().attachToSubparser(subparser);
-        }
+        Commands.getCommandSubparserAttachers().forEach((key, value) -> {
+            var subparser = subparsers.addParser(key);
+            value.attachToSubparser(subparser);
+        });
 
         return parser;
     }
@@ -96,9 +95,16 @@ public class App {
             throw new UserErrorException("Command not implemented!");
         }
 
-        var outputType = ns.<OutputType>get("output");
-        if (!command.getSupportedOutputTypes().contains(outputType)) {
-            throw new UserErrorException("Command doesn't support output type " + outputType.toString());
+        var outputTypeInput = ns.<OutputType>get("output");
+        var outputType = outputTypeInput == null
+                ? command.getSupportedOutputTypes().stream().findFirst().orElse(null)
+                : outputTypeInput;
+        var outputWriter = outputType == null
+                ? null
+                : outputType == OutputType.JSON ? new JsonWriterImpl(System.out) : new PlainTextWriterImpl(System.out);
+
+        if (outputWriter != null && !command.getSupportedOutputTypes().contains(outputType)) {
+            throw new UserErrorException("Command doesn't support output type " + outputType);
         }
 
         var username = ns.getString("username");
@@ -107,7 +113,7 @@ public class App {
         final var useDbusSystem = ns.getBoolean("dbus-system");
         if (useDbus || useDbusSystem) {
             // If username is null, it will connect to the default object path
-            initDbusClient(command, username, useDbusSystem);
+            initDbusClient(command, username, useDbusSystem, outputWriter);
             return;
         }
 
@@ -138,7 +144,7 @@ public class App {
                 throw new UserErrorException("You cannot specify a username (phone number) when linking");
             }
 
-            handleProvisioningCommand((ProvisioningCommand) command, dataPath, serviceEnvironment);
+            handleProvisioningCommand((ProvisioningCommand) command, dataPath, serviceEnvironment, outputWriter);
             return;
         }
 
@@ -146,7 +152,11 @@ public class App {
             var usernames = Manager.getAllLocalUsernames(dataPath);
 
             if (command instanceof MultiLocalCommand) {
-                handleMultiLocalCommand((MultiLocalCommand) command, dataPath, serviceEnvironment, usernames);
+                handleMultiLocalCommand((MultiLocalCommand) command,
+                        dataPath,
+                        serviceEnvironment,
+                        usernames,
+                        outputWriter);
                 return;
             }
 
@@ -171,14 +181,17 @@ public class App {
             throw new UserErrorException("Command only works via dbus");
         }
 
-        handleLocalCommand((LocalCommand) command, username, dataPath, serviceEnvironment);
+        handleLocalCommand((LocalCommand) command, username, dataPath, serviceEnvironment, outputWriter);
     }
 
     private void handleProvisioningCommand(
-            final ProvisioningCommand command, final File dataPath, final ServiceEnvironment serviceEnvironment
+            final ProvisioningCommand command,
+            final File dataPath,
+            final ServiceEnvironment serviceEnvironment,
+            final OutputWriter outputWriter
     ) throws CommandException {
         var pm = ProvisioningManager.init(dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
-        command.handleCommand(ns, pm);
+        command.handleCommand(ns, pm, outputWriter);
     }
 
     private void handleRegistrationCommand(
@@ -208,10 +221,11 @@ public class App {
             final LocalCommand command,
             final String username,
             final File dataPath,
-            final ServiceEnvironment serviceEnvironment
+            final ServiceEnvironment serviceEnvironment,
+            final OutputWriter outputWriter
     ) throws CommandException {
         try (var m = loadManager(username, dataPath, serviceEnvironment)) {
-            command.handleCommand(ns, m);
+            command.handleCommand(ns, m, outputWriter);
         } catch (IOException e) {
             logger.warn("Cleanup failed", e);
         }
@@ -221,7 +235,8 @@ public class App {
             final MultiLocalCommand command,
             final File dataPath,
             final ServiceEnvironment serviceEnvironment,
-            final List<String> usernames
+            final List<String> usernames,
+            final OutputWriter outputWriter
     ) throws CommandException {
         final var managers = new ArrayList<Manager>();
         for (String u : usernames) {
@@ -232,7 +247,17 @@ public class App {
             }
         }
 
-        command.handleCommand(ns, managers);
+        command.handleCommand(ns, managers, new SignalCreator() {
+            @Override
+            public ProvisioningManager getNewProvisioningManager() {
+                return ProvisioningManager.init(dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
+            }
+
+            @Override
+            public RegistrationManager getNewRegistrationManager(String username) throws IOException {
+                return RegistrationManager.init(username, dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
+            }
+        }, outputWriter);
 
         for (var m : managers) {
             try {
@@ -272,7 +297,7 @@ public class App {
     }
 
     private void initDbusClient(
-            final Command command, final String username, final boolean systemBus
+            final Command command, final String username, final boolean systemBus, final OutputWriter outputWriter
     ) throws CommandException {
         try {
             DBusConnection.DBusBusType busType;
@@ -286,7 +311,7 @@ public class App {
                         DbusConfig.getObjectPath(username),
                         Signal.class);
 
-                handleCommand(command, ts, dBusConn);
+                handleCommand(command, ts, dBusConn, outputWriter);
             }
         } catch (DBusException | IOException e) {
             logger.error("Dbus client failed", e);
@@ -294,43 +319,22 @@ public class App {
         }
     }
 
-    private void handleCommand(Command command, Signal ts, DBusConnection dBusConn) throws CommandException {
+    private void handleCommand(
+            Command command, Signal ts, DBusConnection dBusConn, OutputWriter outputWriter
+    ) throws CommandException {
         if (command instanceof ExtendedDbusCommand) {
-            ((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn);
+            ((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn, outputWriter);
         } else if (command instanceof DbusCommand) {
-            ((DbusCommand) command).handleCommand(ns, ts);
+            ((DbusCommand) command).handleCommand(ns, ts, outputWriter);
         } else {
             throw new UserErrorException("Command is not yet implemented via dbus");
         }
     }
 
     /**
-     * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
-     * - $HOME/.config/signal
-     * - $HOME/.config/textsecure
-     *
-     * @return the data directory to be used by signal-cli.
+     * @return the default data directory to be used by signal-cli.
      */
     private static File getDefaultDataPath() {
-        var dataPath = new File(IOUtils.getDataHomeDir(), "signal-cli");
-        if (dataPath.exists()) {
-            return dataPath;
-        }
-
-        var configPath = new File(System.getProperty("user.home"), ".config");
-
-        var legacySettingsPath = new File(configPath, "signal");
-        if (legacySettingsPath.exists()) {
-            logger.warn("Using legacy data path \"{}\", please move it to \"{}\".", legacySettingsPath, dataPath);
-            return legacySettingsPath;
-        }
-
-        legacySettingsPath = new File(configPath, "textsecure");
-        if (legacySettingsPath.exists()) {
-            logger.warn("Using legacy data path \"{}\", please move it to \"{}\".", legacySettingsPath, dataPath);
-            return legacySettingsPath;
-        }
-
-        return dataPath;
+        return new File(IOUtils.getDataHomeDir(), "signal-cli");
     }
 }