- private void initDbusClient(
- final Command command, final String account, final boolean systemBus, final OutputWriter outputWriter
- ) throws CommandException {
- try {
- DBusConnection.DBusBusType busType;
- if (systemBus) {
- busType = DBusConnection.DBusBusType.SYSTEM;
- } else {
- busType = DBusConnection.DBusBusType.SESSION;
- }
- try (var dBusConn = DBusConnection.getConnection(busType)) {
- if (command instanceof ProvisioningCommand c) {
- if (account != null) {
- throw new UserErrorException("You cannot specify a account (phone number) when linking");
- }
-
- handleProvisioningCommand(c, dBusConn, outputWriter);
- return;
- }
-
- if (account == null && command instanceof MultiLocalCommand c) {
- handleMultiLocalCommand(c, dBusConn, outputWriter);
- return;
- }
- if (account != null && command instanceof RegistrationCommand c) {
- handleRegistrationCommand(c, account, dBusConn, outputWriter);
- return;
- }
- if (!(command instanceof LocalCommand localCommand)) {
- throw new UserErrorException("Command only works in multi-account mode");
- }
-
- var accountObjectPath = account == null ? tryGetSingleAccountObjectPath(dBusConn) : null;
- if (accountObjectPath == null) {
- accountObjectPath = DbusConfig.getObjectPath(account);
- }
- handleLocalCommand(localCommand, accountObjectPath, dBusConn, outputWriter);
- }
- } 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);
- }
- }
-
- private String tryGetSingleAccountObjectPath(final DBusConnection dBusConn) throws DBusException, CommandException {
- var control = dBusConn.getRemoteObject(DbusConfig.getBusname(),
- DbusConfig.getObjectPath(),
- SignalControl.class);
- try {
- final var accounts = control.listAccounts();
- if (accounts.size() == 0) {
- throw new UserErrorException("No local users found, you first need to register or link an account");
- } else if (accounts.size() > 1) {
- throw new UserErrorException(
- "Multiple users found, you need to specify an account (phone number) with -a");
- }
-
- return accounts.get(0).getPath();
- } catch (UnknownMethod e) {
- // dbus daemon not running in multi-account mode
- return null;
- }
- }
-