X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/3f582e9c2e7db9adeb48dbc569f3c32509a63ad5..1ad0e94b640d16a8d832287362e1785c78d3ec49:/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 7c972496..bd6b9386 100644 --- a/src/main/java/org/asamk/signal/commands/DaemonCommand.java +++ b/src/main/java/org/asamk/signal/commands/DaemonCommand.java @@ -10,18 +10,22 @@ import org.asamk.signal.ReceiveMessageHandler; 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.DbusSignalControlImpl; import org.asamk.signal.dbus.DbusSignalImpl; +import org.asamk.signal.http.HttpServerHandler; import org.asamk.signal.json.JsonReceiveMessageHandler; import org.asamk.signal.jsonrpc.SignalJsonRpcDispatcherHandler; import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.MultiAccountManager; +import org.asamk.signal.manager.api.ReceiveConfig; import org.asamk.signal.output.JsonWriter; import org.asamk.signal.output.JsonWriterImpl; import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.PlainTextWriter; import org.asamk.signal.util.IOUtils; import org.freedesktop.dbus.connections.impl.DBusConnection; +import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder; import org.freedesktop.dbus.exceptions.DBusException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,6 +70,10 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand { .nargs("?") .setConst("localhost:7583") .help("Expose a JSON-RPC interface on a TCP socket (default localhost:7583)."); + subparser.addArgument("--http") + .nargs("?") + .setConst("localhost:8080") + .help("Expose a JSON-RPC interface as http endpoint."); subparser.addArgument("--no-receive-stdout") .help("Don’t print received messages to stdout.") .action(Arguments.storeTrue()); @@ -76,6 +84,12 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand { subparser.addArgument("--ignore-attachments") .help("Don’t download attachments of received messages.") .action(Arguments.storeTrue()); + subparser.addArgument("--ignore-stories") + .help("Don’t receive story messages from the server.") + .action(Arguments.storeTrue()); + subparser.addArgument("--send-read-receipts") + .help("Send read receipts for all incoming data messages (in addition to the default delivery receipts)") + .action(Arguments.storeTrue()); } @Override @@ -91,8 +105,10 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand { final var noReceiveStdOut = Boolean.TRUE.equals(ns.getBoolean("no-receive-stdout")); final var receiveMode = ns.get("receive-mode"); final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); + final var ignoreStories = Boolean.TRUE.equals(ns.getBoolean("ignore-stories")); + final var sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts")); - m.setIgnoreAttachments(ignoreAttachments); + m.setReceiveConfig(new ReceiveConfig(ignoreAttachments, ignoreStories, sendReadReceipts)); addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START); final Channel inheritedChannel; @@ -117,6 +133,16 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand { final var serverChannel = IOUtils.bindSocket(address); runSocketSingleAccount(m, serverChannel, receiveMode == ReceiveMode.MANUAL); } + final var httpAddress = ns.getString("http"); + if (httpAddress != null) { + final var address = IOUtils.parseInetSocketAddress(httpAddress); + final var handler = new HttpServerHandler(address, m); + try { + handler.init(); + } catch (IOException ex) { + throw new IOErrorException("Failed to initialize HTTP Server", ex); + } + } final var isDbusSystem = Boolean.TRUE.equals(ns.getBoolean("dbus-system")); if (isDbusSystem) { runDbusSingleAccount(m, true, receiveMode != ReceiveMode.ON_START); @@ -153,13 +179,16 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand { final var noReceiveStdOut = Boolean.TRUE.equals(ns.getBoolean("no-receive-stdout")); final var receiveMode = ns.get("receive-mode"); final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); + final var ignoreStories = Boolean.TRUE.equals(ns.getBoolean("ignore-stories")); + final var sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts")); + final var receiveConfig = new ReceiveConfig(ignoreAttachments, ignoreStories, sendReadReceipts); c.getManagers().forEach(m -> { - m.setIgnoreAttachments(ignoreAttachments); + m.setReceiveConfig(receiveConfig); addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START); }); c.addOnManagerAddedHandler(m -> { - m.setIgnoreAttachments(ignoreAttachments); + m.setReceiveConfig(receiveConfig); addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START); }); @@ -185,6 +214,16 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand { final var serverChannel = IOUtils.bindSocket(address); runSocketMultiAccount(c, serverChannel, receiveMode == ReceiveMode.MANUAL); } + final var httpAddress = ns.getString("http"); + if (httpAddress != null) { + final var address = IOUtils.parseInetSocketAddress(httpAddress); + final var handler = new HttpServerHandler(address, c); + try { + handler.init(); + } catch (IOException ex) { + throw new IOErrorException("Failed to initialize HTTP Server", ex); + } + } final var isDbusSystem = Boolean.TRUE.equals(ns.getBoolean("dbus-system")); if (isDbusSystem) { runDbusMultiAccount(c, receiveMode != ReceiveMode.ON_START, true); @@ -282,7 +321,7 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand { private void runDbusSingleAccount( final Manager m, final boolean isDbusSystem, final boolean noReceiveOnStart - ) throws UnexpectedErrorException { + ) throws CommandException { runDbus(isDbusSystem, (conn, objectPath) -> { try { exportDbusObject(conn, objectPath, m, noReceiveOnStart).join(); @@ -293,7 +332,7 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand { private void runDbusMultiAccount( final MultiAccountManager c, final boolean noReceiveOnStart, final boolean isDbusSystem - ) throws UnexpectedErrorException { + ) throws CommandException { runDbus(isDbusSystem, (connection, objectPath) -> { final var signalControl = new DbusSignalControlImpl(c, objectPath); connection.exportObject(signalControl); @@ -332,7 +371,7 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand { private void runDbus( final boolean isDbusSystem, DbusRunner dbusRunner - ) throws UnexpectedErrorException { + ) throws CommandException { DBusConnection.DBusBusType busType; if (isDbusSystem) { busType = DBusConnection.DBusBusType.SYSTEM; @@ -341,10 +380,12 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand { } DBusConnection conn; try { - conn = DBusConnection.getConnection(busType); + conn = DBusConnectionBuilder.forType(busType).build(); dbusRunner.run(conn, DbusConfig.getObjectPath()); } catch (DBusException e) { throw new UnexpectedErrorException("Dbus command failed: " + e.getMessage(), e); + } catch (UnsupportedOperationException e) { + throw new UserErrorException("Failed to connect to Dbus: " + e.getMessage(), e); } try {