1 package org
.asamk
.signal
.commands
;
3 import com
.fasterxml
.jackson
.core
.type
.TypeReference
;
5 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
6 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
7 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
9 import org
.asamk
.signal
.OutputType
;
10 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
11 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
12 import org
.asamk
.signal
.commands
.exceptions
.RateLimitErrorException
;
13 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
14 import org
.asamk
.signal
.manager
.RegistrationManager
;
15 import org
.asamk
.signal
.manager
.api
.CaptchaRequiredException
;
16 import org
.asamk
.signal
.manager
.api
.NonNormalizedPhoneNumberException
;
17 import org
.asamk
.signal
.manager
.api
.RateLimitException
;
18 import org
.asamk
.signal
.manager
.api
.VerificationMethoNotAvailableException
;
19 import org
.asamk
.signal
.output
.JsonWriter
;
20 import org
.asamk
.signal
.util
.CommandUtil
;
22 import java
.io
.IOException
;
23 import java
.util
.List
;
25 public class RegisterCommand
implements RegistrationCommand
, JsonRpcRegistrationCommand
<RegisterCommand
.RegistrationParams
> {
28 public String
getName() {
33 public void attachToSubparser(final Subparser subparser
) {
34 subparser
.help("Register a phone number with SMS or voice verification.");
35 subparser
.addArgument("-v", "--voice")
36 .help("The verification should be done over voice, not SMS.")
37 .action(Arguments
.storeTrue());
38 subparser
.addArgument("--captcha")
39 .help("The captcha token, required if registration failed with a captcha required error.");
43 public void handleCommand(final Namespace ns
, final RegistrationManager m
) throws CommandException
{
44 final boolean voiceVerification
= Boolean
.TRUE
.equals(ns
.getBoolean("voice"));
45 final var captcha
= ns
.getString("captcha");
47 register(m
, voiceVerification
, captcha
);
51 public TypeReference
<RegistrationParams
> getRequestType() {
52 return new TypeReference
<>() {};
56 public List
<OutputType
> getSupportedOutputTypes() {
57 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
61 public void handleCommand(
62 final RegistrationParams request
, final RegistrationManager m
, final JsonWriter jsonWriter
63 ) throws CommandException
{
64 register(m
, Boolean
.TRUE
.equals(request
.voice()), request
.captcha());
67 private void register(
68 final RegistrationManager m
, final boolean voiceVerification
, final String captcha
69 ) throws CommandException
{
71 m
.register(voiceVerification
, captcha
);
72 } catch (RateLimitException e
) {
73 final var message
= CommandUtil
.getRateLimitMessage(e
);
74 throw new RateLimitErrorException(message
, e
);
75 } catch (CaptchaRequiredException e
) {
76 final var message
= CommandUtil
.getCaptchaRequiredMessage(e
, captcha
!= null);
77 throw new UserErrorException(message
);
78 } catch (NonNormalizedPhoneNumberException e
) {
79 throw new UserErrorException("Failed to register: " + e
.getMessage(), e
);
80 } catch (IOException e
) {
81 throw new IOErrorException("Failed to register: %s (%s)".formatted(e
.getMessage(),
82 e
.getClass().getSimpleName()), e
);
83 } catch (VerificationMethoNotAvailableException e
) {
84 throw new UserErrorException("Failed to register: " + e
.getMessage() + (
86 ?
": Before requesting voice verification you need to request SMS verification and wait a minute."
92 public record RegistrationParams(Boolean voice
, String captcha
) {}