signal-cli/src/main/java/org/asamk/signal/commands/RegisterCommand.java
2021-10-27 14:39:40 +02:00

56 lines
2.3 KiB
Java

package org.asamk.signal.commands;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
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.whispersystems.signalservice.api.push.exceptions.CaptchaRequiredException;
import java.io.IOException;
public class RegisterCommand implements RegistrationCommand {
@Override
public String getName() {
return "register";
}
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.help("Register a phone number with SMS or voice verification.");
subparser.addArgument("-v", "--voice")
.help("The verification should be done over voice, not SMS.")
.action(Arguments.storeTrue());
subparser.addArgument("--captcha")
.help("The captcha token, required if registration failed with a captcha required error.");
}
@Override
public void handleCommand(final Namespace ns, final RegistrationManager m) throws CommandException {
final boolean voiceVerification = Boolean.TRUE.equals(ns.getBoolean("voice"));
final var captchaString = ns.getString("captcha");
final var captcha = captchaString == null ? null : captchaString.replace("signalcaptcha://", "");
try {
m.register(voiceVerification, captcha);
} catch (CaptchaRequiredException e) {
String message;
if (captcha == null) {
message = """
Captcha required for verification, use --captcha CAPTCHA
To get the token, go to https://signalcaptchas.org/registration/generate.html
Check the developer tools (F12) console for a failed redirect to signalcaptcha://
Everything after signalcaptcha:// is the captcha token.""";
} else {
message = "Invalid captcha given.";
}
throw new UserErrorException(message);
} catch (IOException e) {
throw new IOErrorException("Request verify error: " + e.getMessage(), e);
}
}
}