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

@ -1,5 +1,7 @@
package org.asamk.signal.manager;
import org.asamk.signal.manager.api.AccountCheckException;
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;
@ -9,11 +11,14 @@ import org.asamk.signal.manager.api.InvalidDeviceLinkException;
import org.asamk.signal.manager.api.InvalidStickerException;
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.NotRegisteredException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendGroupMessageResults;
import org.asamk.signal.manager.api.SendMessageResults;
import org.asamk.signal.manager.api.StickerPack;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
@ -38,7 +43,6 @@ import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -53,7 +57,7 @@ public interface Manager extends Closeable {
ServiceEnvironment serviceEnvironment,
String userAgent,
TrustNewIdentity trustNewIdentity
) throws IOException, NotRegisteredException {
) throws IOException, NotRegisteredException, AccountCheckException {
var pathConfig = PathConfig.createDefault(settingsPath);
if (!SignalAccount.userExists(pathConfig.dataPath(), number)) {
@ -70,7 +74,16 @@ public interface Manager extends Closeable {
account.initDatabase();
final var serviceEnvironmentConfig = ServiceConfig.getServiceEnvironmentConfig(serviceEnvironment, userAgent);
return new ManagerImpl(account, pathConfig, serviceEnvironmentConfig, userAgent);
final var manager = new ManagerImpl(account, pathConfig, serviceEnvironmentConfig, userAgent);
try {
manager.checkAccountState();
} catch (IOException e) {
manager.close();
throw new AccountCheckException("Error while checking account " + account + ": " + e.getMessage(), e);
}
return manager;
}
static void initLogger() {
@ -81,26 +94,8 @@ public interface Manager extends Closeable {
return PhoneNumberFormatter.isValidNumber(e164Number, countryCode);
}
static List<String> getAllLocalAccountNumbers(File settingsPath) {
var pathConfig = PathConfig.createDefault(settingsPath);
final var dataPath = pathConfig.dataPath();
final var files = dataPath.listFiles();
if (files == null) {
return List.of();
}
return Arrays.stream(files)
.filter(File::isFile)
.map(File::getName)
.filter(file -> PhoneNumberFormatter.isValidNumber(file, null))
.toList();
}
String getSelfNumber();
void checkAccountState() throws IOException;
/**
* This is used for checking a set of phone numbers for registration on Signal
*

View file

@ -16,6 +16,7 @@
*/
package org.asamk.signal.manager;
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;
@ -24,6 +25,7 @@ import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
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.SendGroupMessageResults;
@ -31,6 +33,7 @@ import org.asamk.signal.manager.api.SendMessageResult;
import org.asamk.signal.manager.api.SendMessageResults;
import org.asamk.signal.manager.api.StickerPack;
import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
@ -50,6 +53,8 @@ import org.asamk.signal.manager.storage.recipients.Contact;
import org.asamk.signal.manager.storage.recipients.Profile;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.asamk.signal.manager.storage.stickerPacks.JsonStickerPack;
import org.asamk.signal.manager.storage.stickerPacks.StickerPackStore;
import org.asamk.signal.manager.storage.stickers.Sticker;
import org.asamk.signal.manager.util.AttachmentUtils;
import org.asamk.signal.manager.util.KeyUtils;
@ -89,7 +94,7 @@ import java.util.stream.Stream;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
public class ManagerImpl implements Manager {
class ManagerImpl implements Manager {
private final static Logger logger = LoggerFactory.getLogger(ManagerImpl.class);
@ -166,8 +171,7 @@ public class ManagerImpl implements Manager {
return account.getAccount();
}
@Override
public void checkAccountState() throws IOException {
void checkAccountState() throws IOException {
context.getAccountHelper().checkAccountState();
}

View file

@ -1,13 +1,58 @@
package org.asamk.signal.manager;
import org.asamk.signal.manager.api.AccountCheckException;
import org.asamk.signal.manager.api.NotRegisteredException;
import org.asamk.signal.manager.config.ServiceEnvironment;
import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
public interface MultiAccountManager extends AutoCloseable {
static MultiAccountManager init(
final File settingsPath,
final ServiceEnvironment serviceEnvironment,
final String userAgent,
final TrustNewIdentity trustNewIdentity
) {
final var logger = LoggerFactory.getLogger(MultiAccountManager.class);
final var managers = getAllLocalAccountNumbers(settingsPath).stream().map(a -> {
try {
return Manager.init(a, settingsPath, serviceEnvironment, userAgent, trustNewIdentity);
} catch (NotRegisteredException | IOException | AccountCheckException e) {
logger.warn("Ignoring {}: {} ({})", a, e.getMessage(), e.getClass().getSimpleName());
return null;
}
}).filter(Objects::nonNull).toList();
return new MultiAccountManagerImpl(managers, settingsPath, serviceEnvironment, userAgent);
}
static List<String> getAllLocalAccountNumbers(File settingsPath) {
var pathConfig = PathConfig.createDefault(settingsPath);
final var dataPath = pathConfig.dataPath();
final var files = dataPath.listFiles();
if (files == null) {
return List.of();
}
return Arrays.stream(files)
.filter(File::isFile)
.map(File::getName)
.filter(file -> PhoneNumberFormatter.isValidNumber(file, null))
.toList();
}
List<String> getAccountNumbers();
List<Manager> getManagers();

View file

@ -17,7 +17,7 @@ import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
public class MultiAccountManagerImpl implements MultiAccountManager {
class MultiAccountManagerImpl implements MultiAccountManager {
private final static Logger logger = LoggerFactory.getLogger(MultiAccountManagerImpl.class);
@ -25,19 +25,19 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
private final Set<Consumer<Manager>> onManagerRemovedHandlers = new HashSet<>();
private final Set<Manager> managers = new HashSet<>();
private final Map<URI, ProvisioningManager> provisioningManagers = new HashMap<>();
private final File dataPath;
private final File settingsPath;
private final ServiceEnvironment serviceEnvironment;
private final String userAgent;
public MultiAccountManagerImpl(
final Collection<Manager> managers,
final File dataPath,
final File settingsPath,
final ServiceEnvironment serviceEnvironment,
final String userAgent
) {
this.managers.addAll(managers);
managers.forEach(m -> m.addClosedListener(() -> this.removeManager(m)));
this.dataPath = dataPath;
this.settingsPath = settingsPath;
this.serviceEnvironment = serviceEnvironment;
this.userAgent = userAgent;
}
@ -119,12 +119,12 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
}
private ProvisioningManager getNewProvisioningManager() {
return ProvisioningManager.init(dataPath, serviceEnvironment, userAgent, this::addManager);
return ProvisioningManager.init(settingsPath, serviceEnvironment, userAgent, this::addManager);
}
@Override
public RegistrationManager getNewRegistrationManager(String account) throws IOException {
return RegistrationManager.init(account, dataPath, serviceEnvironment, userAgent, this::addManager);
return RegistrationManager.init(account, settingsPath, serviceEnvironment, userAgent, this::addManager);
}
@Override

View file

@ -2,11 +2,11 @@ package org.asamk.signal.manager;
import java.io.File;
public record PathConfig(
record PathConfig(
File dataPath, File attachmentsPath, File avatarsPath, File stickerPacksPath
) {
public static PathConfig createDefault(final File settingsPath) {
static PathConfig createDefault(final File settingsPath) {
return new PathConfig(new File(settingsPath, "data"),
new File(settingsPath, "attachments"),
new File(settingsPath, "avatars"),

View file

@ -1,5 +1,6 @@
package org.asamk.signal.manager;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironment;
@ -32,5 +33,5 @@ public interface ProvisioningManager {
URI getDeviceLinkUri() throws TimeoutException, IOException;
String finishDeviceLink(String deviceName) throws IOException, TimeoutException, UserAlreadyExists;
String finishDeviceLink(String deviceName) throws IOException, TimeoutException, UserAlreadyExistsException;
}

View file

@ -16,6 +16,7 @@
*/
package org.asamk.signal.manager;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
import org.asamk.signal.manager.storage.SignalAccount;
@ -38,7 +39,7 @@ import java.net.URI;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
public class ProvisioningManagerImpl implements ProvisioningManager {
class ProvisioningManagerImpl implements ProvisioningManager {
private final static Logger logger = LoggerFactory.getLogger(ProvisioningManagerImpl.class);
@ -87,14 +88,14 @@ public class ProvisioningManagerImpl implements ProvisioningManager {
}
@Override
public String finishDeviceLink(String deviceName) throws IOException, TimeoutException, UserAlreadyExists {
public String finishDeviceLink(String deviceName) throws IOException, TimeoutException, UserAlreadyExistsException {
var ret = accountManager.getNewDeviceRegistration(tempIdentityKey);
var number = ret.getNumber();
logger.info("Received link information from {}, linking in progress ...", number);
if (SignalAccount.userExists(pathConfig.dataPath(), number) && !canRelinkExistingAccount(number)) {
throw new UserAlreadyExists(number, SignalAccount.getFileName(pathConfig.dataPath(), number));
throw new UserAlreadyExistsException(number, SignalAccount.getFileName(pathConfig.dataPath(), number));
}
var encryptedDeviceName = deviceName == null

View file

@ -47,7 +47,7 @@ import java.util.function.Consumer;
import static org.asamk.signal.manager.config.ServiceConfig.capabilities;
public class RegistrationManagerImpl implements RegistrationManager {
class RegistrationManagerImpl implements RegistrationManager {
private final static Logger logger = LoggerFactory.getLogger(RegistrationManagerImpl.class);

View file

@ -52,7 +52,7 @@ public class SignalDependencies {
private ProfileService profileService;
private SignalServiceCipher cipher;
public SignalDependencies(
SignalDependencies(
final ServiceEnvironmentConfig serviceEnvironmentConfig,
final String userAgent,
final DynamicCredentialsProvider credentialsProvider,

View file

@ -21,7 +21,7 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
* The monitor is also responsible for sending heartbeats/keep-alive messages to prevent
* timeouts.
*/
public final class SignalWebSocketHealthMonitor implements HealthMonitor {
final class SignalWebSocketHealthMonitor implements HealthMonitor {
private final static Logger logger = LoggerFactory.getLogger(SignalWebSocketHealthMonitor.class);

View file

@ -0,0 +1,12 @@
package org.asamk.signal.manager.api;
public class AccountCheckException extends Exception {
public AccountCheckException(String message) {
super(message);
}
public AccountCheckException(String message, Exception e) {
super(message, e);
}
}

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
public class AttachmentInvalidException extends Exception {

View file

@ -1,6 +1,5 @@
package org.asamk.signal.manager.api;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.whispersystems.libsignal.IdentityKey;

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
public class NotMasterDeviceException extends Exception {

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
public class NotRegisteredException extends Exception {

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
public class StickerPackInvalidException extends Exception {

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage;
import org.whispersystems.signalservice.internal.storage.protos.ContactRecord;

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;

View file

@ -1,13 +1,13 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
import java.io.File;
public class UserAlreadyExists extends Exception {
public class UserAlreadyExistsException extends Exception {
private final String number;
private final File fileName;
public UserAlreadyExists(String number, File fileName) {
public UserAlreadyExistsException(String number, File fileName) {
this.number = number;
this.fileName = fileName;
}

View file

@ -1,8 +1,8 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.AttachmentStore;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.util.AttachmentUtils;
import org.asamk.signal.manager.util.IOUtils;

View file

@ -4,7 +4,7 @@ import org.asamk.signal.manager.AttachmentStore;
import org.asamk.signal.manager.AvatarStore;
import org.asamk.signal.manager.JobExecutor;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.StickerPackStore;
import org.asamk.signal.manager.storage.stickerPacks.StickerPackStore;
import org.asamk.signal.manager.storage.SignalAccount;
import java.util.function.Supplier;

View file

@ -1,7 +1,7 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.SendGroupMessageResults;

View file

@ -1,6 +1,6 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.asamk.signal.manager.util.Utils;

View file

@ -2,8 +2,8 @@ package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.UntrustedIdentityException;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.api.UntrustedIdentityException;
import org.asamk.signal.manager.actions.HandleAction;
import org.asamk.signal.manager.actions.RefreshPreKeysAction;
import org.asamk.signal.manager.actions.RenewSessionAction;

View file

@ -2,7 +2,7 @@ package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.UntrustedIdentityException;
import org.asamk.signal.manager.api.UntrustedIdentityException;
import org.asamk.signal.manager.actions.HandleAction;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.messageCache.CachedMessage;

View file

@ -1,10 +1,10 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.JsonStickerPack;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.stickerPacks.JsonStickerPack;
import org.asamk.signal.manager.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View file

@ -1,7 +1,7 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.api.PhoneNumberSharingMode;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.storage.SignalAccount;

View file

@ -1,6 +1,6 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.groups.GroupInfoV1;
import org.asamk.signal.manager.storage.recipients.Contact;

View file

@ -3,7 +3,7 @@ package org.asamk.signal.manager.storage;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.storage.configuration.ConfigurationStore;

View file

@ -1,6 +1,6 @@
package org.asamk.signal.manager.storage.identities;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.whispersystems.libsignal.IdentityKey;

View file

@ -2,7 +2,7 @@ package org.asamk.signal.manager.storage.identities;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.asamk.signal.manager.storage.recipients.RecipientResolver;
import org.asamk.signal.manager.util.IOUtils;

View file

@ -1,6 +1,6 @@
package org.asamk.signal.manager.storage.protocol;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.whispersystems.libsignal.IdentityKey;

View file

@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.Utils;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.slf4j.Logger;

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.storage.stickerPacks;
import org.asamk.signal.manager.api.StickerPack;

View file

@ -1,8 +1,9 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.storage.stickerPacks;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.manager.storage.stickerPacks.JsonStickerPack;
import org.asamk.signal.manager.util.IOUtils;
import org.asamk.signal.manager.util.Utils;
import org.whispersystems.signalservice.api.util.StreamDetails;

View file

@ -1,6 +1,6 @@
package org.asamk.signal.manager.util;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;

View file

@ -2,9 +2,9 @@ package org.asamk.signal.manager.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.manager.JsonStickerPack;
import org.asamk.signal.manager.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.storage.stickerPacks.JsonStickerPack;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload;