Implement register and verify commands for json rpc

This commit is contained in:
AsamK 2021-11-11 15:49:23 +01:00
parent 4a1af0786c
commit 79cc225869
6 changed files with 149 additions and 9 deletions

View file

@ -1,9 +1,13 @@
package org.asamk.signal.commands;
import com.fasterxml.jackson.core.type.TypeReference;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter;
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;
@ -11,8 +15,9 @@ import org.asamk.signal.manager.RegistrationManager;
import org.asamk.signal.manager.api.CaptchaRequiredException;
import java.io.IOException;
import java.util.List;
public class RegisterCommand implements RegistrationCommand {
public class RegisterCommand implements RegistrationCommand, JsonRpcRegistrationCommand<RegisterCommand.RegistrationParams> {
@Override
public String getName() {
@ -32,9 +37,31 @@ public class RegisterCommand implements RegistrationCommand {
@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://", "");
final var captcha = ns.getString("captcha");
register(m, voiceVerification, captcha);
}
@Override
public TypeReference<RegistrationParams> getRequestType() {
return new TypeReference<>() {};
}
@Override
public List<OutputType> getSupportedOutputTypes() {
return List.of(OutputType.PLAIN_TEXT, OutputType.JSON);
}
@Override
public void handleCommand(
final RegistrationParams request, final RegistrationManager m, final JsonWriter jsonWriter
) throws CommandException {
register(m, Boolean.TRUE.equals(request.voice()), request.captcha());
}
private void register(
final RegistrationManager m, final boolean voiceVerification, final String captcha
) throws UserErrorException, IOErrorException {
try {
m.register(voiceVerification, captcha);
} catch (CaptchaRequiredException e) {
@ -53,4 +80,6 @@ public class RegisterCommand implements RegistrationCommand {
throw new IOErrorException("Request verify error: " + e.getMessage(), e);
}
}
record RegistrationParams(Boolean voice, String captcha) {}
}