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
.output
.JsonWriter
;
19 import org
.asamk
.signal
.util
.DateUtils
;
21 import java
.io
.IOException
;
22 import java
.util
.List
;
24 public class RegisterCommand
implements RegistrationCommand
, JsonRpcRegistrationCommand
<RegisterCommand
.RegistrationParams
> {
27 public String
getName() {
32 public void attachToSubparser(final Subparser subparser
) {
33 subparser
.help("Register a phone number with SMS or voice verification.");
34 subparser
.addArgument("-v", "--voice")
35 .help("The verification should be done over voice, not SMS.")
36 .action(Arguments
.storeTrue());
37 subparser
.addArgument("--captcha")
38 .help("The captcha token, required if registration failed with a captcha required error.");
42 public void handleCommand(final Namespace ns
, final RegistrationManager m
) throws CommandException
{
43 final boolean voiceVerification
= Boolean
.TRUE
.equals(ns
.getBoolean("voice"));
44 final var captcha
= ns
.getString("captcha");
46 register(m
, voiceVerification
, captcha
);
50 public TypeReference
<RegistrationParams
> getRequestType() {
51 return new TypeReference
<>() {};
55 public List
<OutputType
> getSupportedOutputTypes() {
56 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
60 public void handleCommand(
61 final RegistrationParams request
, final RegistrationManager m
, final JsonWriter jsonWriter
62 ) throws CommandException
{
63 register(m
, Boolean
.TRUE
.equals(request
.voice()), request
.captcha());
66 private void register(
67 final RegistrationManager m
, final boolean voiceVerification
, final String captcha
68 ) throws CommandException
{
70 m
.register(voiceVerification
, captcha
);
71 } catch (RateLimitException e
) {
72 String message
= "Rate limit reached";
73 if (e
.getNextAttemptTimestamp() > 0) {
74 message
+= "\nNext attempt may be tried at " + DateUtils
.formatTimestamp(e
.getNextAttemptTimestamp());
76 throw new RateLimitErrorException(message
, e
);
77 } catch (CaptchaRequiredException e
) {
79 if (captcha
== null) {
81 Captcha required for verification, use --captcha CAPTCHA
82 To get the token, go to https://signalcaptchas.org/registration/generate.html
83 Check the developer tools (F12) console for a failed redirect to signalcaptcha://
84 Everything after signalcaptcha:// is the captcha token.""";
86 message
= "Invalid captcha given.";
88 if (e
.getNextAttemptTimestamp() > 0) {
89 message
+= "\nNext Captcha may be provided at "
90 + DateUtils
.formatTimestamp(e
.getNextAttemptTimestamp());
92 throw new UserErrorException(message
);
93 } catch (NonNormalizedPhoneNumberException e
) {
94 throw new UserErrorException("Failed to register: " + e
.getMessage(), e
);
95 } catch (IOException e
) {
96 throw new IOErrorException("Failed to register: %s (%s)".formatted(e
.getMessage(),
97 e
.getClass().getSimpleName()), e
);
101 public record RegistrationParams(Boolean voice
, String captcha
) {}