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