From 2c68b5a9e19ccc8632d448166de2aa62d268d4c8 Mon Sep 17 00:00:00 2001 From: AsamK Date: Fri, 29 Nov 2024 21:10:37 +0100 Subject: [PATCH] Add support for using PNI as recipient --- .../manager/api/InvalidNumberException.java | 4 ++++ .../signal/manager/api/RecipientIdentifier.java | 16 ++++++++++++---- .../signal/manager/helper/RecipientHelper.java | 2 +- .../signal/manager/internal/ManagerImpl.java | 2 +- .../storage/recipients/RecipientAddress.java | 4 ++-- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/src/main/java/org/asamk/signal/manager/api/InvalidNumberException.java b/lib/src/main/java/org/asamk/signal/manager/api/InvalidNumberException.java index ba71ef2e9a..8e6a806472 100644 --- a/lib/src/main/java/org/asamk/signal/manager/api/InvalidNumberException.java +++ b/lib/src/main/java/org/asamk/signal/manager/api/InvalidNumberException.java @@ -2,6 +2,10 @@ public class InvalidNumberException extends Exception { + InvalidNumberException(String message) { + super(message); + } + InvalidNumberException(String message, Throwable e) { super(message, e); } diff --git a/lib/src/main/java/org/asamk/signal/manager/api/RecipientIdentifier.java b/lib/src/main/java/org/asamk/signal/manager/api/RecipientIdentifier.java index 794672a4b0..53da2aade3 100644 --- a/lib/src/main/java/org/asamk/signal/manager/api/RecipientIdentifier.java +++ b/lib/src/main/java/org/asamk/signal/manager/api/RecipientIdentifier.java @@ -29,6 +29,14 @@ static Single fromString(String identifier, String localNumber) throws InvalidNu return new Uuid(UUID.fromString(identifier)); } + if (identifier.startsWith("PNI:")) { + final var pni = identifier.substring(4); + if (!UuidUtil.isUuid(pni)) { + throw new InvalidNumberException("Invalid PNI"); + } + return new Pni(UUID.fromString(pni)); + } + if (identifier.startsWith("u:")) { return new Username(identifier.substring(2)); } @@ -50,7 +58,7 @@ static Single fromAddress(RecipientAddress address) { } else if (address.aci().isPresent()) { return new Uuid(UUID.fromString(address.aci().get())); } else if (address.pni().isPresent()) { - return new Pni(address.pni().get()); + return new Pni(UUID.fromString(address.pni().get().substring(4))); } else if (address.username().isPresent()) { return new Username(address.username().get()); } @@ -73,16 +81,16 @@ public RecipientAddress toPartialRecipientAddress() { } } - record Pni(String pni) implements Single { + record Pni(UUID pni) implements Single { @Override public String getIdentifier() { - return pni; + return "PNI:" + pni.toString(); } @Override public RecipientAddress toPartialRecipientAddress() { - return new RecipientAddress(null, pni, null, null); + return new RecipientAddress(null, getIdentifier(), null, null); } } diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/RecipientHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/RecipientHelper.java index 5da66b7e40..125fa0d835 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/RecipientHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/RecipientHelper.java @@ -79,7 +79,7 @@ public RecipientId resolveRecipient(final RecipientIdentifier.Single recipient) if (recipient instanceof RecipientIdentifier.Uuid uuidRecipient) { return account.getRecipientResolver().resolveRecipient(ACI.from(uuidRecipient.uuid())); } else if (recipient instanceof RecipientIdentifier.Pni pniRecipient) { - return account.getRecipientResolver().resolveRecipient(PNI.parseOrThrow(pniRecipient.pni())); + return account.getRecipientResolver().resolveRecipient(PNI.from(pniRecipient.pni())); } else if (recipient instanceof RecipientIdentifier.Number numberRecipient) { final var number = numberRecipient.number(); return account.getRecipientStore().resolveRecipientByNumber(number, () -> { diff --git a/lib/src/main/java/org/asamk/signal/manager/internal/ManagerImpl.java b/lib/src/main/java/org/asamk/signal/manager/internal/ManagerImpl.java index 2f2ba3dbf6..dcdead7355 100644 --- a/lib/src/main/java/org/asamk/signal/manager/internal/ManagerImpl.java +++ b/lib/src/main/java/org/asamk/signal/manager/internal/ManagerImpl.java @@ -888,7 +888,7 @@ public SendMessageResults sendRemoteDeleteMessage( .deleteEntryForRecipientNonGroup(targetSentTimestamp, ACI.from(u.uuid())); } else if (recipient instanceof RecipientIdentifier.Pni pni) { account.getMessageSendLogStore() - .deleteEntryForRecipientNonGroup(targetSentTimestamp, PNI.parseOrThrow(pni.pni())); + .deleteEntryForRecipientNonGroup(targetSentTimestamp, PNI.from(pni.pni())); } else if (recipient instanceof RecipientIdentifier.Single r) { try { final var recipientId = context.getRecipientHelper().resolveRecipient(r); diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java index b80f81da57..ba2b9f266c 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java @@ -69,8 +69,8 @@ public RecipientAddress(SignalServiceAddress address) { } public RecipientAddress(org.asamk.signal.manager.api.RecipientAddress address) { - this(address.aci().map(ACI::parseOrNull), - address.pni().map(PNI::parseOrNull), + this(address.aci().map(ACI::parseOrThrow), + address.pni().map(PNI::parseOrThrow), address.number(), address.username()); }