Skip to content

Commit

Permalink
Move all dbus interaction to dbus package
Browse files Browse the repository at this point in the history
  • Loading branch information
AsamK committed Nov 10, 2023
1 parent 7e9940b commit 9741c93
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
25 changes: 1 addition & 24 deletions src/main/java/org/asamk/signal/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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 {

Expand Down Expand Up @@ -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.
*/
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/org/asamk/signal/commands/JoinGroupCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/asamk/signal/dbus/DbusCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -342,8 +343,13 @@ public SendGroupMessageResults updateGroup(

@Override
public Pair<GroupId, SendGroupMessageResults> 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
Expand Down

0 comments on commit 9741c93

Please sign in to comment.