]>
nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/api/RecipientIdentifier.java
53da2aade3e712fc3833fbcf9737e03376d379c5
1 package org
.asamk
.signal
.manager
.api
;
3 import org
.slf4j
.Logger
;
4 import org
.slf4j
.LoggerFactory
;
5 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
6 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
10 public sealed interface RecipientIdentifier
{
12 String
getIdentifier();
14 record NoteToSelf() implements RecipientIdentifier
{
16 public static final NoteToSelf INSTANCE
= new NoteToSelf();
19 public String
getIdentifier() {
20 return "Note-To-Self";
24 sealed interface Single
extends RecipientIdentifier
{
26 static Single
fromString(String identifier
, String localNumber
) throws InvalidNumberException
{
28 if (UuidUtil
.isUuid(identifier
)) {
29 return new Uuid(UUID
.fromString(identifier
));
32 if (identifier
.startsWith("PNI:")) {
33 final var pni
= identifier
.substring(4);
34 if (!UuidUtil
.isUuid(pni
)) {
35 throw new InvalidNumberException("Invalid PNI");
37 return new Pni(UUID
.fromString(pni
));
40 if (identifier
.startsWith("u:")) {
41 return new Username(identifier
.substring(2));
44 final var normalizedNumber
= PhoneNumberFormatter
.formatNumber(identifier
, localNumber
);
45 if (!normalizedNumber
.equals(identifier
)) {
46 final Logger logger
= LoggerFactory
.getLogger(RecipientIdentifier
.class);
47 logger
.debug("Normalized number {} to {}.", identifier
, normalizedNumber
);
49 return new Number(normalizedNumber
);
50 } catch (org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException e
) {
51 throw new InvalidNumberException(e
.getMessage(), e
);
55 static Single
fromAddress(RecipientAddress address
) {
56 if (address
.number().isPresent()) {
57 return new Number(address
.number().get());
58 } else if (address
.aci().isPresent()) {
59 return new Uuid(UUID
.fromString(address
.aci().get()));
60 } else if (address
.pni().isPresent()) {
61 return new Pni(UUID
.fromString(address
.pni().get().substring(4)));
62 } else if (address
.username().isPresent()) {
63 return new Username(address
.username().get());
65 throw new AssertionError("RecipientAddress without identifier");
68 RecipientAddress
toPartialRecipientAddress();
71 record Uuid(UUID uuid
) implements Single
{
74 public String
getIdentifier() {
75 return uuid
.toString();
79 public RecipientAddress
toPartialRecipientAddress() {
80 return new RecipientAddress(uuid
);
84 record Pni(UUID pni
) implements Single
{
87 public String
getIdentifier() {
88 return "PNI:" + pni
.toString();
92 public RecipientAddress
toPartialRecipientAddress() {
93 return new RecipientAddress(null, getIdentifier(), null, null);
97 record Number(String number
) implements Single
{
100 public String
getIdentifier() {
105 public RecipientAddress
toPartialRecipientAddress() {
106 return new RecipientAddress(number
);
110 record Username(String username
) implements Single
{
113 public String
getIdentifier() {
114 return "u:" + username
;
118 public RecipientAddress
toPartialRecipientAddress() {
119 return new RecipientAddress(null, null, null, username
);
123 record Group(GroupId groupId
) implements RecipientIdentifier
{
126 public String
getIdentifier() {
127 return groupId
.toBase64();