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
.signal
.libsignal
.protocol
.util
.KeyHelper
;
18 import org
.slf4j
.Logger
;
19 import org
.slf4j
.LoggerFactory
;
20 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.DeprecatedVersionException
;
23 import java
.io
.IOException
;
24 import java
.util
.Objects
;
26 import java
.util
.function
.Consumer
;
28 public class SignalAccountFiles
{
30 private static final Logger logger
= LoggerFactory
.getLogger(MultiAccountManager
.class);
32 private final PathConfig pathConfig
;
33 private final ServiceEnvironment serviceEnvironment
;
34 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
35 private final String userAgent
;
36 private final Settings settings
;
37 private final AccountsStore accountsStore
;
39 public SignalAccountFiles(
40 final File settingsPath
,
41 final ServiceEnvironment serviceEnvironment
,
42 final String userAgent
,
43 final Settings settings
44 ) throws IOException
{
45 this.pathConfig
= PathConfig
.createDefault(settingsPath
);
46 this.serviceEnvironment
= serviceEnvironment
;
47 this.serviceEnvironmentConfig
= ServiceConfig
.getServiceEnvironmentConfig(this.serviceEnvironment
, userAgent
);
48 this.userAgent
= userAgent
;
49 this.settings
= settings
;
50 this.accountsStore
= new AccountsStore(pathConfig
.dataPath(), serviceEnvironment
, accountPath
-> {
51 if (accountPath
== null || !SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
56 return SignalAccount
.load(pathConfig
.dataPath(), accountPath
, false, settings
);
57 } catch (Exception e
) {
63 public Set
<String
> getAllLocalAccountNumbers() throws IOException
{
64 return accountsStore
.getAllNumbers();
67 public MultiAccountManager
initMultiAccountManager() throws IOException
{
68 final var managers
= accountsStore
.getAllAccounts().parallelStream().map(a
-> {
70 return initManager(a
.number(), a
.path());
71 } catch (NotRegisteredException
| IOException
| AccountCheckException e
) {
72 logger
.warn("Ignoring {}: {} ({})", a
.number(), e
.getMessage(), e
.getClass().getSimpleName());
74 } catch (Throwable e
) {
75 logger
.error("Failed to load {}: {} ({})", a
.number(), e
.getMessage(), e
.getClass().getSimpleName());
78 }).filter(Objects
::nonNull
).toList();
80 return new MultiAccountManagerImpl(managers
, this);
83 public Manager
initManager(String number
) throws IOException
, NotRegisteredException
, AccountCheckException
{
84 final var accountPath
= accountsStore
.getPathByNumber(number
);
85 return this.initManager(number
, accountPath
);
88 private Manager
initManager(
89 String number
, String accountPath
90 ) throws IOException
, NotRegisteredException
, AccountCheckException
{
91 if (accountPath
== null) {
92 throw new NotRegisteredException();
94 if (!SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
95 throw new NotRegisteredException();
98 var account
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, true, settings
);
99 if (!number
.equals(account
.getNumber())) {
101 throw new IOException("Number in account file doesn't match expected number: " + account
.getNumber());
104 if (!account
.isRegistered()) {
106 throw new NotRegisteredException();
109 if (account
.getServiceEnvironment() != null && account
.getServiceEnvironment() != serviceEnvironment
) {
110 throw new IOException("Account is registered in another environment: " + account
.getServiceEnvironment());
113 account
.initDatabase();
115 final var manager
= new ManagerImpl(account
,
117 new AccountFileUpdaterImpl(accountsStore
, accountPath
),
118 serviceEnvironmentConfig
,
122 manager
.checkAccountState();
123 } catch (DeprecatedVersionException e
) {
125 throw new AccountCheckException("signal-cli version is too old for the Signal-Server, please update.");
126 } catch (IOException e
) {
128 throw new AccountCheckException("Error while checking account " + number
+ ": " + e
.getMessage(), e
);
131 if (account
.getServiceEnvironment() == null) {
132 account
.setServiceEnvironment(serviceEnvironment
);
133 accountsStore
.updateAccount(accountPath
, account
.getNumber(), account
.getAci());
139 public ProvisioningManager
initProvisioningManager() {
140 return initProvisioningManager(null);
143 public ProvisioningManager
initProvisioningManager(Consumer
<Manager
> newManagerListener
) {
144 return new ProvisioningManagerImpl(pathConfig
,
145 serviceEnvironmentConfig
,
151 public RegistrationManager
initRegistrationManager(String number
) throws IOException
{
152 return initRegistrationManager(number
, null);
155 public RegistrationManager
initRegistrationManager(
156 String number
, Consumer
<Manager
> newManagerListener
157 ) throws IOException
{
158 final var accountPath
= accountsStore
.getPathByNumber(number
);
159 if (accountPath
== null || !SignalAccount
.accountFileExists(pathConfig
.dataPath(), accountPath
)) {
160 final var newAccountPath
= accountPath
== null ? accountsStore
.addAccount(number
, null) : accountPath
;
161 var aciIdentityKey
= KeyUtils
.generateIdentityKeyPair();
162 var pniIdentityKey
= KeyUtils
.generateIdentityKeyPair();
163 var registrationId
= KeyHelper
.generateRegistrationId(false);
164 var pniRegistrationId
= KeyHelper
.generateRegistrationId(false);
166 var profileKey
= KeyUtils
.createProfileKey();
167 var account
= SignalAccount
.create(pathConfig
.dataPath(),
178 return new RegistrationManagerImpl(account
,
180 serviceEnvironmentConfig
,
183 new AccountFileUpdaterImpl(accountsStore
, newAccountPath
));
186 var account
= SignalAccount
.load(pathConfig
.dataPath(), accountPath
, true, settings
);
187 if (!number
.equals(account
.getNumber())) {
189 throw new IOException("Number in account file doesn't match expected number: " + account
.getNumber());
192 return new RegistrationManagerImpl(account
,
194 serviceEnvironmentConfig
,
197 new AccountFileUpdaterImpl(accountsStore
, accountPath
));