]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java
2fb3ae568cde6ad329762d0586553f89d35735dc
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / ManagerImpl.java
1 /*
2 Copyright (C) 2015-2021 AsamK and contributors
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 package org.asamk.signal.manager;
18
19 import org.asamk.signal.manager.actions.HandleAction;
20 import org.asamk.signal.manager.api.Configuration;
21 import org.asamk.signal.manager.api.Device;
22 import org.asamk.signal.manager.api.Group;
23 import org.asamk.signal.manager.api.Identity;
24 import org.asamk.signal.manager.api.InactiveGroupLinkException;
25 import org.asamk.signal.manager.api.InvalidDeviceLinkException;
26 import org.asamk.signal.manager.api.Message;
27 import org.asamk.signal.manager.api.Pair;
28 import org.asamk.signal.manager.api.RecipientIdentifier;
29 import org.asamk.signal.manager.api.SendGroupMessageResults;
30 import org.asamk.signal.manager.api.SendMessageResult;
31 import org.asamk.signal.manager.api.SendMessageResults;
32 import org.asamk.signal.manager.api.TypingAction;
33 import org.asamk.signal.manager.api.UpdateGroup;
34 import org.asamk.signal.manager.config.ServiceConfig;
35 import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
36 import org.asamk.signal.manager.groups.GroupId;
37 import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
38 import org.asamk.signal.manager.groups.GroupNotFoundException;
39 import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
40 import org.asamk.signal.manager.groups.LastGroupAdminException;
41 import org.asamk.signal.manager.groups.NotAGroupMemberException;
42 import org.asamk.signal.manager.helper.AttachmentHelper;
43 import org.asamk.signal.manager.helper.ContactHelper;
44 import org.asamk.signal.manager.helper.GroupHelper;
45 import org.asamk.signal.manager.helper.GroupV2Helper;
46 import org.asamk.signal.manager.helper.IdentityHelper;
47 import org.asamk.signal.manager.helper.IncomingMessageHandler;
48 import org.asamk.signal.manager.helper.PinHelper;
49 import org.asamk.signal.manager.helper.PreKeyHelper;
50 import org.asamk.signal.manager.helper.ProfileHelper;
51 import org.asamk.signal.manager.helper.SendHelper;
52 import org.asamk.signal.manager.helper.StorageHelper;
53 import org.asamk.signal.manager.helper.SyncHelper;
54 import org.asamk.signal.manager.helper.UnidentifiedAccessHelper;
55 import org.asamk.signal.manager.jobs.Context;
56 import org.asamk.signal.manager.storage.SignalAccount;
57 import org.asamk.signal.manager.storage.groups.GroupInfo;
58 import org.asamk.signal.manager.storage.identities.IdentityInfo;
59 import org.asamk.signal.manager.storage.messageCache.CachedMessage;
60 import org.asamk.signal.manager.storage.recipients.Contact;
61 import org.asamk.signal.manager.storage.recipients.Profile;
62 import org.asamk.signal.manager.storage.recipients.RecipientAddress;
63 import org.asamk.signal.manager.storage.recipients.RecipientId;
64 import org.asamk.signal.manager.storage.stickers.Sticker;
65 import org.asamk.signal.manager.storage.stickers.StickerPackId;
66 import org.asamk.signal.manager.util.KeyUtils;
67 import org.asamk.signal.manager.util.StickerUtils;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70 import org.whispersystems.libsignal.InvalidKeyException;
71 import org.whispersystems.libsignal.ecc.ECPublicKey;
72 import org.whispersystems.libsignal.util.guava.Optional;
73 import org.whispersystems.signalservice.api.SignalSessionLock;
74 import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
75 import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
76 import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
77 import org.whispersystems.signalservice.api.messages.SignalServiceTypingMessage;
78 import org.whispersystems.signalservice.api.push.ACI;
79 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
80 import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
81 import org.whispersystems.signalservice.api.util.DeviceNameUtil;
82 import org.whispersystems.signalservice.api.util.InvalidNumberException;
83 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
84 import org.whispersystems.signalservice.api.websocket.WebSocketConnectionState;
85 import org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException;
86 import org.whispersystems.signalservice.internal.contacts.crypto.Quote;
87 import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedQuoteException;
88 import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException;
89 import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider;
90 import org.whispersystems.signalservice.internal.util.Hex;
91 import org.whispersystems.signalservice.internal.util.Util;
92
93 import java.io.File;
94 import java.io.IOException;
95 import java.net.URI;
96 import java.net.URISyntaxException;
97 import java.net.URLEncoder;
98 import java.nio.charset.StandardCharsets;
99 import java.security.SignatureException;
100 import java.time.Duration;
101 import java.util.ArrayList;
102 import java.util.Collection;
103 import java.util.HashMap;
104 import java.util.HashSet;
105 import java.util.List;
106 import java.util.Map;
107 import java.util.Set;
108 import java.util.UUID;
109 import java.util.concurrent.ExecutorService;
110 import java.util.concurrent.Executors;
111 import java.util.concurrent.TimeUnit;
112 import java.util.concurrent.TimeoutException;
113 import java.util.concurrent.locks.ReentrantLock;
114 import java.util.stream.Collectors;
115 import java.util.stream.Stream;
116
117 import io.reactivex.rxjava3.core.Observable;
118 import io.reactivex.rxjava3.schedulers.Schedulers;
119
120 import static org.asamk.signal.manager.config.ServiceConfig.capabilities;
121
122 public class ManagerImpl implements Manager {
123
124 private final static Logger logger = LoggerFactory.getLogger(ManagerImpl.class);
125
126 private final ServiceEnvironmentConfig serviceEnvironmentConfig;
127 private final SignalDependencies dependencies;
128
129 private SignalAccount account;
130
131 private final ExecutorService executor = Executors.newCachedThreadPool();
132
133 private final ProfileHelper profileHelper;
134 private final PinHelper pinHelper;
135 private final StorageHelper storageHelper;
136 private final SendHelper sendHelper;
137 private final SyncHelper syncHelper;
138 private final AttachmentHelper attachmentHelper;
139 private final GroupHelper groupHelper;
140 private final ContactHelper contactHelper;
141 private final IncomingMessageHandler incomingMessageHandler;
142 private final PreKeyHelper preKeyHelper;
143 private final IdentityHelper identityHelper;
144
145 private final Context context;
146 private boolean hasCaughtUpWithOldMessages = false;
147 private boolean ignoreAttachments = false;
148
149 private Thread receiveThread;
150 private final Set<ReceiveMessageHandler> weakHandlers = new HashSet<>();
151 private final Set<ReceiveMessageHandler> messageHandlers = new HashSet<>();
152 private final List<Runnable> closedListeners = new ArrayList<>();
153 private boolean isReceivingSynchronous;
154 private boolean needsToRetryFailedMessages = false;
155
156 ManagerImpl(
157 SignalAccount account,
158 PathConfig pathConfig,
159 ServiceEnvironmentConfig serviceEnvironmentConfig,
160 String userAgent
161 ) {
162 this.account = account;
163 this.serviceEnvironmentConfig = serviceEnvironmentConfig;
164
165 final var credentialsProvider = new DynamicCredentialsProvider(account.getAci(),
166 account.getAccount(),
167 account.getPassword(),
168 account.getDeviceId());
169 final var sessionLock = new SignalSessionLock() {
170 private final ReentrantLock LEGACY_LOCK = new ReentrantLock();
171
172 @Override
173 public Lock acquire() {
174 LEGACY_LOCK.lock();
175 return LEGACY_LOCK::unlock;
176 }
177 };
178 this.dependencies = new SignalDependencies(serviceEnvironmentConfig,
179 userAgent,
180 credentialsProvider,
181 account.getSignalProtocolStore(),
182 executor,
183 sessionLock);
184 final var avatarStore = new AvatarStore(pathConfig.avatarsPath());
185 final var attachmentStore = new AttachmentStore(pathConfig.attachmentsPath());
186 final var stickerPackStore = new StickerPackStore(pathConfig.stickerPacksPath());
187
188 this.attachmentHelper = new AttachmentHelper(dependencies, attachmentStore);
189 this.pinHelper = new PinHelper(dependencies.getKeyBackupService());
190 final var unidentifiedAccessHelper = new UnidentifiedAccessHelper(account,
191 dependencies,
192 account::getProfileKey,
193 this::getRecipientProfile);
194 this.profileHelper = new ProfileHelper(account,
195 dependencies,
196 avatarStore,
197 unidentifiedAccessHelper::getAccessFor,
198 this::resolveSignalServiceAddress);
199 final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper::getRecipientProfileKeyCredential,
200 profileHelper::getRecipientProfile,
201 account::getSelfRecipientId,
202 dependencies.getGroupsV2Operations(),
203 dependencies.getGroupsV2Api(),
204 this::resolveSignalServiceAddress);
205 this.sendHelper = new SendHelper(account,
206 dependencies,
207 unidentifiedAccessHelper,
208 this::resolveSignalServiceAddress,
209 account.getRecipientStore(),
210 this::handleIdentityFailure,
211 this::getGroupInfo,
212 profileHelper::getRecipientProfile,
213 this::refreshRegisteredUser);
214 this.groupHelper = new GroupHelper(account,
215 dependencies,
216 attachmentHelper,
217 sendHelper,
218 groupV2Helper,
219 avatarStore,
220 this::resolveSignalServiceAddress,
221 account.getRecipientStore());
222 this.storageHelper = new StorageHelper(account, dependencies, groupHelper, profileHelper);
223 this.contactHelper = new ContactHelper(account);
224 this.syncHelper = new SyncHelper(account,
225 attachmentHelper,
226 sendHelper,
227 groupHelper,
228 avatarStore,
229 this::resolveSignalServiceAddress);
230 preKeyHelper = new PreKeyHelper(account, dependencies);
231
232 this.context = new Context(account,
233 dependencies,
234 stickerPackStore,
235 sendHelper,
236 groupHelper,
237 syncHelper,
238 profileHelper,
239 storageHelper,
240 preKeyHelper);
241 var jobExecutor = new JobExecutor(context);
242
243 this.incomingMessageHandler = new IncomingMessageHandler(account,
244 dependencies,
245 account.getRecipientStore(),
246 this::resolveSignalServiceAddress,
247 groupHelper,
248 contactHelper,
249 attachmentHelper,
250 syncHelper,
251 profileHelper::getRecipientProfile,
252 jobExecutor);
253 this.identityHelper = new IdentityHelper(account,
254 dependencies,
255 this::resolveSignalServiceAddress,
256 syncHelper,
257 profileHelper);
258 }
259
260 @Override
261 public String getSelfNumber() {
262 return account.getAccount();
263 }
264
265 @Override
266 public void checkAccountState() throws IOException {
267 if (account.getLastReceiveTimestamp() == 0) {
268 logger.info("The Signal protocol expects that incoming messages are regularly received.");
269 } else {
270 var diffInMilliseconds = System.currentTimeMillis() - account.getLastReceiveTimestamp();
271 long days = TimeUnit.DAYS.convert(diffInMilliseconds, TimeUnit.MILLISECONDS);
272 if (days > 7) {
273 logger.warn(
274 "Messages have been last received {} days ago. The Signal protocol expects that incoming messages are regularly received.",
275 days);
276 }
277 }
278 try {
279 preKeyHelper.refreshPreKeysIfNecessary();
280 if (account.getAci() == null) {
281 account.setAci(ACI.parseOrNull(dependencies.getAccountManager().getWhoAmI().getAci()));
282 }
283 updateAccountAttributes(null);
284 } catch (AuthorizationFailedException e) {
285 account.setRegistered(false);
286 throw e;
287 }
288 }
289
290 /**
291 * This is used for checking a set of phone numbers for registration on Signal
292 *
293 * @param numbers The set of phone number in question
294 * @return A map of numbers to canonicalized number and uuid. If a number is not registered the uuid is null.
295 * @throws IOException if it's unable to get the contacts to check if they're registered
296 */
297 @Override
298 public Map<String, Pair<String, UUID>> areUsersRegistered(Set<String> numbers) throws IOException {
299 Map<String, String> canonicalizedNumbers = numbers.stream().collect(Collectors.toMap(n -> n, n -> {
300 try {
301 final var canonicalizedNumber = PhoneNumberFormatter.formatNumber(n, account.getAccount());
302 if (!canonicalizedNumber.equals(n)) {
303 logger.debug("Normalized number {} to {}.", n, canonicalizedNumber);
304 }
305 return canonicalizedNumber;
306 } catch (InvalidNumberException e) {
307 return "";
308 }
309 }));
310
311 // Note "registeredUsers" has no optionals. It only gives us info on users who are registered
312 var registeredUsers = getRegisteredUsers(canonicalizedNumbers.values()
313 .stream()
314 .filter(s -> !s.isEmpty())
315 .collect(Collectors.toSet()));
316
317 return numbers.stream().collect(Collectors.toMap(n -> n, n -> {
318 final var number = canonicalizedNumbers.get(n);
319 final var aci = registeredUsers.get(number);
320 return new Pair<>(number.isEmpty() ? null : number, aci == null ? null : aci.uuid());
321 }));
322 }
323
324 @Override
325 public void updateAccountAttributes(String deviceName) throws IOException {
326 final String encryptedDeviceName;
327 if (deviceName == null) {
328 encryptedDeviceName = account.getEncryptedDeviceName();
329 } else {
330 final var privateKey = account.getIdentityKeyPair().getPrivateKey();
331 encryptedDeviceName = DeviceNameUtil.encryptDeviceName(deviceName, privateKey);
332 account.setEncryptedDeviceName(encryptedDeviceName);
333 }
334 dependencies.getAccountManager()
335 .setAccountAttributes(encryptedDeviceName,
336 null,
337 account.getLocalRegistrationId(),
338 true,
339 null,
340 account.getPinMasterKey() == null ? null : account.getPinMasterKey().deriveRegistrationLock(),
341 account.getSelfUnidentifiedAccessKey(),
342 account.isUnrestrictedUnidentifiedAccess(),
343 capabilities,
344 account.isDiscoverableByPhoneNumber());
345 }
346
347 @Override
348 public Configuration getConfiguration() {
349 final var configurationStore = account.getConfigurationStore();
350 return new Configuration(java.util.Optional.ofNullable(configurationStore.getReadReceipts()),
351 java.util.Optional.ofNullable(configurationStore.getUnidentifiedDeliveryIndicators()),
352 java.util.Optional.ofNullable(configurationStore.getTypingIndicators()),
353 java.util.Optional.ofNullable(configurationStore.getLinkPreviews()));
354 }
355
356 @Override
357 public void updateConfiguration(
358 Configuration configuration
359 ) throws IOException, NotMasterDeviceException {
360 if (!account.isMasterDevice()) {
361 throw new NotMasterDeviceException();
362 }
363
364 final var configurationStore = account.getConfigurationStore();
365 if (configuration.readReceipts().isPresent()) {
366 configurationStore.setReadReceipts(configuration.readReceipts().get());
367 }
368 if (configuration.unidentifiedDeliveryIndicators().isPresent()) {
369 configurationStore.setUnidentifiedDeliveryIndicators(configuration.unidentifiedDeliveryIndicators().get());
370 }
371 if (configuration.typingIndicators().isPresent()) {
372 configurationStore.setTypingIndicators(configuration.typingIndicators().get());
373 }
374 if (configuration.linkPreviews().isPresent()) {
375 configurationStore.setLinkPreviews(configuration.linkPreviews().get());
376 }
377 syncHelper.sendConfigurationMessage();
378 }
379
380 /**
381 * @param givenName if null, the previous givenName will be kept
382 * @param familyName if null, the previous familyName will be kept
383 * @param about if null, the previous about text will be kept
384 * @param aboutEmoji if null, the previous about emoji will be kept
385 * @param avatar if avatar is null the image from the local avatar store is used (if present),
386 */
387 @Override
388 public void setProfile(
389 String givenName, final String familyName, String about, String aboutEmoji, java.util.Optional<File> avatar
390 ) throws IOException {
391 profileHelper.setProfile(givenName,
392 familyName,
393 about,
394 aboutEmoji,
395 avatar == null ? null : Optional.fromNullable(avatar.orElse(null)));
396 syncHelper.sendSyncFetchProfileMessage();
397 }
398
399 @Override
400 public void unregister() throws IOException {
401 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
402 // If this is the master device, other users can't send messages to this number anymore.
403 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
404 dependencies.getAccountManager().setGcmId(Optional.absent());
405
406 account.setRegistered(false);
407 close();
408 }
409
410 @Override
411 public void deleteAccount() throws IOException {
412 try {
413 pinHelper.removeRegistrationLockPin();
414 } catch (IOException e) {
415 logger.warn("Failed to remove registration lock pin");
416 }
417 account.setRegistrationLockPin(null, null);
418
419 dependencies.getAccountManager().deleteAccount();
420
421 account.setRegistered(false);
422 close();
423 }
424
425 @Override
426 public void submitRateLimitRecaptchaChallenge(String challenge, String captcha) throws IOException {
427 captcha = captcha == null ? null : captcha.replace("signalcaptcha://", "");
428
429 dependencies.getAccountManager().submitRateLimitRecaptchaChallenge(challenge, captcha);
430 }
431
432 @Override
433 public List<Device> getLinkedDevices() throws IOException {
434 var devices = dependencies.getAccountManager().getDevices();
435 account.setMultiDevice(devices.size() > 1);
436 var identityKey = account.getIdentityKeyPair().getPrivateKey();
437 return devices.stream().map(d -> {
438 String deviceName = d.getName();
439 if (deviceName != null) {
440 try {
441 deviceName = DeviceNameUtil.decryptDeviceName(deviceName, identityKey);
442 } catch (IOException e) {
443 logger.debug("Failed to decrypt device name, maybe plain text?", e);
444 }
445 }
446 return new Device(d.getId(),
447 deviceName,
448 d.getCreated(),
449 d.getLastSeen(),
450 d.getId() == account.getDeviceId());
451 }).toList();
452 }
453
454 @Override
455 public void removeLinkedDevices(long deviceId) throws IOException {
456 dependencies.getAccountManager().removeDevice(deviceId);
457 var devices = dependencies.getAccountManager().getDevices();
458 account.setMultiDevice(devices.size() > 1);
459 }
460
461 @Override
462 public void addDeviceLink(URI linkUri) throws IOException, InvalidDeviceLinkException {
463 var info = DeviceLinkInfo.parseDeviceLinkUri(linkUri);
464
465 addDevice(info.deviceIdentifier(), info.deviceKey());
466 }
467
468 private void addDevice(
469 String deviceIdentifier, ECPublicKey deviceKey
470 ) throws IOException, InvalidDeviceLinkException {
471 var identityKeyPair = account.getIdentityKeyPair();
472 var verificationCode = dependencies.getAccountManager().getNewDeviceVerificationCode();
473
474 try {
475 dependencies.getAccountManager()
476 .addDevice(deviceIdentifier,
477 deviceKey,
478 identityKeyPair,
479 Optional.of(account.getProfileKey().serialize()),
480 verificationCode);
481 } catch (InvalidKeyException e) {
482 throw new InvalidDeviceLinkException("Invalid device link", e);
483 }
484 account.setMultiDevice(true);
485 }
486
487 @Override
488 public void setRegistrationLockPin(java.util.Optional<String> pin) throws IOException {
489 if (!account.isMasterDevice()) {
490 throw new RuntimeException("Only master device can set a PIN");
491 }
492 if (pin.isPresent()) {
493 final var masterKey = account.getPinMasterKey() != null
494 ? account.getPinMasterKey()
495 : KeyUtils.createMasterKey();
496
497 pinHelper.setRegistrationLockPin(pin.get(), masterKey);
498
499 account.setRegistrationLockPin(pin.get(), masterKey);
500 } else {
501 // Remove KBS Pin
502 pinHelper.removeRegistrationLockPin();
503
504 account.setRegistrationLockPin(null, null);
505 }
506 }
507
508 void refreshPreKeys() throws IOException {
509 preKeyHelper.refreshPreKeys();
510 }
511
512 @Override
513 public Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws IOException {
514 return profileHelper.getRecipientProfile(resolveRecipient(recipient));
515 }
516
517 private Profile getRecipientProfile(RecipientId recipientId) {
518 return profileHelper.getRecipientProfile(recipientId);
519 }
520
521 @Override
522 public List<Group> getGroups() {
523 return account.getGroupStore().getGroups().stream().map(this::toGroup).toList();
524 }
525
526 private Group toGroup(final GroupInfo groupInfo) {
527 if (groupInfo == null) {
528 return null;
529 }
530
531 return new Group(groupInfo.getGroupId(),
532 groupInfo.getTitle(),
533 groupInfo.getDescription(),
534 groupInfo.getGroupInviteLink(),
535 groupInfo.getMembers()
536 .stream()
537 .map(account.getRecipientStore()::resolveRecipientAddress)
538 .collect(Collectors.toSet()),
539 groupInfo.getPendingMembers()
540 .stream()
541 .map(account.getRecipientStore()::resolveRecipientAddress)
542 .collect(Collectors.toSet()),
543 groupInfo.getRequestingMembers()
544 .stream()
545 .map(account.getRecipientStore()::resolveRecipientAddress)
546 .collect(Collectors.toSet()),
547 groupInfo.getAdminMembers()
548 .stream()
549 .map(account.getRecipientStore()::resolveRecipientAddress)
550 .collect(Collectors.toSet()),
551 groupInfo.isBlocked(),
552 groupInfo.getMessageExpirationTimer(),
553 groupInfo.getPermissionAddMember(),
554 groupInfo.getPermissionEditDetails(),
555 groupInfo.getPermissionSendMessage(),
556 groupInfo.isMember(account.getSelfRecipientId()),
557 groupInfo.isAdmin(account.getSelfRecipientId()));
558 }
559
560 @Override
561 public SendGroupMessageResults quitGroup(
562 GroupId groupId, Set<RecipientIdentifier.Single> groupAdmins
563 ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException {
564 final var newAdmins = resolveRecipients(groupAdmins);
565 return groupHelper.quitGroup(groupId, newAdmins);
566 }
567
568 @Override
569 public void deleteGroup(GroupId groupId) throws IOException {
570 groupHelper.deleteGroup(groupId);
571 }
572
573 @Override
574 public Pair<GroupId, SendGroupMessageResults> createGroup(
575 String name, Set<RecipientIdentifier.Single> members, File avatarFile
576 ) throws IOException, AttachmentInvalidException {
577 return groupHelper.createGroup(name, members == null ? null : resolveRecipients(members), avatarFile);
578 }
579
580 @Override
581 public SendGroupMessageResults updateGroup(
582 final GroupId groupId, final UpdateGroup updateGroup
583 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException {
584 return groupHelper.updateGroup(groupId,
585 updateGroup.getName(),
586 updateGroup.getDescription(),
587 updateGroup.getMembers() == null ? null : resolveRecipients(updateGroup.getMembers()),
588 updateGroup.getRemoveMembers() == null ? null : resolveRecipients(updateGroup.getRemoveMembers()),
589 updateGroup.getAdmins() == null ? null : resolveRecipients(updateGroup.getAdmins()),
590 updateGroup.getRemoveAdmins() == null ? null : resolveRecipients(updateGroup.getRemoveAdmins()),
591 updateGroup.isResetGroupLink(),
592 updateGroup.getGroupLinkState(),
593 updateGroup.getAddMemberPermission(),
594 updateGroup.getEditDetailsPermission(),
595 updateGroup.getAvatarFile(),
596 updateGroup.getExpirationTimer(),
597 updateGroup.getIsAnnouncementGroup());
598 }
599
600 @Override
601 public Pair<GroupId, SendGroupMessageResults> joinGroup(
602 GroupInviteLinkUrl inviteLinkUrl
603 ) throws IOException, InactiveGroupLinkException {
604 return groupHelper.joinGroup(inviteLinkUrl);
605 }
606
607 private SendMessageResults sendMessage(
608 SignalServiceDataMessage.Builder messageBuilder, Set<RecipientIdentifier> recipients
609 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
610 var results = new HashMap<RecipientIdentifier, List<SendMessageResult>>();
611 long timestamp = System.currentTimeMillis();
612 messageBuilder.withTimestamp(timestamp);
613 for (final var recipient : recipients) {
614 if (recipient instanceof RecipientIdentifier.Single single) {
615 final var recipientId = resolveRecipient(single);
616 final var result = sendHelper.sendMessage(messageBuilder, recipientId);
617 results.put(recipient,
618 List.of(SendMessageResult.from(result,
619 account.getRecipientStore(),
620 account.getRecipientStore()::resolveRecipientAddress)));
621 } else if (recipient instanceof RecipientIdentifier.NoteToSelf) {
622 final var result = sendHelper.sendSelfMessage(messageBuilder);
623 results.put(recipient,
624 List.of(SendMessageResult.from(result,
625 account.getRecipientStore(),
626 account.getRecipientStore()::resolveRecipientAddress)));
627 } else if (recipient instanceof RecipientIdentifier.Group group) {
628 final var result = sendHelper.sendAsGroupMessage(messageBuilder, group.groupId());
629 results.put(recipient,
630 result.stream()
631 .map(sendMessageResult -> SendMessageResult.from(sendMessageResult,
632 account.getRecipientStore(),
633 account.getRecipientStore()::resolveRecipientAddress))
634 .toList());
635 }
636 }
637 return new SendMessageResults(timestamp, results);
638 }
639
640 private SendMessageResults sendTypingMessage(
641 SignalServiceTypingMessage.Action action, Set<RecipientIdentifier> recipients
642 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
643 var results = new HashMap<RecipientIdentifier, List<SendMessageResult>>();
644 final var timestamp = System.currentTimeMillis();
645 for (var recipient : recipients) {
646 if (recipient instanceof RecipientIdentifier.Single) {
647 final var message = new SignalServiceTypingMessage(action, timestamp, Optional.absent());
648 final var recipientId = resolveRecipient((RecipientIdentifier.Single) recipient);
649 final var result = sendHelper.sendTypingMessage(message, recipientId);
650 results.put(recipient,
651 List.of(SendMessageResult.from(result,
652 account.getRecipientStore(),
653 account.getRecipientStore()::resolveRecipientAddress)));
654 } else if (recipient instanceof RecipientIdentifier.Group) {
655 final var groupId = ((RecipientIdentifier.Group) recipient).groupId();
656 final var message = new SignalServiceTypingMessage(action, timestamp, Optional.of(groupId.serialize()));
657 final var result = sendHelper.sendGroupTypingMessage(message, groupId);
658 results.put(recipient,
659 result.stream()
660 .map(r -> SendMessageResult.from(r,
661 account.getRecipientStore(),
662 account.getRecipientStore()::resolveRecipientAddress))
663 .toList());
664 }
665 }
666 return new SendMessageResults(timestamp, results);
667 }
668
669 @Override
670 public SendMessageResults sendTypingMessage(
671 TypingAction action, Set<RecipientIdentifier> recipients
672 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
673 return sendTypingMessage(action.toSignalService(), recipients);
674 }
675
676 @Override
677 public SendMessageResults sendReadReceipt(
678 RecipientIdentifier.Single sender, List<Long> messageIds
679 ) throws IOException {
680 final var timestamp = System.currentTimeMillis();
681 var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.READ,
682 messageIds,
683 timestamp);
684
685 final var result = sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(sender));
686 return new SendMessageResults(timestamp,
687 Map.of(sender,
688 List.of(SendMessageResult.from(result,
689 account.getRecipientStore(),
690 account.getRecipientStore()::resolveRecipientAddress))));
691 }
692
693 @Override
694 public SendMessageResults sendViewedReceipt(
695 RecipientIdentifier.Single sender, List<Long> messageIds
696 ) throws IOException {
697 final var timestamp = System.currentTimeMillis();
698 var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.VIEWED,
699 messageIds,
700 timestamp);
701
702 final var result = sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(sender));
703 return new SendMessageResults(timestamp,
704 Map.of(sender,
705 List.of(SendMessageResult.from(result,
706 account.getRecipientStore(),
707 account.getRecipientStore()::resolveRecipientAddress))));
708 }
709
710 @Override
711 public SendMessageResults sendMessage(
712 Message message, Set<RecipientIdentifier> recipients
713 ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
714 final var messageBuilder = SignalServiceDataMessage.newBuilder();
715 applyMessage(messageBuilder, message);
716 return sendMessage(messageBuilder, recipients);
717 }
718
719 private void applyMessage(
720 final SignalServiceDataMessage.Builder messageBuilder, final Message message
721 ) throws AttachmentInvalidException, IOException {
722 messageBuilder.withBody(message.messageText());
723 final var attachments = message.attachments();
724 if (attachments != null) {
725 messageBuilder.withAttachments(attachmentHelper.uploadAttachments(attachments));
726 }
727 if (message.mentions().size() > 0) {
728 messageBuilder.withMentions(resolveMentions(message.mentions()));
729 }
730 if (message.quote().isPresent()) {
731 final var quote = message.quote().get();
732 messageBuilder.withQuote(new SignalServiceDataMessage.Quote(quote.timestamp(),
733 resolveSignalServiceAddress(resolveRecipient(quote.author())),
734 quote.message(),
735 List.of(),
736 resolveMentions(quote.mentions())));
737 }
738 }
739
740 private ArrayList<SignalServiceDataMessage.Mention> resolveMentions(final List<Message.Mention> mentionList) throws IOException {
741 final var mentions = new ArrayList<SignalServiceDataMessage.Mention>();
742 for (final var m : mentionList) {
743 final var recipientId = resolveRecipient(m.recipient());
744 mentions.add(new SignalServiceDataMessage.Mention(resolveSignalServiceAddress(recipientId).getAci(),
745 m.start(),
746 m.length()));
747 }
748 return mentions;
749 }
750
751 @Override
752 public SendMessageResults sendRemoteDeleteMessage(
753 long targetSentTimestamp, Set<RecipientIdentifier> recipients
754 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
755 var delete = new SignalServiceDataMessage.RemoteDelete(targetSentTimestamp);
756 final var messageBuilder = SignalServiceDataMessage.newBuilder().withRemoteDelete(delete);
757 return sendMessage(messageBuilder, recipients);
758 }
759
760 @Override
761 public SendMessageResults sendMessageReaction(
762 String emoji,
763 boolean remove,
764 RecipientIdentifier.Single targetAuthor,
765 long targetSentTimestamp,
766 Set<RecipientIdentifier> recipients
767 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
768 var targetAuthorRecipientId = resolveRecipient(targetAuthor);
769 var reaction = new SignalServiceDataMessage.Reaction(emoji,
770 remove,
771 resolveSignalServiceAddress(targetAuthorRecipientId),
772 targetSentTimestamp);
773 final var messageBuilder = SignalServiceDataMessage.newBuilder().withReaction(reaction);
774 return sendMessage(messageBuilder, recipients);
775 }
776
777 @Override
778 public SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException {
779 var messageBuilder = SignalServiceDataMessage.newBuilder().asEndSessionMessage();
780
781 try {
782 return sendMessage(messageBuilder,
783 recipients.stream().map(RecipientIdentifier.class::cast).collect(Collectors.toSet()));
784 } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
785 throw new AssertionError(e);
786 } finally {
787 for (var recipient : recipients) {
788 final var recipientId = resolveRecipient(recipient);
789 account.getSessionStore().deleteAllSessions(recipientId);
790 }
791 }
792 }
793
794 @Override
795 public void deleteRecipient(final RecipientIdentifier.Single recipient) throws IOException {
796 account.removeRecipient(resolveRecipient(recipient));
797 }
798
799 @Override
800 public void deleteContact(final RecipientIdentifier.Single recipient) throws IOException {
801 account.getContactStore().deleteContact(resolveRecipient(recipient));
802 }
803
804 @Override
805 public void setContactName(
806 RecipientIdentifier.Single recipient, String name
807 ) throws NotMasterDeviceException, IOException {
808 if (!account.isMasterDevice()) {
809 throw new NotMasterDeviceException();
810 }
811 contactHelper.setContactName(resolveRecipient(recipient), name);
812 }
813
814 @Override
815 public void setContactBlocked(
816 RecipientIdentifier.Single recipient, boolean blocked
817 ) throws NotMasterDeviceException, IOException {
818 if (!account.isMasterDevice()) {
819 throw new NotMasterDeviceException();
820 }
821 contactHelper.setContactBlocked(resolveRecipient(recipient), blocked);
822 // TODO cycle our profile key
823 syncHelper.sendBlockedList();
824 }
825
826 @Override
827 public void setGroupBlocked(
828 final GroupId groupId, final boolean blocked
829 ) throws GroupNotFoundException, IOException, NotMasterDeviceException {
830 if (!account.isMasterDevice()) {
831 throw new NotMasterDeviceException();
832 }
833 groupHelper.setGroupBlocked(groupId, blocked);
834 // TODO cycle our profile key
835 syncHelper.sendBlockedList();
836 }
837
838 /**
839 * Change the expiration timer for a contact
840 */
841 @Override
842 public void setExpirationTimer(
843 RecipientIdentifier.Single recipient, int messageExpirationTimer
844 ) throws IOException {
845 var recipientId = resolveRecipient(recipient);
846 contactHelper.setExpirationTimer(recipientId, messageExpirationTimer);
847 final var messageBuilder = SignalServiceDataMessage.newBuilder().asExpirationUpdate();
848 try {
849 sendMessage(messageBuilder, Set.of(recipient));
850 } catch (NotAGroupMemberException | GroupNotFoundException | GroupSendingNotAllowedException e) {
851 throw new AssertionError(e);
852 }
853 }
854
855 /**
856 * Upload the sticker pack from path.
857 *
858 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
859 * @return if successful, returns the URL to install the sticker pack in the signal app
860 */
861 @Override
862 public URI uploadStickerPack(File path) throws IOException, StickerPackInvalidException {
863 var manifest = StickerUtils.getSignalServiceStickerManifestUpload(path);
864
865 var messageSender = dependencies.getMessageSender();
866
867 var packKey = KeyUtils.createStickerUploadKey();
868 var packIdString = messageSender.uploadStickerManifest(manifest, packKey);
869 var packId = StickerPackId.deserialize(Hex.fromStringCondensed(packIdString));
870
871 var sticker = new Sticker(packId, packKey);
872 account.getStickerStore().updateSticker(sticker);
873
874 try {
875 return new URI("https",
876 "signal.art",
877 "/addstickers/",
878 "pack_id="
879 + URLEncoder.encode(Hex.toStringCondensed(packId.serialize()), StandardCharsets.UTF_8)
880 + "&pack_key="
881 + URLEncoder.encode(Hex.toStringCondensed(packKey), StandardCharsets.UTF_8));
882 } catch (URISyntaxException e) {
883 throw new AssertionError(e);
884 }
885 }
886
887 @Override
888 public void requestAllSyncData() throws IOException {
889 syncHelper.requestAllSyncData();
890 retrieveRemoteStorage();
891 }
892
893 void retrieveRemoteStorage() throws IOException {
894 if (account.getStorageKey() != null) {
895 storageHelper.readDataFromStorage();
896 }
897 }
898
899 private RecipientId refreshRegisteredUser(RecipientId recipientId) throws IOException {
900 final var address = resolveSignalServiceAddress(recipientId);
901 if (!address.getNumber().isPresent()) {
902 return recipientId;
903 }
904 final var number = address.getNumber().get();
905 final var uuid = getRegisteredUser(number);
906 return resolveRecipientTrusted(new SignalServiceAddress(uuid, number));
907 }
908
909 private ACI getRegisteredUser(final String number) throws IOException {
910 final Map<String, ACI> aciMap;
911 try {
912 aciMap = getRegisteredUsers(Set.of(number));
913 } catch (NumberFormatException e) {
914 throw new IOException(number, e);
915 }
916 final var uuid = aciMap.get(number);
917 if (uuid == null) {
918 throw new IOException(number, null);
919 }
920 return uuid;
921 }
922
923 private Map<String, ACI> getRegisteredUsers(final Set<String> numbers) throws IOException {
924 final Map<String, ACI> registeredUsers;
925 try {
926 registeredUsers = dependencies.getAccountManager()
927 .getRegisteredUsers(ServiceConfig.getIasKeyStore(),
928 numbers,
929 serviceEnvironmentConfig.getCdsMrenclave());
930 } catch (Quote.InvalidQuoteFormatException | UnauthenticatedQuoteException | SignatureException | UnauthenticatedResponseException | InvalidKeyException e) {
931 throw new IOException(e);
932 }
933
934 // Store numbers as recipients, so we have the number/uuid association
935 registeredUsers.forEach((number, aci) -> resolveRecipientTrusted(new SignalServiceAddress(aci, number)));
936
937 return registeredUsers;
938 }
939
940 private void retryFailedReceivedMessages(ReceiveMessageHandler handler) {
941 Set<HandleAction> queuedActions = new HashSet<>();
942 for (var cachedMessage : account.getMessageCache().getCachedMessages()) {
943 var actions = retryFailedReceivedMessage(handler, cachedMessage);
944 if (actions != null) {
945 queuedActions.addAll(actions);
946 }
947 }
948 handleQueuedActions(queuedActions);
949 }
950
951 private List<HandleAction> retryFailedReceivedMessage(
952 final ReceiveMessageHandler handler, final CachedMessage cachedMessage
953 ) {
954 var envelope = cachedMessage.loadEnvelope();
955 if (envelope == null) {
956 cachedMessage.delete();
957 return null;
958 }
959
960 final var result = incomingMessageHandler.handleRetryEnvelope(envelope, ignoreAttachments, handler);
961 final var actions = result.first();
962 final var exception = result.second();
963
964 if (exception instanceof UntrustedIdentityException) {
965 if (System.currentTimeMillis() - envelope.getServerDeliveredTimestamp() > 1000L * 60 * 60 * 24 * 30) {
966 // Envelope is more than a month old, cleaning up.
967 cachedMessage.delete();
968 return null;
969 }
970 if (!envelope.hasSourceUuid()) {
971 final var identifier = ((UntrustedIdentityException) exception).getSender();
972 final var recipientId = account.getRecipientStore().resolveRecipient(identifier);
973 try {
974 account.getMessageCache().replaceSender(cachedMessage, recipientId);
975 } catch (IOException ioException) {
976 logger.warn("Failed to move cached message to recipient folder: {}", ioException.getMessage());
977 }
978 }
979 return null;
980 }
981
982 // If successful and for all other errors that are not recoverable, delete the cached message
983 cachedMessage.delete();
984 return actions;
985 }
986
987 @Override
988 public void addReceiveHandler(final ReceiveMessageHandler handler, final boolean isWeakListener) {
989 if (isReceivingSynchronous) {
990 throw new IllegalStateException("Already receiving message synchronously.");
991 }
992 synchronized (messageHandlers) {
993 if (isWeakListener) {
994 weakHandlers.add(handler);
995 } else {
996 messageHandlers.add(handler);
997 startReceiveThreadIfRequired();
998 }
999 }
1000 }
1001
1002 private void startReceiveThreadIfRequired() {
1003 if (receiveThread != null) {
1004 return;
1005 }
1006 receiveThread = new Thread(() -> {
1007 logger.debug("Starting receiving messages");
1008 while (!Thread.interrupted()) {
1009 try {
1010 receiveMessagesInternal(Duration.ofMinutes(1), false, (envelope, e) -> {
1011 synchronized (messageHandlers) {
1012 Stream.concat(messageHandlers.stream(), weakHandlers.stream()).forEach(h -> {
1013 try {
1014 h.handleMessage(envelope, e);
1015 } catch (Exception ex) {
1016 logger.warn("Message handler failed, ignoring", ex);
1017 }
1018 });
1019 }
1020 });
1021 break;
1022 } catch (IOException e) {
1023 logger.warn("Receiving messages failed, retrying", e);
1024 }
1025 }
1026 logger.debug("Finished receiving messages");
1027 hasCaughtUpWithOldMessages = false;
1028 synchronized (messageHandlers) {
1029 receiveThread = null;
1030
1031 // Check if in the meantime another handler has been registered
1032 if (!messageHandlers.isEmpty()) {
1033 logger.debug("Another handler has been registered, starting receive thread again");
1034 startReceiveThreadIfRequired();
1035 }
1036 }
1037 });
1038
1039 receiveThread.start();
1040 }
1041
1042 @Override
1043 public void removeReceiveHandler(final ReceiveMessageHandler handler) {
1044 final Thread thread;
1045 synchronized (messageHandlers) {
1046 weakHandlers.remove(handler);
1047 messageHandlers.remove(handler);
1048 if (!messageHandlers.isEmpty() || receiveThread == null || isReceivingSynchronous) {
1049 return;
1050 }
1051 thread = receiveThread;
1052 receiveThread = null;
1053 }
1054
1055 stopReceiveThread(thread);
1056 }
1057
1058 private void stopReceiveThread(final Thread thread) {
1059 thread.interrupt();
1060 try {
1061 thread.join();
1062 } catch (InterruptedException ignored) {
1063 }
1064 }
1065
1066 @Override
1067 public boolean isReceiving() {
1068 if (isReceivingSynchronous) {
1069 return true;
1070 }
1071 synchronized (messageHandlers) {
1072 return messageHandlers.size() > 0;
1073 }
1074 }
1075
1076 @Override
1077 public void receiveMessages(Duration timeout, ReceiveMessageHandler handler) throws IOException {
1078 receiveMessages(timeout, true, handler);
1079 }
1080
1081 @Override
1082 public void receiveMessages(ReceiveMessageHandler handler) throws IOException {
1083 receiveMessages(Duration.ofMinutes(1), false, handler);
1084 }
1085
1086 private void receiveMessages(
1087 Duration timeout, boolean returnOnTimeout, ReceiveMessageHandler handler
1088 ) throws IOException {
1089 if (isReceiving()) {
1090 throw new IllegalStateException("Already receiving message.");
1091 }
1092 isReceivingSynchronous = true;
1093 receiveThread = Thread.currentThread();
1094 try {
1095 receiveMessagesInternal(timeout, returnOnTimeout, handler);
1096 } finally {
1097 receiveThread = null;
1098 hasCaughtUpWithOldMessages = false;
1099 isReceivingSynchronous = false;
1100 }
1101 }
1102
1103 private void receiveMessagesInternal(
1104 Duration timeout, boolean returnOnTimeout, ReceiveMessageHandler handler
1105 ) throws IOException {
1106 needsToRetryFailedMessages = true;
1107
1108 // Use a Map here because java Set doesn't have a get method ...
1109 Map<HandleAction, HandleAction> queuedActions = new HashMap<>();
1110
1111 final var signalWebSocket = dependencies.getSignalWebSocket();
1112 final var webSocketStateDisposable = Observable.merge(signalWebSocket.getUnidentifiedWebSocketState(),
1113 signalWebSocket.getWebSocketState())
1114 .subscribeOn(Schedulers.computation())
1115 .observeOn(Schedulers.computation())
1116 .distinctUntilChanged()
1117 .subscribe(this::onWebSocketStateChange);
1118 signalWebSocket.connect();
1119
1120 hasCaughtUpWithOldMessages = false;
1121 var backOffCounter = 0;
1122 final var MAX_BACKOFF_COUNTER = 9;
1123
1124 while (!Thread.interrupted()) {
1125 if (needsToRetryFailedMessages) {
1126 retryFailedReceivedMessages(handler);
1127 needsToRetryFailedMessages = false;
1128 }
1129 SignalServiceEnvelope envelope;
1130 final CachedMessage[] cachedMessage = {null};
1131 final var nowMillis = System.currentTimeMillis();
1132 if (nowMillis - account.getLastReceiveTimestamp() > 60000) {
1133 account.setLastReceiveTimestamp(nowMillis);
1134 }
1135 logger.debug("Checking for new message from server");
1136 try {
1137 var result = signalWebSocket.readOrEmpty(timeout.toMillis(), envelope1 -> {
1138 final var recipientId = envelope1.hasSourceUuid()
1139 ? resolveRecipient(envelope1.getSourceAddress())
1140 : null;
1141 // store message on disk, before acknowledging receipt to the server
1142 cachedMessage[0] = account.getMessageCache().cacheMessage(envelope1, recipientId);
1143 });
1144 backOffCounter = 0;
1145
1146 if (result.isPresent()) {
1147 envelope = result.get();
1148 logger.debug("New message received from server");
1149 } else {
1150 logger.debug("Received indicator that server queue is empty");
1151 handleQueuedActions(queuedActions.keySet());
1152 queuedActions.clear();
1153
1154 hasCaughtUpWithOldMessages = true;
1155 synchronized (this) {
1156 this.notifyAll();
1157 }
1158
1159 // Continue to wait another timeout for new messages
1160 continue;
1161 }
1162 } catch (AssertionError e) {
1163 if (e.getCause() instanceof InterruptedException) {
1164 Thread.currentThread().interrupt();
1165 break;
1166 } else {
1167 throw e;
1168 }
1169 } catch (IOException e) {
1170 logger.debug("Pipe unexpectedly unavailable: {}", e.getMessage());
1171 if (e instanceof WebSocketUnavailableException || "Connection closed!".equals(e.getMessage())) {
1172 final var sleepMilliseconds = 100 * (long) Math.pow(2, backOffCounter);
1173 backOffCounter = Math.min(backOffCounter + 1, MAX_BACKOFF_COUNTER);
1174 logger.warn("Connection closed unexpectedly, reconnecting in {} ms", sleepMilliseconds);
1175 try {
1176 Thread.sleep(sleepMilliseconds);
1177 } catch (InterruptedException interruptedException) {
1178 return;
1179 }
1180 hasCaughtUpWithOldMessages = false;
1181 signalWebSocket.connect();
1182 continue;
1183 }
1184 throw e;
1185 } catch (TimeoutException e) {
1186 backOffCounter = 0;
1187 if (returnOnTimeout) return;
1188 continue;
1189 }
1190
1191 final var result = incomingMessageHandler.handleEnvelope(envelope, ignoreAttachments, handler);
1192 for (final var h : result.first()) {
1193 final var existingAction = queuedActions.get(h);
1194 if (existingAction == null) {
1195 queuedActions.put(h, h);
1196 } else {
1197 existingAction.mergeOther(h);
1198 }
1199 }
1200 final var exception = result.second();
1201
1202 if (hasCaughtUpWithOldMessages) {
1203 handleQueuedActions(queuedActions.keySet());
1204 queuedActions.clear();
1205 }
1206 if (cachedMessage[0] != null) {
1207 if (exception instanceof UntrustedIdentityException) {
1208 logger.debug("Keeping message with untrusted identity in message cache");
1209 final var address = ((UntrustedIdentityException) exception).getSender();
1210 final var recipientId = resolveRecipient(address);
1211 if (!envelope.hasSourceUuid()) {
1212 try {
1213 cachedMessage[0] = account.getMessageCache().replaceSender(cachedMessage[0], recipientId);
1214 } catch (IOException ioException) {
1215 logger.warn("Failed to move cached message to recipient folder: {}",
1216 ioException.getMessage());
1217 }
1218 }
1219 } else {
1220 cachedMessage[0].delete();
1221 }
1222 }
1223 }
1224 handleQueuedActions(queuedActions.keySet());
1225 queuedActions.clear();
1226 dependencies.getSignalWebSocket().disconnect();
1227 webSocketStateDisposable.dispose();
1228 }
1229
1230 private void onWebSocketStateChange(final WebSocketConnectionState s) {
1231 if (s.equals(WebSocketConnectionState.AUTHENTICATION_FAILED)) {
1232 account.setRegistered(false);
1233 try {
1234 close();
1235 } catch (IOException e) {
1236 e.printStackTrace();
1237 }
1238 }
1239 }
1240
1241 @Override
1242 public void setIgnoreAttachments(final boolean ignoreAttachments) {
1243 this.ignoreAttachments = ignoreAttachments;
1244 }
1245
1246 @Override
1247 public boolean hasCaughtUpWithOldMessages() {
1248 return hasCaughtUpWithOldMessages;
1249 }
1250
1251 private void handleQueuedActions(final Collection<HandleAction> queuedActions) {
1252 logger.debug("Handling message actions");
1253 var interrupted = false;
1254 for (var action : queuedActions) {
1255 try {
1256 action.execute(context);
1257 } catch (Throwable e) {
1258 if ((e instanceof AssertionError || e instanceof RuntimeException)
1259 && e.getCause() instanceof InterruptedException) {
1260 interrupted = true;
1261 continue;
1262 }
1263 logger.warn("Message action failed.", e);
1264 }
1265 }
1266 if (interrupted) {
1267 Thread.currentThread().interrupt();
1268 }
1269 }
1270
1271 @Override
1272 public boolean isContactBlocked(final RecipientIdentifier.Single recipient) {
1273 final RecipientId recipientId;
1274 try {
1275 recipientId = resolveRecipient(recipient);
1276 } catch (IOException e) {
1277 return false;
1278 }
1279 return contactHelper.isContactBlocked(recipientId);
1280 }
1281
1282 @Override
1283 public void sendContacts() throws IOException {
1284 syncHelper.sendContacts();
1285 }
1286
1287 @Override
1288 public List<Pair<RecipientAddress, Contact>> getContacts() {
1289 return account.getContactStore()
1290 .getContacts()
1291 .stream()
1292 .map(p -> new Pair<>(account.getRecipientStore().resolveRecipientAddress(p.first()), p.second()))
1293 .toList();
1294 }
1295
1296 @Override
1297 public String getContactOrProfileName(RecipientIdentifier.Single recipient) {
1298 final RecipientId recipientId;
1299 try {
1300 recipientId = resolveRecipient(recipient);
1301 } catch (IOException e) {
1302 return null;
1303 }
1304
1305 final var contact = account.getContactStore().getContact(recipientId);
1306 if (contact != null && !Util.isEmpty(contact.getName())) {
1307 return contact.getName();
1308 }
1309
1310 final var profile = getRecipientProfile(recipientId);
1311 if (profile != null) {
1312 return profile.getDisplayName();
1313 }
1314
1315 return null;
1316 }
1317
1318 @Override
1319 public Group getGroup(GroupId groupId) {
1320 return toGroup(groupHelper.getGroup(groupId));
1321 }
1322
1323 private GroupInfo getGroupInfo(GroupId groupId) {
1324 return groupHelper.getGroup(groupId);
1325 }
1326
1327 @Override
1328 public List<Identity> getIdentities() {
1329 return account.getIdentityKeyStore().getIdentities().stream().map(this::toIdentity).toList();
1330 }
1331
1332 private Identity toIdentity(final IdentityInfo identityInfo) {
1333 if (identityInfo == null) {
1334 return null;
1335 }
1336
1337 final var address = account.getRecipientStore().resolveRecipientAddress(identityInfo.getRecipientId());
1338 final var scannableFingerprint = identityHelper.computeSafetyNumberForScanning(identityInfo.getRecipientId(),
1339 identityInfo.getIdentityKey());
1340 return new Identity(address,
1341 identityInfo.getIdentityKey(),
1342 identityHelper.computeSafetyNumber(identityInfo.getRecipientId(), identityInfo.getIdentityKey()),
1343 scannableFingerprint == null ? null : scannableFingerprint.getSerialized(),
1344 identityInfo.getTrustLevel(),
1345 identityInfo.getDateAdded());
1346 }
1347
1348 @Override
1349 public List<Identity> getIdentities(RecipientIdentifier.Single recipient) {
1350 IdentityInfo identity;
1351 try {
1352 identity = account.getIdentityKeyStore().getIdentity(resolveRecipient(recipient));
1353 } catch (IOException e) {
1354 identity = null;
1355 }
1356 return identity == null ? List.of() : List.of(toIdentity(identity));
1357 }
1358
1359 /**
1360 * Trust this the identity with this fingerprint
1361 *
1362 * @param recipient account of the identity
1363 * @param fingerprint Fingerprint
1364 */
1365 @Override
1366 public boolean trustIdentityVerified(RecipientIdentifier.Single recipient, byte[] fingerprint) {
1367 RecipientId recipientId;
1368 try {
1369 recipientId = resolveRecipient(recipient);
1370 } catch (IOException e) {
1371 return false;
1372 }
1373 final var updated = identityHelper.trustIdentityVerified(recipientId, fingerprint);
1374 if (updated && this.isReceiving()) {
1375 needsToRetryFailedMessages = true;
1376 }
1377 return updated;
1378 }
1379
1380 /**
1381 * Trust this the identity with this safety number
1382 *
1383 * @param recipient account of the identity
1384 * @param safetyNumber Safety number
1385 */
1386 @Override
1387 public boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, String safetyNumber) {
1388 RecipientId recipientId;
1389 try {
1390 recipientId = resolveRecipient(recipient);
1391 } catch (IOException e) {
1392 return false;
1393 }
1394 final var updated = identityHelper.trustIdentityVerifiedSafetyNumber(recipientId, safetyNumber);
1395 if (updated && this.isReceiving()) {
1396 needsToRetryFailedMessages = true;
1397 }
1398 return updated;
1399 }
1400
1401 /**
1402 * Trust this the identity with this scannable safety number
1403 *
1404 * @param recipient account of the identity
1405 * @param safetyNumber Scannable safety number
1406 */
1407 @Override
1408 public boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, byte[] safetyNumber) {
1409 RecipientId recipientId;
1410 try {
1411 recipientId = resolveRecipient(recipient);
1412 } catch (IOException e) {
1413 return false;
1414 }
1415 final var updated = identityHelper.trustIdentityVerifiedSafetyNumber(recipientId, safetyNumber);
1416 if (updated && this.isReceiving()) {
1417 needsToRetryFailedMessages = true;
1418 }
1419 return updated;
1420 }
1421
1422 /**
1423 * Trust all keys of this identity without verification
1424 *
1425 * @param recipient account of the identity
1426 */
1427 @Override
1428 public boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient) {
1429 RecipientId recipientId;
1430 try {
1431 recipientId = resolveRecipient(recipient);
1432 } catch (IOException e) {
1433 return false;
1434 }
1435 final var updated = identityHelper.trustIdentityAllKeys(recipientId);
1436 if (updated && this.isReceiving()) {
1437 needsToRetryFailedMessages = true;
1438 }
1439 return updated;
1440 }
1441
1442 @Override
1443 public void addClosedListener(final Runnable listener) {
1444 synchronized (closedListeners) {
1445 closedListeners.add(listener);
1446 }
1447 }
1448
1449 private void handleIdentityFailure(
1450 final RecipientId recipientId,
1451 final org.whispersystems.signalservice.api.messages.SendMessageResult.IdentityFailure identityFailure
1452 ) {
1453 this.identityHelper.handleIdentityFailure(recipientId, identityFailure);
1454 }
1455
1456 private SignalServiceAddress resolveSignalServiceAddress(RecipientId recipientId) {
1457 final var address = account.getRecipientStore().resolveRecipientAddress(recipientId);
1458 if (address.uuid().isPresent()) {
1459 return address.toSignalServiceAddress();
1460 }
1461
1462 // Address in recipient store doesn't have a uuid, this shouldn't happen
1463 // Try to retrieve the uuid from the server
1464 final var number = address.number().get();
1465 final ACI aci;
1466 try {
1467 aci = getRegisteredUser(number);
1468 } catch (IOException e) {
1469 logger.warn("Failed to get uuid for e164 number: {}", number, e);
1470 // Return SignalServiceAddress with unknown UUID
1471 return address.toSignalServiceAddress();
1472 }
1473 return resolveSignalServiceAddress(account.getRecipientStore().resolveRecipient(aci));
1474 }
1475
1476 private Set<RecipientId> resolveRecipients(Collection<RecipientIdentifier.Single> recipients) throws IOException {
1477 final var recipientIds = new HashSet<RecipientId>(recipients.size());
1478 for (var number : recipients) {
1479 final var recipientId = resolveRecipient(number);
1480 recipientIds.add(recipientId);
1481 }
1482 return recipientIds;
1483 }
1484
1485 private RecipientId resolveRecipient(final RecipientIdentifier.Single recipient) throws IOException {
1486 if (recipient instanceof RecipientIdentifier.Uuid uuidRecipient) {
1487 return account.getRecipientStore().resolveRecipient(ACI.from(uuidRecipient.uuid()));
1488 } else {
1489 final var number = ((RecipientIdentifier.Number) recipient).number();
1490 return account.getRecipientStore().resolveRecipient(number, () -> {
1491 try {
1492 return getRegisteredUser(number);
1493 } catch (IOException e) {
1494 return null;
1495 }
1496 });
1497 }
1498 }
1499
1500 private RecipientId resolveRecipient(RecipientAddress address) {
1501 return account.getRecipientStore().resolveRecipient(address);
1502 }
1503
1504 private RecipientId resolveRecipient(SignalServiceAddress address) {
1505 return account.getRecipientStore().resolveRecipient(address);
1506 }
1507
1508 private RecipientId resolveRecipientTrusted(SignalServiceAddress address) {
1509 return account.getRecipientStore().resolveRecipientTrusted(address);
1510 }
1511
1512 @Override
1513 public void close() throws IOException {
1514 Thread thread;
1515 synchronized (messageHandlers) {
1516 weakHandlers.clear();
1517 messageHandlers.clear();
1518 thread = receiveThread;
1519 receiveThread = null;
1520 }
1521 if (thread != null) {
1522 stopReceiveThread(thread);
1523 }
1524 executor.shutdown();
1525
1526 dependencies.getSignalWebSocket().disconnect();
1527
1528 synchronized (closedListeners) {
1529 closedListeners.forEach(Runnable::run);
1530 closedListeners.clear();
1531 }
1532
1533 if (account != null) {
1534 account.close();
1535 }
1536 account = null;
1537 }
1538 }