X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/5427fa0132d04a23f459d490f8e9a0ed33730d2e..6eb486e85869995263c543db94e6b07ac2691db7:/lib/src/main/java/org/asamk/signal/manager/Manager.java diff --git a/lib/src/main/java/org/asamk/signal/manager/Manager.java b/lib/src/main/java/org/asamk/signal/manager/Manager.java index f9d86ce6..a1a4b278 100644 --- a/lib/src/main/java/org/asamk/signal/manager/Manager.java +++ b/lib/src/main/java/org/asamk/signal/manager/Manager.java @@ -341,7 +341,8 @@ public class Manager implements Closeable { } public void updateAccountAttributes() throws IOException { - accountManager.setAccountAttributes(null, + accountManager.setAccountAttributes(account.getDeviceName(), + null, account.getLocalRegistrationId(), true, // set legacy pin only if no KBS master key is set @@ -354,18 +355,22 @@ public class Manager implements Closeable { } /** - * @param name if null, the previous name will be kept + * @param givenName if null, the previous givenName will be kept + * @param familyName if null, the previous familyName will be kept * @param about if null, the previous about text will be kept * @param aboutEmoji if null, the previous about emoji will be kept * @param avatar if avatar is null the image from the local avatar store is used (if present), - * if it's Optional.absent(), the avatar will be removed */ - public void setProfile(String name, String about, String aboutEmoji, Optional avatar) throws IOException { + public void setProfile( + String givenName, final String familyName, String about, String aboutEmoji, Optional avatar + ) throws IOException { var profile = getRecipientProfile(account.getSelfRecipientId()); var builder = profile == null ? Profile.newBuilder() : Profile.newBuilder(profile); - if (name != null) { - builder.withGivenName(name); - builder.withFamilyName(null); + if (givenName != null) { + builder.withGivenName(givenName); + } + if (familyName != null) { + builder.withFamilyName(familyName); } if (about != null) { builder.withAbout(about); @@ -528,12 +533,6 @@ public class Manager implements Closeable { ServiceConfig.AUTOMATIC_NETWORK_RETRY); } - public Profile getRecipientProfile( - SignalServiceAddress address - ) { - return getRecipientProfile(resolveRecipient(address), false); - } - public Profile getRecipientProfile( RecipientId recipientId ) { @@ -713,9 +712,10 @@ public class Manager implements Closeable { public Pair> sendGroupMessageReaction( String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, GroupId groupId ) throws IOException, InvalidNumberException, NotAGroupMemberException, GroupNotFoundException { + var targetAuthorRecipientId = canonicalizeAndResolveRecipient(targetAuthor); var reaction = new SignalServiceDataMessage.Reaction(emoji, remove, - canonicalizeAndResolveSignalServiceAddress(targetAuthor), + resolveSignalServiceAddress(targetAuthorRecipientId), targetSentTimestamp); final var messageBuilder = SignalServiceDataMessage.newBuilder().withReaction(reaction); @@ -948,14 +948,15 @@ public class Manager implements Closeable { } g = (GroupInfoV1) group; - if (!g.isMember(resolveRecipient(recipient))) { + final var recipientId = resolveRecipient(recipient); + if (!g.isMember(recipientId)) { throw new NotAGroupMemberException(groupId, g.name); } var messageBuilder = getGroupUpdateMessageBuilder(g); // Send group message only to the recipient who requested it - return sendMessage(messageBuilder, Set.of(resolveRecipient(recipient))); + return sendMessage(messageBuilder, Set.of(recipientId)); } private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfoV1 g) throws AttachmentInvalidException { @@ -1065,9 +1066,10 @@ public class Manager implements Closeable { public Pair> sendMessageReaction( String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, List recipients ) throws IOException, InvalidNumberException { + var targetAuthorRecipientId = canonicalizeAndResolveRecipient(targetAuthor); var reaction = new SignalServiceDataMessage.Reaction(emoji, remove, - canonicalizeAndResolveSignalServiceAddress(targetAuthor), + resolveSignalServiceAddress(targetAuthorRecipientId), targetSentTimestamp); final var messageBuilder = SignalServiceDataMessage.newBuilder().withReaction(reaction); return sendMessage(messageBuilder, getSignalServiceAddresses(recipients)); @@ -1087,6 +1089,11 @@ public class Manager implements Closeable { } } + SendMessageResult renewSession(RecipientId recipientId) throws IOException { + account.getSessionStore().archiveSessions(recipientId); + return sendNullMessage(recipientId); + } + public String getContactName(String number) throws InvalidNumberException { var contact = account.getContactStore().getContact(canonicalizeAndResolveRecipient(number)); return contact == null || contact.getName() == null ? "" : contact.getName(); @@ -1288,7 +1295,7 @@ public class Manager implements Closeable { final var addressesMissingUuid = new HashSet(); for (var number : numbers) { - final var resolvedAddress = canonicalizeAndResolveSignalServiceAddress(number); + final var resolvedAddress = resolveSignalServiceAddress(canonicalizeAndResolveRecipient(number)); if (resolvedAddress.getUuid().isPresent()) { signalServiceAddresses.add(resolvedAddress); } else { @@ -1389,7 +1396,7 @@ public class Manager implements Closeable { final var expirationTime = contact != null ? contact.getMessageExpirationTime() : 0; messageBuilder.withExpiration(expirationTime); message = messageBuilder.build(); - results.add(sendMessage(resolveSignalServiceAddress(recipientId), message)); + results.add(sendMessage(recipientId, message)); } return new Pair<>(timestamp, results); } @@ -1423,9 +1430,10 @@ public class Manager implements Closeable { private SendMessageResult sendSelfMessage(SignalServiceDataMessage message) throws IOException { var messageSender = createMessageSender(); - var recipient = account.getSelfAddress(); + var recipientId = account.getSelfRecipientId(); - final var unidentifiedAccess = unidentifiedAccessHelper.getAccessFor(resolveRecipient(recipient)); + final var unidentifiedAccess = unidentifiedAccessHelper.getAccessFor(recipientId); + var recipient = resolveSignalServiceAddress(recipientId); var transcript = new SentTranscriptMessage(Optional.of(recipient), message.getTimestamp(), message, @@ -1447,11 +1455,11 @@ public class Manager implements Closeable { } private SendMessageResult sendMessage( - SignalServiceAddress address, SignalServiceDataMessage message + RecipientId recipientId, SignalServiceDataMessage message ) throws IOException { var messageSender = createMessageSender(); - final var recipientId = resolveRecipient(address); + final var address = resolveSignalServiceAddress(recipientId); try { try { return messageSender.sendMessage(address, unidentifiedAccessHelper.getAccessFor(recipientId), message); @@ -1466,6 +1474,23 @@ public class Manager implements Closeable { } } + private SendMessageResult sendNullMessage(RecipientId recipientId) throws IOException { + var messageSender = createMessageSender(); + + final var address = resolveSignalServiceAddress(recipientId); + try { + try { + return messageSender.sendNullMessage(address, unidentifiedAccessHelper.getAccessFor(recipientId)); + } catch (UnregisteredUserException e) { + final var newRecipientId = refreshRegisteredUser(recipientId); + final var newAddress = resolveSignalServiceAddress(newRecipientId); + return messageSender.sendNullMessage(newAddress, unidentifiedAccessHelper.getAccessFor(newRecipientId)); + } + } catch (UntrustedIdentityException e) { + return SendMessageResult.identityFailure(address, e.getIdentityKey()); + } + } + private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, SelfSendException, UnsupportedDataMessageException, org.whispersystems.libsignal.UntrustedIdentityException { var cipher = new SignalServiceCipher(account.getSelfAddress(), account.getSignalProtocolStore(), @@ -1682,11 +1707,11 @@ public class Manager implements Closeable { private void storeProfileKeysFromMembers(final DecryptedGroup group) { for (var member : group.getMembersList()) { - final var address = resolveRecipient(new SignalServiceAddress(UuidUtil.parseOrThrow(member.getUuid() - .toByteArray()), null)); + final var uuid = UuidUtil.parseOrThrow(member.getUuid().toByteArray()); + final var recipientId = account.getRecipientStore().resolveRecipient(uuid); try { account.getProfileStore() - .storeProfileKey(address, new ProfileKey(member.getProfileKey().toByteArray())); + .storeProfileKey(recipientId, new ProfileKey(member.getProfileKey().toByteArray())); } catch (InvalidInputException ignored) { } } @@ -1723,8 +1748,8 @@ public class Manager implements Closeable { content = decryptMessage(envelope); } catch (org.whispersystems.libsignal.UntrustedIdentityException e) { if (!envelope.hasSource()) { - final var recipientId = resolveRecipient(((org.whispersystems.libsignal.UntrustedIdentityException) e) - .getName()); + final var identifier = ((org.whispersystems.libsignal.UntrustedIdentityException) e).getName(); + final var recipientId = resolveRecipient(identifier); try { account.getMessageCache().replaceSender(cachedMessage, recipientId); } catch (IOException ioException) { @@ -1810,6 +1835,11 @@ public class Manager implements Closeable { exception = e; } var actions = handleMessage(envelope, content, ignoreAttachments); + if (exception instanceof ProtocolInvalidMessageException) { + final var sender = resolveRecipient(((ProtocolInvalidMessageException) exception).getSender()); + logger.debug("Received invalid message, queuing renew session action."); + actions.add(new RenewSessionAction(sender)); + } if (hasCaughtUpWithOldMessages) { for (var action : actions) { try { @@ -1834,8 +1864,8 @@ public class Manager implements Closeable { } if (cachedMessage[0] != null) { if (exception instanceof org.whispersystems.libsignal.UntrustedIdentityException) { - final var recipientId = resolveRecipient(((org.whispersystems.libsignal.UntrustedIdentityException) exception) - .getName()); + final var identifier = ((org.whispersystems.libsignal.UntrustedIdentityException) exception).getName(); + final var recipientId = resolveRecipient(identifier); queuedActions.add(new RetrieveProfileAction(recipientId)); if (!envelope.hasSource()) { try { @@ -2550,14 +2580,6 @@ public class Manager implements Closeable { theirIdentityKey); } - @Deprecated - public SignalServiceAddress canonicalizeAndResolveSignalServiceAddress(String identifier) throws InvalidNumberException { - var canonicalizedNumber = UuidUtil.isUuid(identifier) - ? identifier - : PhoneNumberFormatter.formatNumber(identifier, account.getUsername()); - return resolveSignalServiceAddress(canonicalizedNumber); - } - @Deprecated public SignalServiceAddress resolveSignalServiceAddress(String identifier) { var address = Utils.getSignalServiceAddressFromIdentifier(identifier);