X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/89f568dd1f4bdf7c2c55b705ed20911fef41a076..c88c92086efcf5c0ad417589db997ef1a034e775:/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java diff --git a/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java b/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java index df4820cc..2f58657f 100644 --- a/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java +++ b/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java @@ -30,8 +30,8 @@ import org.asamk.signal.manager.api.SendGroupMessageResults; import org.asamk.signal.manager.api.SendMessageResult; import org.asamk.signal.manager.api.SendMessageResults; import org.asamk.signal.manager.api.TypingAction; +import org.asamk.signal.manager.api.UnregisteredRecipientException; import org.asamk.signal.manager.api.UpdateGroup; -import org.asamk.signal.manager.config.ServiceConfig; import org.asamk.signal.manager.config.ServiceEnvironmentConfig; import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupInviteLinkUrl; @@ -48,6 +48,7 @@ import org.asamk.signal.manager.helper.IncomingMessageHandler; import org.asamk.signal.manager.helper.PinHelper; import org.asamk.signal.manager.helper.PreKeyHelper; import org.asamk.signal.manager.helper.ProfileHelper; +import org.asamk.signal.manager.helper.RecipientHelper; import org.asamk.signal.manager.helper.SendHelper; import org.asamk.signal.manager.helper.StorageHelper; import org.asamk.signal.manager.helper.SyncHelper; @@ -76,14 +77,12 @@ import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage; import org.whispersystems.signalservice.api.messages.SignalServiceTypingMessage; import org.whispersystems.signalservice.api.push.ACI; -import org.whispersystems.signalservice.api.push.SignalServiceAddress; +import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException; import org.whispersystems.signalservice.api.util.DeviceNameUtil; import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.PhoneNumberFormatter; +import org.whispersystems.signalservice.api.websocket.WebSocketConnectionState; import org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException; -import org.whispersystems.signalservice.internal.contacts.crypto.Quote; -import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedQuoteException; -import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException; import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider; import org.whispersystems.signalservice.internal.util.Hex; import org.whispersystems.signalservice.internal.util.Util; @@ -94,7 +93,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.security.SignatureException; +import java.time.Duration; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -111,13 +110,15 @@ import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; import java.util.stream.Stream; +import io.reactivex.rxjava3.core.Observable; +import io.reactivex.rxjava3.schedulers.Schedulers; + import static org.asamk.signal.manager.config.ServiceConfig.capabilities; public class ManagerImpl implements Manager { private final static Logger logger = LoggerFactory.getLogger(ManagerImpl.class); - private final ServiceEnvironmentConfig serviceEnvironmentConfig; private final SignalDependencies dependencies; private SignalAccount account; @@ -135,6 +136,7 @@ public class ManagerImpl implements Manager { private final IncomingMessageHandler incomingMessageHandler; private final PreKeyHelper preKeyHelper; private final IdentityHelper identityHelper; + private final RecipientHelper recipientHelper; private final Context context; private boolean hasCaughtUpWithOldMessages = false; @@ -145,6 +147,7 @@ public class ManagerImpl implements Manager { private final Set messageHandlers = new HashSet<>(); private final List closedListeners = new ArrayList<>(); private boolean isReceivingSynchronous; + private boolean needsToRetryFailedMessages = false; ManagerImpl( SignalAccount account, @@ -153,7 +156,6 @@ public class ManagerImpl implements Manager { String userAgent ) { this.account = account; - this.serviceEnvironmentConfig = serviceEnvironmentConfig; final var credentialsProvider = new DynamicCredentialsProvider(account.getAci(), account.getAccount(), @@ -184,32 +186,33 @@ public class ManagerImpl implements Manager { dependencies, account::getProfileKey, this::getRecipientProfile); + this.recipientHelper = new RecipientHelper(account, dependencies, serviceEnvironmentConfig); this.profileHelper = new ProfileHelper(account, dependencies, avatarStore, unidentifiedAccessHelper::getAccessFor, - this::resolveSignalServiceAddress); - final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper::getRecipientProfileKeyCredential, - this::getRecipientProfile, + recipientHelper::resolveSignalServiceAddress); + final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper, account::getSelfRecipientId, dependencies.getGroupsV2Operations(), dependencies.getGroupsV2Api(), - this::resolveSignalServiceAddress); + recipientHelper::resolveSignalServiceAddress); this.sendHelper = new SendHelper(account, dependencies, unidentifiedAccessHelper, - this::resolveSignalServiceAddress, + recipientHelper::resolveSignalServiceAddress, account.getRecipientStore(), this::handleIdentityFailure, this::getGroupInfo, - this::refreshRegisteredUser); + profileHelper, + recipientHelper::refreshRegisteredUser); this.groupHelper = new GroupHelper(account, dependencies, attachmentHelper, sendHelper, groupV2Helper, avatarStore, - this::resolveSignalServiceAddress, + recipientHelper::resolveSignalServiceAddress, account.getRecipientStore()); this.storageHelper = new StorageHelper(account, dependencies, groupHelper, profileHelper); this.contactHelper = new ContactHelper(account); @@ -218,7 +221,7 @@ public class ManagerImpl implements Manager { sendHelper, groupHelper, avatarStore, - this::resolveSignalServiceAddress); + recipientHelper::resolveSignalServiceAddress); preKeyHelper = new PreKeyHelper(account, dependencies); this.context = new Context(account, @@ -235,16 +238,16 @@ public class ManagerImpl implements Manager { this.incomingMessageHandler = new IncomingMessageHandler(account, dependencies, account.getRecipientStore(), - this::resolveSignalServiceAddress, + recipientHelper::resolveSignalServiceAddress, groupHelper, contactHelper, attachmentHelper, syncHelper, - this::getRecipientProfile, + profileHelper::getRecipientProfile, jobExecutor); this.identityHelper = new IdentityHelper(account, dependencies, - this::resolveSignalServiceAddress, + recipientHelper::resolveSignalServiceAddress, syncHelper, profileHelper); } @@ -267,11 +270,16 @@ public class ManagerImpl implements Manager { days); } } - preKeyHelper.refreshPreKeysIfNecessary(); - if (account.getAci() == null) { - account.setAci(dependencies.getAccountManager().getOwnAci()); + try { + preKeyHelper.refreshPreKeysIfNecessary(); + if (account.getAci() == null) { + account.setAci(ACI.parseOrNull(dependencies.getAccountManager().getWhoAmI().getAci())); + } + updateAccountAttributes(null); + } catch (AuthorizationFailedException e) { + account.setRegistered(false); + throw e; } - updateAccountAttributes(null); } /** @@ -279,23 +287,28 @@ public class ManagerImpl implements Manager { * * @param numbers The set of phone number in question * @return A map of numbers to canonicalized number and uuid. If a number is not registered the uuid is null. - * @throws IOException if its unable to get the contacts to check if they're registered + * @throws IOException if it's unable to get the contacts to check if they're registered */ @Override public Map> areUsersRegistered(Set numbers) throws IOException { - Map canonicalizedNumbers = numbers.stream().collect(Collectors.toMap(n -> n, n -> { + final var canonicalizedNumbers = numbers.stream().collect(Collectors.toMap(n -> n, n -> { try { - return PhoneNumberFormatter.formatNumber(n, account.getAccount()); + final var canonicalizedNumber = PhoneNumberFormatter.formatNumber(n, account.getAccount()); + if (!canonicalizedNumber.equals(n)) { + logger.debug("Normalized number {} to {}.", n, canonicalizedNumber); + } + return canonicalizedNumber; } catch (InvalidNumberException e) { return ""; } })); // Note "registeredUsers" has no optionals. It only gives us info on users who are registered - var registeredUsers = getRegisteredUsers(canonicalizedNumbers.values() + final var canonicalizedNumbersSet = canonicalizedNumbers.values() .stream() .filter(s -> !s.isEmpty()) - .collect(Collectors.toSet())); + .collect(Collectors.toSet()); + final var registeredUsers = recipientHelper.getRegisteredUsers(canonicalizedNumbersSet); return numbers.stream().collect(Collectors.toMap(n -> n, n -> { final var number = canonicalizedNumbers.get(n); @@ -431,7 +444,7 @@ public class ManagerImpl implements Manager { d.getCreated(), d.getLastSeen(), d.getId() == account.getDeviceId()); - }).collect(Collectors.toList()); + }).toList(); } @Override @@ -493,8 +506,8 @@ public class ManagerImpl implements Manager { } @Override - public Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws IOException { - return profileHelper.getRecipientProfile(resolveRecipient(recipient)); + public Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException { + return profileHelper.getRecipientProfile(recipientHelper.resolveRecipient(recipient)); } private Profile getRecipientProfile(RecipientId recipientId) { @@ -503,7 +516,7 @@ public class ManagerImpl implements Manager { @Override public List getGroups() { - return account.getGroupStore().getGroups().stream().map(this::toGroup).collect(Collectors.toList()); + return account.getGroupStore().getGroups().stream().map(this::toGroup).toList(); } private Group toGroup(final GroupInfo groupInfo) { @@ -543,8 +556,8 @@ public class ManagerImpl implements Manager { @Override public SendGroupMessageResults quitGroup( GroupId groupId, Set groupAdmins - ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException { - final var newAdmins = resolveRecipients(groupAdmins); + ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException, UnregisteredRecipientException { + final var newAdmins = recipientHelper.resolveRecipients(groupAdmins); return groupHelper.quitGroup(groupId, newAdmins); } @@ -556,21 +569,27 @@ public class ManagerImpl implements Manager { @Override public Pair createGroup( String name, Set members, File avatarFile - ) throws IOException, AttachmentInvalidException { - return groupHelper.createGroup(name, members == null ? null : resolveRecipients(members), avatarFile); + ) throws IOException, AttachmentInvalidException, UnregisteredRecipientException { + return groupHelper.createGroup(name, + members == null ? null : recipientHelper.resolveRecipients(members), + avatarFile); } @Override public SendGroupMessageResults updateGroup( final GroupId groupId, final UpdateGroup updateGroup - ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException { + ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException, UnregisteredRecipientException { return groupHelper.updateGroup(groupId, updateGroup.getName(), updateGroup.getDescription(), - updateGroup.getMembers() == null ? null : resolveRecipients(updateGroup.getMembers()), - updateGroup.getRemoveMembers() == null ? null : resolveRecipients(updateGroup.getRemoveMembers()), - updateGroup.getAdmins() == null ? null : resolveRecipients(updateGroup.getAdmins()), - updateGroup.getRemoveAdmins() == null ? null : resolveRecipients(updateGroup.getRemoveAdmins()), + updateGroup.getMembers() == null ? null : recipientHelper.resolveRecipients(updateGroup.getMembers()), + updateGroup.getRemoveMembers() == null + ? null + : recipientHelper.resolveRecipients(updateGroup.getRemoveMembers()), + updateGroup.getAdmins() == null ? null : recipientHelper.resolveRecipients(updateGroup.getAdmins()), + updateGroup.getRemoveAdmins() == null + ? null + : recipientHelper.resolveRecipients(updateGroup.getRemoveAdmins()), updateGroup.isResetGroupLink(), updateGroup.getGroupLinkState(), updateGroup.getAddMemberPermission(), @@ -595,12 +614,17 @@ public class ManagerImpl implements Manager { messageBuilder.withTimestamp(timestamp); for (final var recipient : recipients) { if (recipient instanceof RecipientIdentifier.Single single) { - final var recipientId = resolveRecipient(single); - final var result = sendHelper.sendMessage(messageBuilder, recipientId); - results.put(recipient, - List.of(SendMessageResult.from(result, - account.getRecipientStore(), - account.getRecipientStore()::resolveRecipientAddress))); + try { + final var recipientId = recipientHelper.resolveRecipient(single); + final var result = sendHelper.sendMessage(messageBuilder, recipientId); + results.put(recipient, + List.of(SendMessageResult.from(result, + account.getRecipientStore(), + account.getRecipientStore()::resolveRecipientAddress))); + } catch (UnregisteredRecipientException e) { + results.put(recipient, + List.of(SendMessageResult.unregisteredFailure(single.toPartialRecipientAddress()))); + } } else if (recipient instanceof RecipientIdentifier.NoteToSelf) { final var result = sendHelper.sendSelfMessage(messageBuilder); results.put(recipient, @@ -614,62 +638,99 @@ public class ManagerImpl implements Manager { .map(sendMessageResult -> SendMessageResult.from(sendMessageResult, account.getRecipientStore(), account.getRecipientStore()::resolveRecipientAddress)) - .collect(Collectors.toList())); + .toList()); } } return new SendMessageResults(timestamp, results); } - private void sendTypingMessage( + private SendMessageResults sendTypingMessage( SignalServiceTypingMessage.Action action, Set recipients - ) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { + ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { + var results = new HashMap>(); final var timestamp = System.currentTimeMillis(); for (var recipient : recipients) { - if (recipient instanceof RecipientIdentifier.Single) { + if (recipient instanceof RecipientIdentifier.Single single) { final var message = new SignalServiceTypingMessage(action, timestamp, Optional.absent()); - final var recipientId = resolveRecipient((RecipientIdentifier.Single) recipient); - sendHelper.sendTypingMessage(message, recipientId); + try { + final var recipientId = recipientHelper.resolveRecipient(single); + final var result = sendHelper.sendTypingMessage(message, recipientId); + results.put(recipient, + List.of(SendMessageResult.from(result, + account.getRecipientStore(), + account.getRecipientStore()::resolveRecipientAddress))); + } catch (UnregisteredRecipientException e) { + results.put(recipient, + List.of(SendMessageResult.unregisteredFailure(single.toPartialRecipientAddress()))); + } } else if (recipient instanceof RecipientIdentifier.Group) { final var groupId = ((RecipientIdentifier.Group) recipient).groupId(); final var message = new SignalServiceTypingMessage(action, timestamp, Optional.of(groupId.serialize())); - sendHelper.sendGroupTypingMessage(message, groupId); + final var result = sendHelper.sendGroupTypingMessage(message, groupId); + results.put(recipient, + result.stream() + .map(r -> SendMessageResult.from(r, + account.getRecipientStore(), + account.getRecipientStore()::resolveRecipientAddress)) + .toList()); } } + return new SendMessageResults(timestamp, results); } @Override - public void sendTypingMessage( + public SendMessageResults sendTypingMessage( TypingAction action, Set recipients - ) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { - sendTypingMessage(action.toSignalService(), recipients); + ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { + return sendTypingMessage(action.toSignalService(), recipients); } @Override - public void sendReadReceipt( + public SendMessageResults sendReadReceipt( RecipientIdentifier.Single sender, List messageIds - ) throws IOException, UntrustedIdentityException { + ) throws IOException { + final var timestamp = System.currentTimeMillis(); var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.READ, messageIds, - System.currentTimeMillis()); + timestamp); - sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(sender)); + return sendReceiptMessage(sender, timestamp, receiptMessage); } @Override - public void sendViewedReceipt( + public SendMessageResults sendViewedReceipt( RecipientIdentifier.Single sender, List messageIds - ) throws IOException, UntrustedIdentityException { + ) throws IOException { + final var timestamp = System.currentTimeMillis(); var receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.VIEWED, messageIds, - System.currentTimeMillis()); + timestamp); - sendHelper.sendReceiptMessage(receiptMessage, resolveRecipient(sender)); + return sendReceiptMessage(sender, timestamp, receiptMessage); + } + + private SendMessageResults sendReceiptMessage( + final RecipientIdentifier.Single sender, + final long timestamp, + final SignalServiceReceiptMessage receiptMessage + ) throws IOException { + try { + final var result = sendHelper.sendReceiptMessage(receiptMessage, recipientHelper.resolveRecipient(sender)); + return new SendMessageResults(timestamp, + Map.of(sender, + List.of(SendMessageResult.from(result, + account.getRecipientStore(), + account.getRecipientStore()::resolveRecipientAddress)))); + } catch (UnregisteredRecipientException e) { + return new SendMessageResults(timestamp, + Map.of(sender, List.of(SendMessageResult.unregisteredFailure(sender.toPartialRecipientAddress())))); + } } @Override public SendMessageResults sendMessage( Message message, Set recipients - ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { + ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException { final var messageBuilder = SignalServiceDataMessage.newBuilder(); applyMessage(messageBuilder, message); return sendMessage(messageBuilder, recipients); @@ -677,12 +738,33 @@ public class ManagerImpl implements Manager { private void applyMessage( final SignalServiceDataMessage.Builder messageBuilder, final Message message - ) throws AttachmentInvalidException, IOException { + ) throws AttachmentInvalidException, IOException, UnregisteredRecipientException { messageBuilder.withBody(message.messageText()); final var attachments = message.attachments(); if (attachments != null) { messageBuilder.withAttachments(attachmentHelper.uploadAttachments(attachments)); } + if (message.mentions().size() > 0) { + messageBuilder.withMentions(resolveMentions(message.mentions())); + } + if (message.quote().isPresent()) { + final var quote = message.quote().get(); + messageBuilder.withQuote(new SignalServiceDataMessage.Quote(quote.timestamp(), + recipientHelper.resolveSignalServiceAddress(recipientHelper.resolveRecipient(quote.author())), + quote.message(), + List.of(), + resolveMentions(quote.mentions()))); + } + } + + private ArrayList resolveMentions(final List mentionList) throws IOException, UnregisteredRecipientException { + final var mentions = new ArrayList(); + for (final var m : mentionList) { + final var recipientId = recipientHelper.resolveRecipient(m.recipient()); + mentions.add(new SignalServiceDataMessage.Mention(recipientHelper.resolveSignalServiceAddress(recipientId) + .getAci(), m.start(), m.length())); + } + return mentions; } @Override @@ -701,11 +783,11 @@ public class ManagerImpl implements Manager { RecipientIdentifier.Single targetAuthor, long targetSentTimestamp, Set recipients - ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { - var targetAuthorRecipientId = resolveRecipient(targetAuthor); + ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException { + var targetAuthorRecipientId = recipientHelper.resolveRecipient(targetAuthor); var reaction = new SignalServiceDataMessage.Reaction(emoji, remove, - resolveSignalServiceAddress(targetAuthorRecipientId), + recipientHelper.resolveSignalServiceAddress(targetAuthorRecipientId), targetSentTimestamp); final var messageBuilder = SignalServiceDataMessage.newBuilder().withReaction(reaction); return sendMessage(messageBuilder, recipients); @@ -722,31 +804,47 @@ public class ManagerImpl implements Manager { throw new AssertionError(e); } finally { for (var recipient : recipients) { - final var recipientId = resolveRecipient(recipient); + final RecipientId recipientId; + try { + recipientId = recipientHelper.resolveRecipient(recipient); + } catch (UnregisteredRecipientException e) { + continue; + } account.getSessionStore().deleteAllSessions(recipientId); } } } + @Override + public void deleteRecipient(final RecipientIdentifier.Single recipient) { + account.removeRecipient(account.getRecipientStore().resolveRecipient(recipient.toPartialRecipientAddress())); + } + + @Override + public void deleteContact(final RecipientIdentifier.Single recipient) { + account.getContactStore() + .deleteContact(account.getRecipientStore().resolveRecipient(recipient.toPartialRecipientAddress())); + } + @Override public void setContactName( RecipientIdentifier.Single recipient, String name - ) throws NotMasterDeviceException, IOException { + ) throws NotMasterDeviceException, IOException, UnregisteredRecipientException { if (!account.isMasterDevice()) { throw new NotMasterDeviceException(); } - contactHelper.setContactName(resolveRecipient(recipient), name); + contactHelper.setContactName(recipientHelper.resolveRecipient(recipient), name); } @Override public void setContactBlocked( RecipientIdentifier.Single recipient, boolean blocked - ) throws NotMasterDeviceException, IOException { + ) throws NotMasterDeviceException, IOException, UnregisteredRecipientException { if (!account.isMasterDevice()) { throw new NotMasterDeviceException(); } - contactHelper.setContactBlocked(resolveRecipient(recipient), blocked); - // TODO cycle our profile key + contactHelper.setContactBlocked(recipientHelper.resolveRecipient(recipient), blocked); + // TODO cycle our profile key, if we're not together in a group with recipient syncHelper.sendBlockedList(); } @@ -768,8 +866,8 @@ public class ManagerImpl implements Manager { @Override public void setExpirationTimer( RecipientIdentifier.Single recipient, int messageExpirationTimer - ) throws IOException { - var recipientId = resolveRecipient(recipient); + ) throws IOException, UnregisteredRecipientException { + var recipientId = recipientHelper.resolveRecipient(recipient); contactHelper.setExpirationTimer(recipientId, messageExpirationTimer); final var messageBuilder = SignalServiceDataMessage.newBuilder().asExpirationUpdate(); try { @@ -823,47 +921,6 @@ public class ManagerImpl implements Manager { } } - private RecipientId refreshRegisteredUser(RecipientId recipientId) throws IOException { - final var address = resolveSignalServiceAddress(recipientId); - if (!address.getNumber().isPresent()) { - return recipientId; - } - final var number = address.getNumber().get(); - final var uuid = getRegisteredUser(number); - return resolveRecipientTrusted(new SignalServiceAddress(uuid, number)); - } - - private ACI getRegisteredUser(final String number) throws IOException { - final Map aciMap; - try { - aciMap = getRegisteredUsers(Set.of(number)); - } catch (NumberFormatException e) { - throw new IOException(number, e); - } - final var uuid = aciMap.get(number); - if (uuid == null) { - throw new IOException(number, null); - } - return uuid; - } - - private Map getRegisteredUsers(final Set numbers) throws IOException { - final Map registeredUsers; - try { - registeredUsers = dependencies.getAccountManager() - .getRegisteredUsers(ServiceConfig.getIasKeyStore(), - numbers, - serviceEnvironmentConfig.getCdsMrenclave()); - } catch (Quote.InvalidQuoteFormatException | UnauthenticatedQuoteException | SignatureException | UnauthenticatedResponseException | InvalidKeyException e) { - throw new IOException(e); - } - - // Store numbers as recipients, so we have the number/uuid association - registeredUsers.forEach((number, aci) -> resolveRecipientTrusted(new SignalServiceAddress(aci, number))); - - return registeredUsers; - } - private void retryFailedReceivedMessages(ReceiveMessageHandler handler) { Set queuedActions = new HashSet<>(); for (var cachedMessage : account.getMessageCache().getCachedMessages()) { @@ -934,7 +991,7 @@ public class ManagerImpl implements Manager { logger.debug("Starting receiving messages"); while (!Thread.interrupted()) { try { - receiveMessagesInternal(1L, TimeUnit.HOURS, false, (envelope, e) -> { + receiveMessagesInternal(Duration.ofMinutes(1), false, (envelope, e) -> { synchronized (messageHandlers) { Stream.concat(messageHandlers.stream(), weakHandlers.stream()).forEach(h -> { try { @@ -1001,17 +1058,17 @@ public class ManagerImpl implements Manager { } @Override - public void receiveMessages(long timeout, TimeUnit unit, ReceiveMessageHandler handler) throws IOException { - receiveMessages(timeout, unit, true, handler); + public void receiveMessages(Duration timeout, ReceiveMessageHandler handler) throws IOException { + receiveMessages(timeout, true, handler); } @Override public void receiveMessages(ReceiveMessageHandler handler) throws IOException { - receiveMessages(1L, TimeUnit.HOURS, false, handler); + receiveMessages(Duration.ofMinutes(1), false, handler); } private void receiveMessages( - long timeout, TimeUnit unit, boolean returnOnTimeout, ReceiveMessageHandler handler + Duration timeout, boolean returnOnTimeout, ReceiveMessageHandler handler ) throws IOException { if (isReceiving()) { throw new IllegalStateException("Already receiving message."); @@ -1019,7 +1076,7 @@ public class ManagerImpl implements Manager { isReceivingSynchronous = true; receiveThread = Thread.currentThread(); try { - receiveMessagesInternal(timeout, unit, returnOnTimeout, handler); + receiveMessagesInternal(timeout, returnOnTimeout, handler); } finally { receiveThread = null; hasCaughtUpWithOldMessages = false; @@ -1028,14 +1085,20 @@ public class ManagerImpl implements Manager { } private void receiveMessagesInternal( - long timeout, TimeUnit unit, boolean returnOnTimeout, ReceiveMessageHandler handler + Duration timeout, boolean returnOnTimeout, ReceiveMessageHandler handler ) throws IOException { - retryFailedReceivedMessages(handler); + needsToRetryFailedMessages = true; // Use a Map here because java Set doesn't have a get method ... Map queuedActions = new HashMap<>(); final var signalWebSocket = dependencies.getSignalWebSocket(); + final var webSocketStateDisposable = Observable.merge(signalWebSocket.getUnidentifiedWebSocketState(), + signalWebSocket.getWebSocketState()) + .subscribeOn(Schedulers.computation()) + .observeOn(Schedulers.computation()) + .distinctUntilChanged() + .subscribe(this::onWebSocketStateChange); signalWebSocket.connect(); hasCaughtUpWithOldMessages = false; @@ -1043,6 +1106,10 @@ public class ManagerImpl implements Manager { final var MAX_BACKOFF_COUNTER = 9; while (!Thread.interrupted()) { + if (needsToRetryFailedMessages) { + retryFailedReceivedMessages(handler); + needsToRetryFailedMessages = false; + } SignalServiceEnvelope envelope; final CachedMessage[] cachedMessage = {null}; final var nowMillis = System.currentTimeMillis(); @@ -1051,10 +1118,9 @@ public class ManagerImpl implements Manager { } logger.debug("Checking for new message from server"); try { - var result = signalWebSocket.readOrEmpty(unit.toMillis(timeout), envelope1 -> { - final var recipientId = envelope1.hasSourceUuid() - ? resolveRecipient(envelope1.getSourceAddress()) - : null; + var result = signalWebSocket.readOrEmpty(timeout.toMillis(), envelope1 -> { + final var recipientId = envelope1.hasSourceUuid() ? account.getRecipientStore() + .resolveRecipient(envelope1.getSourceAddress()) : null; // store message on disk, before acknowledging receipt to the server cachedMessage[0] = account.getMessageCache().cacheMessage(envelope1, recipientId); }); @@ -1124,7 +1190,7 @@ public class ManagerImpl implements Manager { if (exception instanceof UntrustedIdentityException) { logger.debug("Keeping message with untrusted identity in message cache"); final var address = ((UntrustedIdentityException) exception).getSender(); - final var recipientId = resolveRecipient(address); + final var recipientId = account.getRecipientStore().resolveRecipient(address); if (!envelope.hasSourceUuid()) { try { cachedMessage[0] = account.getMessageCache().replaceSender(cachedMessage[0], recipientId); @@ -1141,6 +1207,18 @@ public class ManagerImpl implements Manager { handleQueuedActions(queuedActions.keySet()); queuedActions.clear(); dependencies.getSignalWebSocket().disconnect(); + webSocketStateDisposable.dispose(); + } + + private void onWebSocketStateChange(final WebSocketConnectionState s) { + if (s.equals(WebSocketConnectionState.AUTHENTICATION_FAILED)) { + account.setRegistered(false); + try { + close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } @Override @@ -1157,6 +1235,7 @@ public class ManagerImpl implements Manager { logger.debug("Handling message actions"); var interrupted = false; for (var action : queuedActions) { + logger.debug("Executing action {}", action.getClass().getSimpleName()); try { action.execute(context); } catch (Throwable e) { @@ -1177,8 +1256,8 @@ public class ManagerImpl implements Manager { public boolean isContactBlocked(final RecipientIdentifier.Single recipient) { final RecipientId recipientId; try { - recipientId = resolveRecipient(recipient); - } catch (IOException e) { + recipientId = recipientHelper.resolveRecipient(recipient); + } catch (IOException | UnregisteredRecipientException e) { return false; } return contactHelper.isContactBlocked(recipientId); @@ -1195,15 +1274,15 @@ public class ManagerImpl implements Manager { .getContacts() .stream() .map(p -> new Pair<>(account.getRecipientStore().resolveRecipientAddress(p.first()), p.second())) - .collect(Collectors.toList()); + .toList(); } @Override public String getContactOrProfileName(RecipientIdentifier.Single recipient) { final RecipientId recipientId; try { - recipientId = resolveRecipient(recipient); - } catch (IOException e) { + recipientId = recipientHelper.resolveRecipient(recipient); + } catch (IOException | UnregisteredRecipientException e) { return null; } @@ -1212,7 +1291,7 @@ public class ManagerImpl implements Manager { return contact.getName(); } - final var profile = getRecipientProfile(recipientId); + final var profile = profileHelper.getRecipientProfile(recipientId); if (profile != null) { return profile.getDisplayName(); } @@ -1231,11 +1310,7 @@ public class ManagerImpl implements Manager { @Override public List getIdentities() { - return account.getIdentityKeyStore() - .getIdentities() - .stream() - .map(this::toIdentity) - .collect(Collectors.toList()); + return account.getIdentityKeyStore().getIdentities().stream().map(this::toIdentity).toList(); } private Identity toIdentity(final IdentityInfo identityInfo) { @@ -1258,8 +1333,8 @@ public class ManagerImpl implements Manager { public List getIdentities(RecipientIdentifier.Single recipient) { IdentityInfo identity; try { - identity = account.getIdentityKeyStore().getIdentity(resolveRecipient(recipient)); - } catch (IOException e) { + identity = account.getIdentityKeyStore().getIdentity(recipientHelper.resolveRecipient(recipient)); + } catch (IOException | UnregisteredRecipientException e) { identity = null; } return identity == null ? List.of() : List.of(toIdentity(identity)); @@ -1272,14 +1347,20 @@ public class ManagerImpl implements Manager { * @param fingerprint Fingerprint */ @Override - public boolean trustIdentityVerified(RecipientIdentifier.Single recipient, byte[] fingerprint) { + public boolean trustIdentityVerified( + RecipientIdentifier.Single recipient, byte[] fingerprint + ) throws UnregisteredRecipientException { RecipientId recipientId; try { - recipientId = resolveRecipient(recipient); + recipientId = recipientHelper.resolveRecipient(recipient); } catch (IOException e) { return false; } - return identityHelper.trustIdentityVerified(recipientId, fingerprint); + final var updated = identityHelper.trustIdentityVerified(recipientId, fingerprint); + if (updated && this.isReceiving()) { + needsToRetryFailedMessages = true; + } + return updated; } /** @@ -1289,14 +1370,20 @@ public class ManagerImpl implements Manager { * @param safetyNumber Safety number */ @Override - public boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, String safetyNumber) { + public boolean trustIdentityVerifiedSafetyNumber( + RecipientIdentifier.Single recipient, String safetyNumber + ) throws UnregisteredRecipientException { RecipientId recipientId; try { - recipientId = resolveRecipient(recipient); + recipientId = recipientHelper.resolveRecipient(recipient); } catch (IOException e) { return false; } - return identityHelper.trustIdentityVerifiedSafetyNumber(recipientId, safetyNumber); + final var updated = identityHelper.trustIdentityVerifiedSafetyNumber(recipientId, safetyNumber); + if (updated && this.isReceiving()) { + needsToRetryFailedMessages = true; + } + return updated; } /** @@ -1306,14 +1393,20 @@ public class ManagerImpl implements Manager { * @param safetyNumber Scannable safety number */ @Override - public boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, byte[] safetyNumber) { + public boolean trustIdentityVerifiedSafetyNumber( + RecipientIdentifier.Single recipient, byte[] safetyNumber + ) throws UnregisteredRecipientException { RecipientId recipientId; try { - recipientId = resolveRecipient(recipient); + recipientId = recipientHelper.resolveRecipient(recipient); } catch (IOException e) { return false; } - return identityHelper.trustIdentityVerifiedSafetyNumber(recipientId, safetyNumber); + final var updated = identityHelper.trustIdentityVerifiedSafetyNumber(recipientId, safetyNumber); + if (updated && this.isReceiving()) { + needsToRetryFailedMessages = true; + } + return updated; } /** @@ -1322,14 +1415,18 @@ public class ManagerImpl implements Manager { * @param recipient account of the identity */ @Override - public boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient) { + public boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient) throws UnregisteredRecipientException { RecipientId recipientId; try { - recipientId = resolveRecipient(recipient); + recipientId = recipientHelper.resolveRecipient(recipient); } catch (IOException e) { return false; } - return identityHelper.trustIdentityAllKeys(recipientId); + final var updated = identityHelper.trustIdentityAllKeys(recipientId); + if (updated && this.isReceiving()) { + needsToRetryFailedMessages = true; + } + return updated; } @Override @@ -1346,62 +1443,6 @@ public class ManagerImpl implements Manager { this.identityHelper.handleIdentityFailure(recipientId, identityFailure); } - private SignalServiceAddress resolveSignalServiceAddress(RecipientId recipientId) { - final var address = account.getRecipientStore().resolveRecipientAddress(recipientId); - if (address.getUuid().isPresent()) { - return address.toSignalServiceAddress(); - } - - // Address in recipient store doesn't have a uuid, this shouldn't happen - // Try to retrieve the uuid from the server - final var number = address.getNumber().get(); - final ACI aci; - try { - aci = getRegisteredUser(number); - } catch (IOException e) { - logger.warn("Failed to get uuid for e164 number: {}", number, e); - // Return SignalServiceAddress with unknown UUID - return address.toSignalServiceAddress(); - } - return resolveSignalServiceAddress(account.getRecipientStore().resolveRecipient(aci)); - } - - private Set resolveRecipients(Collection recipients) throws IOException { - final var recipientIds = new HashSet(recipients.size()); - for (var number : recipients) { - final var recipientId = resolveRecipient(number); - recipientIds.add(recipientId); - } - return recipientIds; - } - - private RecipientId resolveRecipient(final RecipientIdentifier.Single recipient) throws IOException { - if (recipient instanceof RecipientIdentifier.Uuid uuidRecipient) { - return account.getRecipientStore().resolveRecipient(ACI.from(uuidRecipient.uuid())); - } else { - final var number = ((RecipientIdentifier.Number) recipient).number(); - return account.getRecipientStore().resolveRecipient(number, () -> { - try { - return getRegisteredUser(number); - } catch (IOException e) { - return null; - } - }); - } - } - - private RecipientId resolveRecipient(RecipientAddress address) { - return account.getRecipientStore().resolveRecipient(address); - } - - private RecipientId resolveRecipient(SignalServiceAddress address) { - return account.getRecipientStore().resolveRecipient(address); - } - - private RecipientId resolveRecipientTrusted(SignalServiceAddress address) { - return account.getRecipientStore().resolveRecipientTrusted(address); - } - @Override public void close() throws IOException { Thread thread;