From 83d471818d23626aa47a7180e0c59c43c89dafaa Mon Sep 17 00:00:00 2001 From: AsamK Date: Thu, 22 Feb 2024 19:59:19 +0100 Subject: [PATCH] Allow setting a username with explicit descriminator Fixes #1469 --- graalvm-config-dir/jni-config.json | 4 +++ .../signal/manager/helper/AccountHelper.java | 25 +++++++++++++++++-- .../signal/manager/internal/ManagerImpl.java | 6 ++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/graalvm-config-dir/jni-config.json b/graalvm-config-dir/jni-config.json index 5b47a7b6de..b79debed00 100644 --- a/graalvm-config-dir/jni-config.json +++ b/graalvm-config-dir/jni-config.json @@ -165,6 +165,10 @@ { "name":"org.signal.libsignal.protocol.state.SignedPreKeyStore" }, +{ + "name":"org.signal.libsignal.usernames.BadDiscriminatorCharacterException", + "methods":[{"name":"","parameterTypes":["java.lang.String"] }] +}, { "name":"org.signal.libsignal.usernames.BadNicknameCharacterException", "methods":[{"name":"","parameterTypes":["java.lang.String"] }] diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/AccountHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/AccountHelper.java index 06b1fe80fb..5abac1e7b4 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/AccountHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/AccountHelper.java @@ -321,21 +321,42 @@ public void handlePniChangeNumberMessage( public static final int USERNAME_MIN_LENGTH = 3; public static final int USERNAME_MAX_LENGTH = 32; - public void reserveUsername(String nickname) throws IOException, BaseUsernameException { + public void reserveUsernameFromNickname(String nickname) throws IOException, BaseUsernameException { final var currentUsername = account.getUsername(); if (currentUsername != null) { final var currentNickname = currentUsername.substring(0, currentUsername.indexOf('.')); if (currentNickname.equals(nickname)) { try { refreshCurrentUsername(); + return; } catch (IOException | BaseUsernameException e) { logger.warn("[reserveUsername] Failed to refresh current username, trying to claim new username"); } - return; } } final var candidates = Username.candidatesFrom(nickname, USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH); + reserveUsername(candidates); + } + + public void reserveExactUsername(String username) throws IOException, BaseUsernameException { + final var currentUsername = account.getUsername(); + if (currentUsername != null) { + if (currentUsername.equals(username)) { + try { + refreshCurrentUsername(); + return; + } catch (IOException | BaseUsernameException e) { + logger.warn("[reserveUsername] Failed to refresh current username, trying to claim new username"); + } + } + } + + final var candidates = List.of(new Username(username)); + reserveUsername(candidates); + } + + private void reserveUsername(final List candidates) throws IOException { final var candidateHashes = new ArrayList(); for (final var candidate : candidates) { candidateHashes.add(Base64.encodeUrlSafeWithoutPadding(candidate.getHash())); 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 2caffc3842..51a0e5ace4 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 @@ -361,7 +361,11 @@ public UsernameLinkUrl getUsernameLink() { @Override public void setUsername(final String username) throws IOException, InvalidUsernameException { try { - context.getAccountHelper().reserveUsername(username); + if (username.contains(".")) { + context.getAccountHelper().reserveExactUsername(username); + } else { + context.getAccountHelper().reserveUsernameFromNickname(username); + } } catch (BaseUsernameException e) { throw new InvalidUsernameException(e.getMessage() + " (" + e.getClass().getSimpleName() + ")", e); }