1 package org
.asamk
.signal
.commands
;
3 import com
.fasterxml
.jackson
.core
.type
.TypeReference
;
5 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
6 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
8 import org
.asamk
.signal
.OutputType
;
9 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
10 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
11 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
12 import org
.asamk
.signal
.manager
.RegistrationManager
;
13 import org
.asamk
.signal
.manager
.api
.IncorrectPinException
;
14 import org
.asamk
.signal
.manager
.api
.PinLockedException
;
15 import org
.asamk
.signal
.output
.JsonWriter
;
16 import org
.slf4j
.Logger
;
17 import org
.slf4j
.LoggerFactory
;
19 import java
.io
.IOException
;
20 import java
.util
.List
;
22 public class VerifyCommand
implements RegistrationCommand
, JsonRpcRegistrationCommand
<VerifyCommand
.VerifyParams
> {
24 private static final Logger logger
= LoggerFactory
.getLogger(VerifyCommand
.class);
27 public String
getName() {
32 public void attachToSubparser(final Subparser subparser
) {
33 subparser
.help("Verify the number using the code received via SMS or voice.");
34 subparser
.addArgument("verification-code").help("The verification code you received via sms or voice call.");
35 subparser
.addArgument("-p", "--pin").help("The registration lock PIN, that was set by the user (Optional)");
39 public void handleCommand(final Namespace ns
, final RegistrationManager m
) throws CommandException
{
40 var verificationCode
= ns
.getString("verification-code");
41 var pin
= ns
.getString("pin");
43 verify(m
, verificationCode
, pin
);
47 public TypeReference
<VerifyParams
> 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 VerifyParams request
, final RegistrationManager m
, final JsonWriter jsonWriter
59 ) throws CommandException
{
60 verify(m
, request
.verificationCode(), request
.pin());
64 final RegistrationManager m
, final String verificationCode
, final String pin
65 ) throws UserErrorException
, IOErrorException
{
67 m
.verifyAccount(verificationCode
, pin
);
68 } catch (PinLockedException e
) {
69 throw new UserErrorException(
70 "Verification failed! This number is locked with a pin. Hours remaining until reset: "
71 + (e
.getTimeRemaining() / 1000 / 60 / 60)
72 + "\nUse '--pin PIN_CODE' to specify the registration lock PIN");
73 } catch (IncorrectPinException e
) {
74 throw new UserErrorException("Verification failed! Invalid pin, tries remaining: " + e
.getTriesRemaining());
75 } catch (IOException e
) {
76 throw new IOErrorException("Verify error: " + e
.getMessage(), e
);
80 public record VerifyParams(String verificationCode
, String pin
) {}