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
.api
.ServiceEnvironment
;
6 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
7 import org
.asamk
.signal
.manager
.config
.ServiceEnvironmentConfig
;
8 import org
.asamk
.signal
.manager
.internal
.AccountFileUpdaterImpl
;
9 import org
.asamk
.signal
.manager
.internal
.ManagerImpl
;
10 import org
.asamk
.signal
.manager
.internal
.MultiAccountManagerImpl
;
11 import org
.asamk
.signal
.manager
.internal
.PathConfig
;
12 import org
.asamk
.signal
.manager
.internal
.ProvisioningManagerImpl
;
13 import org
.asamk
.signal
.manager
.internal
.RegistrationManagerImpl
;
14 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
15 import org
.asamk
.signal
.manager
.storage
.accounts
.AccountsStore
;
16 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
17 import org
.slf4j
.Logger
;
18 import org
.slf4j
.LoggerFactory
;
19 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.DeprecatedVersionException
;
22 import java
.io
.IOException
;
23 import java
.util
.Objects
;
25 import java
.util
.function
.Consumer
;
27 public class SignalAccountFiles
{
29 private static final Logger logger
= LoggerFactory
.getLogger(MultiAccountManager
.class);
31 private final PathConfig pathConfig
;
32 private final ServiceEnvironment serviceEnvironment
;
33 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
34 private final String userAgent
;
35 private final Settings settings
;
36 private final AccountsStore accountsStore
;
38 public SignalAccountFiles(
39 final File settingsPath
,
40 final ServiceEnvironment serviceEnvironment
,
41 final String userAgent
,
42 final Settings settings
43 ) throws IOException
{
44 this.pathConfig
= PathConfig
.createDefault(settingsPath
);
45 this.serviceEnvironment
= serviceEnvironment
;
46 this.serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(this.serviceEnvironment
, userAgent
);
47 this.userAgent
= userAgent
;
48 this.settings
= settings
;
49 this.accountsStore
= new AccountsStore(pathConfig
.dataPath(), serviceEnvironment
, accountPath
-> {
50 if (accountPath
== null || !SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
55 return SignalAccount
.load(pathConfig
.dataPath(), accountPath
, false, settings
);
56 } catch (Exception e
) {
62 public Set
<String
> getAllLocalAccountNumbers() throws IOException
{
63 return accountsStore
.getAllNumbers();
66 public MultiAccountManager
initMultiAccountManager() throws IOException
{
67 final var managers
= accountsStore
.getAllAccounts().parallelStream().map(a
-> {
69 return initManager(a
.number(), a
.path());
70 } catch (NotRegisteredException
| IOException
| AccountCheckException e
) {
71 logger
.warn("Ignoring {}: {} ({})", a
.number(), e
.getMessage(), e
.getClass().getSimpleName());
73 } catch (Throwable e
) {
74 logger
.error("Failed to load {}: {} ({})", a
.number(), e
.getMessage(), e
.getClass().getSimpleName());
77 }).filter(Objects
::nonNull
).toList();
79 return new MultiAccountManagerImpl(managers
, this);
82 public Manager
initManager(String number
) throws IOException
, NotRegisteredException
, AccountCheckException
{
83 final var accountPath
= accountsStore
.getPathByNumber(number
);
84 return this.initManager(number
, accountPath
);
87 private Manager
initManager(
88 String number
, String accountPath
89 ) throws IOException
, NotRegisteredException
, AccountCheckException
{
90 if (accountPath
== null) {
91 throw new NotRegisteredException();
93 if (!SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
94 throw new NotRegisteredException();
97 var account
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, true, settings
);
98 if (!number
.equals(account
.getNumber())) {
100 throw new IOException("Number in account file doesn't match expected number: " + account
.getNumber());
103 if (!account
.isRegistered()) {
105 throw new NotRegisteredException();
108 if (account
.getServiceEnvironment() != null && account
.getServiceEnvironment() != serviceEnvironment
) {
109 throw new IOException("Account is registered in another environment: " + account
.getServiceEnvironment());
112 account
.initDatabase();
114 final var manager
= new ManagerImpl(account
,
116 new AccountFileUpdaterImpl(accountsStore
, accountPath
),
117 serviceEnvironmentConfig
,
121 manager
.checkAccountState();
122 } catch (DeprecatedVersionException e
) {
124 throw new AccountCheckException("signal-cli version is too old for the Signal-Server, please update.");
125 } catch (IOException e
) {
127 throw new AccountCheckException("Error while checking account " + number
+ ": " + e
.getMessage(), e
);
130 if (account
.getServiceEnvironment() == null) {
131 account
.setServiceEnvironment(serviceEnvironment
);
132 accountsStore
.updateAccount(accountPath
, account
.getNumber(), account
.getAci());
138 public ProvisioningManager
initProvisioningManager() {
139 return initProvisioningManager(null);
142 public ProvisioningManager
initProvisioningManager(Consumer
<Manager
> newManagerListener
) {
143 return new ProvisioningManagerImpl(pathConfig
,
144 serviceEnvironmentConfig
,
150 public RegistrationManager
initRegistrationManager(String number
) throws IOException
{
151 return initRegistrationManager(number
, null);
154 public RegistrationManager
initRegistrationManager(
155 String number
, Consumer
<Manager
> newManagerListener
156 ) throws IOException
{
157 final var accountPath
= accountsStore
.getPathByNumber(number
);
158 if (accountPath
== null || !SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
159 final var newAccountPath
= accountPath
== null ? accountsStore
.addAccount(number
, null) : accountPath
;
160 var aciIdentityKey
= KeyUtils
.generateIdentityKeyPair();
161 var pniIdentityKey
= KeyUtils
.generateIdentityKeyPair();
163 var profileKey
= KeyUtils
.createProfileKey();
164 var account
= SignalAccount
.create(pathConfig
.dataPath(),
172 account
.initDatabase();
174 return new RegistrationManagerImpl(account
,
176 serviceEnvironmentConfig
,
179 new AccountFileUpdaterImpl(accountsStore
, newAccountPath
));
182 var account
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, true, settings
);
183 if (!number
.equals(account
.getNumber())) {
185 throw new IOException("Number in account file doesn't match expected number: " + account
.getNumber());
187 account
.initDatabase();
189 return new RegistrationManagerImpl(account
,
191 serviceEnvironmentConfig
,
194 new AccountFileUpdaterImpl(accountsStore
, accountPath
));