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
.JsonWriter
;
10 import org
.asamk
.signal
.OutputType
;
11 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
12 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
13 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
14 import org
.asamk
.signal
.manager
.RegistrationManager
;
15 import org
.asamk
.signal
.manager
.api
.CaptchaRequiredException
;
17 import java
.io
.IOException
;
18 import java
.util
.List
;
20 public class RegisterCommand
implements RegistrationCommand
, JsonRpcRegistrationCommand
<RegisterCommand
.RegistrationParams
> {
23 public String
getName() {
28 public void attachToSubparser(final Subparser subparser
) {
29 subparser
.help("Register a phone number with SMS or voice verification.");
30 subparser
.addArgument("-v", "--voice")
31 .help("The verification should be done over voice, not SMS.")
32 .action(Arguments
.storeTrue());
33 subparser
.addArgument("--captcha")
34 .help("The captcha token, required if registration failed with a captcha required error.");
38 public void handleCommand(final Namespace ns
, final RegistrationManager m
) throws CommandException
{
39 final boolean voiceVerification
= Boolean
.TRUE
.equals(ns
.getBoolean("voice"));
40 final var captcha
= ns
.getString("captcha");
42 register(m
, voiceVerification
, captcha
);
46 public TypeReference
<RegistrationParams
> getRequestType() {
47 return new TypeReference
<>() {};
51 public List
<OutputType
> getSupportedOutputTypes() {
52 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
56 public void handleCommand(
57 final RegistrationParams request
, final RegistrationManager m
, final JsonWriter jsonWriter
58 ) throws CommandException
{
59 register(m
, Boolean
.TRUE
.equals(request
.voice()), request
.captcha());
62 private void register(
63 final RegistrationManager m
, final boolean voiceVerification
, final String captcha
64 ) throws UserErrorException
, IOErrorException
{
66 m
.register(voiceVerification
, captcha
);
67 } catch (CaptchaRequiredException e
) {
69 if (captcha
== null) {
71 Captcha required for verification, use --captcha CAPTCHA
72 To get the token, go to https://signalcaptchas.org/registration/generate.html
73 Check the developer tools (F12) console for a failed redirect to signalcaptcha://
74 Everything after signalcaptcha:// is the captcha token.""";
76 message
= "Invalid captcha given.";
78 throw new UserErrorException(message
);
79 } catch (IOException e
) {
80 throw new IOErrorException("Request verify error: " + e
.getMessage(), e
);
84 record RegistrationParams(Boolean voice
, String captcha
) {}