]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/App.java
Check if required quote-author parameter is missing
[signal-cli] / src / main / java / org / asamk / signal / App.java
index 9ac4b190813aa0480e3538cab98a44dfacfe98dd..e494d201016189cec989f6ef0372962fefc9f1b2 100644 (file)
@@ -16,7 +16,6 @@ import org.asamk.signal.commands.exceptions.CommandException;
 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.DbusCommandHandler;
 import org.asamk.signal.manager.Manager;
 import org.asamk.signal.manager.RegistrationManager;
 import org.asamk.signal.manager.Settings;
@@ -29,11 +28,6 @@ import org.asamk.signal.output.JsonWriterImpl;
 import org.asamk.signal.output.OutputWriter;
 import org.asamk.signal.output.PlainTextWriterImpl;
 import org.asamk.signal.util.IOUtils;
-import org.freedesktop.dbus.connections.impl.DBusConnection;
-import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
-import org.freedesktop.dbus.errors.ServiceUnknown;
-import org.freedesktop.dbus.exceptions.DBusException;
-import org.freedesktop.dbus.exceptions.DBusExecutionException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,10 +38,11 @@ import java.io.OutputStreamWriter;
 import java.util.Set;
 
 import static net.sourceforge.argparse4j.DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS;
+import static org.asamk.signal.dbus.DbusCommandHandler.initDbusClient;
 
 public class App {
 
-    private final static Logger logger = LoggerFactory.getLogger(App.class);
+    private static final Logger logger = LoggerFactory.getLogger(App.class);
 
     private final Namespace ns;
 
@@ -81,6 +76,10 @@ public class App {
                 .dest("global-dbus-system")
                 .help("Make request via system dbus.")
                 .action(Arguments.storeTrue());
+        parser.addArgument("--bus-name")
+                .dest("global-bus-name")
+                .setDefault(DbusConfig.getBusname())
+                .help("Specify the D-Bus bus name to connect to.");
 
         parser.addArgument("-o", "--output")
                 .help("Choose to output in plain text or JSON")
@@ -100,6 +99,9 @@ public class App {
                 .help("Disable message send log (for resending messages that recipient couldn't decrypt)")
                 .action(Arguments.storeTrue());
 
+        parser.epilog(
+                "The global arguments are shown with 'signal-cli -h' and need to come before the subcommand, while the subcommand-specific arguments (shown with 'signal-cli SUBCOMMAND -h') need to be given after the subcommand.");
+
         var subparsers = parser.addSubparsers().title("subcommands").dest("command");
 
         Commands.getCommandSubparserAttachers().forEach((key, value) -> {
@@ -130,8 +132,9 @@ public class App {
         final var useDbus = Boolean.TRUE.equals(ns.getBoolean("global-dbus"));
         final var useDbusSystem = Boolean.TRUE.equals(ns.getBoolean("global-dbus-system"));
         if (useDbus || useDbusSystem) {
+            final var busName = ns.getString("global-bus-name");
             // If account is null, it will connect to the default object path
-            initDbusClient(command, account, useDbusSystem, commandHandler);
+            initDbusClient(command, account, useDbusSystem, busName, commandHandler);
             return;
         }
 
@@ -165,20 +168,7 @@ public class App {
                 return;
             }
 
-            Set<String> accounts;
-            try {
-                accounts = signalAccountFiles.getAllLocalAccountNumbers();
-            } catch (IOException e) {
-                throw new IOErrorException("Failed to load local accounts file", e);
-            }
-            if (accounts.size() == 0) {
-                throw new UserErrorException("No local users found, you first need to register or link an account");
-            } else if (accounts.size() > 1) {
-                throw new UserErrorException(
-                        "Multiple users found, you need to specify an account (phone number) with -a");
-            }
-
-            account = accounts.stream().findFirst().get();
+            account = getAccountIfOnlyOne(signalAccountFiles);
         } else if (!Manager.isValidNumber(account, null)) {
             throw new UserErrorException("Invalid account (phone number), make sure you include the country code.");
         }
@@ -196,6 +186,21 @@ public class App {
         throw new UserErrorException("Command only works in multi-account mode");
     }
 
+    private static String getAccountIfOnlyOne(final SignalAccountFiles signalAccountFiles) throws IOErrorException, UserErrorException {
+        Set<String> accounts;
+        try {
+            accounts = signalAccountFiles.getAllLocalAccountNumbers();
+        } catch (IOException e) {
+            throw new IOErrorException("Failed to load local accounts file", e);
+        }
+        if (accounts.isEmpty()) {
+            throw new UserErrorException("No local users found, you first need to register or link an account");
+        } else if (accounts.size() > 1) {
+            throw new UserErrorException("Multiple users found, you need to specify an account (phone number) with -a");
+        }
+        return accounts.stream().findFirst().get();
+    }
+
     private OutputWriter getOutputWriter(final Command command) throws UserErrorException {
         final var outputTypeInput = ns.<OutputType>get("output");
         final var outputType = outputTypeInput == null ? command.getSupportedOutputTypes()
@@ -274,8 +279,6 @@ public class App {
     ) throws CommandException {
         try (var m = loadManager(account, signalAccountFiles)) {
             commandHandler.handleLocalCommand(command, m);
-        } catch (IOException e) {
-            logger.warn("Cleanup failed", e);
         }
     }
 
@@ -292,7 +295,8 @@ public class App {
     }
 
     private RegistrationManager loadRegistrationManager(
-            final String account, final SignalAccountFiles signalAccountFiles
+            final String account,
+            final SignalAccountFiles signalAccountFiles
     ) throws UnexpectedErrorException {
         try {
             return signalAccountFiles.initRegistrationManager(account);
@@ -306,7 +310,8 @@ public class App {
     }
 
     private Manager loadManager(
-            final String account, final SignalAccountFiles signalAccountFiles
+            final String account,
+            final SignalAccountFiles signalAccountFiles
     ) throws CommandException {
         logger.trace("Loading account file for {}", account);
         try {
@@ -331,24 +336,6 @@ public class App {
         }
     }
 
-    private void initDbusClient(
-            final Command command, final String account, final boolean systemBus, final CommandHandler commandHandler
-    ) throws CommandException {
-        try {
-            final var busType = systemBus ? DBusConnection.DBusBusType.SYSTEM : DBusConnection.DBusBusType.SESSION;
-            try (var dBusConn = DBusConnectionBuilder.forType(busType).build()) {
-                DbusCommandHandler.handleCommand(command, account, dBusConn, commandHandler);
-            }
-        } catch (ServiceUnknown e) {
-            throw new UserErrorException("signal-cli DBus daemon not running on "
-                    + (systemBus ? "system" : "session")
-                    + " bus: "
-                    + e.getMessage(), e);
-        } catch (DBusExecutionException | DBusException | IOException e) {
-            throw new UnexpectedErrorException("Dbus client failed: " + e.getMessage(), e);
-        }
-    }
-
     /**
      * @return the default data directory to be used by signal-cli.
      */