Implement multi account commands for dbus client

This commit is contained in:
AsamK 2021-12-31 18:17:32 +01:00
parent 00cda598c8
commit 805f976d9e
12 changed files with 335 additions and 29 deletions

View file

@ -18,6 +18,9 @@ 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.DbusManagerImpl;
import org.asamk.signal.dbus.DbusMultiAccountManagerImpl;
import org.asamk.signal.dbus.DbusProvisioningManagerImpl;
import org.asamk.signal.dbus.DbusRegistrationManagerImpl;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.MultiAccountManagerImpl;
import org.asamk.signal.manager.NotRegisteredException;
@ -220,6 +223,22 @@ public class App {
command.handleCommand(ns, pm, outputWriter);
}
private void handleProvisioningCommand(
final ProvisioningCommand c, final DBusConnection dBusConn, final OutputWriter outputWriter
) throws CommandException, DBusException {
final var signalControl = dBusConn.getRemoteObject(DbusConfig.getBusname(),
DbusConfig.getObjectPath(),
SignalControl.class);
final var provisioningManager = new DbusProvisioningManagerImpl(signalControl, dBusConn);
try {
c.handleCommand(ns, provisioningManager, outputWriter);
} catch (UnsupportedOperationException e) {
throw new UserErrorException("Command is not yet implemented via dbus", e);
} catch (DBusExecutionException e) {
throw new UnexpectedErrorException(e.getMessage(), e);
}
}
private void handleRegistrationCommand(
final RegistrationCommand command,
final String account,
@ -243,6 +262,21 @@ public class App {
}
}
private void handleRegistrationCommand(
final RegistrationCommand c, String account, final DBusConnection dBusConn, final OutputWriter outputWriter
) throws CommandException, DBusException {
final var signalControl = dBusConn.getRemoteObject(DbusConfig.getBusname(),
DbusConfig.getObjectPath(),
SignalControl.class);
try (final var registrationManager = new DbusRegistrationManagerImpl(account, signalControl, dBusConn)) {
c.handleCommand(ns, registrationManager);
} catch (UnsupportedOperationException e) {
throw new UserErrorException("Command is not yet implemented via dbus", e);
} catch (DBusExecutionException e) {
throw new UnexpectedErrorException(e.getMessage(), e);
}
}
private void handleLocalCommand(
final LocalCommand command,
final String account,
@ -258,6 +292,22 @@ public class App {
}
}
private void handleLocalCommand(
final LocalCommand c,
String accountObjectPath,
final DBusConnection dBusConn,
final OutputWriter outputWriter
) throws CommandException, DBusException {
var signal = dBusConn.getRemoteObject(DbusConfig.getBusname(), accountObjectPath, Signal.class);
try (final var m = new DbusManagerImpl(signal, dBusConn)) {
c.handleCommand(ns, m, outputWriter);
} catch (UnsupportedOperationException e) {
throw new UserErrorException("Command is not yet implemented via dbus", e);
} catch (DBusExecutionException e) {
throw new UnexpectedErrorException(e.getMessage(), e);
}
}
private void handleMultiLocalCommand(
final MultiLocalCommand command,
final File dataPath,
@ -283,6 +333,19 @@ public class App {
}
}
private void handleMultiLocalCommand(
final MultiLocalCommand c, final DBusConnection dBusConn, final OutputWriter outputWriter
) throws CommandException, DBusException {
final var signalControl = dBusConn.getRemoteObject(DbusConfig.getBusname(),
DbusConfig.getObjectPath(),
SignalControl.class);
try (final var multiAccountManager = new DbusMultiAccountManagerImpl(signalControl, dBusConn)) {
c.handleCommand(ns, multiAccountManager, outputWriter);
} catch (UnsupportedOperationException e) {
throw new UserErrorException("Command is not yet implemented via dbus", e);
}
}
private Manager loadManager(
final String account,
final File dataPath,
@ -331,13 +394,32 @@ public class App {
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);
}
var ts = dBusConn.getRemoteObject(DbusConfig.getBusname(), accountObjectPath, Signal.class);
handleCommand(command, ts, dBusConn, outputWriter);
handleLocalCommand(localCommand, accountObjectPath, dBusConn, outputWriter);
}
} catch (ServiceUnknown e) {
throw new UserErrorException("signal-cli DBus daemon not running on "
@ -369,22 +451,6 @@ public class App {
}
}
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");
}
}
/**
* @return the default data directory to be used by signal-cli.
*/