mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 11:00:38 +00:00
Implement multi account commands for dbus client
This commit is contained in:
parent
00cda598c8
commit
805f976d9e
12 changed files with 335 additions and 29 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue