]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/App.java
Switch to a less cpu intensive function to check if libsignal-client is available
[signal-cli] / src / main / java / org / asamk / signal / App.java
index f807e8fdfb5dfa6ef7622b4bad4e003be733ecec..d06cd7988d1e096b3733fbeb42c9583c976458b3 100644 (file)
@@ -18,6 +18,9 @@ import org.asamk.signal.commands.exceptions.IOErrorException;
 import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
 import org.asamk.signal.commands.exceptions.UserErrorException;
 import org.asamk.signal.dbus.DbusManagerImpl;
+import org.asamk.signal.dbus.DbusMultiAccountManagerImpl;
+import org.asamk.signal.dbus.DbusProvisioningManagerImpl;
+import org.asamk.signal.dbus.DbusRegistrationManagerImpl;
 import org.asamk.signal.manager.Manager;
 import org.asamk.signal.manager.MultiAccountManagerImpl;
 import org.asamk.signal.manager.NotRegisteredException;
@@ -64,8 +67,11 @@ public class App {
 
         parser.addArgument("-v", "--version").help("Show package version.").action(Arguments.version());
         parser.addArgument("--verbose")
-                .help("Raise log level and include lib signal logs.")
-                .action(Arguments.storeTrue());
+                .help("Raise log level and include lib signal logs. Specify multiple times for even more logs.")
+                .action(Arguments.count());
+        parser.addArgument("--log-file")
+                .type(File.class)
+                .help("Write log output to the given file. If --verbose is also given, the detailed logs will only be written to the log file.");
         parser.addArgument("-c", "--config")
                 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
 
@@ -152,7 +158,7 @@ public class App {
         final var serviceEnvironmentCli = ns.<ServiceEnvironmentCli>get("service-environment");
         final var serviceEnvironment = serviceEnvironmentCli == ServiceEnvironmentCli.LIVE
                 ? ServiceEnvironment.LIVE
-                : ServiceEnvironment.SANDBOX;
+                : ServiceEnvironment.STAGING;
 
         final var trustNewIdentityCli = ns.<TrustNewIdentityCli>get("trust-new-identities");
         final var trustNewIdentity = trustNewIdentityCli == TrustNewIdentityCli.ON_FIRST_USE
@@ -220,6 +226,22 @@ public class App {
         command.handleCommand(ns, pm, outputWriter);
     }
 
+    private void handleProvisioningCommand(
+            final ProvisioningCommand c, final DBusConnection dBusConn, final OutputWriter outputWriter
+    ) throws CommandException, DBusException {
+        final var signalControl = dBusConn.getRemoteObject(DbusConfig.getBusname(),
+                DbusConfig.getObjectPath(),
+                SignalControl.class);
+        final var provisioningManager = new DbusProvisioningManagerImpl(signalControl, dBusConn);
+        try {
+            c.handleCommand(ns, provisioningManager, outputWriter);
+        } catch (UnsupportedOperationException e) {
+            throw new UserErrorException("Command is not yet implemented via dbus", e);
+        } catch (DBusExecutionException e) {
+            throw new UnexpectedErrorException(e.getMessage(), e);
+        }
+    }
+
     private void handleRegistrationCommand(
             final RegistrationCommand command,
             final String account,
@@ -243,6 +265,21 @@ public class App {
         }
     }
 
+    private void handleRegistrationCommand(
+            final RegistrationCommand c, String account, final DBusConnection dBusConn, final OutputWriter outputWriter
+    ) throws CommandException, DBusException {
+        final var signalControl = dBusConn.getRemoteObject(DbusConfig.getBusname(),
+                DbusConfig.getObjectPath(),
+                SignalControl.class);
+        try (final var registrationManager = new DbusRegistrationManagerImpl(account, signalControl, dBusConn)) {
+            c.handleCommand(ns, registrationManager);
+        } catch (UnsupportedOperationException e) {
+            throw new UserErrorException("Command is not yet implemented via dbus", e);
+        } catch (DBusExecutionException e) {
+            throw new UnexpectedErrorException(e.getMessage(), e);
+        }
+    }
+
     private void handleLocalCommand(
             final LocalCommand command,
             final String account,
@@ -258,6 +295,22 @@ public class App {
         }
     }
 
+    private void handleLocalCommand(
+            final LocalCommand c,
+            String accountObjectPath,
+            final DBusConnection dBusConn,
+            final OutputWriter outputWriter
+    ) throws CommandException, DBusException {
+        var signal = dBusConn.getRemoteObject(DbusConfig.getBusname(), accountObjectPath, Signal.class);
+        try (final var m = new DbusManagerImpl(signal, dBusConn)) {
+            c.handleCommand(ns, m, outputWriter);
+        } catch (UnsupportedOperationException e) {
+            throw new UserErrorException("Command is not yet implemented via dbus", e);
+        } catch (DBusExecutionException e) {
+            throw new UnexpectedErrorException(e.getMessage(), e);
+        }
+    }
+
     private void handleMultiLocalCommand(
             final MultiLocalCommand command,
             final File dataPath,
@@ -283,6 +336,19 @@ public class App {
         }
     }
 
+    private void handleMultiLocalCommand(
+            final MultiLocalCommand c, final DBusConnection dBusConn, final OutputWriter outputWriter
+    ) throws CommandException, DBusException {
+        final var signalControl = dBusConn.getRemoteObject(DbusConfig.getBusname(),
+                DbusConfig.getObjectPath(),
+                SignalControl.class);
+        try (final var multiAccountManager = new DbusMultiAccountManagerImpl(signalControl, dBusConn)) {
+            c.handleCommand(ns, multiAccountManager, outputWriter);
+        } catch (UnsupportedOperationException e) {
+            throw new UserErrorException("Command is not yet implemented via dbus", e);
+        }
+    }
+
     private Manager loadManager(
             final String account,
             final File dataPath,
@@ -290,6 +356,7 @@ public class App {
             final TrustNewIdentity trustNewIdentity
     ) throws CommandException {
         Manager manager;
+        logger.trace("Loading account file for {}", account);
         try {
             manager = Manager.init(account, dataPath, serviceEnvironment, BaseConfig.USER_AGENT, trustNewIdentity);
         } catch (NotRegisteredException e) {
@@ -304,6 +371,7 @@ public class App {
                     + ")", e);
         }
 
+        logger.trace("Checking account state");
         try {
             manager.checkAccountState();
         } catch (IOException e) {
@@ -329,13 +397,32 @@ public class App {
                 busType = DBusConnection.DBusBusType.SESSION;
             }
             try (var dBusConn = DBusConnection.getConnection(busType)) {
+                if (command instanceof ProvisioningCommand c) {
+                    if (account != null) {
+                        throw new UserErrorException("You cannot specify a account (phone number) when linking");
+                    }
+
+                    handleProvisioningCommand(c, dBusConn, outputWriter);
+                    return;
+                }
+
+                if (account == null && command instanceof MultiLocalCommand c) {
+                    handleMultiLocalCommand(c, dBusConn, outputWriter);
+                    return;
+                }
+                if (account != null && command instanceof RegistrationCommand c) {
+                    handleRegistrationCommand(c, account, dBusConn, outputWriter);
+                    return;
+                }
+                if (!(command instanceof LocalCommand localCommand)) {
+                    throw new UserErrorException("Command only works in multi-account mode");
+                }
+
                 var accountObjectPath = account == null ? tryGetSingleAccountObjectPath(dBusConn) : null;
                 if (accountObjectPath == null) {
                     accountObjectPath = DbusConfig.getObjectPath(account);
                 }
-                var ts = dBusConn.getRemoteObject(DbusConfig.getBusname(), accountObjectPath, Signal.class);
-
-                handleCommand(command, ts, dBusConn, outputWriter);
+                handleLocalCommand(localCommand, accountObjectPath, dBusConn, outputWriter);
             }
         } catch (ServiceUnknown e) {
             throw new UserErrorException("signal-cli DBus daemon not running on "
@@ -367,22 +454,6 @@ public class App {
         }
     }
 
-    private void handleCommand(
-            Command command, Signal ts, DBusConnection dBusConn, OutputWriter outputWriter
-    ) throws CommandException {
-        if (command instanceof LocalCommand localCommand) {
-            try (final var m = new DbusManagerImpl(ts, dBusConn)) {
-                localCommand.handleCommand(ns, m, outputWriter);
-            } catch (UnsupportedOperationException e) {
-                throw new UserErrorException("Command is not yet implemented via dbus", e);
-            } catch (IOException | DBusExecutionException e) {
-                throw new UnexpectedErrorException(e.getMessage(), e);
-            }
-        } else {
-            throw new UserErrorException("Command is not yet implemented via dbus");
-        }
-    }
-
     /**
      * @return the default data directory to be used by signal-cli.
      */