Cleanup manager package

This commit is contained in:
AsamK 2022-02-06 17:21:39 +01:00
parent 3040da99c4
commit 5cccf52103
55 changed files with 190 additions and 160 deletions

View file

@ -22,10 +22,11 @@ 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;
import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.RegistrationManager;
import org.asamk.signal.manager.api.AccountCheckException;
import org.asamk.signal.manager.api.NotRegisteredException;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironment;
import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
@ -46,8 +47,6 @@ import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import static net.sourceforge.argparse4j.DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS;
@ -143,18 +142,18 @@ public class App {
return;
}
final File dataPath;
var config = ns.getString("config");
if (config != null) {
dataPath = new File(config);
} else {
dataPath = getDefaultDataPath();
}
if (!ServiceConfig.isSignalClientAvailable()) {
throw new UserErrorException("Missing required native library dependency: libsignal-client");
}
final File configPath;
var config = ns.getString("config");
if (config != null) {
configPath = new File(config);
} else {
configPath = getDefaultConfigPath();
}
final var serviceEnvironmentCli = ns.<ServiceEnvironmentCli>get("service-environment");
final var serviceEnvironment = serviceEnvironmentCli == ServiceEnvironmentCli.LIVE
? ServiceEnvironment.LIVE
@ -170,23 +169,21 @@ public class App {
throw new UserErrorException("You cannot specify a account (phone number) when linking");
}
handleProvisioningCommand(provisioningCommand, dataPath, serviceEnvironment, outputWriter);
handleProvisioningCommand(provisioningCommand, configPath, serviceEnvironment, outputWriter);
return;
}
if (account == null) {
var accounts = Manager.getAllLocalAccountNumbers(dataPath);
if (command instanceof MultiLocalCommand multiLocalCommand) {
handleMultiLocalCommand(multiLocalCommand,
dataPath,
configPath,
serviceEnvironment,
accounts,
outputWriter,
trustNewIdentity);
return;
}
var accounts = MultiAccountManager.getAllLocalAccountNumbers(configPath);
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) {
@ -200,7 +197,7 @@ public class App {
}
if (command instanceof RegistrationCommand registrationCommand) {
handleRegistrationCommand(registrationCommand, account, dataPath, serviceEnvironment);
handleRegistrationCommand(registrationCommand, account, configPath, serviceEnvironment);
return;
}
@ -210,7 +207,7 @@ public class App {
handleLocalCommand((LocalCommand) command,
account,
dataPath,
configPath,
serviceEnvironment,
outputWriter,
trustNewIdentity);
@ -218,11 +215,11 @@ public class App {
private void handleProvisioningCommand(
final ProvisioningCommand command,
final File dataPath,
final File configPath,
final ServiceEnvironment serviceEnvironment,
final OutputWriter outputWriter
) throws CommandException {
var pm = ProvisioningManager.init(dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
var pm = ProvisioningManager.init(configPath, serviceEnvironment, BaseConfig.USER_AGENT);
command.handleCommand(ns, pm, outputWriter);
}
@ -245,12 +242,12 @@ public class App {
private void handleRegistrationCommand(
final RegistrationCommand command,
final String account,
final File dataPath,
final File configPath,
final ServiceEnvironment serviceEnvironment
) throws CommandException {
final RegistrationManager manager;
try {
manager = RegistrationManager.init(account, dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
manager = RegistrationManager.init(account, configPath, serviceEnvironment, BaseConfig.USER_AGENT);
} catch (Throwable e) {
throw new UnexpectedErrorException("Error loading or creating state file: "
+ e.getMessage()
@ -283,12 +280,12 @@ public class App {
private void handleLocalCommand(
final LocalCommand command,
final String account,
final File dataPath,
final File configPath,
final ServiceEnvironment serviceEnvironment,
final OutputWriter outputWriter,
final TrustNewIdentity trustNewIdentity
) throws CommandException {
try (var m = loadManager(account, dataPath, serviceEnvironment, trustNewIdentity)) {
try (var m = loadManager(account, configPath, serviceEnvironment, trustNewIdentity)) {
command.handleCommand(ns, m, outputWriter);
} catch (IOException e) {
logger.warn("Cleanup failed", e);
@ -313,25 +310,15 @@ public class App {
private void handleMultiLocalCommand(
final MultiLocalCommand command,
final File dataPath,
final File configPath,
final ServiceEnvironment serviceEnvironment,
final List<String> accounts,
final OutputWriter outputWriter,
final TrustNewIdentity trustNewIdentity
) throws CommandException {
final var managers = new ArrayList<Manager>();
for (String a : accounts) {
try {
managers.add(loadManager(a, dataPath, serviceEnvironment, trustNewIdentity));
} catch (CommandException e) {
logger.warn("Ignoring {}: {}", a, e.getMessage());
}
}
try (var multiAccountManager = new MultiAccountManagerImpl(managers,
dataPath,
try (var multiAccountManager = MultiAccountManager.init(configPath,
serviceEnvironment,
BaseConfig.USER_AGENT)) {
BaseConfig.USER_AGENT,
trustNewIdentity)) {
command.handleCommand(ns, multiAccountManager, outputWriter);
}
}
@ -351,16 +338,22 @@ public class App {
private Manager loadManager(
final String account,
final File dataPath,
final File configPath,
final ServiceEnvironment serviceEnvironment,
final TrustNewIdentity trustNewIdentity
) throws CommandException {
Manager manager;
logger.trace("Loading account file for {}", account);
try {
manager = Manager.init(account, dataPath, serviceEnvironment, BaseConfig.USER_AGENT, trustNewIdentity);
return Manager.init(account, configPath, serviceEnvironment, BaseConfig.USER_AGENT, trustNewIdentity);
} catch (NotRegisteredException e) {
throw new UserErrorException("User " + account + " is not registered.");
} catch (AccountCheckException ace) {
if (ace.getCause() instanceof IOException e) {
throw new IOErrorException("Error while checking account " + account + ": " + e.getMessage(), e);
} else {
throw new UnexpectedErrorException("Error while checking account " + account + ": " + ace.getMessage(),
ace);
}
} catch (Throwable e) {
throw new UnexpectedErrorException("Error loading state file for user "
+ account
@ -370,20 +363,6 @@ public class App {
+ e.getClass().getSimpleName()
+ ")", e);
}
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(
@ -457,7 +436,7 @@ public class App {
/**
* @return the default data directory to be used by signal-cli.
*/
private static File getDefaultDataPath() {
private static File getDefaultConfigPath() {
return new File(IOUtils.getDataHomeDir(), "signal-cli");
}
}

View file

@ -1,7 +1,7 @@
package org.asamk.signal;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.UntrustedIdentityException;
import org.asamk.signal.manager.api.UntrustedIdentityException;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.groups.GroupId;

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.output.OutputWriter;

View file

@ -6,7 +6,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.manager.UserAlreadyExists;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.asamk.signal.output.JsonWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -56,7 +56,7 @@ public class FinishLinkCommand implements JsonRpcMultiCommand<FinishLinkCommand.
throw new UserErrorException("Link request timed out, please try again.");
} catch (IOException e) {
throw new IOErrorException("Link request error: " + e.getMessage(), e);
} catch (UserAlreadyExists e) {
} catch (UserAlreadyExistsException e) {
throw new UserErrorException("The user "
+ e.getNumber()
+ " already exists\nDelete \""

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.UserAlreadyExists;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.slf4j.Logger;
@ -49,7 +49,7 @@ public class LinkCommand implements ProvisioningCommand {
throw new UserErrorException("Link request timed out, please try again.");
} catch (IOException e) {
throw new IOErrorException("Link request error: " + e.getMessage(), e);
} catch (UserAlreadyExists e) {
} catch (UserAlreadyExistsException e) {
throw new UserErrorException("The user "
+ e.getNumber()
+ " already exists\nDelete \""

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.output.OutputWriter;
import java.io.IOException;

View file

@ -7,8 +7,8 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.RecipientIdentifier;

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.output.OutputWriter;
import java.io.IOException;

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.output.OutputWriter;

View file

@ -7,8 +7,8 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.Configuration;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.output.OutputWriter;
import java.io.IOException;

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.util.CommandUtil;

View file

@ -7,8 +7,8 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.SendGroupMessageResults;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.api.UpdateGroup;

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;

View file

@ -2,10 +2,9 @@ package org.asamk.signal.dbus;
import org.asamk.Signal;
import org.asamk.signal.DbusConfig;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.Configuration;
import org.asamk.signal.manager.api.Device;
import org.asamk.signal.manager.api.Group;
@ -14,6 +13,7 @@ import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendGroupMessageResults;
@ -84,11 +84,6 @@ public class DbusManagerImpl implements Manager {
return signal.getSelfNumber();
}
@Override
public void checkAccountState() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public Map<String, Pair<String, UUID>> areUsersRegistered(final Set<String> numbers) throws IOException {
final var numbersList = new ArrayList<>(numbers);

View file

@ -2,7 +2,7 @@ package org.asamk.signal.dbus;
import org.asamk.SignalControl;
import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.UserAlreadyExists;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import java.io.IOException;
@ -27,9 +27,7 @@ public class DbusProvisioningManagerImpl implements ProvisioningManager {
}
public DbusProvisioningManagerImpl(
final SignalControl signalControl,
DBusConnection connection,
URI deviceLinkUri
final SignalControl signalControl, DBusConnection connection, URI deviceLinkUri
) {
this.signalControl = signalControl;
this.connection = connection;
@ -47,7 +45,7 @@ public class DbusProvisioningManagerImpl implements ProvisioningManager {
}
@Override
public String finishDeviceLink(final String deviceName) throws IOException, TimeoutException, UserAlreadyExists {
public String finishDeviceLink(final String deviceName) throws IOException, TimeoutException, UserAlreadyExistsException {
return signalControl.finishLink(deviceLinkUri.toString(), deviceName);
}
}

View file

@ -7,7 +7,7 @@ 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.asamk.signal.manager.UserAlreadyExists;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.asamk.signal.manager.api.CaptchaRequiredException;
import org.asamk.signal.manager.api.IncorrectPinException;
import org.asamk.signal.manager.api.PinLockedException;
@ -100,7 +100,7 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
final ProvisioningManager provisioningManager = c.getProvisioningManagerFor(deviceLinkUri);
try {
provisioningManager.finishDeviceLink(newDeviceName);
} catch (IOException | TimeoutException | UserAlreadyExists e) {
} catch (IOException | TimeoutException | UserAlreadyExistsException e) {
e.printStackTrace();
}
});
@ -127,7 +127,7 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
try {
final var provisioningManager = c.getProvisioningManagerFor(new URI(deviceLinkUri));
return provisioningManager.finishDeviceLink(newDeviceName);
} catch (TimeoutException | IOException | UserAlreadyExists | URISyntaxException e) {
} catch (TimeoutException | IOException | UserAlreadyExistsException | URISyntaxException e) {
throw new SignalControl.Error.Failure(e.getClass().getSimpleName() + " " + e.getMessage());
}
}

View file

@ -2,16 +2,16 @@ package org.asamk.signal.dbus;
import org.asamk.Signal;
import org.asamk.signal.BaseConfig;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.Identity;
import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
import org.asamk.signal.manager.api.InvalidNumberException;
import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendMessageResult;

View file

@ -3,7 +3,7 @@ package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.UntrustedIdentityException;
import org.asamk.signal.manager.api.UntrustedIdentityException;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;