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
.util
.KeyUtils
;
11 import org
.signal
.libsignal
.protocol
.util
.KeyHelper
;
12 import org
.slf4j
.Logger
;
13 import org
.slf4j
.LoggerFactory
;
14 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.DeprecatedVersionException
;
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 Settings settings
;
31 private final AccountsStore accountsStore
;
33 public SignalAccountFiles(
34 final File settingsPath
,
35 final ServiceEnvironment serviceEnvironment
,
36 final String userAgent
,
37 final Settings settings
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.settings
= settings
;
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, settings
);
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());
68 } catch (Throwable e
) {
69 logger
.error("Failed to load {}: {} ({})", a
.number(), e
.getMessage(), e
.getClass().getSimpleName());
72 }).filter(Objects
::nonNull
).toList();
74 return new MultiAccountManagerImpl(managers
, this);
77 public Manager
initManager(String number
) throws IOException
, NotRegisteredException
, AccountCheckException
{
78 final var accountPath
= accountsStore
.getPathByNumber(number
);
79 return this.initManager(number
, accountPath
);
82 private Manager
initManager(
83 String number
, String accountPath
84 ) throws IOException
, NotRegisteredException
, AccountCheckException
{
85 if (accountPath
== null) {
86 throw new NotRegisteredException();
88 if (!SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
89 throw new NotRegisteredException();
92 var account
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, true, settings
);
93 if (!number
.equals(account
.getNumber())) {
95 throw new IOException("Number in account file doesn't match expected number: " + account
.getNumber());
98 if (!account
.isRegistered()) {
100 throw new NotRegisteredException();
103 if (account
.getServiceEnvironment() != null && account
.getServiceEnvironment() != serviceEnvironment
) {
104 throw new IOException("Account is registered in another environment: " + account
.getServiceEnvironment());
107 account
.initDatabase();
109 final var manager
= new ManagerImpl(account
,
111 new AccountFileUpdaterImpl(accountsStore
, accountPath
),
112 serviceEnvironmentConfig
,
116 manager
.checkAccountState();
117 } catch (DeprecatedVersionException e
) {
119 throw new AccountCheckException("signal-cli version is too old for the Signal-Server, please update.");
120 } catch (IOException e
) {
122 throw new AccountCheckException("Error while checking account " + number
+ ": " + e
.getMessage(), e
);
125 if (account
.getServiceEnvironment() == null) {
126 account
.setServiceEnvironment(serviceEnvironment
);
127 accountsStore
.updateAccount(accountPath
, account
.getNumber(), account
.getAci());
133 public ProvisioningManager
initProvisioningManager() {
134 return initProvisioningManager(null);
137 public ProvisioningManager
initProvisioningManager(Consumer
<Manager
> newManagerListener
) {
138 return new ProvisioningManagerImpl(pathConfig
,
139 serviceEnvironmentConfig
,
145 public RegistrationManager
initRegistrationManager(String number
) throws IOException
{
146 return initRegistrationManager(number
, null);
149 public RegistrationManager
initRegistrationManager(
150 String number
, Consumer
<Manager
> newManagerListener
151 ) throws IOException
{
152 final var accountPath
= accountsStore
.getPathByNumber(number
);
153 if (accountPath
== null || !SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
154 final var newAccountPath
= accountPath
== null ? accountsStore
.addAccount(number
, null) : accountPath
;
155 var aciIdentityKey
= KeyUtils
.generateIdentityKeyPair();
156 var pniIdentityKey
= KeyUtils
.generateIdentityKeyPair();
157 var registrationId
= KeyHelper
.generateRegistrationId(false);
158 var pniRegistrationId
= KeyHelper
.generateRegistrationId(false);
160 var profileKey
= KeyUtils
.createProfileKey();
161 var account
= SignalAccount
.create(pathConfig
.dataPath(),
172 return new RegistrationManagerImpl(account
,
174 serviceEnvironmentConfig
,
177 new AccountFileUpdaterImpl(accountsStore
, newAccountPath
));
180 var account
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, true, settings
);
181 if (!number
.equals(account
.getNumber())) {
183 throw new IOException("Number in account file doesn't match expected number: " + account
.getNumber());
186 return new RegistrationManagerImpl(account
,
188 serviceEnvironmentConfig
,
191 new AccountFileUpdaterImpl(accountsStore
, accountPath
));