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
.UserErrorException
;
13 import org
.asamk
.signal
.manager
.RegistrationManager
;
14 import org
.asamk
.signal
.manager
.api
.CaptchaRequiredException
;
15 import org
.asamk
.signal
.manager
.api
.NonNormalizedPhoneNumberException
;
16 import org
.asamk
.signal
.manager
.api
.RateLimitException
;
17 import org
.asamk
.signal
.output
.JsonWriter
;
18 import org
.asamk
.signal
.util
.DateUtils
;
20 import java
.io
.IOException
;
21 import java
.util
.List
;
23 public class RegisterCommand
implements RegistrationCommand
, JsonRpcRegistrationCommand
<RegisterCommand
.RegistrationParams
> {
26 public String
getName() {
31 public void attachToSubparser(final Subparser subparser
) {
32 subparser
.help("Register a phone number with SMS or voice verification.");
33 subparser
.addArgument("-v", "--voice")
34 .help("The verification should be done over voice, not SMS.")
35 .action(Arguments
.storeTrue());
36 subparser
.addArgument("--captcha")
37 .help("The captcha token, required if registration failed with a captcha required error.");
41 public void handleCommand(final Namespace ns
, final RegistrationManager m
) throws CommandException
{
42 final boolean voiceVerification
= Boolean
.TRUE
.equals(ns
.getBoolean("voice"));
43 final var captcha
= ns
.getString("captcha");
45 register(m
, voiceVerification
, captcha
);
49 public TypeReference
<RegistrationParams
> getRequestType() {
50 return new TypeReference
<>() {};
54 public List
<OutputType
> getSupportedOutputTypes() {
55 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
59 public void handleCommand(
60 final RegistrationParams request
, final RegistrationManager m
, final JsonWriter jsonWriter
61 ) throws CommandException
{
62 register(m
, Boolean
.TRUE
.equals(request
.voice()), request
.captcha());
65 private void register(
66 final RegistrationManager m
, final boolean voiceVerification
, final String captcha
67 ) throws UserErrorException
, IOErrorException
{
69 m
.register(voiceVerification
, captcha
);
70 } catch (RateLimitException e
) {
71 String message
= "Rate limit reached";
72 if (e
.getNextAttemptTimestamp() > 0) {
73 message
+= "\nNext attempt may be tried at " + DateUtils
.formatTimestamp(e
.getNextAttemptTimestamp());
75 throw new UserErrorException(message
);
76 } catch (CaptchaRequiredException e
) {
78 if (captcha
== null) {
80 Captcha required for verification, use --captcha CAPTCHA
81 To get the token, go to https://signalcaptchas.org/registration/generate.html
82 Check the developer tools (F12) console for a failed redirect to signalcaptcha://
83 Everything after signalcaptcha:// is the captcha token.""";
85 message
= "Invalid captcha given.";
87 if (e
.getNextAttemptTimestamp() > 0) {
88 message
+= "\nNext Captcha may be provided at "
89 + DateUtils
.formatTimestamp(e
.getNextAttemptTimestamp());
91 throw new UserErrorException(message
);
92 } catch (NonNormalizedPhoneNumberException e
) {
93 throw new UserErrorException("Failed to register: " + e
.getMessage(), e
);
94 } catch (IOException e
) {
95 throw new IOErrorException("Failed to register: " + e
.getMessage(), e
);
99 record RegistrationParams(Boolean voice
, String captcha
) {}