]>
nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/api/RecipientIdentifier.java
1 package org
.asamk
.signal
.manager
.api
;
3 import org
.asamk
.signal
.manager
.util
.PhoneNumberFormatter
;
4 import org
.slf4j
.Logger
;
5 import org
.slf4j
.LoggerFactory
;
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
{
27 if (UuidUtil
.isUuid(identifier
)) {
28 return new Uuid(UUID
.fromString(identifier
));
31 if (identifier
.startsWith("PNI:")) {
32 final var pni
= identifier
.substring(4);
33 if (!UuidUtil
.isUuid(pni
)) {
34 throw new InvalidNumberException("Invalid PNI");
36 return new Pni(UUID
.fromString(pni
));
39 if (identifier
.startsWith("u:")) {
40 return new Username(identifier
.substring(2));
43 final var normalizedNumber
= PhoneNumberFormatter
.formatNumber(identifier
, localNumber
);
44 if (!normalizedNumber
.equals(identifier
)) {
45 final Logger logger
= LoggerFactory
.getLogger(RecipientIdentifier
.class);
46 logger
.debug("Normalized number {} to {}.", identifier
, normalizedNumber
);
48 return new Number(normalizedNumber
);
51 static Single
fromAddress(RecipientAddress address
) {
52 if (address
.number().isPresent()) {
53 return new Number(address
.number().get());
54 } else if (address
.aci().isPresent()) {
55 return new Uuid(UUID
.fromString(address
.aci().get()));
56 } else if (address
.pni().isPresent()) {
57 return new Pni(UUID
.fromString(address
.pni().get().substring(4)));
58 } else if (address
.username().isPresent()) {
59 return new Username(address
.username().get());
61 throw new AssertionError("RecipientAddress without identifier");
64 RecipientAddress
toPartialRecipientAddress();
67 record Uuid(UUID uuid
) implements Single
{
70 public String
getIdentifier() {
71 return uuid
.toString();
75 public RecipientAddress
toPartialRecipientAddress() {
76 return new RecipientAddress(uuid
);
80 record Pni(UUID pni
) implements Single
{
83 public String
getIdentifier() {
84 return "PNI:" + pni
.toString();
88 public RecipientAddress
toPartialRecipientAddress() {
89 return new RecipientAddress(null, getIdentifier(), null, null);
93 record Number(String number
) implements Single
{
96 public String
getIdentifier() {
101 public RecipientAddress
toPartialRecipientAddress() {
102 return new RecipientAddress(number
);
106 record Username(String username
) implements Single
{
109 public String
getIdentifier() {
110 return "u:" + username
;
114 public RecipientAddress
toPartialRecipientAddress() {
115 return new RecipientAddress(null, null, null, username
);
119 record Group(GroupId groupId
) implements RecipientIdentifier
{
122 public String
getIdentifier() {
123 return groupId
.toBase64();