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
.output
.JsonWriter
;
18 import java
.io
.IOException
;
19 import java
.util
.List
;
21 public class RegisterCommand
implements RegistrationCommand
, JsonRpcRegistrationCommand
<RegisterCommand
.RegistrationParams
> {
24 public String
getName() {
29 public void attachToSubparser(final Subparser subparser
) {
30 subparser
.help("Register a phone number with SMS or voice verification.");
31 subparser
.addArgument("-v", "--voice")
32 .help("The verification should be done over voice, not SMS.")
33 .action(Arguments
.storeTrue());
34 subparser
.addArgument("--captcha")
35 .help("The captcha token, required if registration failed with a captcha required error.");
39 public void handleCommand(final Namespace ns
, final RegistrationManager m
) throws CommandException
{
40 final boolean voiceVerification
= Boolean
.TRUE
.equals(ns
.getBoolean("voice"));
41 final var captcha
= ns
.getString("captcha");
43 register(m
, voiceVerification
, captcha
);
47 public TypeReference
<RegistrationParams
> getRequestType() {
48 return new TypeReference
<>() {};
52 public List
<OutputType
> getSupportedOutputTypes() {
53 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
57 public void handleCommand(
58 final RegistrationParams request
, final RegistrationManager m
, final JsonWriter jsonWriter
59 ) throws CommandException
{
60 register(m
, Boolean
.TRUE
.equals(request
.voice()), request
.captcha());
63 private void register(
64 final RegistrationManager m
, final boolean voiceVerification
, final String captcha
65 ) throws UserErrorException
, IOErrorException
{
67 m
.register(voiceVerification
, captcha
);
68 } catch (CaptchaRequiredException e
) {
70 if (captcha
== null) {
72 Captcha required for verification, use --captcha CAPTCHA
73 To get the token, go to https://signalcaptchas.org/registration/generate.html
74 Check the developer tools (F12) console for a failed redirect to signalcaptcha://
75 Everything after signalcaptcha:// is the captcha token.""";
77 message
= "Invalid captcha given.";
79 throw new UserErrorException(message
);
80 } catch (NonNormalizedPhoneNumberException e
) {
81 throw new UserErrorException("Failed to register: " + e
.getMessage(), e
);
82 } catch (IOException e
) {
83 throw new IOErrorException("Failed to register: " + e
.getMessage(), e
);
87 record RegistrationParams(Boolean voice
, String captcha
) {}