X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/e74be0c345321888c1fbfa05616cb90cf3f07ffb..17d00d6ca8d10ffb783707b12181b29c8fb8a97a:/src/main/java/org/asamk/signal/commands/DaemonCommand.java diff --git a/src/main/java/org/asamk/signal/commands/DaemonCommand.java b/src/main/java/org/asamk/signal/commands/DaemonCommand.java index c5ee2edc..8cafea10 100644 --- a/src/main/java/org/asamk/signal/commands/DaemonCommand.java +++ b/src/main/java/org/asamk/signal/commands/DaemonCommand.java @@ -4,77 +4,134 @@ 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.OutputType; +import org.asamk.signal.commands.exceptions.CommandException; +import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.dbus.DbusSignalImpl; import org.asamk.signal.manager.Manager; 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.ArrayList; +import java.util.List; +import java.util.Set; 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 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) { - DBusConnection conn = null; - try { + public Set getSupportedOutputTypes() { + return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON); + } + + @Override + public void handleCommand(final Namespace ns, final Manager m) throws CommandException { + var inJson = ns.get("output") == OutputType.JSON; + + 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)) { + var objectPath = DbusConfig.getObjectPath(); + var t = run(conn, objectPath, m, ignoreAttachments, inJson); + + conn.requestBusName(DbusConfig.getBusname()); + try { - DBusConnection.DBusBusType busType; - if (ns.getBoolean("system")) { - busType = DBusConnection.DBusBusType.SYSTEM; - } else { - busType = DBusConnection.DBusBusType.SESSION; - } - conn = DBusConnection.getConnection(busType); - conn.exportObject(SIGNAL_OBJECTPATH, new DbusSignalImpl(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; + t.join(); + } catch (InterruptedException ignored) { } - boolean ignoreAttachments = ns.getBoolean("ignore_attachments"); - 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; + } 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 managers) throws CommandException { + var inJson = ns.get("output") == OutputType.JSON; + + 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)) { + var receiveThreads = new ArrayList(); + for (var m : managers) { + var objectPath = DbusConfig.getObjectPath(m.getUsername()); + var thread = run(conn, objectPath, m, ignoreAttachments, inJson); + receiveThreads.add(thread); } - } finally { - if (conn != null) { - conn.disconnect(); + + conn.requestBusName(DbusConfig.getBusname()); + + for (var t : receiveThreads) { + try { + t.join(); + } catch (InterruptedException ignored) { + } } + } 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, boolean ignoreAttachments, boolean inJson + ) throws DBusException { + conn.exportObject(objectPath, new DbusSignalImpl(m)); + + final var thread = new Thread(() -> { + while (true) { + try { + m.receiveMessages(1, + TimeUnit.HOURS, + false, + ignoreAttachments, + inJson + ? new JsonDbusReceiveMessageHandler(m, conn, objectPath) + : new DbusReceiveMessageHandler(m, conn, objectPath)); + } catch (IOException e) { + logger.warn("Receiving messages failed, retrying", e); + } + } + }); + + logger.info("Exported dbus object: " + objectPath); + + thread.start(); + + return thread; + } }