]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/commands/DaemonCommand.java
Refactor command creation
[signal-cli] / src / main / java / org / asamk / signal / commands / DaemonCommand.java
index 9b6c0d63b331a66ee766d27ffd7684c4f0e7510d..494892930eec05576c19d616e6e3360bc47fb1ae 100644 (file)
@@ -3,74 +3,145 @@ package org.asamk.signal.commands;
 import net.sourceforge.argparse4j.impl.Arguments;
 import net.sourceforge.argparse4j.inf.Namespace;
 import net.sourceforge.argparse4j.inf.Subparser;
+
+import org.asamk.signal.DbusConfig;
 import org.asamk.signal.DbusReceiveMessageHandler;
 import org.asamk.signal.JsonDbusReceiveMessageHandler;
+import org.asamk.signal.JsonWriter;
+import org.asamk.signal.OutputType;
+import org.asamk.signal.OutputWriter;
+import org.asamk.signal.PlainTextWriter;
+import org.asamk.signal.commands.exceptions.CommandException;
+import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
+import org.asamk.signal.dbus.DbusSignalControlImpl;
+import org.asamk.signal.dbus.DbusSignalImpl;
 import org.asamk.signal.manager.Manager;
-import org.freedesktop.dbus.DBusConnection;
+import org.freedesktop.dbus.connections.impl.DBusConnection;
 import org.freedesktop.dbus.exceptions.DBusException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
-import static org.asamk.signal.DbusConfig.SIGNAL_BUSNAME;
-import static org.asamk.signal.DbusConfig.SIGNAL_OBJECTPATH;
-import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
+public class DaemonCommand implements MultiLocalCommand {
 
-public class DaemonCommand implements LocalCommand {
+    private final static Logger logger = LoggerFactory.getLogger(DaemonCommand.class);
+
+    @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())
                 .help("Use DBus system bus instead of user bus.");
         subparser.addArgument("--ignore-attachments")
                 .help("Don’t download attachments of received messages.")
                 .action(Arguments.storeTrue());
-        subparser.addArgument("--json")
-                .help("Output received messages in json format, one json object per line.")
-                .action(Arguments.storeTrue());
     }
 
     @Override
-    public int handleCommand(final Namespace ns, final Manager m) {
-        if (!m.isRegistered()) {
-            System.err.println("User is not registered.");
-            return 1;
+    public List<OutputType> getSupportedOutputTypes() {
+        return List.of(OutputType.PLAIN_TEXT, OutputType.JSON);
+    }
+
+    @Override
+    public void handleCommand(
+            final Namespace ns, final Manager m, final OutputWriter outputWriter
+    ) throws CommandException {
+        boolean ignoreAttachments = ns.getBoolean("ignore-attachments");
+
+        DBusConnection.DBusBusType busType;
+        if (ns.getBoolean("system")) {
+            busType = DBusConnection.DBusBusType.SYSTEM;
+        } else {
+            busType = DBusConnection.DBusBusType.SESSION;
         }
-        DBusConnection conn = null;
-        try {
-            try {
-                int busType;
-                if (ns.getBoolean("system")) {
-                    busType = DBusConnection.SYSTEM;
-                } else {
-                    busType = DBusConnection.SESSION;
-                }
-                conn = DBusConnection.getConnection(busType);
-                conn.exportObject(SIGNAL_OBJECTPATH, m);
-                conn.requestBusName(SIGNAL_BUSNAME);
-            } catch (UnsatisfiedLinkError e) {
-                System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
-                return 1;
-            } catch (DBusException e) {
-                e.printStackTrace();
-                return 2;
-            }
-            boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
+
+        try (var conn = DBusConnection.getConnection(busType)) {
+            var objectPath = DbusConfig.getObjectPath();
+            var t = run(conn, objectPath, m, outputWriter, ignoreAttachments);
+
+            conn.requestBusName(DbusConfig.getBusname());
+
             try {
-                m.receiveMessages(1, TimeUnit.HOURS, false, ignoreAttachments, ns.getBoolean("json") ? new JsonDbusReceiveMessageHandler(m, conn, SIGNAL_OBJECTPATH) : new DbusReceiveMessageHandler(m, conn, SIGNAL_OBJECTPATH));
-                return 0;
-            } catch (IOException e) {
-                System.err.println("Error while receiving messages: " + e.getMessage());
-                return 3;
-            } catch (AssertionError e) {
-                handleAssertionError(e);
-                return 1;
+                t.join();
+            } catch (InterruptedException ignored) {
             }
-        } finally {
-            if (conn != null) {
-                conn.disconnect();
+        } catch (DBusException | IOException e) {
+            logger.error("Dbus command failed", e);
+            throw new UnexpectedErrorException("Dbus command failed");
+        }
+    }
+
+    @Override
+    public void handleCommand(
+            final Namespace ns, final List<Manager> managers, final SignalCreator c, final OutputWriter outputWriter
+    ) throws CommandException {
+        boolean ignoreAttachments = ns.getBoolean("ignore-attachments");
+
+        DBusConnection.DBusBusType busType;
+        if (ns.getBoolean("system")) {
+            busType = DBusConnection.DBusBusType.SYSTEM;
+        } else {
+            busType = DBusConnection.DBusBusType.SESSION;
+        }
+
+        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, outputWriter, ignoreAttachments);
+                } catch (DBusException e) {
+                    logger.error("Failed to export object", e);
+                    return null;
+                }
+            }, DbusConfig.getObjectPath());
+            conn.exportObject(signalControl);
+
+            for (var m : managers) {
+                signalControl.addManager(m);
             }
+
+            conn.requestBusName(DbusConfig.getBusname());
+
+            signalControl.run();
+        } catch (DBusException | IOException e) {
+            logger.error("Dbus command failed", e);
+            throw new UnexpectedErrorException("Dbus command failed");
         }
     }
+
+    private Thread run(
+            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 (!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);
+                } catch (InterruptedException ignored) {
+                    break;
+                }
+            }
+        });
+
+        thread.start();
+
+        return thread;
+    }
 }