package org.asamk.signal.commands;
+import com.fasterxml.jackson.core.type.TypeReference;
+
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
-import org.asamk.signal.manager.Manager;
-import org.whispersystems.signalservice.internal.push.LockedException;
+
+import org.asamk.signal.OutputType;
+import org.asamk.signal.commands.exceptions.CommandException;
+import org.asamk.signal.commands.exceptions.IOErrorException;
+import org.asamk.signal.commands.exceptions.UserErrorException;
+import org.asamk.signal.manager.RegistrationManager;
+import org.asamk.signal.manager.api.IncorrectPinException;
+import org.asamk.signal.manager.api.PinLockMissingException;
+import org.asamk.signal.manager.api.PinLockedException;
+import org.asamk.signal.output.JsonWriter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.IOException;
+import java.util.List;
+
+public class VerifyCommand implements RegistrationCommand, JsonRpcRegistrationCommand<VerifyCommand.VerifyParams> {
-public class VerifyCommand implements LocalCommand {
+ private static final Logger logger = LoggerFactory.getLogger(VerifyCommand.class);
+
+ @Override
+ public String getName() {
+ return "verify";
+ }
@Override
public void attachToSubparser(final Subparser subparser) {
- subparser.addArgument("verificationCode")
- .help("The verification code you received via sms or voice call.");
- subparser.addArgument("-p", "--pin")
- .help("The registration lock PIN, that was set by the user (Optional)");
+ subparser.help("Verify the number using the code received via SMS or voice.");
+ subparser.addArgument("verification-code").help("The verification code you received via sms or voice call.");
+ subparser.addArgument("-p", "--pin").help("The registration lock PIN, that was set by the user (Optional)");
}
@Override
- public int handleCommand(final Namespace ns, final Manager m) {
- if (!m.userHasKeys()) {
- System.err.println("User has no keys, first call register.");
- return 1;
- }
- if (m.isRegistered()) {
- System.err.println("User registration is already verified");
- return 1;
- }
+ public void handleCommand(final Namespace ns, final RegistrationManager m) throws CommandException {
+ var verificationCode = ns.getString("verification-code");
+ var pin = ns.getString("pin");
+
+ verify(m, verificationCode, pin);
+ }
+
+ @Override
+ public TypeReference<VerifyParams> getRequestType() {
+ return new TypeReference<>() {};
+ }
+
+ @Override
+ public List<OutputType> getSupportedOutputTypes() {
+ return List.of(OutputType.PLAIN_TEXT, OutputType.JSON);
+ }
+
+ @Override
+ public void handleCommand(
+ final VerifyParams request,
+ final RegistrationManager m,
+ final JsonWriter jsonWriter
+ ) throws CommandException {
+ verify(m, request.verificationCode(), request.pin());
+ }
+
+ private void verify(
+ final RegistrationManager m,
+ final String verificationCode,
+ final String pin
+ ) throws UserErrorException, IOErrorException {
try {
- String verificationCode = ns.getString("verificationCode");
- String pin = ns.getString("pin");
m.verifyAccount(verificationCode, pin);
- return 0;
- } catch (LockedException e) {
- System.err.println("Verification failed! This number is locked with a pin. Hours remaining until reset: " + (e.getTimeRemaining() / 1000 / 60 / 60));
- System.err.println("Use '--pin PIN_CODE' to specify the registration lock PIN");
- return 3;
+ } catch (PinLockedException e) {
+ throw new UserErrorException(
+ "Verification failed! This number is locked with a pin. Hours remaining until reset: "
+ + (e.getTimeRemaining() / 1000 / 60 / 60)
+ + "\nUse '--pin PIN_CODE' to specify the registration lock PIN");
+ } catch (IncorrectPinException e) {
+ throw new UserErrorException("Verification failed! Invalid pin, tries remaining: " + e.getTriesRemaining());
+ } catch (PinLockMissingException e) {
+ throw new UserErrorException("Account is pin locked, but pin data has been deleted on the server.");
} catch (IOException e) {
- System.err.println("Verify error: " + e.getMessage());
- return 3;
+ throw new IOErrorException("Verify error: " + e.getMessage(), e);
}
}
+
+ public record VerifyParams(String verificationCode, String pin) {}
}