]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/helper/StorageHelper.java
113a69209b257f435871afbb70423817f678031a
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / helper / StorageHelper.java
1 package org.asamk.signal.manager.helper;
2
3 import org.asamk.signal.manager.api.Contact;
4 import org.asamk.signal.manager.api.GroupId;
5 import org.asamk.signal.manager.api.PhoneNumberSharingMode;
6 import org.asamk.signal.manager.api.Profile;
7 import org.asamk.signal.manager.api.TrustLevel;
8 import org.asamk.signal.manager.internal.SignalDependencies;
9 import org.asamk.signal.manager.storage.SignalAccount;
10 import org.asamk.signal.manager.storage.recipients.RecipientAddress;
11 import org.signal.libsignal.protocol.IdentityKey;
12 import org.signal.libsignal.protocol.InvalidKeyException;
13 import org.signal.libsignal.zkgroup.InvalidInputException;
14 import org.signal.libsignal.zkgroup.groups.GroupMasterKey;
15 import org.signal.libsignal.zkgroup.profiles.ProfileKey;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.whispersystems.signalservice.api.storage.SignalAccountRecord;
19 import org.whispersystems.signalservice.api.storage.SignalStorageManifest;
20 import org.whispersystems.signalservice.api.storage.SignalStorageRecord;
21 import org.whispersystems.signalservice.api.storage.StorageId;
22 import org.whispersystems.signalservice.internal.storage.protos.ManifestRecord;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.stream.Collectors;
31
32 public class StorageHelper {
33
34 private final static Logger logger = LoggerFactory.getLogger(StorageHelper.class);
35
36 private final SignalAccount account;
37 private final SignalDependencies dependencies;
38 private final Context context;
39
40 public StorageHelper(final Context context) {
41 this.account = context.getAccount();
42 this.dependencies = context.getDependencies();
43 this.context = context;
44 }
45
46 public void readDataFromStorage() throws IOException {
47 final var storageKey = account.getOrCreateStorageKey();
48 if (storageKey == null) {
49 logger.debug("Storage key unknown, requesting from primary device.");
50 context.getSyncHelper().requestSyncKeys();
51 return;
52 }
53
54 logger.debug("Reading data from remote storage");
55 Optional<SignalStorageManifest> manifest;
56 try {
57 manifest = dependencies.getAccountManager()
58 .getStorageManifestIfDifferentVersion(storageKey, account.getStorageManifestVersion());
59 } catch (InvalidKeyException e) {
60 logger.warn("Manifest couldn't be decrypted, ignoring.");
61 return;
62 }
63
64 if (manifest.isEmpty()) {
65 logger.debug("Manifest is up to date, does not exist or couldn't be decrypted, ignoring.");
66 return;
67 }
68
69 logger.trace("Remote storage manifest has {} records", manifest.get().getStorageIds().size());
70 final var storageIds = manifest.get()
71 .getStorageIds()
72 .stream()
73 .filter(id -> !id.isUnknown())
74 .collect(Collectors.toSet());
75
76 Optional<SignalStorageManifest> localManifest = account.getStorageManifest();
77 localManifest.ifPresent(m -> m.getStorageIds().forEach(storageIds::remove));
78
79 logger.trace("Reading {} new records", manifest.get().getStorageIds().size());
80 for (final var record : getSignalStorageRecords(storageIds)) {
81 logger.debug("Reading record of type {}", record.getType());
82 if (record.getType() == ManifestRecord.Identifier.Type.ACCOUNT.getValue()) {
83 readAccountRecord(record);
84 } else if (record.getType() == ManifestRecord.Identifier.Type.GROUPV2.getValue()) {
85 readGroupV2Record(record);
86 } else if (record.getType() == ManifestRecord.Identifier.Type.GROUPV1.getValue()) {
87 readGroupV1Record(record);
88 } else if (record.getType() == ManifestRecord.Identifier.Type.CONTACT.getValue()) {
89 readContactRecord(record);
90 }
91 }
92 account.setStorageManifestVersion(manifest.get().getVersion());
93 account.setStorageManifest(manifest.get());
94 logger.debug("Done reading data from remote storage");
95 }
96
97 private void readContactRecord(final SignalStorageRecord record) {
98 if (record == null || record.getContact().isEmpty()) {
99 return;
100 }
101
102 final var contactRecord = record.getContact().get();
103 final var aci = contactRecord.getAci().orElse(null);
104 final var pni = contactRecord.getPni().orElse(null);
105 if (contactRecord.getNumber().isEmpty() && aci == null && pni == null) {
106 return;
107 }
108 final var address = new RecipientAddress(aci, pni, contactRecord.getNumber().orElse(null));
109 var recipientId = account.getRecipientResolver().resolveRecipient(address);
110 if (aci != null && contactRecord.getUsername().isPresent()) {
111 recipientId = account.getRecipientTrustedResolver()
112 .resolveRecipientTrusted(aci, contactRecord.getUsername().get());
113 }
114
115 final var contact = account.getContactStore().getContact(recipientId);
116 final var blocked = contact != null && contact.isBlocked();
117 final var profileShared = contact != null && contact.isProfileSharingEnabled();
118 final var archived = contact != null && contact.isArchived();
119 final var contactGivenName = contact == null ? null : contact.getGivenName();
120 final var contactFamilyName = contact == null ? null : contact.getFamilyName();
121 if (blocked != contactRecord.isBlocked()
122 || profileShared != contactRecord.isProfileSharingEnabled()
123 || archived != contactRecord.isArchived()
124 || (
125 contactRecord.getSystemGivenName().isPresent() && !contactRecord.getSystemGivenName()
126 .get()
127 .equals(contactGivenName)
128 )
129 || (
130 contactRecord.getSystemFamilyName().isPresent() && !contactRecord.getSystemFamilyName()
131 .get()
132 .equals(contactFamilyName)
133 )) {
134 logger.debug("Storing new or updated contact {}", recipientId);
135 final var contactBuilder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact);
136 final var newContact = contactBuilder.withBlocked(contactRecord.isBlocked())
137 .withProfileSharingEnabled(contactRecord.isProfileSharingEnabled())
138 .withArchived(contactRecord.isArchived());
139 if (contactRecord.getSystemGivenName().isPresent() || contactRecord.getSystemFamilyName().isPresent()) {
140 newContact.withGivenName(contactRecord.getSystemGivenName().orElse(null))
141 .withFamilyName(contactRecord.getSystemFamilyName().orElse(null));
142 }
143 account.getContactStore().storeContact(recipientId, newContact.build());
144 }
145
146 final var profile = account.getProfileStore().getProfile(recipientId);
147 final var profileGivenName = profile == null ? null : profile.getGivenName();
148 final var profileFamilyName = profile == null ? null : profile.getFamilyName();
149 if ((
150 contactRecord.getProfileGivenName().isPresent() && !contactRecord.getProfileGivenName()
151 .get()
152 .equals(profileGivenName)
153 ) || (
154 contactRecord.getProfileFamilyName().isPresent() && !contactRecord.getProfileFamilyName()
155 .get()
156 .equals(profileFamilyName)
157 )) {
158 final var profileBuilder = profile == null ? Profile.newBuilder() : Profile.newBuilder(profile);
159 final var newProfile = profileBuilder.withGivenName(contactRecord.getProfileGivenName().orElse(null))
160 .withFamilyName(contactRecord.getProfileFamilyName().orElse(null))
161 .build();
162 account.getProfileStore().storeProfile(recipientId, newProfile);
163 }
164 if (contactRecord.getProfileKey().isPresent()) {
165 try {
166 logger.trace("Storing profile key {}", recipientId);
167 final var profileKey = new ProfileKey(contactRecord.getProfileKey().get());
168 account.getProfileStore().storeProfileKey(recipientId, profileKey);
169 } catch (InvalidInputException e) {
170 logger.warn("Received invalid contact profile key from storage");
171 }
172 }
173 if (contactRecord.getIdentityKey().isPresent() && aci != null) {
174 try {
175 logger.trace("Storing identity key {}", recipientId);
176 final var identityKey = new IdentityKey(contactRecord.getIdentityKey().get());
177 account.getIdentityKeyStore().saveIdentity(aci, identityKey);
178
179 final var trustLevel = TrustLevel.fromIdentityState(contactRecord.getIdentityState());
180 if (trustLevel != null) {
181 account.getIdentityKeyStore().setIdentityTrustLevel(aci, identityKey, trustLevel);
182 }
183 } catch (InvalidKeyException e) {
184 logger.warn("Received invalid contact identity key from storage");
185 }
186 }
187 }
188
189 private void readGroupV1Record(final SignalStorageRecord record) {
190 if (record == null || record.getGroupV1().isEmpty()) {
191 return;
192 }
193
194 final var groupV1Record = record.getGroupV1().get();
195 final var groupIdV1 = GroupId.v1(groupV1Record.getGroupId());
196
197 var group = account.getGroupStore().getGroup(groupIdV1);
198 if (group == null) {
199 try {
200 context.getGroupHelper().sendGroupInfoRequest(groupIdV1, account.getSelfRecipientId());
201 } catch (Throwable e) {
202 logger.warn("Failed to send group request", e);
203 }
204 group = account.getGroupStore().getOrCreateGroupV1(groupIdV1);
205 }
206 if (group != null && group.isBlocked() != groupV1Record.isBlocked()) {
207 group.setBlocked(groupV1Record.isBlocked());
208 account.getGroupStore().updateGroup(group);
209 }
210 }
211
212 private void readGroupV2Record(final SignalStorageRecord record) {
213 if (record == null || record.getGroupV2().isEmpty()) {
214 return;
215 }
216
217 final var groupV2Record = record.getGroupV2().get();
218 if (groupV2Record.isArchived()) {
219 return;
220 }
221
222 final GroupMasterKey groupMasterKey;
223 try {
224 groupMasterKey = new GroupMasterKey(groupV2Record.getMasterKeyBytes());
225 } catch (InvalidInputException e) {
226 logger.warn("Received invalid group master key from storage");
227 return;
228 }
229
230 final var group = context.getGroupHelper().getOrMigrateGroup(groupMasterKey, 0, null);
231 if (group.isBlocked() != groupV2Record.isBlocked()) {
232 group.setBlocked(groupV2Record.isBlocked());
233 account.getGroupStore().updateGroup(group);
234 }
235 }
236
237 private void readAccountRecord(final SignalStorageRecord record) throws IOException {
238 if (record == null) {
239 logger.warn("Could not find account record, even though we had an ID, ignoring.");
240 return;
241 }
242
243 SignalAccountRecord accountRecord = record.getAccount().orElse(null);
244 if (accountRecord == null) {
245 logger.warn("The storage record didn't actually have an account, ignoring.");
246 return;
247 }
248
249 if (!accountRecord.getE164().equals(account.getNumber())) {
250 context.getAccountHelper().checkWhoAmiI();
251 }
252
253 account.getConfigurationStore().setReadReceipts(accountRecord.isReadReceiptsEnabled());
254 account.getConfigurationStore().setTypingIndicators(accountRecord.isTypingIndicatorsEnabled());
255 account.getConfigurationStore()
256 .setUnidentifiedDeliveryIndicators(accountRecord.isSealedSenderIndicatorsEnabled());
257 account.getConfigurationStore().setLinkPreviews(accountRecord.isLinkPreviewsEnabled());
258 account.getConfigurationStore().setPhoneNumberSharingMode(switch (accountRecord.getPhoneNumberSharingMode()) {
259 case EVERYBODY -> PhoneNumberSharingMode.EVERYBODY;
260 case NOBODY -> PhoneNumberSharingMode.NOBODY;
261 case CONTACTS_ONLY -> PhoneNumberSharingMode.CONTACTS;
262 });
263 account.getConfigurationStore().setPhoneNumberUnlisted(accountRecord.isPhoneNumberUnlisted());
264 account.setUsername(accountRecord.getUsername());
265
266 if (accountRecord.getProfileKey().isPresent()) {
267 ProfileKey profileKey;
268 try {
269 profileKey = new ProfileKey(accountRecord.getProfileKey().get());
270 } catch (InvalidInputException e) {
271 logger.warn("Received invalid profile key from storage");
272 profileKey = null;
273 }
274 if (profileKey != null) {
275 account.setProfileKey(profileKey);
276 final var avatarPath = accountRecord.getAvatarUrlPath().orElse(null);
277 context.getProfileHelper().downloadProfileAvatar(account.getSelfRecipientId(), avatarPath, profileKey);
278 }
279 }
280
281 context.getProfileHelper()
282 .setProfile(false,
283 false,
284 accountRecord.getGivenName().orElse(null),
285 accountRecord.getFamilyName().orElse(null),
286 null,
287 null,
288 null,
289 null);
290 }
291
292 private SignalStorageRecord getSignalStorageRecord(final StorageId accountId) throws IOException {
293 List<SignalStorageRecord> records;
294 try {
295 records = dependencies.getAccountManager()
296 .readStorageRecords(account.getStorageKey(), Collections.singletonList(accountId));
297 } catch (InvalidKeyException e) {
298 logger.warn("Failed to read storage records, ignoring.");
299 return null;
300 }
301 return !records.isEmpty() ? records.get(0) : null;
302 }
303
304 private List<SignalStorageRecord> getSignalStorageRecords(final Collection<StorageId> storageIds) throws IOException {
305 List<SignalStorageRecord> records;
306 try {
307 records = dependencies.getAccountManager()
308 .readStorageRecords(account.getStorageKey(), new ArrayList<>(storageIds));
309 } catch (InvalidKeyException e) {
310 logger.warn("Failed to read storage records, ignoring.");
311 return List.of();
312 }
313 return records;
314 }
315 }