]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java
16a9f4cbbd6049d92a06b0f66d162c00665d41f8
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / storage / recipients / RecipientStore.java
1 package org.asamk.signal.manager.storage.recipients;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4
5 import org.asamk.signal.manager.api.Pair;
6 import org.asamk.signal.manager.storage.Utils;
7 import org.asamk.signal.manager.storage.contacts.ContactsStore;
8 import org.asamk.signal.manager.storage.profiles.ProfileStore;
9 import org.signal.zkgroup.InvalidInputException;
10 import org.signal.zkgroup.profiles.ProfileKey;
11 import org.signal.zkgroup.profiles.ProfileKeyCredential;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.whispersystems.signalservice.api.push.ACI;
15 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
16 import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
17 import org.whispersystems.signalservice.api.util.UuidUtil;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Base64;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Objects;
32 import java.util.Optional;
33 import java.util.Set;
34 import java.util.UUID;
35 import java.util.function.Supplier;
36 import java.util.stream.Collectors;
37
38 public class RecipientStore implements RecipientResolver, ContactsStore, ProfileStore {
39
40 private final static Logger logger = LoggerFactory.getLogger(RecipientStore.class);
41
42 private final ObjectMapper objectMapper;
43 private final File file;
44 private final RecipientMergeHandler recipientMergeHandler;
45
46 private final Map<RecipientId, Recipient> recipients;
47 private final Map<RecipientId, RecipientId> recipientsMerged = new HashMap<>();
48
49 private long lastId;
50
51 public static RecipientStore load(File file, RecipientMergeHandler recipientMergeHandler) throws IOException {
52 final var objectMapper = Utils.createStorageObjectMapper();
53 try (var inputStream = new FileInputStream(file)) {
54 final var storage = objectMapper.readValue(inputStream, Storage.class);
55 final var recipients = storage.recipients.stream().map(r -> {
56 final var recipientId = new RecipientId(r.id);
57 final var address = new RecipientAddress(Optional.ofNullable(r.uuid).map(UuidUtil::parseOrThrow),
58 Optional.ofNullable(r.number));
59
60 Contact contact = null;
61 if (r.contact != null) {
62 contact = new Contact(r.contact.name,
63 r.contact.color,
64 r.contact.messageExpirationTime,
65 r.contact.blocked,
66 r.contact.archived);
67 }
68
69 ProfileKey profileKey = null;
70 if (r.profileKey != null) {
71 try {
72 profileKey = new ProfileKey(Base64.getDecoder().decode(r.profileKey));
73 } catch (InvalidInputException ignored) {
74 }
75 }
76
77 ProfileKeyCredential profileKeyCredential = null;
78 if (r.profileKeyCredential != null) {
79 try {
80 profileKeyCredential = new ProfileKeyCredential(Base64.getDecoder()
81 .decode(r.profileKeyCredential));
82 } catch (Throwable ignored) {
83 }
84 }
85
86 Profile profile = null;
87 if (r.profile != null) {
88 profile = new Profile(r.profile.lastUpdateTimestamp,
89 r.profile.givenName,
90 r.profile.familyName,
91 r.profile.about,
92 r.profile.aboutEmoji,
93 r.profile.avatarUrlPath,
94 Profile.UnidentifiedAccessMode.valueOfOrUnknown(r.profile.unidentifiedAccessMode),
95 r.profile.capabilities.stream()
96 .map(Profile.Capability::valueOfOrNull)
97 .filter(Objects::nonNull)
98 .collect(Collectors.toSet()));
99 }
100
101 return new Recipient(recipientId, address, contact, profileKey, profileKeyCredential, profile);
102 }).collect(Collectors.toMap(Recipient::getRecipientId, r -> r));
103
104 return new RecipientStore(objectMapper, file, recipientMergeHandler, recipients, storage.lastId);
105 } catch (FileNotFoundException e) {
106 logger.debug("Creating new recipient store.");
107 return new RecipientStore(objectMapper, file, recipientMergeHandler, new HashMap<>(), 0);
108 }
109 }
110
111 private RecipientStore(
112 final ObjectMapper objectMapper,
113 final File file,
114 final RecipientMergeHandler recipientMergeHandler,
115 final Map<RecipientId, Recipient> recipients,
116 final long lastId
117 ) {
118 this.objectMapper = objectMapper;
119 this.file = file;
120 this.recipientMergeHandler = recipientMergeHandler;
121 this.recipients = recipients;
122 this.lastId = lastId;
123 }
124
125 public RecipientAddress resolveRecipientAddress(RecipientId recipientId) {
126 synchronized (recipients) {
127 return getRecipient(recipientId).getAddress();
128 }
129 }
130
131 public Recipient getRecipient(RecipientId recipientId) {
132 synchronized (recipients) {
133 return getRecipientLocked(recipientId);
134 }
135 }
136
137 @Override
138 public RecipientId resolveRecipient(ACI aci) {
139 return resolveRecipient(new RecipientAddress(aci == null ? null : aci.uuid()), false);
140 }
141
142 @Override
143 public RecipientId resolveRecipient(final String identifier) {
144 return resolveRecipient(Utils.getRecipientAddressFromIdentifier(identifier), false);
145 }
146
147 public RecipientId resolveRecipient(
148 final String number, Supplier<ACI> aciSupplier
149 ) throws UnregisteredUserException {
150 final Optional<Recipient> byNumber;
151 synchronized (recipients) {
152 byNumber = findByNumberLocked(number);
153 }
154 if (byNumber.isEmpty() || byNumber.get().getAddress().getUuid().isEmpty()) {
155 final var aci = aciSupplier.get();
156 if (aci == null) {
157 throw new UnregisteredUserException(number, null);
158 }
159
160 return resolveRecipient(new RecipientAddress(aci.uuid(), number), false);
161 }
162 return byNumber.get().getRecipientId();
163 }
164
165 public RecipientId resolveRecipient(RecipientAddress address) {
166 return resolveRecipient(address, false);
167 }
168
169 @Override
170 public RecipientId resolveRecipient(final SignalServiceAddress address) {
171 return resolveRecipient(new RecipientAddress(address), false);
172 }
173
174 public RecipientId resolveRecipientTrusted(RecipientAddress address) {
175 return resolveRecipient(address, true);
176 }
177
178 public RecipientId resolveRecipientTrusted(SignalServiceAddress address) {
179 return resolveRecipient(new RecipientAddress(address), true);
180 }
181
182 public List<RecipientId> resolveRecipientsTrusted(List<RecipientAddress> addresses) {
183 final List<RecipientId> recipientIds;
184 final List<Pair<RecipientId, RecipientId>> toBeMerged = new ArrayList<>();
185 synchronized (recipients) {
186 recipientIds = addresses.stream().map(address -> {
187 final var pair = resolveRecipientLocked(address, true);
188 if (pair.second().isPresent()) {
189 toBeMerged.add(new Pair<>(pair.first(), pair.second().get()));
190 }
191 return pair.first();
192 }).collect(Collectors.toList());
193 }
194 for (var pair : toBeMerged) {
195 recipientMergeHandler.mergeRecipients(pair.first(), pair.second());
196 }
197 return recipientIds;
198 }
199
200 @Override
201 public void storeContact(final RecipientId recipientId, final Contact contact) {
202 synchronized (recipients) {
203 final var recipient = recipients.get(recipientId);
204 storeRecipientLocked(recipientId, Recipient.newBuilder(recipient).withContact(contact).build());
205 }
206 }
207
208 @Override
209 public Contact getContact(final RecipientId recipientId) {
210 final var recipient = getRecipient(recipientId);
211 return recipient == null ? null : recipient.getContact();
212 }
213
214 @Override
215 public List<Pair<RecipientId, Contact>> getContacts() {
216 return recipients.entrySet()
217 .stream()
218 .filter(e -> e.getValue().getContact() != null)
219 .map(e -> new Pair<>(e.getKey(), e.getValue().getContact()))
220 .collect(Collectors.toList());
221 }
222
223 @Override
224 public void deleteContact(final RecipientId recipientId) {
225 synchronized (recipients) {
226 final var recipient = recipients.get(recipientId);
227 storeRecipientLocked(recipientId, Recipient.newBuilder(recipient).withContact(null).build());
228 }
229 }
230
231 public void deleteRecipientData(final RecipientId recipientId) {
232 synchronized (recipients) {
233 final var recipient = recipients.get(recipientId);
234 storeRecipientLocked(recipientId,
235 Recipient.newBuilder()
236 .withRecipientId(recipientId)
237 .withAddress(new RecipientAddress(recipient.getAddress().getUuid().orElse(null)))
238 .build());
239 }
240 }
241
242 @Override
243 public Profile getProfile(final RecipientId recipientId) {
244 final var recipient = getRecipient(recipientId);
245 return recipient == null ? null : recipient.getProfile();
246 }
247
248 @Override
249 public ProfileKey getProfileKey(final RecipientId recipientId) {
250 final var recipient = getRecipient(recipientId);
251 return recipient == null ? null : recipient.getProfileKey();
252 }
253
254 @Override
255 public ProfileKeyCredential getProfileKeyCredential(final RecipientId recipientId) {
256 final var recipient = getRecipient(recipientId);
257 return recipient == null ? null : recipient.getProfileKeyCredential();
258 }
259
260 @Override
261 public void storeProfile(final RecipientId recipientId, final Profile profile) {
262 synchronized (recipients) {
263 final var recipient = recipients.get(recipientId);
264 storeRecipientLocked(recipientId, Recipient.newBuilder(recipient).withProfile(profile).build());
265 }
266 }
267
268 @Override
269 public void storeProfileKey(final RecipientId recipientId, final ProfileKey profileKey) {
270 synchronized (recipients) {
271 final var recipient = recipients.get(recipientId);
272 if (profileKey != null && profileKey.equals(recipient.getProfileKey())) {
273 return;
274 }
275
276 final var newRecipient = Recipient.newBuilder(recipient)
277 .withProfileKey(profileKey)
278 .withProfileKeyCredential(null)
279 .withProfile(recipient.getProfile() == null
280 ? null
281 : Profile.newBuilder(recipient.getProfile()).withLastUpdateTimestamp(0).build())
282 .build();
283 storeRecipientLocked(recipientId, newRecipient);
284 }
285 }
286
287 @Override
288 public void storeProfileKeyCredential(
289 final RecipientId recipientId, final ProfileKeyCredential profileKeyCredential
290 ) {
291 synchronized (recipients) {
292 final var recipient = recipients.get(recipientId);
293 storeRecipientLocked(recipientId,
294 Recipient.newBuilder(recipient).withProfileKeyCredential(profileKeyCredential).build());
295 }
296 }
297
298 public boolean isEmpty() {
299 synchronized (recipients) {
300 return recipients.isEmpty();
301 }
302 }
303
304 /**
305 * @param isHighTrust true, if the number/uuid connection was obtained from a trusted source.
306 * Has no effect, if the address contains only a number or a uuid.
307 */
308 private RecipientId resolveRecipient(RecipientAddress address, boolean isHighTrust) {
309 final Pair<RecipientId, Optional<RecipientId>> pair;
310 synchronized (recipients) {
311 pair = resolveRecipientLocked(address, isHighTrust);
312 if (pair.second().isPresent()) {
313 recipientsMerged.put(pair.second().get(), pair.first());
314 }
315 }
316
317 if (pair.second().isPresent()) {
318 recipientMergeHandler.mergeRecipients(pair.first(), pair.second().get());
319 }
320 return pair.first();
321 }
322
323 private Pair<RecipientId, Optional<RecipientId>> resolveRecipientLocked(
324 RecipientAddress address, boolean isHighTrust
325 ) {
326 final var byNumber = address.getNumber().isEmpty()
327 ? Optional.<Recipient>empty()
328 : findByNumberLocked(address.getNumber().get());
329 final var byUuid = address.getUuid().isEmpty()
330 ? Optional.<Recipient>empty()
331 : findByUuidLocked(address.getUuid().get());
332
333 if (byNumber.isEmpty() && byUuid.isEmpty()) {
334 logger.debug("Got new recipient, both uuid and number are unknown");
335
336 if (isHighTrust || address.getUuid().isEmpty() || address.getNumber().isEmpty()) {
337 return new Pair<>(addNewRecipientLocked(address), Optional.empty());
338 }
339
340 return new Pair<>(addNewRecipientLocked(new RecipientAddress(address.getUuid().get())), Optional.empty());
341 }
342
343 if (!isHighTrust || address.getUuid().isEmpty() || address.getNumber().isEmpty() || byNumber.equals(byUuid)) {
344 return new Pair<>(byUuid.or(() -> byNumber).map(Recipient::getRecipientId).get(), Optional.empty());
345 }
346
347 if (byNumber.isEmpty()) {
348 logger.debug("Got recipient existing with uuid, updating with high trust number");
349 updateRecipientAddressLocked(byUuid.get().getRecipientId(), address);
350 return new Pair<>(byUuid.get().getRecipientId(), Optional.empty());
351 }
352
353 if (byUuid.isEmpty()) {
354 if (byNumber.get().getAddress().getUuid().isPresent()) {
355 logger.debug(
356 "Got recipient existing with number, but different uuid, so stripping its number and adding new recipient");
357
358 updateRecipientAddressLocked(byNumber.get().getRecipientId(),
359 new RecipientAddress(byNumber.get().getAddress().getUuid().get()));
360 return new Pair<>(addNewRecipientLocked(address), Optional.empty());
361 }
362
363 logger.debug("Got recipient existing with number and no uuid, updating with high trust uuid");
364 updateRecipientAddressLocked(byNumber.get().getRecipientId(), address);
365 return new Pair<>(byNumber.get().getRecipientId(), Optional.empty());
366 }
367
368 if (byNumber.get().getAddress().getUuid().isPresent()) {
369 logger.debug(
370 "Got separate recipients for high trust number and uuid, recipient for number has different uuid, so stripping its number");
371
372 updateRecipientAddressLocked(byNumber.get().getRecipientId(),
373 new RecipientAddress(byNumber.get().getAddress().getUuid().get()));
374 updateRecipientAddressLocked(byUuid.get().getRecipientId(), address);
375 return new Pair<>(byUuid.get().getRecipientId(), Optional.empty());
376 }
377
378 logger.debug("Got separate recipients for high trust number and uuid, need to merge them");
379 updateRecipientAddressLocked(byUuid.get().getRecipientId(), address);
380 mergeRecipientsLocked(byUuid.get().getRecipientId(), byNumber.get().getRecipientId());
381 return new Pair<>(byUuid.get().getRecipientId(), byNumber.map(Recipient::getRecipientId));
382 }
383
384 private RecipientId addNewRecipientLocked(final RecipientAddress address) {
385 final var nextRecipientId = nextIdLocked();
386 storeRecipientLocked(nextRecipientId, new Recipient(nextRecipientId, address, null, null, null, null));
387 return nextRecipientId;
388 }
389
390 private void updateRecipientAddressLocked(
391 final RecipientId recipientId, final RecipientAddress address
392 ) {
393 final var recipient = recipients.get(recipientId);
394 storeRecipientLocked(recipientId, Recipient.newBuilder(recipient).withAddress(address).build());
395 }
396
397 private Recipient getRecipientLocked(RecipientId recipientId) {
398 while (recipientsMerged.containsKey(recipientId)) {
399 recipientId = recipientsMerged.get(recipientId);
400 }
401 return recipients.get(recipientId);
402 }
403
404 private void storeRecipientLocked(
405 final RecipientId recipientId, final Recipient recipient
406 ) {
407 final var existingRecipient = getRecipientLocked(recipientId);
408 if (existingRecipient == null || !existingRecipient.equals(recipient)) {
409 recipients.put(recipientId, recipient);
410 saveLocked();
411 }
412 }
413
414 private void mergeRecipientsLocked(RecipientId recipientId, RecipientId toBeMergedRecipientId) {
415 final var recipient = recipients.get(recipientId);
416 final var toBeMergedRecipient = recipients.get(toBeMergedRecipientId);
417 recipients.put(recipientId,
418 new Recipient(recipientId,
419 recipient.getAddress(),
420 recipient.getContact() != null ? recipient.getContact() : toBeMergedRecipient.getContact(),
421 recipient.getProfileKey() != null
422 ? recipient.getProfileKey()
423 : toBeMergedRecipient.getProfileKey(),
424 recipient.getProfileKeyCredential() != null
425 ? recipient.getProfileKeyCredential()
426 : toBeMergedRecipient.getProfileKeyCredential(),
427 recipient.getProfile() != null ? recipient.getProfile() : toBeMergedRecipient.getProfile()));
428 recipients.remove(toBeMergedRecipientId);
429 saveLocked();
430 }
431
432 private Optional<Recipient> findByNumberLocked(final String number) {
433 return recipients.entrySet()
434 .stream()
435 .filter(entry -> entry.getValue().getAddress().getNumber().isPresent() && number.equals(entry.getValue()
436 .getAddress()
437 .getNumber()
438 .get()))
439 .findFirst()
440 .map(Map.Entry::getValue);
441 }
442
443 private Optional<Recipient> findByUuidLocked(final UUID uuid) {
444 return recipients.entrySet()
445 .stream()
446 .filter(entry -> entry.getValue().getAddress().getUuid().isPresent() && uuid.equals(entry.getValue()
447 .getAddress()
448 .getUuid()
449 .get()))
450 .findFirst()
451 .map(Map.Entry::getValue);
452 }
453
454 private RecipientId nextIdLocked() {
455 return new RecipientId(++this.lastId);
456 }
457
458 private void saveLocked() {
459 final var base64 = Base64.getEncoder();
460 var storage = new Storage(recipients.entrySet().stream().map(pair -> {
461 final var recipient = pair.getValue();
462 final var contact = recipient.getContact() == null
463 ? null
464 : new Storage.Recipient.Contact(recipient.getContact().getName(),
465 recipient.getContact().getColor(),
466 recipient.getContact().getMessageExpirationTime(),
467 recipient.getContact().isBlocked(),
468 recipient.getContact().isArchived());
469 final var profile = recipient.getProfile() == null
470 ? null
471 : new Storage.Recipient.Profile(recipient.getProfile().getLastUpdateTimestamp(),
472 recipient.getProfile().getGivenName(),
473 recipient.getProfile().getFamilyName(),
474 recipient.getProfile().getAbout(),
475 recipient.getProfile().getAboutEmoji(),
476 recipient.getProfile().getAvatarUrlPath(),
477 recipient.getProfile().getUnidentifiedAccessMode().name(),
478 recipient.getProfile()
479 .getCapabilities()
480 .stream()
481 .map(Enum::name)
482 .collect(Collectors.toSet()));
483 return new Storage.Recipient(pair.getKey().id(),
484 recipient.getAddress().getNumber().orElse(null),
485 recipient.getAddress().getUuid().map(UUID::toString).orElse(null),
486 recipient.getProfileKey() == null
487 ? null
488 : base64.encodeToString(recipient.getProfileKey().serialize()),
489 recipient.getProfileKeyCredential() == null
490 ? null
491 : base64.encodeToString(recipient.getProfileKeyCredential().serialize()),
492 contact,
493 profile);
494 }).collect(Collectors.toList()), lastId);
495
496 // Write to memory first to prevent corrupting the file in case of serialization errors
497 try (var inMemoryOutput = new ByteArrayOutputStream()) {
498 objectMapper.writeValue(inMemoryOutput, storage);
499
500 var input = new ByteArrayInputStream(inMemoryOutput.toByteArray());
501 try (var outputStream = new FileOutputStream(file)) {
502 input.transferTo(outputStream);
503 }
504 } catch (Exception e) {
505 logger.error("Error saving recipient store file: {}", e.getMessage());
506 }
507 }
508
509 private record Storage(List<Recipient> recipients, long lastId) {
510
511 private record Recipient(
512 long id,
513 String number,
514 String uuid,
515 String profileKey,
516 String profileKeyCredential,
517 Storage.Recipient.Contact contact,
518 Storage.Recipient.Profile profile
519 ) {
520
521 private record Contact(
522 String name, String color, int messageExpirationTime, boolean blocked, boolean archived
523 ) {}
524
525 private record Profile(
526 long lastUpdateTimestamp,
527 String givenName,
528 String familyName,
529 String about,
530 String aboutEmoji,
531 String avatarUrlPath,
532 String unidentifiedAccessMode,
533 Set<String> capabilities
534 ) {}
535 }
536 }
537
538 public interface RecipientMergeHandler {
539
540 void mergeRecipients(RecipientId recipientId, RecipientId toBeMergedRecipientId);
541 }
542 }