X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/55dde93811ac137741b49d3fab2483e689592038..9ffacfe90e54a86da289d3e6fb3c3df738f57abb:/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 5f0f551c..9d0b344a 100644 --- a/lib/src/main/java/org/asamk/signal/manager/SignalAccountFiles.java +++ b/lib/src/main/java/org/asamk/signal/manager/SignalAccountFiles.java @@ -12,6 +12,7 @@ import org.asamk.signal.manager.util.KeyUtils; import org.signal.libsignal.protocol.util.KeyHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException; import java.io.File; import java.io.IOException; @@ -41,20 +42,33 @@ public class SignalAccountFiles { 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()); } catch (NotRegisteredException | IOException | AccountCheckException e) { logger.warn("Ignoring {}: {} ({})", a.number(), e.getMessage(), e.getClass().getSimpleName()); return null; + } catch (Throwable e) { + logger.error("Failed to load {}: {} ({})", a.number(), e.getMessage(), e.getClass().getSimpleName()); + throw e; } }).filter(Objects::nonNull).toList(); @@ -95,12 +109,15 @@ public class SignalAccountFiles { final var manager = new ManagerImpl(account, pathConfig, - (newNumber, newAci) -> accountsStore.updateAccount(accountPath, newNumber, newAci), + new AccountFileUpdaterImpl(accountsStore, accountPath), serviceEnvironmentConfig, userAgent); try { manager.checkAccountState(); + } catch (DeprecatedVersionException e) { + manager.close(); + throw new AccountCheckException("signal-cli version is too old for the Signal-Server, please update."); } catch (IOException e) { manager.close(); throw new AccountCheckException("Error while checking account " + number + ": " + e.getMessage(), e); @@ -108,6 +125,7 @@ public class SignalAccountFiles { if (account.getServiceEnvironment() == null) { account.setServiceEnvironment(serviceEnvironment); + accountsStore.updateAccount(accountPath, account.getNumber(), account.getAci()); } return manager; @@ -138,6 +156,7 @@ 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(), @@ -147,6 +166,7 @@ public class SignalAccountFiles { aciIdentityKey, pniIdentityKey, registrationId, + pniRegistrationId, profileKey, trustNewIdentity); @@ -155,7 +175,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); @@ -169,6 +189,6 @@ public class SignalAccountFiles { serviceEnvironmentConfig, userAgent, newManagerListener, - (newNumber, newAci) -> accountsStore.updateAccount(accountPath, newNumber, newAci)); + new AccountFileUpdaterImpl(accountsStore, accountPath)); } }