]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/commands/RegisterCommand.java
Fix typo
[signal-cli] / src / main / java / org / asamk / signal / commands / RegisterCommand.java
index 3ff6dc094fade5b1f3d623ab739c311c526b3dc3..cd361218d4df641e53a369f0ee1805ac6715f67e 100644 (file)
@@ -9,13 +9,15 @@ import net.sourceforge.argparse4j.inf.Subparser;
 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.RateLimitErrorException;
 import org.asamk.signal.commands.exceptions.UserErrorException;
 import org.asamk.signal.manager.RegistrationManager;
 import org.asamk.signal.manager.api.CaptchaRequiredException;
 import org.asamk.signal.manager.api.NonNormalizedPhoneNumberException;
 import org.asamk.signal.manager.api.RateLimitException;
+import org.asamk.signal.manager.api.VerificationMethodNotAvailableException;
 import org.asamk.signal.output.JsonWriter;
-import org.asamk.signal.util.DateUtils;
+import org.asamk.signal.util.CommandUtil;
 
 import java.io.IOException;
 import java.util.List;
@@ -35,14 +37,18 @@ public class RegisterCommand implements RegistrationCommand, JsonRpcRegistration
                 .action(Arguments.storeTrue());
         subparser.addArgument("--captcha")
                 .help("The captcha token, required if registration failed with a captcha required error.");
+        subparser.addArgument("--reregister")
+                .action(Arguments.storeTrue())
+                .help("Register even if account is already registered");
     }
 
     @Override
     public void handleCommand(final Namespace ns, final RegistrationManager m) throws CommandException {
         final boolean voiceVerification = Boolean.TRUE.equals(ns.getBoolean("voice"));
         final var captcha = ns.getString("captcha");
+        final var reregister = Boolean.TRUE.equals(ns.getBoolean("reregister"));
 
-        register(m, voiceVerification, captcha);
+        register(m, voiceVerification, captcha, reregister);
     }
 
     @Override
@@ -59,43 +65,33 @@ public class RegisterCommand implements RegistrationCommand, JsonRpcRegistration
     public void handleCommand(
             final RegistrationParams request, final RegistrationManager m, final JsonWriter jsonWriter
     ) throws CommandException {
-        register(m, Boolean.TRUE.equals(request.voice()), request.captcha());
+        register(m, Boolean.TRUE.equals(request.voice()), request.captcha(), Boolean.TRUE.equals(request.reregister()));
     }
 
     private void register(
-            final RegistrationManager m, final boolean voiceVerification, final String captcha
-    ) throws UserErrorException, IOErrorException {
+            final RegistrationManager m, final boolean voiceVerification, final String captcha, final boolean reregister
+    ) throws CommandException {
         try {
-            m.register(voiceVerification, captcha);
+            m.register(voiceVerification, captcha, reregister);
         } catch (RateLimitException e) {
-            String message = "Rate limit reached";
-            if (e.getNextAttemptTimestamp() > 0) {
-                message += "\nNext attempt may be tried at " + DateUtils.formatTimestamp(e.getNextAttemptTimestamp());
-            }
-            throw new UserErrorException(message);
+            final var message = CommandUtil.getRateLimitMessage(e);
+            throw new RateLimitErrorException(message, e);
         } 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.";
-            }
-            if (e.getNextAttemptTimestamp() > 0) {
-                message += "\nNext Captcha may be provided at "
-                        + DateUtils.formatTimestamp(e.getNextAttemptTimestamp());
-            }
+            final var message = CommandUtil.getCaptchaRequiredMessage(e, captcha != null);
             throw new UserErrorException(message);
         } catch (NonNormalizedPhoneNumberException e) {
             throw new UserErrorException("Failed to register: " + e.getMessage(), e);
         } catch (IOException e) {
             throw new IOErrorException("Failed to register: %s (%s)".formatted(e.getMessage(),
                     e.getClass().getSimpleName()), e);
+        } catch (VerificationMethodNotAvailableException e) {
+            throw new UserErrorException("Failed to register: " + e.getMessage() + (
+                    voiceVerification
+                            ? ": Before requesting voice verification you need to request SMS verification and wait a minute."
+                            : ""
+            ), e);
         }
     }
 
-    record RegistrationParams(Boolean voice, String captcha) {}
+    public record RegistrationParams(Boolean voice, String captcha, Boolean reregister) {}
 }