X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/d51dd7ae575222b0baea7265c18ebc79f4a7b001..ce4afbe4c269d74e4cc3da93744e0dc980e536d7:/src/main/java/org/asamk/signal/dbus/DbusCommandHandler.java diff --git a/src/main/java/org/asamk/signal/dbus/DbusCommandHandler.java b/src/main/java/org/asamk/signal/dbus/DbusCommandHandler.java index 6e7a6ee1..f8a65e7e 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusCommandHandler.java +++ b/src/main/java/org/asamk/signal/dbus/DbusCommandHandler.java @@ -13,12 +13,34 @@ import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UserErrorException; import org.freedesktop.dbus.connections.impl.DBusConnection; +import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder; +import org.freedesktop.dbus.errors.ServiceUnknown; import org.freedesktop.dbus.errors.UnknownMethod; import org.freedesktop.dbus.exceptions.DBusException; import org.freedesktop.dbus.exceptions.DBusExecutionException; +import java.io.IOException; + public class DbusCommandHandler { + public static void initDbusClient( + final Command command, final String account, final boolean systemBus, final CommandHandler commandHandler + ) throws CommandException { + try { + final var busType = systemBus ? DBusConnection.DBusBusType.SYSTEM : DBusConnection.DBusBusType.SESSION; + try (var dBusConn = DBusConnectionBuilder.forType(busType).build()) { + handleCommand(command, account, dBusConn, commandHandler); + } + } catch (ServiceUnknown e) { + throw new UserErrorException("signal-cli DBus daemon not running on " + + (systemBus ? "system" : "session") + + " bus: " + + e.getMessage(), e); + } catch (DBusExecutionException | DBusException | IOException e) { + throw new UnexpectedErrorException("Dbus client failed: " + e.getMessage(), e); + } + } + public static void handleCommand( final Command command, final String account,