1 package org
.asamk
.signal
.manager
;
3 import org
.asamk
.signal
.manager
.api
.AccountCheckException
;
4 import org
.asamk
.signal
.manager
.api
.NotRegisteredException
;
5 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
6 import org
.asamk
.signal
.manager
.config
.ServiceEnvironment
;
7 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
8 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
9 import org
.asamk
.signal
.manager
.storage
.accounts
.AccountsStore
;
10 import org
.asamk
.signal
.manager
.storage
.identities
.TrustNewIdentity
;
11 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
12 import org
.slf4j
.Logger
;
13 import org
.slf4j
.LoggerFactory
;
14 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
17 import java
.io
.IOException
;
18 import java
.util
.Objects
;
20 import java
.util
.function
.Consumer
;
22 public class SignalAccountFiles
{
24 private static final Logger logger
= LoggerFactory
.getLogger(MultiAccountManager
.class);
26 private final PathConfig pathConfig
;
27 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
28 private final String userAgent
;
29 private final TrustNewIdentity trustNewIdentity
;
30 private final AccountsStore accountsStore
;
32 public SignalAccountFiles(
33 final File settingsPath
,
34 final ServiceEnvironment serviceEnvironment
,
35 final String userAgent
,
36 final TrustNewIdentity trustNewIdentity
37 ) throws IOException
{
38 this.pathConfig
= PathConfig
.createDefault(settingsPath
);
39 this.serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(serviceEnvironment
, userAgent
);
40 this.userAgent
= userAgent
;
41 this.trustNewIdentity
= trustNewIdentity
;
42 this.accountsStore
= new AccountsStore(pathConfig
.dataPath());
45 public Set
<String
> getAllLocalAccountNumbers() {
46 return accountsStore
.getAllNumbers();
49 public MultiAccountManager
initMultiAccountManager() {
50 final var managers
= getAllLocalAccountNumbers().stream().map(a
-> {
52 return initManager(a
);
53 } catch (NotRegisteredException
| IOException
| AccountCheckException e
) {
54 logger
.warn("Ignoring {}: {} ({})", a
, e
.getMessage(), e
.getClass().getSimpleName());
57 }).filter(Objects
::nonNull
).toList();
59 return new MultiAccountManagerImpl(managers
, this);
62 public Manager
initManager(String number
) throws IOException
, NotRegisteredException
, AccountCheckException
{
63 final var accountPath
= accountsStore
.getPathByNumber(number
);
64 if (accountPath
== null || !SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
65 throw new NotRegisteredException();
68 var account
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, true, trustNewIdentity
);
69 if (!number
.equals(account
.getNumber())) {
71 throw new IOException("Number in account file doesn't match expected number: " + account
.getNumber());
74 if (!account
.isRegistered()) {
76 throw new NotRegisteredException();
79 account
.initDatabase();
81 final var manager
= new ManagerImpl(account
,
83 (newNumber
, newAci
) -> accountsStore
.updateAccount(accountPath
, newNumber
, newAci
),
84 serviceEnvironmentConfig
,
88 manager
.checkAccountState();
89 } catch (IOException e
) {
91 throw new AccountCheckException("Error while checking account " + number
+ ": " + e
.getMessage(), e
);
97 public ProvisioningManager
initProvisioningManager() {
98 return initProvisioningManager(null);
101 public ProvisioningManager
initProvisioningManager(Consumer
<Manager
> newManagerListener
) {
102 return new ProvisioningManagerImpl(pathConfig
,
103 serviceEnvironmentConfig
,
109 public RegistrationManager
initRegistrationManager(String number
) throws IOException
{
110 return initRegistrationManager(number
, null);
113 public RegistrationManager
initRegistrationManager(
114 String number
, Consumer
<Manager
> newManagerListener
115 ) throws IOException
{
116 final var accountPath
= accountsStore
.getPathByNumber(number
);
117 if (accountPath
== null || !SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
118 final var newAccountPath
= accountPath
== null ? accountsStore
.addAccount(number
, null) : accountPath
;
119 var identityKey
= KeyUtils
.generateIdentityKeyPair();
120 var registrationId
= KeyHelper
.generateRegistrationId(false);
122 var profileKey
= KeyUtils
.createProfileKey();
123 var account
= SignalAccount
.create(pathConfig
.dataPath(),
131 return new RegistrationManagerImpl(account
,
133 serviceEnvironmentConfig
,
136 (newNumber
, newAci
) -> accountsStore
.updateAccount(newAccountPath
, newNumber
, newAci
));
139 var account
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, true, trustNewIdentity
);
140 if (!number
.equals(account
.getNumber())) {
142 throw new IOException("Number in account file doesn't match expected number: " + account
.getNumber());
145 return new RegistrationManagerImpl(account
,
147 serviceEnvironmentConfig
,
150 (newNumber
, newAci
) -> accountsStore
.updateAccount(accountPath
, newNumber
, newAci
));