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
.signal
.libsignal
.protocol
.util
.KeyHelper
;
13 import org
.slf4j
.Logger
;
14 import org
.slf4j
.LoggerFactory
;
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 ServiceEnvironment serviceEnvironment
;
28 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
29 private final String userAgent
;
30 private final TrustNewIdentity trustNewIdentity
;
31 private final AccountsStore accountsStore
;
33 public SignalAccountFiles(
34 final File settingsPath
,
35 final ServiceEnvironment serviceEnvironment
,
36 final String userAgent
,
37 final TrustNewIdentity trustNewIdentity
38 ) throws IOException
{
39 this.pathConfig
= PathConfig
.createDefault(settingsPath
);
40 this.serviceEnvironment
= serviceEnvironment
;
41 this.serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(this.serviceEnvironment
, userAgent
);
42 this.userAgent
= userAgent
;
43 this.trustNewIdentity
= trustNewIdentity
;
44 this.accountsStore
= new AccountsStore(pathConfig
.dataPath(), serviceEnvironment
, accountPath
-> {
45 if (accountPath
== null || !SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
50 return SignalAccount
.load(pathConfig
.dataPath(), accountPath
, false, trustNewIdentity
);
51 } catch (Exception e
) {
57 public Set
<String
> getAllLocalAccountNumbers() throws IOException
{
58 return accountsStore
.getAllNumbers();
61 public MultiAccountManager
initMultiAccountManager() throws IOException
{
62 final var managers
= accountsStore
.getAllAccounts().parallelStream().map(a
-> {
64 return initManager(a
.number(), a
.path());
65 } catch (NotRegisteredException
| IOException
| AccountCheckException e
) {
66 logger
.warn("Ignoring {}: {} ({})", a
.number(), e
.getMessage(), e
.getClass().getSimpleName());
69 }).filter(Objects
::nonNull
).toList();
71 return new MultiAccountManagerImpl(managers
, this);
74 public Manager
initManager(String number
) throws IOException
, NotRegisteredException
, AccountCheckException
{
75 final var accountPath
= accountsStore
.getPathByNumber(number
);
76 return this.initManager(number
, accountPath
);
79 private Manager
initManager(
80 String number
, String accountPath
81 ) throws IOException
, NotRegisteredException
, AccountCheckException
{
82 if (accountPath
== null) {
83 throw new NotRegisteredException();
85 if (!SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
86 throw new NotRegisteredException();
89 var account
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, true, trustNewIdentity
);
90 if (!number
.equals(account
.getNumber())) {
92 throw new IOException("Number in account file doesn't match expected number: " + account
.getNumber());
95 if (!account
.isRegistered()) {
97 throw new NotRegisteredException();
100 if (account
.getServiceEnvironment() != null && account
.getServiceEnvironment() != serviceEnvironment
) {
101 throw new IOException("Account is registered in another environment: " + account
.getServiceEnvironment());
104 account
.initDatabase();
106 final var manager
= new ManagerImpl(account
,
108 new AccountFileUpdaterImpl(accountsStore
, accountPath
),
109 serviceEnvironmentConfig
,
113 manager
.checkAccountState();
114 } catch (IOException e
) {
116 throw new AccountCheckException("Error while checking account " + number
+ ": " + e
.getMessage(), e
);
119 if (account
.getServiceEnvironment() == null) {
120 account
.setServiceEnvironment(serviceEnvironment
);
121 accountsStore
.updateAccount(accountPath
, account
.getNumber(), account
.getAci());
127 public ProvisioningManager
initProvisioningManager() {
128 return initProvisioningManager(null);
131 public ProvisioningManager
initProvisioningManager(Consumer
<Manager
> newManagerListener
) {
132 return new ProvisioningManagerImpl(pathConfig
,
133 serviceEnvironmentConfig
,
139 public RegistrationManager
initRegistrationManager(String number
) throws IOException
{
140 return initRegistrationManager(number
, null);
143 public RegistrationManager
initRegistrationManager(
144 String number
, Consumer
<Manager
> newManagerListener
145 ) throws IOException
{
146 final var accountPath
= accountsStore
.getPathByNumber(number
);
147 if (accountPath
== null || !SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
148 final var newAccountPath
= accountPath
== null ? accountsStore
.addAccount(number
, null) : accountPath
;
149 var aciIdentityKey
= KeyUtils
.generateIdentityKeyPair();
150 var pniIdentityKey
= KeyUtils
.generateIdentityKeyPair();
151 var registrationId
= KeyHelper
.generateRegistrationId(false);
152 var pniRegistrationId
= KeyHelper
.generateRegistrationId(false);
154 var profileKey
= KeyUtils
.createProfileKey();
155 var account
= SignalAccount
.create(pathConfig
.dataPath(),
166 return new RegistrationManagerImpl(account
,
168 serviceEnvironmentConfig
,
171 new AccountFileUpdaterImpl(accountsStore
, newAccountPath
));
174 var account
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, true, trustNewIdentity
);
175 if (!number
.equals(account
.getNumber())) {
177 throw new IOException("Number in account file doesn't match expected number: " + account
.getNumber());
180 return new RegistrationManagerImpl(account
,
182 serviceEnvironmentConfig
,
185 new AccountFileUpdaterImpl(accountsStore
, accountPath
));