X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/d27a12a6cfcf9afca362a8d359ee808b1507e0b3..dc8b83a1109865a4a375b21c3838412d9e2d9215:/lib/src/main/java/org/asamk/signal/manager/SignalAccountFiles.java diff --git a/lib/src/main/java/org/asamk/signal/manager/SignalAccountFiles.java b/lib/src/main/java/org/asamk/signal/manager/SignalAccountFiles.java index 0ca9b0ab..95fd453d 100644 --- a/lib/src/main/java/org/asamk/signal/manager/SignalAccountFiles.java +++ b/lib/src/main/java/org/asamk/signal/manager/SignalAccountFiles.java @@ -24,6 +24,7 @@ public class SignalAccountFiles { private static final Logger logger = LoggerFactory.getLogger(MultiAccountManager.class); private final PathConfig pathConfig; + private final ServiceEnvironment serviceEnvironment; private final ServiceEnvironmentConfig serviceEnvironmentConfig; private final String userAgent; private final TrustNewIdentity trustNewIdentity; @@ -36,17 +37,28 @@ public class SignalAccountFiles { final TrustNewIdentity trustNewIdentity ) throws IOException { this.pathConfig = PathConfig.createDefault(settingsPath); - this.serviceEnvironmentConfig = ServiceConfig.getServiceEnvironmentConfig(serviceEnvironment, userAgent); + this.serviceEnvironment = serviceEnvironment; + this.serviceEnvironmentConfig = ServiceConfig.getServiceEnvironmentConfig(this.serviceEnvironment, userAgent); this.userAgent = userAgent; this.trustNewIdentity = trustNewIdentity; - this.accountsStore = new AccountsStore(pathConfig.dataPath()); + this.accountsStore = new AccountsStore(pathConfig.dataPath(), serviceEnvironment, accountPath -> { + if (accountPath == null || !SignalAccount.accountFileExists(pathConfig.dataPath(), accountPath)) { + return null; + } + + try { + return SignalAccount.load(pathConfig.dataPath(), accountPath, false, trustNewIdentity); + } catch (Exception e) { + return null; + } + }); } - public Set getAllLocalAccountNumbers() { + public Set getAllLocalAccountNumbers() throws IOException { return accountsStore.getAllNumbers(); } - public MultiAccountManager initMultiAccountManager() { + public MultiAccountManager initMultiAccountManager() throws IOException { final var managers = accountsStore.getAllAccounts().parallelStream().map(a -> { try { return initManager(a.number(), a.path()); @@ -85,11 +97,15 @@ public class SignalAccountFiles { throw new NotRegisteredException(); } + if (account.getServiceEnvironment() != null && account.getServiceEnvironment() != serviceEnvironment) { + throw new IOException("Account is registered in another environment: " + account.getServiceEnvironment()); + } + account.initDatabase(); final var manager = new ManagerImpl(account, pathConfig, - (newNumber, newAci) -> accountsStore.updateAccount(accountPath, newNumber, newAci), + new AccountFileUpdaterImpl(accountsStore, accountPath), serviceEnvironmentConfig, userAgent); @@ -100,6 +116,11 @@ public class SignalAccountFiles { throw new AccountCheckException("Error while checking account " + number + ": " + e.getMessage(), e); } + if (account.getServiceEnvironment() == null) { + account.setServiceEnvironment(serviceEnvironment); + accountsStore.updateAccount(accountPath, account.getNumber(), account.getAci()); + } + return manager; } @@ -128,14 +149,17 @@ public class SignalAccountFiles { var aciIdentityKey = KeyUtils.generateIdentityKeyPair(); var pniIdentityKey = KeyUtils.generateIdentityKeyPair(); var registrationId = KeyHelper.generateRegistrationId(false); + var pniRegistrationId = KeyHelper.generateRegistrationId(false); var profileKey = KeyUtils.createProfileKey(); var account = SignalAccount.create(pathConfig.dataPath(), newAccountPath, number, + serviceEnvironment, aciIdentityKey, pniIdentityKey, registrationId, + pniRegistrationId, profileKey, trustNewIdentity); @@ -144,7 +168,7 @@ public class SignalAccountFiles { serviceEnvironmentConfig, userAgent, newManagerListener, - (newNumber, newAci) -> accountsStore.updateAccount(newAccountPath, newNumber, newAci)); + new AccountFileUpdaterImpl(accountsStore, newAccountPath)); } var account = SignalAccount.load(pathConfig.dataPath(), accountPath, true, trustNewIdentity); @@ -158,6 +182,6 @@ public class SignalAccountFiles { serviceEnvironmentConfig, userAgent, newManagerListener, - (newNumber, newAci) -> accountsStore.updateAccount(accountPath, newNumber, newAci)); + new AccountFileUpdaterImpl(accountsStore, accountPath)); } }