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