diff --git a/src/main/java/org/asamk/signal/App.java b/src/main/java/org/asamk/signal/App.java index 83c0ef5606..d9ffa6222b 100644 --- a/src/main/java/org/asamk/signal/App.java +++ b/src/main/java/org/asamk/signal/App.java @@ -16,7 +16,6 @@ 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.DbusCommandHandler; import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.RegistrationManager; import org.asamk.signal.manager.Settings; @@ -29,11 +28,6 @@ import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.PlainTextWriterImpl; import org.asamk.signal.util.IOUtils; -import org.freedesktop.dbus.connections.impl.DBusConnection; -import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder; -import org.freedesktop.dbus.errors.ServiceUnknown; -import org.freedesktop.dbus.exceptions.DBusException; -import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,6 +38,7 @@ import java.util.Set; import static net.sourceforge.argparse4j.DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS; +import static org.asamk.signal.dbus.DbusCommandHandler.initDbusClient; public class App { @@ -331,24 +326,6 @@ private Manager loadManager( } } - private 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()) { - DbusCommandHandler.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); - } - } - /** * @return the default data directory to be used by signal-cli. */ diff --git a/src/main/java/org/asamk/signal/commands/JoinGroupCommand.java b/src/main/java/org/asamk/signal/commands/JoinGroupCommand.java index 7a5b2601f3..f9e9f513bd 100644 --- a/src/main/java/org/asamk/signal/commands/JoinGroupCommand.java +++ b/src/main/java/org/asamk/signal/commands/JoinGroupCommand.java @@ -5,7 +5,6 @@ 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.manager.Manager; import org.asamk.signal.manager.api.GroupInviteLinkUrl; @@ -15,7 +14,6 @@ import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.PlainTextWriter; import org.asamk.signal.util.SendMessageResultUtils; -import org.freedesktop.dbus.exceptions.DBusExecutionException; import java.io.IOException; import java.util.Map; @@ -93,9 +91,6 @@ public void handleCommand( + " (" + e.getClass().getSimpleName() + ")", e); - } catch (DBusExecutionException e) { - throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass() - .getSimpleName() + ")", e); } catch (InactiveGroupLinkException e) { throw new UserErrorException("Group link is not valid: " + e.getMessage()); } catch (PendingAdminApprovalException e) { diff --git a/src/main/java/org/asamk/signal/dbus/DbusCommandHandler.java b/src/main/java/org/asamk/signal/dbus/DbusCommandHandler.java index 6e7a6ee1be..f8a65e7eef 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.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, diff --git a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java index cd6c67150b..ea09dba4a9 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java @@ -50,6 +50,7 @@ import org.freedesktop.dbus.DBusPath; import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.exceptions.DBusException; +import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.interfaces.DBusInterface; import org.freedesktop.dbus.interfaces.DBusSigHandler; import org.freedesktop.dbus.types.Variant; @@ -342,8 +343,13 @@ public SendGroupMessageResults updateGroup( @Override public Pair joinGroup(final GroupInviteLinkUrl inviteLinkUrl) throws IOException, InactiveGroupLinkException { - final var newGroupId = signal.joinGroup(inviteLinkUrl.getUrl()); - return new Pair<>(GroupId.unknownVersion(newGroupId), new SendGroupMessageResults(0, List.of())); + try { + final var newGroupId = signal.joinGroup(inviteLinkUrl.getUrl()); + return new Pair<>(GroupId.unknownVersion(newGroupId), new SendGroupMessageResults(0, List.of())); + } catch (DBusExecutionException e) { + throw new IOException("Failed to join group: " + e.getMessage() + " (" + e.getClass().getSimpleName() + ")", + e); + } } @Override