-
- logger.trace("Checking account state");
- try {
- manager.checkAccountState();
- } catch (IOException e) {
- try {
- manager.close();
- } catch (IOException ie) {
- logger.warn("Failed to close broken account", ie);
- }
- throw new IOErrorException("Error while checking account " + account + ": " + e.getMessage(), e);
- }
-
- return manager;
- }
-
- 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)) {
- var accountObjectPath = account == null ? tryGetSingleAccountObjectPath(dBusConn) : null;
- if (accountObjectPath == null) {
- accountObjectPath = DbusConfig.getObjectPath(account);
- }
- var ts = dBusConn.getRemoteObject(DbusConfig.getBusname(), accountObjectPath, Signal.class);
-
- handleCommand(command, ts, 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;
- }
- }
-
- private void handleCommand(
- Command command, Signal ts, DBusConnection dBusConn, OutputWriter outputWriter
- ) throws CommandException {
- if (command instanceof LocalCommand localCommand) {
- try (final var m = new DbusManagerImpl(ts, dBusConn)) {
- localCommand.handleCommand(ns, m, outputWriter);
- } catch (UnsupportedOperationException e) {
- throw new UserErrorException("Command is not yet implemented via dbus", e);
- } catch (IOException | DBusExecutionException e) {
- throw new UnexpectedErrorException(e.getMessage(), e);
- }
- } else {
- throw new UserErrorException("Command is not yet implemented via dbus");
- }