mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 02:20:39 +00:00
parent
0bb2a64781
commit
83d471818d
3 changed files with 32 additions and 3 deletions
|
@ -165,6 +165,10 @@
|
||||||
{
|
{
|
||||||
"name":"org.signal.libsignal.protocol.state.SignedPreKeyStore"
|
"name":"org.signal.libsignal.protocol.state.SignedPreKeyStore"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name":"org.signal.libsignal.usernames.BadDiscriminatorCharacterException",
|
||||||
|
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name":"org.signal.libsignal.usernames.BadNicknameCharacterException",
|
"name":"org.signal.libsignal.usernames.BadNicknameCharacterException",
|
||||||
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]
|
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]
|
||||||
|
|
|
@ -321,21 +321,42 @@ public class AccountHelper {
|
||||||
public static final int USERNAME_MIN_LENGTH = 3;
|
public static final int USERNAME_MIN_LENGTH = 3;
|
||||||
public static final int USERNAME_MAX_LENGTH = 32;
|
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();
|
final var currentUsername = account.getUsername();
|
||||||
if (currentUsername != null) {
|
if (currentUsername != null) {
|
||||||
final var currentNickname = currentUsername.substring(0, currentUsername.indexOf('.'));
|
final var currentNickname = currentUsername.substring(0, currentUsername.indexOf('.'));
|
||||||
if (currentNickname.equals(nickname)) {
|
if (currentNickname.equals(nickname)) {
|
||||||
try {
|
try {
|
||||||
refreshCurrentUsername();
|
refreshCurrentUsername();
|
||||||
|
return;
|
||||||
} catch (IOException | BaseUsernameException e) {
|
} catch (IOException | BaseUsernameException e) {
|
||||||
logger.warn("[reserveUsername] Failed to refresh current username, trying to claim new username");
|
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);
|
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<Username> candidates) throws IOException {
|
||||||
final var candidateHashes = new ArrayList<String>();
|
final var candidateHashes = new ArrayList<String>();
|
||||||
for (final var candidate : candidates) {
|
for (final var candidate : candidates) {
|
||||||
candidateHashes.add(Base64.encodeUrlSafeWithoutPadding(candidate.getHash()));
|
candidateHashes.add(Base64.encodeUrlSafeWithoutPadding(candidate.getHash()));
|
||||||
|
|
|
@ -361,7 +361,11 @@ public class ManagerImpl implements Manager {
|
||||||
@Override
|
@Override
|
||||||
public void setUsername(final String username) throws IOException, InvalidUsernameException {
|
public void setUsername(final String username) throws IOException, InvalidUsernameException {
|
||||||
try {
|
try {
|
||||||
context.getAccountHelper().reserveUsername(username);
|
if (username.contains(".")) {
|
||||||
|
context.getAccountHelper().reserveExactUsername(username);
|
||||||
|
} else {
|
||||||
|
context.getAccountHelper().reserveUsernameFromNickname(username);
|
||||||
|
}
|
||||||
} catch (BaseUsernameException e) {
|
} catch (BaseUsernameException e) {
|
||||||
throw new InvalidUsernameException(e.getMessage() + " (" + e.getClass().getSimpleName() + ")", e);
|
throw new InvalidUsernameException(e.getMessage() + " (" + e.getClass().getSimpleName() + ")", e);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue