2 Copyright (C) 2015-2021 AsamK and contributors
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.
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.
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/>.
17 package org
.asamk
.signal
.manager
;
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
;
94 import java
.io
.IOException
;
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
;
117 import io
.reactivex
.rxjava3
.core
.Observable
;
118 import io
.reactivex
.rxjava3
.schedulers
.Schedulers
;
120 import static org
.asamk
.signal
.manager
.config
.ServiceConfig
.capabilities
;
122 public class ManagerImpl
implements Manager
{
124 private final static Logger logger
= LoggerFactory
.getLogger(ManagerImpl
.class);
126 private final ServiceEnvironmentConfig serviceEnvironmentConfig
;
127 private final SignalDependencies dependencies
;
129 private SignalAccount account
;
131 private final ExecutorService executor
= Executors
.newCachedThreadPool();
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
;
145 private final Context context
;
146 private boolean hasCaughtUpWithOldMessages
= false;
147 private boolean ignoreAttachments
= false;
149 private Thread receiveThread
;
150 private final Set
<ReceiveMessageHandler
> weakHandlers
= new HashSet
<>();
151 private final Set
<ReceiveMessageHandler
> messageHandlers
= new HashSet
<>();
152 private final List
<Runnable
> closedListeners
= new ArrayList
<>();
153 private boolean isReceivingSynchronous
;
154 private boolean needsToRetryFailedMessages
= false;
157 SignalAccount account
,
158 PathConfig pathConfig
,
159 ServiceEnvironmentConfig serviceEnvironmentConfig
,
162 this.account
= account
;
163 this.serviceEnvironmentConfig
= serviceEnvironmentConfig
;
165 final var credentialsProvider
= new DynamicCredentialsProvider(account
.getAci(),
166 account
.getAccount(),
167 account
.getPassword(),
168 account
.getDeviceId());
169 final var sessionLock
= new SignalSessionLock() {
170 private final ReentrantLock LEGACY_LOCK
= new ReentrantLock();
173 public Lock
acquire() {
175 return LEGACY_LOCK
::unlock
;
178 this.dependencies
= new SignalDependencies(serviceEnvironmentConfig
,
181 account
.getSignalProtocolStore(),
184 final var avatarStore
= new AvatarStore(pathConfig
.avatarsPath());
185 final var attachmentStore
= new AttachmentStore(pathConfig
.attachmentsPath());
186 final var stickerPackStore
= new StickerPackStore(pathConfig
.stickerPacksPath());
188 this.attachmentHelper
= new AttachmentHelper(dependencies
, attachmentStore
);
189 this.pinHelper
= new PinHelper(dependencies
.getKeyBackupService());
190 final var unidentifiedAccessHelper
= new UnidentifiedAccessHelper(account
,
192 account
::getProfileKey
,
193 this::getRecipientProfile
);
194 this.profileHelper
= new ProfileHelper(account
,
197 unidentifiedAccessHelper
::getAccessFor
,
198 this::resolveSignalServiceAddress
);
199 final GroupV2Helper groupV2Helper
= new GroupV2Helper(profileHelper
::getRecipientProfileKeyCredential
,
200 profileHelper
::getRecipientProfile
,
201 account
::getSelfRecipientId
,
202 dependencies
.getGroupsV2Operations(),
203 dependencies
.getGroupsV2Api(),
204 this::resolveSignalServiceAddress
);
205 this.sendHelper
= new SendHelper(account
,
207 unidentifiedAccessHelper
,
208 this::resolveSignalServiceAddress
,
209 account
.getRecipientStore(),
210 this::handleIdentityFailure
,
212 profileHelper
::getRecipientProfile
,
213 this::refreshRegisteredUser
);
214 this.groupHelper
= new GroupHelper(account
,
220 this::resolveSignalServiceAddress
,
221 account
.getRecipientStore());
222 this.storageHelper
= new StorageHelper(account
, dependencies
, groupHelper
, profileHelper
);
223 this.contactHelper
= new ContactHelper(account
);
224 this.syncHelper
= new SyncHelper(account
,
229 this::resolveSignalServiceAddress
);
230 preKeyHelper
= new PreKeyHelper(account
, dependencies
);
232 this.context
= new Context(account
,
241 var jobExecutor
= new JobExecutor(context
);
243 this.incomingMessageHandler
= new IncomingMessageHandler(account
,
245 account
.getRecipientStore(),
246 this::resolveSignalServiceAddress
,
251 profileHelper
::getRecipientProfile
,
253 this.identityHelper
= new IdentityHelper(account
,
255 this::resolveSignalServiceAddress
,
261 public String
getSelfNumber() {
262 return account
.getAccount();
266 public void checkAccountState() throws IOException
{
267 if (account
.getLastReceiveTimestamp() == 0) {
268 logger
.info("The Signal protocol expects that incoming messages are regularly received.");
270 var diffInMilliseconds
= System
.currentTimeMillis() - account
.getLastReceiveTimestamp();
271 long days
= TimeUnit
.DAYS
.convert(diffInMilliseconds
, TimeUnit
.MILLISECONDS
);
274 "Messages have been last received {} days ago. The Signal protocol expects that incoming messages are regularly received.",
279 preKeyHelper
.refreshPreKeysIfNecessary();
280 if (account
.getAci() == null) {
281 account
.setAci(ACI
.parseOrNull(dependencies
.getAccountManager().getWhoAmI().getAci()));
283 updateAccountAttributes(null);
284 } catch (AuthorizationFailedException e
) {
285 account
.setRegistered(false);
291 * This is used for checking a set of phone numbers for registration on Signal
293 * @param numbers The set of phone number in question
294 * @return A map of numbers to canonicalized number and uuid. If a number is not registered the uuid is null.
295 * @throws IOException if it's unable to get the contacts to check if they're registered
298 public Map
<String
, Pair
<String
, UUID
>> areUsersRegistered(Set
<String
> numbers
) throws IOException
{
299 Map
<String
, String
> canonicalizedNumbers
= numbers
.stream().collect(Collectors
.toMap(n
-> n
, n
-> {
301 final var canonicalizedNumber
= PhoneNumberFormatter
.formatNumber(n
, account
.getAccount());
302 if (!canonicalizedNumber
.equals(n
)) {
303 logger
.debug("Normalized number {} to {}.", n
, canonicalizedNumber
);
305 return canonicalizedNumber
;
306 } catch (InvalidNumberException e
) {
311 // Note "registeredUsers" has no optionals. It only gives us info on users who are registered
312 var registeredUsers
= getRegisteredUsers(canonicalizedNumbers
.values()
314 .filter(s
-> !s
.isEmpty())
315 .collect(Collectors
.toSet()));
317 return numbers
.stream().collect(Collectors
.toMap(n
-> n
, n
-> {
318 final var number
= canonicalizedNumbers
.get(n
);
319 final var aci
= registeredUsers
.get(number
);
320 return new Pair
<>(number
.isEmpty() ?
null : number
, aci
== null ?
null : aci
.uuid());
325 public void updateAccountAttributes(String deviceName
) throws IOException
{
326 final String encryptedDeviceName
;
327 if (deviceName
== null) {
328 encryptedDeviceName
= account
.getEncryptedDeviceName();
330 final var privateKey
= account
.getIdentityKeyPair().getPrivateKey();
331 encryptedDeviceName
= DeviceNameUtil
.encryptDeviceName(deviceName
, privateKey
);
332 account
.setEncryptedDeviceName(encryptedDeviceName
);
334 dependencies
.getAccountManager()
335 .setAccountAttributes(encryptedDeviceName
,
337 account
.getLocalRegistrationId(),
340 account
.getPinMasterKey() == null ?
null : account
.getPinMasterKey().deriveRegistrationLock(),
341 account
.getSelfUnidentifiedAccessKey(),
342 account
.isUnrestrictedUnidentifiedAccess(),
344 account
.isDiscoverableByPhoneNumber());
348 public Configuration
getConfiguration() {
349 final var configurationStore
= account
.getConfigurationStore();
350 return new Configuration(java
.util
.Optional
.ofNullable(configurationStore
.getReadReceipts()),
351 java
.util
.Optional
.ofNullable(configurationStore
.getUnidentifiedDeliveryIndicators()),
352 java
.util
.Optional
.ofNullable(configurationStore
.getTypingIndicators()),
353 java
.util
.Optional
.ofNullable(configurationStore
.getLinkPreviews()));
357 public void updateConfiguration(
358 Configuration configuration
359 ) throws IOException
, NotMasterDeviceException
{
360 if (!account
.isMasterDevice()) {
361 throw new NotMasterDeviceException();
364 final var configurationStore
= account
.getConfigurationStore();
365 if (configuration
.readReceipts().isPresent()) {
366 configurationStore
.setReadReceipts(configuration
.readReceipts().get());
368 if (configuration
.unidentifiedDeliveryIndicators().isPresent()) {
369 configurationStore
.setUnidentifiedDeliveryIndicators(configuration
.unidentifiedDeliveryIndicators().get());
371 if (configuration
.typingIndicators().isPresent()) {
372 configurationStore
.setTypingIndicators(configuration
.typingIndicators().get());
374 if (configuration
.linkPreviews().isPresent()) {
375 configurationStore
.setLinkPreviews(configuration
.linkPreviews().get());
377 syncHelper
.sendConfigurationMessage();
381 * @param givenName if null, the previous givenName will be kept
382 * @param familyName if null, the previous familyName will be kept
383 * @param about if null, the previous about text will be kept
384 * @param aboutEmoji if null, the previous about emoji will be kept
385 * @param avatar if avatar is null the image from the local avatar store is used (if present),
388 public void setProfile(
389 String givenName
, final String familyName
, String about
, String aboutEmoji
, java
.util
.Optional
<File
> avatar
390 ) throws IOException
{
391 profileHelper
.setProfile(givenName
,
395 avatar
== null ?
null : Optional
.fromNullable(avatar
.orElse(null)));
396 syncHelper
.sendSyncFetchProfileMessage();
400 public void unregister() throws IOException
{
401 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
402 // If this is the master device, other users can't send messages to this number anymore.
403 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
404 dependencies
.getAccountManager().setGcmId(Optional
.absent());
406 account
.setRegistered(false);
411 public void deleteAccount() throws IOException
{
413 pinHelper
.removeRegistrationLockPin();
414 } catch (IOException e
) {
415 logger
.warn("Failed to remove registration lock pin");
417 account
.setRegistrationLockPin(null, null);
419 dependencies
.getAccountManager().deleteAccount();
421 account
.setRegistered(false);
426 public void submitRateLimitRecaptchaChallenge(String challenge
, String captcha
) throws IOException
{
427 captcha
= captcha
== null ?
null : captcha
.replace("signalcaptcha://", "");
429 dependencies
.getAccountManager().submitRateLimitRecaptchaChallenge(challenge
, captcha
);
433 public List
<Device
> getLinkedDevices() throws IOException
{
434 var devices
= dependencies
.getAccountManager().getDevices();
435 account
.setMultiDevice(devices
.size() > 1);
436 var identityKey
= account
.getIdentityKeyPair().getPrivateKey();
437 return devices
.stream().map(d
-> {
438 String deviceName
= d
.getName();
439 if (deviceName
!= null) {
441 deviceName
= DeviceNameUtil
.decryptDeviceName(deviceName
, identityKey
);
442 } catch (IOException e
) {
443 logger
.debug("Failed to decrypt device name, maybe plain text?", e
);
446 return new Device(d
.getId(),
450 d
.getId() == account
.getDeviceId());
455 public void removeLinkedDevices(long deviceId
) throws IOException
{
456 dependencies
.getAccountManager().removeDevice(deviceId
);
457 var devices
= dependencies
.getAccountManager().getDevices();
458 account
.setMultiDevice(devices
.size() > 1);
462 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidDeviceLinkException
{
463 var info
= DeviceLinkInfo
.parseDeviceLinkUri(linkUri
);
465 addDevice(info
.deviceIdentifier(), info
.deviceKey());
468 private void addDevice(
469 String deviceIdentifier
, ECPublicKey deviceKey
470 ) throws IOException
, InvalidDeviceLinkException
{
471 var identityKeyPair
= account
.getIdentityKeyPair();
472 var verificationCode
= dependencies
.getAccountManager().getNewDeviceVerificationCode();
475 dependencies
.getAccountManager()
476 .addDevice(deviceIdentifier
,
479 Optional
.of(account
.getProfileKey().serialize()),
481 } catch (InvalidKeyException e
) {
482 throw new InvalidDeviceLinkException("Invalid device link", e
);
484 account
.setMultiDevice(true);
488 public void setRegistrationLockPin(java
.util
.Optional
<String
> pin
) throws IOException
{
489 if (!account
.isMasterDevice()) {
490 throw new RuntimeException("Only master device can set a PIN");
492 if (pin
.isPresent()) {
493 final var masterKey
= account
.getPinMasterKey() != null
494 ? account
.getPinMasterKey()
495 : KeyUtils
.createMasterKey();
497 pinHelper
.setRegistrationLockPin(pin
.get(), masterKey
);
499 account
.setRegistrationLockPin(pin
.get(), masterKey
);
502 pinHelper
.removeRegistrationLockPin();
504 account
.setRegistrationLockPin(null, null);
508 void refreshPreKeys() throws IOException
{
509 preKeyHelper
.refreshPreKeys();
513 public Profile
getRecipientProfile(RecipientIdentifier
.Single recipient
) throws IOException
{
514 return profileHelper
.getRecipientProfile(resolveRecipient(recipient
));
517 private Profile
getRecipientProfile(RecipientId recipientId
) {
518 return profileHelper
.getRecipientProfile(recipientId
);
522 public List
<Group
> getGroups() {
523 return account
.getGroupStore().getGroups().stream().map(this::toGroup
).toList();
526 private Group
toGroup(final GroupInfo groupInfo
) {
527 if (groupInfo
== null) {
531 return new Group(groupInfo
.getGroupId(),
532 groupInfo
.getTitle(),
533 groupInfo
.getDescription(),
534 groupInfo
.getGroupInviteLink(),
535 groupInfo
.getMembers()
537 .map(account
.getRecipientStore()::resolveRecipientAddress
)
538 .collect(Collectors
.toSet()),
539 groupInfo
.getPendingMembers()
541 .map(account
.getRecipientStore()::resolveRecipientAddress
)
542 .collect(Collectors
.toSet()),
543 groupInfo
.getRequestingMembers()
545 .map(account
.getRecipientStore()::resolveRecipientAddress
)
546 .collect(Collectors
.toSet()),
547 groupInfo
.getAdminMembers()
549 .map(account
.getRecipientStore()::resolveRecipientAddress
)
550 .collect(Collectors
.toSet()),
551 groupInfo
.isBlocked(),
552 groupInfo
.getMessageExpirationTimer(),
553 groupInfo
.getPermissionAddMember(),
554 groupInfo
.getPermissionEditDetails(),
555 groupInfo
.getPermissionSendMessage(),
556 groupInfo
.isMember(account
.getSelfRecipientId()),
557 groupInfo
.isAdmin(account
.getSelfRecipientId()));
561 public SendGroupMessageResults
quitGroup(
562 GroupId groupId
, Set
<RecipientIdentifier
.Single
> groupAdmins
563 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
564 final var newAdmins
= resolveRecipients(groupAdmins
);
565 return groupHelper
.quitGroup(groupId
, newAdmins
);
569 public void deleteGroup(GroupId groupId
) throws IOException
{
570 groupHelper
.deleteGroup(groupId
);
574 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
575 String name
, Set
<RecipientIdentifier
.Single
> members
, File avatarFile
576 ) throws IOException
, AttachmentInvalidException
{
577 return groupHelper
.createGroup(name
, members
== null ?
null : resolveRecipients(members
), avatarFile
);
581 public SendGroupMessageResults
updateGroup(
582 final GroupId groupId
, final UpdateGroup updateGroup
583 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
584 return groupHelper
.updateGroup(groupId
,
585 updateGroup
.getName(),
586 updateGroup
.getDescription(),
587 updateGroup
.getMembers() == null ?
null : resolveRecipients(updateGroup
.getMembers()),
588 updateGroup
.getRemoveMembers() == null ?
null : resolveRecipients(updateGroup
.getRemoveMembers()),
589 updateGroup
.getAdmins() == null ?
null : resolveRecipients(updateGroup
.getAdmins()),
590 updateGroup
.getRemoveAdmins() == null ?
null : resolveRecipients(updateGroup
.getRemoveAdmins()),
591 updateGroup
.isResetGroupLink(),
592 updateGroup
.getGroupLinkState(),
593 updateGroup
.getAddMemberPermission(),
594 updateGroup
.getEditDetailsPermission(),
595 updateGroup
.getAvatarFile(),
596 updateGroup
.getExpirationTimer(),
597 updateGroup
.getIsAnnouncementGroup());
601 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(
602 GroupInviteLinkUrl inviteLinkUrl
603 ) throws IOException
, InactiveGroupLinkException
{
604 return groupHelper
.joinGroup(inviteLinkUrl
);
607 private SendMessageResults
sendMessage(
608 SignalServiceDataMessage
.Builder messageBuilder
, Set
<RecipientIdentifier
> recipients
609 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
610 var results
= new HashMap
<RecipientIdentifier
, List
<SendMessageResult
>>();
611 long timestamp
= System
.currentTimeMillis();
612 messageBuilder
.withTimestamp(timestamp
);
613 for (final var recipient
: recipients
) {
614 if (recipient
instanceof RecipientIdentifier
.Single single
) {
615 final var recipientId
= resolveRecipient(single
);
616 final var result
= sendHelper
.sendMessage(messageBuilder
, recipientId
);
617 results
.put(recipient
,
618 List
.of(SendMessageResult
.from(result
,
619 account
.getRecipientStore(),
620 account
.getRecipientStore()::resolveRecipientAddress
)));
621 } else if (recipient
instanceof RecipientIdentifier
.NoteToSelf
) {
622 final var result
= sendHelper
.sendSelfMessage(messageBuilder
);
623 results
.put(recipient
,
624 List
.of(SendMessageResult
.from(result
,
625 account
.getRecipientStore(),
626 account
.getRecipientStore()::resolveRecipientAddress
)));
627 } else if (recipient
instanceof RecipientIdentifier
.Group group
) {
628 final var result
= sendHelper
.sendAsGroupMessage(messageBuilder
, group
.groupId());
629 results
.put(recipient
,
631 .map(sendMessageResult
-> SendMessageResult
.from(sendMessageResult
,
632 account
.getRecipientStore(),
633 account
.getRecipientStore()::resolveRecipientAddress
))
637 return new SendMessageResults(timestamp
, results
);
640 private SendMessageResults
sendTypingMessage(
641 SignalServiceTypingMessage
.Action action
, Set
<RecipientIdentifier
> recipients
642 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
643 var results
= new HashMap
<RecipientIdentifier
, List
<SendMessageResult
>>();
644 final var timestamp
= System
.currentTimeMillis();
645 for (var recipient
: recipients
) {
646 if (recipient
instanceof RecipientIdentifier
.Single
) {
647 final var message
= new SignalServiceTypingMessage(action
, timestamp
, Optional
.absent());
648 final var recipientId
= resolveRecipient((RecipientIdentifier
.Single
) recipient
);
649 final var result
= sendHelper
.sendTypingMessage(message
, recipientId
);
650 results
.put(recipient
,
651 List
.of(SendMessageResult
.from(result
,
652 account
.getRecipientStore(),
653 account
.getRecipientStore()::resolveRecipientAddress
)));
654 } else if (recipient
instanceof RecipientIdentifier
.Group
) {
655 final var groupId
= ((RecipientIdentifier
.Group
) recipient
).groupId();
656 final var message
= new SignalServiceTypingMessage(action
, timestamp
, Optional
.of(groupId
.serialize()));
657 final var result
= sendHelper
.sendGroupTypingMessage(message
, groupId
);
658 results
.put(recipient
,
660 .map(r
-> SendMessageResult
.from(r
,
661 account
.getRecipientStore(),
662 account
.getRecipientStore()::resolveRecipientAddress
))
666 return new SendMessageResults(timestamp
, results
);
670 public SendMessageResults
sendTypingMessage(
671 TypingAction action
, Set
<RecipientIdentifier
> recipients
672 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
673 return sendTypingMessage(action
.toSignalService(), recipients
);
677 public SendMessageResults
sendReadReceipt(
678 RecipientIdentifier
.Single sender
, List
<Long
> messageIds
679 ) throws IOException
{
680 final var timestamp
= System
.currentTimeMillis();
681 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.READ
,
685 final var result
= sendHelper
.sendReceiptMessage(receiptMessage
, resolveRecipient(sender
));
686 return new SendMessageResults(timestamp
,
688 List
.of(SendMessageResult
.from(result
,
689 account
.getRecipientStore(),
690 account
.getRecipientStore()::resolveRecipientAddress
))));
694 public SendMessageResults
sendViewedReceipt(
695 RecipientIdentifier
.Single sender
, List
<Long
> messageIds
696 ) throws IOException
{
697 final var timestamp
= System
.currentTimeMillis();
698 var receiptMessage
= new SignalServiceReceiptMessage(SignalServiceReceiptMessage
.Type
.VIEWED
,
702 final var result
= sendHelper
.sendReceiptMessage(receiptMessage
, resolveRecipient(sender
));
703 return new SendMessageResults(timestamp
,
705 List
.of(SendMessageResult
.from(result
,
706 account
.getRecipientStore(),
707 account
.getRecipientStore()::resolveRecipientAddress
))));
711 public SendMessageResults
sendMessage(
712 Message message
, Set
<RecipientIdentifier
> recipients
713 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
714 final var messageBuilder
= SignalServiceDataMessage
.newBuilder();
715 applyMessage(messageBuilder
, message
);
716 return sendMessage(messageBuilder
, recipients
);
719 private void applyMessage(
720 final SignalServiceDataMessage
.Builder messageBuilder
, final Message message
721 ) throws AttachmentInvalidException
, IOException
{
722 messageBuilder
.withBody(message
.messageText());
723 final var attachments
= message
.attachments();
724 if (attachments
!= null) {
725 messageBuilder
.withAttachments(attachmentHelper
.uploadAttachments(attachments
));
727 if (message
.mentions().size() > 0) {
728 messageBuilder
.withMentions(resolveMentions(message
.mentions()));
730 if (message
.quote().isPresent()) {
731 final var quote
= message
.quote().get();
732 messageBuilder
.withQuote(new SignalServiceDataMessage
.Quote(quote
.timestamp(),
733 resolveSignalServiceAddress(resolveRecipient(quote
.author())),
736 resolveMentions(quote
.mentions())));
740 private ArrayList
<SignalServiceDataMessage
.Mention
> resolveMentions(final List
<Message
.Mention
> mentionList
) throws IOException
{
741 final var mentions
= new ArrayList
<SignalServiceDataMessage
.Mention
>();
742 for (final var m
: mentionList
) {
743 final var recipientId
= resolveRecipient(m
.recipient());
744 mentions
.add(new SignalServiceDataMessage
.Mention(resolveSignalServiceAddress(recipientId
).getAci(),
752 public SendMessageResults
sendRemoteDeleteMessage(
753 long targetSentTimestamp
, Set
<RecipientIdentifier
> recipients
754 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
755 var delete
= new SignalServiceDataMessage
.RemoteDelete(targetSentTimestamp
);
756 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withRemoteDelete(delete
);
757 return sendMessage(messageBuilder
, recipients
);
761 public SendMessageResults
sendMessageReaction(
764 RecipientIdentifier
.Single targetAuthor
,
765 long targetSentTimestamp
,
766 Set
<RecipientIdentifier
> recipients
767 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
768 var targetAuthorRecipientId
= resolveRecipient(targetAuthor
);
769 var reaction
= new SignalServiceDataMessage
.Reaction(emoji
,
771 resolveSignalServiceAddress(targetAuthorRecipientId
),
772 targetSentTimestamp
);
773 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().withReaction(reaction
);
774 return sendMessage(messageBuilder
, recipients
);
778 public SendMessageResults
sendEndSessionMessage(Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
779 var messageBuilder
= SignalServiceDataMessage
.newBuilder().asEndSessionMessage();
782 return sendMessage(messageBuilder
,
783 recipients
.stream().map(RecipientIdentifier
.class::cast
).collect(Collectors
.toSet()));
784 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
785 throw new AssertionError(e
);
787 for (var recipient
: recipients
) {
788 final var recipientId
= resolveRecipient(recipient
);
789 account
.getSessionStore().deleteAllSessions(recipientId
);
795 public void deleteRecipient(final RecipientIdentifier
.Single recipient
) throws IOException
{
796 account
.removeRecipient(resolveRecipient(recipient
));
800 public void deleteContact(final RecipientIdentifier
.Single recipient
) throws IOException
{
801 account
.getContactStore().deleteContact(resolveRecipient(recipient
));
805 public void setContactName(
806 RecipientIdentifier
.Single recipient
, String name
807 ) throws NotMasterDeviceException
, IOException
{
808 if (!account
.isMasterDevice()) {
809 throw new NotMasterDeviceException();
811 contactHelper
.setContactName(resolveRecipient(recipient
), name
);
815 public void setContactBlocked(
816 RecipientIdentifier
.Single recipient
, boolean blocked
817 ) throws NotMasterDeviceException
, IOException
{
818 if (!account
.isMasterDevice()) {
819 throw new NotMasterDeviceException();
821 contactHelper
.setContactBlocked(resolveRecipient(recipient
), blocked
);
822 // TODO cycle our profile key
823 syncHelper
.sendBlockedList();
827 public void setGroupBlocked(
828 final GroupId groupId
, final boolean blocked
829 ) throws GroupNotFoundException
, IOException
, NotMasterDeviceException
{
830 if (!account
.isMasterDevice()) {
831 throw new NotMasterDeviceException();
833 groupHelper
.setGroupBlocked(groupId
, blocked
);
834 // TODO cycle our profile key
835 syncHelper
.sendBlockedList();
839 * Change the expiration timer for a contact
842 public void setExpirationTimer(
843 RecipientIdentifier
.Single recipient
, int messageExpirationTimer
844 ) throws IOException
{
845 var recipientId
= resolveRecipient(recipient
);
846 contactHelper
.setExpirationTimer(recipientId
, messageExpirationTimer
);
847 final var messageBuilder
= SignalServiceDataMessage
.newBuilder().asExpirationUpdate();
849 sendMessage(messageBuilder
, Set
.of(recipient
));
850 } catch (NotAGroupMemberException
| GroupNotFoundException
| GroupSendingNotAllowedException e
) {
851 throw new AssertionError(e
);
856 * Upload the sticker pack from path.
858 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
859 * @return if successful, returns the URL to install the sticker pack in the signal app
862 public URI
uploadStickerPack(File path
) throws IOException
, StickerPackInvalidException
{
863 var manifest
= StickerUtils
.getSignalServiceStickerManifestUpload(path
);
865 var messageSender
= dependencies
.getMessageSender();
867 var packKey
= KeyUtils
.createStickerUploadKey();
868 var packIdString
= messageSender
.uploadStickerManifest(manifest
, packKey
);
869 var packId
= StickerPackId
.deserialize(Hex
.fromStringCondensed(packIdString
));
871 var sticker
= new Sticker(packId
, packKey
);
872 account
.getStickerStore().updateSticker(sticker
);
875 return new URI("https",
879 + URLEncoder
.encode(Hex
.toStringCondensed(packId
.serialize()), StandardCharsets
.UTF_8
)
881 + URLEncoder
.encode(Hex
.toStringCondensed(packKey
), StandardCharsets
.UTF_8
));
882 } catch (URISyntaxException e
) {
883 throw new AssertionError(e
);
888 public void requestAllSyncData() throws IOException
{
889 syncHelper
.requestAllSyncData();
890 retrieveRemoteStorage();
893 void retrieveRemoteStorage() throws IOException
{
894 if (account
.getStorageKey() != null) {
895 storageHelper
.readDataFromStorage();
899 private RecipientId
refreshRegisteredUser(RecipientId recipientId
) throws IOException
{
900 final var address
= resolveSignalServiceAddress(recipientId
);
901 if (!address
.getNumber().isPresent()) {
904 final var number
= address
.getNumber().get();
905 final var uuid
= getRegisteredUser(number
);
906 return resolveRecipientTrusted(new SignalServiceAddress(uuid
, number
));
909 private ACI
getRegisteredUser(final String number
) throws IOException
{
910 final Map
<String
, ACI
> aciMap
;
912 aciMap
= getRegisteredUsers(Set
.of(number
));
913 } catch (NumberFormatException e
) {
914 throw new IOException(number
, e
);
916 final var uuid
= aciMap
.get(number
);
918 throw new IOException(number
, null);
923 private Map
<String
, ACI
> getRegisteredUsers(final Set
<String
> numbers
) throws IOException
{
924 final Map
<String
, ACI
> registeredUsers
;
926 registeredUsers
= dependencies
.getAccountManager()
927 .getRegisteredUsers(ServiceConfig
.getIasKeyStore(),
929 serviceEnvironmentConfig
.getCdsMrenclave());
930 } catch (Quote
.InvalidQuoteFormatException
| UnauthenticatedQuoteException
| SignatureException
| UnauthenticatedResponseException
| InvalidKeyException e
) {
931 throw new IOException(e
);
934 // Store numbers as recipients, so we have the number/uuid association
935 registeredUsers
.forEach((number
, aci
) -> resolveRecipientTrusted(new SignalServiceAddress(aci
, number
)));
937 return registeredUsers
;
940 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
) {
941 Set
<HandleAction
> queuedActions
= new HashSet
<>();
942 for (var cachedMessage
: account
.getMessageCache().getCachedMessages()) {
943 var actions
= retryFailedReceivedMessage(handler
, cachedMessage
);
944 if (actions
!= null) {
945 queuedActions
.addAll(actions
);
948 handleQueuedActions(queuedActions
);
951 private List
<HandleAction
> retryFailedReceivedMessage(
952 final ReceiveMessageHandler handler
, final CachedMessage cachedMessage
954 var envelope
= cachedMessage
.loadEnvelope();
955 if (envelope
== null) {
956 cachedMessage
.delete();
960 final var result
= incomingMessageHandler
.handleRetryEnvelope(envelope
, ignoreAttachments
, handler
);
961 final var actions
= result
.first();
962 final var exception
= result
.second();
964 if (exception
instanceof UntrustedIdentityException
) {
965 if (System
.currentTimeMillis() - envelope
.getServerDeliveredTimestamp() > 1000L * 60 * 60 * 24 * 30) {
966 // Envelope is more than a month old, cleaning up.
967 cachedMessage
.delete();
970 if (!envelope
.hasSourceUuid()) {
971 final var identifier
= ((UntrustedIdentityException
) exception
).getSender();
972 final var recipientId
= account
.getRecipientStore().resolveRecipient(identifier
);
974 account
.getMessageCache().replaceSender(cachedMessage
, recipientId
);
975 } catch (IOException ioException
) {
976 logger
.warn("Failed to move cached message to recipient folder: {}", ioException
.getMessage());
982 // If successful and for all other errors that are not recoverable, delete the cached message
983 cachedMessage
.delete();
988 public void addReceiveHandler(final ReceiveMessageHandler handler
, final boolean isWeakListener
) {
989 if (isReceivingSynchronous
) {
990 throw new IllegalStateException("Already receiving message synchronously.");
992 synchronized (messageHandlers
) {
993 if (isWeakListener
) {
994 weakHandlers
.add(handler
);
996 messageHandlers
.add(handler
);
997 startReceiveThreadIfRequired();
1002 private void startReceiveThreadIfRequired() {
1003 if (receiveThread
!= null) {
1006 receiveThread
= new Thread(() -> {
1007 logger
.debug("Starting receiving messages");
1008 while (!Thread
.interrupted()) {
1010 receiveMessagesInternal(Duration
.ofMinutes(1), false, (envelope
, e
) -> {
1011 synchronized (messageHandlers
) {
1012 Stream
.concat(messageHandlers
.stream(), weakHandlers
.stream()).forEach(h
-> {
1014 h
.handleMessage(envelope
, e
);
1015 } catch (Exception ex
) {
1016 logger
.warn("Message handler failed, ignoring", ex
);
1022 } catch (IOException e
) {
1023 logger
.warn("Receiving messages failed, retrying", e
);
1026 logger
.debug("Finished receiving messages");
1027 hasCaughtUpWithOldMessages
= false;
1028 synchronized (messageHandlers
) {
1029 receiveThread
= null;
1031 // Check if in the meantime another handler has been registered
1032 if (!messageHandlers
.isEmpty()) {
1033 logger
.debug("Another handler has been registered, starting receive thread again");
1034 startReceiveThreadIfRequired();
1039 receiveThread
.start();
1043 public void removeReceiveHandler(final ReceiveMessageHandler handler
) {
1044 final Thread thread
;
1045 synchronized (messageHandlers
) {
1046 weakHandlers
.remove(handler
);
1047 messageHandlers
.remove(handler
);
1048 if (!messageHandlers
.isEmpty() || receiveThread
== null || isReceivingSynchronous
) {
1051 thread
= receiveThread
;
1052 receiveThread
= null;
1055 stopReceiveThread(thread
);
1058 private void stopReceiveThread(final Thread thread
) {
1062 } catch (InterruptedException ignored
) {
1067 public boolean isReceiving() {
1068 if (isReceivingSynchronous
) {
1071 synchronized (messageHandlers
) {
1072 return messageHandlers
.size() > 0;
1077 public void receiveMessages(Duration timeout
, ReceiveMessageHandler handler
) throws IOException
{
1078 receiveMessages(timeout
, true, handler
);
1082 public void receiveMessages(ReceiveMessageHandler handler
) throws IOException
{
1083 receiveMessages(Duration
.ofMinutes(1), false, handler
);
1086 private void receiveMessages(
1087 Duration timeout
, boolean returnOnTimeout
, ReceiveMessageHandler handler
1088 ) throws IOException
{
1089 if (isReceiving()) {
1090 throw new IllegalStateException("Already receiving message.");
1092 isReceivingSynchronous
= true;
1093 receiveThread
= Thread
.currentThread();
1095 receiveMessagesInternal(timeout
, returnOnTimeout
, handler
);
1097 receiveThread
= null;
1098 hasCaughtUpWithOldMessages
= false;
1099 isReceivingSynchronous
= false;
1103 private void receiveMessagesInternal(
1104 Duration timeout
, boolean returnOnTimeout
, ReceiveMessageHandler handler
1105 ) throws IOException
{
1106 needsToRetryFailedMessages
= true;
1108 // Use a Map here because java Set doesn't have a get method ...
1109 Map
<HandleAction
, HandleAction
> queuedActions
= new HashMap
<>();
1111 final var signalWebSocket
= dependencies
.getSignalWebSocket();
1112 final var webSocketStateDisposable
= Observable
.merge(signalWebSocket
.getUnidentifiedWebSocketState(),
1113 signalWebSocket
.getWebSocketState())
1114 .subscribeOn(Schedulers
.computation())
1115 .observeOn(Schedulers
.computation())
1116 .distinctUntilChanged()
1117 .subscribe(this::onWebSocketStateChange
);
1118 signalWebSocket
.connect();
1120 hasCaughtUpWithOldMessages
= false;
1121 var backOffCounter
= 0;
1122 final var MAX_BACKOFF_COUNTER
= 9;
1124 while (!Thread
.interrupted()) {
1125 if (needsToRetryFailedMessages
) {
1126 retryFailedReceivedMessages(handler
);
1127 needsToRetryFailedMessages
= false;
1129 SignalServiceEnvelope envelope
;
1130 final CachedMessage
[] cachedMessage
= {null};
1131 final var nowMillis
= System
.currentTimeMillis();
1132 if (nowMillis
- account
.getLastReceiveTimestamp() > 60000) {
1133 account
.setLastReceiveTimestamp(nowMillis
);
1135 logger
.debug("Checking for new message from server");
1137 var result
= signalWebSocket
.readOrEmpty(timeout
.toMillis(), envelope1
-> {
1138 final var recipientId
= envelope1
.hasSourceUuid()
1139 ?
resolveRecipient(envelope1
.getSourceAddress())
1141 // store message on disk, before acknowledging receipt to the server
1142 cachedMessage
[0] = account
.getMessageCache().cacheMessage(envelope1
, recipientId
);
1146 if (result
.isPresent()) {
1147 envelope
= result
.get();
1148 logger
.debug("New message received from server");
1150 logger
.debug("Received indicator that server queue is empty");
1151 handleQueuedActions(queuedActions
.keySet());
1152 queuedActions
.clear();
1154 hasCaughtUpWithOldMessages
= true;
1155 synchronized (this) {
1159 // Continue to wait another timeout for new messages
1162 } catch (AssertionError e
) {
1163 if (e
.getCause() instanceof InterruptedException
) {
1164 Thread
.currentThread().interrupt();
1169 } catch (IOException e
) {
1170 logger
.debug("Pipe unexpectedly unavailable: {}", e
.getMessage());
1171 if (e
instanceof WebSocketUnavailableException
|| "Connection closed!".equals(e
.getMessage())) {
1172 final var sleepMilliseconds
= 100 * (long) Math
.pow(2, backOffCounter
);
1173 backOffCounter
= Math
.min(backOffCounter
+ 1, MAX_BACKOFF_COUNTER
);
1174 logger
.warn("Connection closed unexpectedly, reconnecting in {} ms", sleepMilliseconds
);
1176 Thread
.sleep(sleepMilliseconds
);
1177 } catch (InterruptedException interruptedException
) {
1180 hasCaughtUpWithOldMessages
= false;
1181 signalWebSocket
.connect();
1185 } catch (TimeoutException e
) {
1187 if (returnOnTimeout
) return;
1191 final var result
= incomingMessageHandler
.handleEnvelope(envelope
, ignoreAttachments
, handler
);
1192 for (final var h
: result
.first()) {
1193 final var existingAction
= queuedActions
.get(h
);
1194 if (existingAction
== null) {
1195 queuedActions
.put(h
, h
);
1197 existingAction
.mergeOther(h
);
1200 final var exception
= result
.second();
1202 if (hasCaughtUpWithOldMessages
) {
1203 handleQueuedActions(queuedActions
.keySet());
1204 queuedActions
.clear();
1206 if (cachedMessage
[0] != null) {
1207 if (exception
instanceof UntrustedIdentityException
) {
1208 logger
.debug("Keeping message with untrusted identity in message cache");
1209 final var address
= ((UntrustedIdentityException
) exception
).getSender();
1210 final var recipientId
= resolveRecipient(address
);
1211 if (!envelope
.hasSourceUuid()) {
1213 cachedMessage
[0] = account
.getMessageCache().replaceSender(cachedMessage
[0], recipientId
);
1214 } catch (IOException ioException
) {
1215 logger
.warn("Failed to move cached message to recipient folder: {}",
1216 ioException
.getMessage());
1220 cachedMessage
[0].delete();
1224 handleQueuedActions(queuedActions
.keySet());
1225 queuedActions
.clear();
1226 dependencies
.getSignalWebSocket().disconnect();
1227 webSocketStateDisposable
.dispose();
1230 private void onWebSocketStateChange(final WebSocketConnectionState s
) {
1231 if (s
.equals(WebSocketConnectionState
.AUTHENTICATION_FAILED
)) {
1232 account
.setRegistered(false);
1235 } catch (IOException e
) {
1236 e
.printStackTrace();
1242 public void setIgnoreAttachments(final boolean ignoreAttachments
) {
1243 this.ignoreAttachments
= ignoreAttachments
;
1247 public boolean hasCaughtUpWithOldMessages() {
1248 return hasCaughtUpWithOldMessages
;
1251 private void handleQueuedActions(final Collection
<HandleAction
> queuedActions
) {
1252 logger
.debug("Handling message actions");
1253 var interrupted
= false;
1254 for (var action
: queuedActions
) {
1256 action
.execute(context
);
1257 } catch (Throwable e
) {
1258 if ((e
instanceof AssertionError
|| e
instanceof RuntimeException
)
1259 && e
.getCause() instanceof InterruptedException
) {
1263 logger
.warn("Message action failed.", e
);
1267 Thread
.currentThread().interrupt();
1272 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
1273 final RecipientId recipientId
;
1275 recipientId
= resolveRecipient(recipient
);
1276 } catch (IOException e
) {
1279 return contactHelper
.isContactBlocked(recipientId
);
1283 public void sendContacts() throws IOException
{
1284 syncHelper
.sendContacts();
1288 public List
<Pair
<RecipientAddress
, Contact
>> getContacts() {
1289 return account
.getContactStore()
1292 .map(p
-> new Pair
<>(account
.getRecipientStore().resolveRecipientAddress(p
.first()), p
.second()))
1297 public String
getContactOrProfileName(RecipientIdentifier
.Single recipient
) {
1298 final RecipientId recipientId
;
1300 recipientId
= resolveRecipient(recipient
);
1301 } catch (IOException e
) {
1305 final var contact
= account
.getContactStore().getContact(recipientId
);
1306 if (contact
!= null && !Util
.isEmpty(contact
.getName())) {
1307 return contact
.getName();
1310 final var profile
= getRecipientProfile(recipientId
);
1311 if (profile
!= null) {
1312 return profile
.getDisplayName();
1319 public Group
getGroup(GroupId groupId
) {
1320 return toGroup(groupHelper
.getGroup(groupId
));
1323 private GroupInfo
getGroupInfo(GroupId groupId
) {
1324 return groupHelper
.getGroup(groupId
);
1328 public List
<Identity
> getIdentities() {
1329 return account
.getIdentityKeyStore().getIdentities().stream().map(this::toIdentity
).toList();
1332 private Identity
toIdentity(final IdentityInfo identityInfo
) {
1333 if (identityInfo
== null) {
1337 final var address
= account
.getRecipientStore().resolveRecipientAddress(identityInfo
.getRecipientId());
1338 final var scannableFingerprint
= identityHelper
.computeSafetyNumberForScanning(identityInfo
.getRecipientId(),
1339 identityInfo
.getIdentityKey());
1340 return new Identity(address
,
1341 identityInfo
.getIdentityKey(),
1342 identityHelper
.computeSafetyNumber(identityInfo
.getRecipientId(), identityInfo
.getIdentityKey()),
1343 scannableFingerprint
== null ?
null : scannableFingerprint
.getSerialized(),
1344 identityInfo
.getTrustLevel(),
1345 identityInfo
.getDateAdded());
1349 public List
<Identity
> getIdentities(RecipientIdentifier
.Single recipient
) {
1350 IdentityInfo identity
;
1352 identity
= account
.getIdentityKeyStore().getIdentity(resolveRecipient(recipient
));
1353 } catch (IOException e
) {
1356 return identity
== null ? List
.of() : List
.of(toIdentity(identity
));
1360 * Trust this the identity with this fingerprint
1362 * @param recipient account of the identity
1363 * @param fingerprint Fingerprint
1366 public boolean trustIdentityVerified(RecipientIdentifier
.Single recipient
, byte[] fingerprint
) {
1367 RecipientId recipientId
;
1369 recipientId
= resolveRecipient(recipient
);
1370 } catch (IOException e
) {
1373 final var updated
= identityHelper
.trustIdentityVerified(recipientId
, fingerprint
);
1374 if (updated
&& this.isReceiving()) {
1375 needsToRetryFailedMessages
= true;
1381 * Trust this the identity with this safety number
1383 * @param recipient account of the identity
1384 * @param safetyNumber Safety number
1387 public boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier
.Single recipient
, String safetyNumber
) {
1388 RecipientId recipientId
;
1390 recipientId
= resolveRecipient(recipient
);
1391 } catch (IOException e
) {
1394 final var updated
= identityHelper
.trustIdentityVerifiedSafetyNumber(recipientId
, safetyNumber
);
1395 if (updated
&& this.isReceiving()) {
1396 needsToRetryFailedMessages
= true;
1402 * Trust this the identity with this scannable safety number
1404 * @param recipient account of the identity
1405 * @param safetyNumber Scannable safety number
1408 public boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier
.Single recipient
, byte[] safetyNumber
) {
1409 RecipientId recipientId
;
1411 recipientId
= resolveRecipient(recipient
);
1412 } catch (IOException e
) {
1415 final var updated
= identityHelper
.trustIdentityVerifiedSafetyNumber(recipientId
, safetyNumber
);
1416 if (updated
&& this.isReceiving()) {
1417 needsToRetryFailedMessages
= true;
1423 * Trust all keys of this identity without verification
1425 * @param recipient account of the identity
1428 public boolean trustIdentityAllKeys(RecipientIdentifier
.Single recipient
) {
1429 RecipientId recipientId
;
1431 recipientId
= resolveRecipient(recipient
);
1432 } catch (IOException e
) {
1435 final var updated
= identityHelper
.trustIdentityAllKeys(recipientId
);
1436 if (updated
&& this.isReceiving()) {
1437 needsToRetryFailedMessages
= true;
1443 public void addClosedListener(final Runnable listener
) {
1444 synchronized (closedListeners
) {
1445 closedListeners
.add(listener
);
1449 private void handleIdentityFailure(
1450 final RecipientId recipientId
,
1451 final org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
.IdentityFailure identityFailure
1453 this.identityHelper
.handleIdentityFailure(recipientId
, identityFailure
);
1456 private SignalServiceAddress
resolveSignalServiceAddress(RecipientId recipientId
) {
1457 final var address
= account
.getRecipientStore().resolveRecipientAddress(recipientId
);
1458 if (address
.uuid().isPresent()) {
1459 return address
.toSignalServiceAddress();
1462 // Address in recipient store doesn't have a uuid, this shouldn't happen
1463 // Try to retrieve the uuid from the server
1464 final var number
= address
.number().get();
1467 aci
= getRegisteredUser(number
);
1468 } catch (IOException e
) {
1469 logger
.warn("Failed to get uuid for e164 number: {}", number
, e
);
1470 // Return SignalServiceAddress with unknown UUID
1471 return address
.toSignalServiceAddress();
1473 return resolveSignalServiceAddress(account
.getRecipientStore().resolveRecipient(aci
));
1476 private Set
<RecipientId
> resolveRecipients(Collection
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
1477 final var recipientIds
= new HashSet
<RecipientId
>(recipients
.size());
1478 for (var number
: recipients
) {
1479 final var recipientId
= resolveRecipient(number
);
1480 recipientIds
.add(recipientId
);
1482 return recipientIds
;
1485 private RecipientId
resolveRecipient(final RecipientIdentifier
.Single recipient
) throws IOException
{
1486 if (recipient
instanceof RecipientIdentifier
.Uuid uuidRecipient
) {
1487 return account
.getRecipientStore().resolveRecipient(ACI
.from(uuidRecipient
.uuid()));
1489 final var number
= ((RecipientIdentifier
.Number
) recipient
).number();
1490 return account
.getRecipientStore().resolveRecipient(number
, () -> {
1492 return getRegisteredUser(number
);
1493 } catch (IOException e
) {
1500 private RecipientId
resolveRecipient(RecipientAddress address
) {
1501 return account
.getRecipientStore().resolveRecipient(address
);
1504 private RecipientId
resolveRecipient(SignalServiceAddress address
) {
1505 return account
.getRecipientStore().resolveRecipient(address
);
1508 private RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
1509 return account
.getRecipientStore().resolveRecipientTrusted(address
);
1513 public void close() throws IOException
{
1515 synchronized (messageHandlers
) {
1516 weakHandlers
.clear();
1517 messageHandlers
.clear();
1518 thread
= receiveThread
;
1519 receiveThread
= null;
1521 if (thread
!= null) {
1522 stopReceiveThread(thread
);
1524 executor
.shutdown();
1526 dependencies
.getSignalWebSocket().disconnect();
1528 synchronized (closedListeners
) {
1529 closedListeners
.forEach(Runnable
::run
);
1530 closedListeners
.clear();
1533 if (account
!= null) {