]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/commands/DaemonCommand.java
remove org.asamk.signal.manager.api.Configuration
[signal-cli] / src / main / java / org / asamk / signal / commands / DaemonCommand.java
index 0591486c931f80b85450ae136fd74b24a6cbaeff..3e453b5c0908ef086fbf6afce3a7314b39e61f48 100644 (file)
@@ -54,10 +54,10 @@ public class DaemonCommand implements MultiLocalCommand {
     public void handleCommand(
             final Namespace ns, final Manager m, final OutputWriter outputWriter
     ) throws CommandException {
-        boolean ignoreAttachments = ns.getBoolean("ignore-attachments");
+        boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
 
         DBusConnection.DBusBusType busType;
-        if (ns.getBoolean("system")) {
+        if (Boolean.TRUE.equals(ns.getBoolean("system"))) {
             busType = DBusConnection.DBusBusType.SYSTEM;
         } else {
             busType = DBusConnection.DBusBusType.SESSION;
@@ -68,6 +68,7 @@ public class DaemonCommand implements MultiLocalCommand {
             var t = run(conn, objectPath, m, outputWriter, ignoreAttachments);
 
             conn.requestBusName(DbusConfig.getBusname());
+            logger.info("DBus daemon running in single-user mode for " + m.getSelfNumber());
 
             try {
                 t.join();
@@ -75,7 +76,7 @@ public class DaemonCommand implements MultiLocalCommand {
             }
         } catch (DBusException | IOException e) {
             logger.error("Dbus command failed", e);
-            throw new UnexpectedErrorException("Dbus command failed");
+            throw new UnexpectedErrorException("Dbus command failed", e);
         }
     }
 
@@ -83,10 +84,10 @@ public class DaemonCommand implements MultiLocalCommand {
     public void handleCommand(
             final Namespace ns, final List<Manager> managers, final SignalCreator c, final OutputWriter outputWriter
     ) throws CommandException {
-        boolean ignoreAttachments = ns.getBoolean("ignore-attachments");
+        boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
 
         DBusConnection.DBusBusType busType;
-        if (ns.getBoolean("system")) {
+        if (Boolean.TRUE.equals(ns.getBoolean("system"))) {
             busType = DBusConnection.DBusBusType.SYSTEM;
         } else {
             busType = DBusConnection.DBusBusType.SESSION;
@@ -95,7 +96,7 @@ public class DaemonCommand implements MultiLocalCommand {
         try (var conn = DBusConnection.getConnection(busType)) {
             final var signalControl = new DbusSignalControlImpl(c, m -> {
                 try {
-                    final var objectPath = DbusConfig.getObjectPath(m.getUsername());
+                    final var objectPath = DbusConfig.getObjectPath(m.getSelfNumber());
                     return run(conn, objectPath, m, outputWriter, ignoreAttachments);
                 } catch (DBusException e) {
                     logger.error("Failed to export object", e);
@@ -109,20 +110,24 @@ public class DaemonCommand implements MultiLocalCommand {
             }
 
             conn.requestBusName(DbusConfig.getBusname());
+            logger.info("DBus daemon running in anonymous mode");
 
             signalControl.run();
         } catch (DBusException | IOException e) {
             logger.error("Dbus command failed", e);
-            throw new UnexpectedErrorException("Dbus command failed");
+            throw new UnexpectedErrorException("Dbus command failed", e);
         }
     }
 
     private Thread run(
             DBusConnection conn, String objectPath, Manager m, OutputWriter outputWriter, boolean ignoreAttachments
     ) throws DBusException {
-        conn.exportObject(new DbusSignalImpl(m, objectPath));
+        final var signal = new DbusSignalImpl(m, conn, objectPath);
+        conn.exportObject(signal);
+        final var initThread = new Thread(signal::initObjects);
+        initThread.start();
 
-        logger.info("Exported dbus object: " + objectPath);
+        logger.debug("Exported dbus object: " + objectPath);
 
         final var thread = new Thread(() -> {
             while (!Thread.interrupted()) {
@@ -136,6 +141,11 @@ public class DaemonCommand implements MultiLocalCommand {
                     logger.warn("Receiving messages failed, retrying", e);
                 }
             }
+            try {
+                initThread.join();
+            } catch (InterruptedException ignored) {
+            }
+            signal.close();
         });
 
         thread.start();