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

@ -371,6 +371,10 @@
"allDeclaredMethods":true,
"allDeclaredClasses":true}
,
{
"name":"org.asamk.SignalControl$Error$Failure",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]}
,
{
"name":"org.asamk.signal.commands.FinishLinkCommand$FinishLinkParams",
"allDeclaredFields":true,

View file

@ -118,6 +118,9 @@
{
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MY\\E"
},
{
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_NG\\E"
},
{
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_NL\\E"
},

View file

@ -20,8 +20,6 @@ public interface MultiAccountManager extends AutoCloseable {
ProvisioningManager getProvisioningManagerFor(URI deviceLinkUri);
ProvisioningManager getNewProvisioningManager();
RegistrationManager getNewRegistrationManager(String account) throws IOException;
@Override

View file

@ -111,8 +111,7 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
return provisioningManagers.remove(deviceLinkUri);
}
@Override
public ProvisioningManager getNewProvisioningManager() {
private ProvisioningManager getNewProvisioningManager() {
return ProvisioningManager.init(dataPath, serviceEnvironment, userAgent, this::addManager);
}

View file

@ -243,7 +243,7 @@ public class RegistrationManagerImpl implements RegistrationManager {
}
@Override
public void close() throws IOException {
public void close() {
if (account != null) {
account.close();
account = null;

View file

@ -26,10 +26,16 @@ public interface SignalControl extends DBusInterface {
String link(String newDeviceName) throws Error.Failure;
String startLink() throws Error.Failure;
String finishLink(String deviceLinkUri, String newDeviceName) throws Error.Failure;
String version();
List<DBusPath> listAccounts();
DBusPath getAccount(String number);
interface Error {
class Failure extends DBusExecutionException {

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.
*/

View file

@ -614,7 +614,7 @@ public class DbusManagerImpl implements Manager {
}
@Override
public void close() throws IOException {
public void close() {
synchronized (this) {
this.notify();
}
@ -665,9 +665,9 @@ public class DbusManagerImpl implements Manager {
return string == null ? "" : string;
}
private <T extends DBusInterface> T getRemoteObject(final DBusPath devicePath, final Class<T> type) {
private <T extends DBusInterface> T getRemoteObject(final DBusPath path, final Class<T> type) {
try {
return connection.getRemoteObject(DbusConfig.getBusname(), devicePath.getPath(), type);
return connection.getRemoteObject(DbusConfig.getBusname(), path.getPath(), type);
} catch (DBusException e) {
throw new AssertionError(e);
}

View file

@ -0,0 +1,98 @@
package org.asamk.signal.dbus;
import org.asamk.Signal;
import org.asamk.SignalControl;
import org.asamk.signal.DbusConfig;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.RegistrationManager;
import org.freedesktop.dbus.DBusPath;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.interfaces.DBusInterface;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
/**
* This class implements the MultiAccountManager interface using the DBus Signal interface, where possible.
* It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
*/
public class DbusMultiAccountManagerImpl implements MultiAccountManager {
private final SignalControl signalControl;
private final DBusConnection connection;
// TODO add listeners for added/removed accounts
private final Set<Consumer<Manager>> onManagerAddedHandlers = new HashSet<>();
private final Set<Consumer<Manager>> onManagerRemovedHandlers = new HashSet<>();
public DbusMultiAccountManagerImpl(final SignalControl signalControl, DBusConnection connection) {
this.signalControl = signalControl;
this.connection = connection;
}
@Override
public List<String> getAccountNumbers() {
return signalControl.listAccounts()
.stream()
.map(a -> getRemoteObject(a, Signal.class).getSelfNumber())
.toList();
}
@Override
public void addOnManagerAddedHandler(final Consumer<Manager> handler) {
synchronized (onManagerAddedHandlers) {
onManagerAddedHandlers.add(handler);
}
}
@Override
public void addOnManagerRemovedHandler(final Consumer<Manager> handler) {
synchronized (onManagerRemovedHandlers) {
onManagerRemovedHandlers.add(handler);
}
}
@Override
public Manager getManager(final String phoneNumber) {
return new DbusManagerImpl(getRemoteObject(signalControl.getAccount(phoneNumber), Signal.class), connection);
}
@Override
public URI getNewProvisioningDeviceLinkUri() throws TimeoutException, IOException {
try {
return new URI(signalControl.startLink());
} catch (URISyntaxException e) {
throw new IOException(e);
}
}
@Override
public ProvisioningManager getProvisioningManagerFor(final URI deviceLinkUri) {
return new DbusProvisioningManagerImpl(signalControl, connection, deviceLinkUri);
}
@Override
public RegistrationManager getNewRegistrationManager(final String account) throws IOException {
return new DbusRegistrationManagerImpl(account, signalControl, connection);
}
@Override
public void close() {
}
private <T extends DBusInterface> T getRemoteObject(final DBusPath path, final Class<T> type) {
try {
return connection.getRemoteObject(DbusConfig.getBusname(), path.getPath(), type);
} catch (DBusException e) {
throw new AssertionError(e);
}
}
}

View file

@ -0,0 +1,53 @@
package org.asamk.signal.dbus;
import org.asamk.SignalControl;
import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.UserAlreadyExists;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeoutException;
/**
* This class implements the ProvisioningManager interface using the DBus Signal interface, where possible.
* It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
*/
public class DbusProvisioningManagerImpl implements ProvisioningManager {
private final SignalControl signalControl;
private final DBusConnection connection;
private URI deviceLinkUri;
public DbusProvisioningManagerImpl(final SignalControl signalControl, DBusConnection connection) {
this.signalControl = signalControl;
this.connection = connection;
}
public DbusProvisioningManagerImpl(
final SignalControl signalControl,
DBusConnection connection,
URI deviceLinkUri
) {
this.signalControl = signalControl;
this.connection = connection;
this.deviceLinkUri = deviceLinkUri;
}
@Override
public URI getDeviceLinkUri() throws TimeoutException, IOException {
try {
deviceLinkUri = new URI(signalControl.startLink());
return deviceLinkUri;
} catch (URISyntaxException e) {
throw new IOException(e);
}
}
@Override
public String finishDeviceLink(final String deviceName) throws IOException, TimeoutException, UserAlreadyExists {
return signalControl.finishLink(deviceLinkUri.toString(), deviceName);
}
}

View file

@ -0,0 +1,53 @@
package org.asamk.signal.dbus;
import org.asamk.SignalControl;
import org.asamk.signal.manager.RegistrationManager;
import org.asamk.signal.manager.api.CaptchaRequiredException;
import org.asamk.signal.manager.api.IncorrectPinException;
import org.asamk.signal.manager.api.PinLockedException;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import java.io.IOException;
/**
* This class implements the RegistrationManager interface using the DBus Signal interface, where possible.
* It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
*/
public class DbusRegistrationManagerImpl implements RegistrationManager {
private final String number;
private final SignalControl signalControl;
private final DBusConnection connection;
public DbusRegistrationManagerImpl(String number, final SignalControl signalControl, DBusConnection connection) {
this.number = number;
this.signalControl = signalControl;
this.connection = connection;
}
@Override
public void register(
final boolean voiceVerification, final String captcha
) throws IOException, CaptchaRequiredException {
if (captcha == null) {
signalControl.register(number, voiceVerification);
} else {
signalControl.registerWithCaptcha(number, voiceVerification, captcha);
}
}
@Override
public void verifyAccount(
final String verificationCode, final String pin
) throws IOException, PinLockedException, IncorrectPinException {
if (pin == null) {
signalControl.verify(number, verificationCode);
} else {
signalControl.verifyWithPin(number, verificationCode, pin);
}
}
@Override
public void close() {
}
}

View file

@ -15,6 +15,7 @@ import org.freedesktop.dbus.DBusPath;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.OverlappingFileLockException;
import java.util.List;
import java.util.concurrent.TimeoutException;
@ -88,9 +89,9 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
@Override
public String link(final String newDeviceName) throws Error.Failure {
try {
final ProvisioningManager provisioningManager = c.getNewProvisioningManager();
final URI deviceLinkUri = provisioningManager.getDeviceLinkUri();
final URI deviceLinkUri = c.getNewProvisioningDeviceLinkUri();
new Thread(() -> {
final ProvisioningManager provisioningManager = c.getProvisioningManagerFor(deviceLinkUri);
try {
provisioningManager.finishDeviceLink(newDeviceName);
} catch (IOException | TimeoutException | UserAlreadyExists e) {
@ -103,6 +104,26 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
}
}
@Override
public String startLink() throws Error.Failure {
try {
final URI deviceLinkUri = c.getNewProvisioningDeviceLinkUri();
return deviceLinkUri.toString();
} catch (TimeoutException | IOException e) {
throw new SignalControl.Error.Failure(e.getClass().getSimpleName() + " " + e.getMessage());
}
}
@Override
public String finishLink(String deviceLinkUri, final String newDeviceName) throws Error.Failure {
try {
final var provisioningManager = c.getProvisioningManagerFor(new URI(deviceLinkUri));
return provisioningManager.finishDeviceLink(newDeviceName);
} catch (TimeoutException | IOException | UserAlreadyExists | URISyntaxException e) {
throw new SignalControl.Error.Failure(e.getClass().getSimpleName() + " " + e.getMessage());
}
}
@Override
public String version() {
return BaseConfig.PROJECT_VERSION;
@ -112,4 +133,9 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
public List<DBusPath> listAccounts() {
return c.getAccountNumbers().stream().map(u -> new DBusPath(DbusConfig.getObjectPath(u))).toList();
}
@Override
public DBusPath getAccount(final String number) {
return new DBusPath(DbusConfig.getObjectPath(number));
}
}