]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/commands/DaemonCommand.java
Remove workaround for getBoolean from JsonRpcLocalCommand
[signal-cli] / src / main / java / org / asamk / signal / commands / DaemonCommand.java
index 7988c8efcdc44d1faff1a09a91d05e8c51e771d9..5045db9a28c89fce326bdfe780c9c479ba3c7ea8 100644 (file)
@@ -23,15 +23,19 @@ import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.util.List;
-import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
 public class DaemonCommand implements MultiLocalCommand {
 
     private final static Logger logger = LoggerFactory.getLogger(DaemonCommand.class);
-    private final OutputWriter outputWriter;
 
-    public static void attachToSubparser(final Subparser subparser) {
+    @Override
+    public String getName() {
+        return "daemon";
+    }
+
+    @Override
+    public void attachToSubparser(final Subparser subparser) {
         subparser.help("Run in daemon mode and provide an experimental dbus interface.");
         subparser.addArgument("--system")
                 .action(Arguments.storeTrue())
@@ -41,21 +45,19 @@ public class DaemonCommand implements MultiLocalCommand {
                 .action(Arguments.storeTrue());
     }
 
-    public DaemonCommand(final OutputWriter outputWriter) {
-        this.outputWriter = outputWriter;
-    }
-
     @Override
-    public Set<OutputType> getSupportedOutputTypes() {
-        return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON);
+    public List<OutputType> getSupportedOutputTypes() {
+        return List.of(OutputType.PLAIN_TEXT, OutputType.JSON);
     }
 
     @Override
-    public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
-        boolean ignoreAttachments = ns.getBoolean("ignore-attachments");
+    public void handleCommand(
+            final Namespace ns, final Manager m, final OutputWriter outputWriter
+    ) throws CommandException {
+        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;
@@ -63,7 +65,7 @@ public class DaemonCommand implements MultiLocalCommand {
 
         try (var conn = DBusConnection.getConnection(busType)) {
             var objectPath = DbusConfig.getObjectPath();
-            var t = run(conn, objectPath, m, ignoreAttachments);
+            var t = run(conn, objectPath, m, outputWriter, ignoreAttachments);
 
             conn.requestBusName(DbusConfig.getBusname());
 
@@ -73,18 +75,18 @@ 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);
         }
     }
 
     @Override
     public void handleCommand(
-            final Namespace ns, final List<Manager> managers, SignalCreator c
+            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;
@@ -93,8 +95,8 @@ 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());
-                    return run(conn, objectPath, m, ignoreAttachments);
+                    final var objectPath = DbusConfig.getObjectPath(m.getSelfNumber());
+                    return run(conn, objectPath, m, outputWriter, ignoreAttachments);
                 } catch (DBusException e) {
                     logger.error("Failed to export object", e);
                     return null;
@@ -111,24 +113,25 @@ public class DaemonCommand implements MultiLocalCommand {
             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, boolean ignoreAttachments
+            DBusConnection conn, String objectPath, Manager m, OutputWriter outputWriter, boolean ignoreAttachments
     ) throws DBusException {
         conn.exportObject(new DbusSignalImpl(m, objectPath));
 
         logger.info("Exported dbus object: " + objectPath);
 
         final var thread = new Thread(() -> {
-            while (true) {
+            while (!Thread.interrupted()) {
                 try {
                     final var receiveMessageHandler = outputWriter instanceof JsonWriter
                             ? new JsonDbusReceiveMessageHandler(m, (JsonWriter) outputWriter, conn, objectPath)
                             : new DbusReceiveMessageHandler(m, (PlainTextWriter) outputWriter, conn, objectPath);
                     m.receiveMessages(1, TimeUnit.HOURS, false, ignoreAttachments, receiveMessageHandler);
+                    break;
                 } catch (IOException e) {
                     logger.warn("Receiving messages failed, retrying", e);
                 }