Add MultiAccountManager

This commit is contained in:
AsamK 2021-11-11 13:29:32 +01:00
parent 6261934dda
commit 4a1af0786c
18 changed files with 221 additions and 131 deletions

View file

@ -19,6 +19,7 @@ import org.asamk.signal.dbus.DbusSignalControlImpl;
import org.asamk.signal.dbus.DbusSignalImpl;
import org.asamk.signal.jsonrpc.SignalJsonRpcDispatcherHandler;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.util.IOUtils;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
@ -141,7 +142,7 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
@Override
public void handleCommand(
final Namespace ns, final SignalCreator c, final OutputWriter outputWriter
final Namespace ns, final MultiAccountManager c, final OutputWriter outputWriter
) throws CommandException {
logger.info("Starting daemon in multi-account mode");
final var noReceiveStdOut = Boolean.TRUE.equals(ns.getBoolean("no-receive-stdout"));
@ -220,7 +221,7 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
}
private void runSocketMultiAccount(
final SignalCreator c, final ServerSocketChannel serverChannel, final boolean noReceiveOnStart
final MultiAccountManager c, final ServerSocketChannel serverChannel, final boolean noReceiveOnStart
) {
runSocket(serverChannel, channel -> {
final var handler = getSignalJsonRpcDispatcherHandler(channel, noReceiveOnStart);
@ -276,7 +277,7 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
}
private void runDbusMultiAccount(
final SignalCreator c, final boolean noReceiveOnStart, final boolean isDbusSystem
final MultiAccountManager c, final boolean noReceiveOnStart, final boolean isDbusSystem
) throws UnexpectedErrorException {
runDbus(isDbusSystem, (connection, objectPath) -> {
final var signalControl = new DbusSignalControlImpl(c, objectPath);

View file

@ -2,8 +2,9 @@ package org.asamk.signal.commands;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.MultiAccountManager;
public interface JsonRpcMultiCommand<T> extends JsonRpcCommand<T> {
void handleCommand(T request, SignalCreator c, JsonWriter jsonWriter) throws CommandException;
void handleCommand(T request, MultiAccountManager c, JsonWriter jsonWriter) throws CommandException;
}

View file

@ -7,6 +7,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.MultiAccountManager;
import java.util.List;
import java.util.Map;
@ -18,7 +19,7 @@ public interface JsonRpcMultiLocalCommand extends JsonRpcMultiCommand<Map<String
}
default void handleCommand(
Map<String, Object> request, SignalCreator c, JsonWriter jsonWriter
Map<String, Object> request, MultiAccountManager c, JsonWriter jsonWriter
) throws CommandException {
Namespace commandNamespace = new JsonRpcNamespace(request == null ? Map.of() : request);
handleCommand(commandNamespace, c, jsonWriter);

View file

@ -43,9 +43,8 @@ public class LinkCommand implements ProvisioningCommand {
}
try {
writer.println("{}", m.getDeviceLinkUri());
try (var manager = m.finishDeviceLink(deviceName)) {
writer.println("Associated with: {}", manager.getSelfNumber());
}
var number = m.finishDeviceLink(deviceName);
writer.println("Associated with: {}", number);
} catch (TimeoutException e) {
throw new UserErrorException("Link request timed out, please try again.");
} catch (IOException e) {

View file

@ -7,6 +7,7 @@ import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.MultiAccountManager;
import java.util.stream.Collectors;
@ -24,7 +25,7 @@ public class ListAccountsCommand implements JsonRpcMultiLocalCommand {
@Override
public void handleCommand(
final Namespace ns, final SignalCreator c, final OutputWriter outputWriter
final Namespace ns, final MultiAccountManager c, final OutputWriter outputWriter
) throws CommandException {
final var accountNumbers = c.getAccountNumbers();
if (outputWriter instanceof JsonWriter jsonWriter) {

View file

@ -4,8 +4,9 @@ import net.sourceforge.argparse4j.inf.Namespace;
import org.asamk.signal.OutputWriter;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.MultiAccountManager;
public interface MultiLocalCommand extends CliCommand {
void handleCommand(Namespace ns, SignalCreator c, OutputWriter outputWriter) throws CommandException;
void handleCommand(Namespace ns, MultiAccountManager c, OutputWriter outputWriter) throws CommandException;
}

View file

@ -1,24 +0,0 @@
package org.asamk.signal.commands;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.RegistrationManager;
import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;
public interface SignalCreator {
List<String> getAccountNumbers();
void addManager(Manager m);
void addOnManagerAddedHandler(Consumer<Manager> handler);
Manager getManager(String phoneNumber);
ProvisioningManager getNewProvisioningManager();
RegistrationManager getNewRegistrationManager(String username) throws IOException;
}

View file

@ -32,8 +32,7 @@ public class SubmitRateLimitChallengeCommand implements JsonRpcLocalCommand {
@Override
public void handleCommand(final Namespace ns, final Manager m, OutputWriter outputWriter) throws CommandException {
final var challenge = ns.getString("challenge");
final var captchaString = ns.getString("captcha");
final var captcha = captchaString == null ? null : captchaString.replace("signalcaptcha://", "");
final var captcha = ns.getString("captcha");
try {
m.submitRateLimitRecaptchaChallenge(challenge, captcha);

View file

@ -32,8 +32,7 @@ public class VerifyCommand implements RegistrationCommand {
var pin = ns.getString("pin");
try {
final var manager = m.verifyAccount(verificationCode, pin);
manager.close();
m.verifyAccount(verificationCode, pin);
} catch (PinLockedException e) {
throw new UserErrorException(
"Verification failed! This number is locked with a pin. Hours remaining until reset: "

View file

@ -4,6 +4,7 @@ import org.asamk.signal.BaseConfig;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.MultiAccountManager;
import java.util.Map;
@ -23,7 +24,7 @@ public class VersionCommand implements JsonRpcSingleCommand<Void>, JsonRpcMultiC
@Override
public void handleCommand(
final Void request, final SignalCreator c, final JsonWriter jsonWriter
final Void request, final MultiAccountManager c, final JsonWriter jsonWriter
) throws CommandException {
outputVersion(jsonWriter);
}