]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/util/ErrorUtils.java
Add submitRateLimitChallenge command
[signal-cli] / src / main / java / org / asamk / signal / util / ErrorUtils.java
index 2442ddb6eb00d1b46f41ce65380cc519657961a6..e245492588f751ef82d2b967f44c6b5bb955f0cc 100644 (file)
@@ -1,17 +1,21 @@
 package org.asamk.signal.util;
 
-import org.asamk.signal.PlainTextWriter;
 import org.asamk.signal.commands.exceptions.CommandException;
 import org.asamk.signal.commands.exceptions.IOErrorException;
+import org.asamk.signal.manager.api.RecipientIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.whispersystems.signalservice.api.messages.SendMessageResult;
 import org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
+import static org.asamk.signal.util.Util.getLegacyIdentifier;
+
 public class ErrorUtils {
 
     private final static Logger logger = LoggerFactory.getLogger(ErrorUtils.class);
@@ -19,17 +23,28 @@ public class ErrorUtils {
     private ErrorUtils() {
     }
 
-    public static void handleTimestampAndSendMessageResults(
-            PlainTextWriter writer, long timestamp, List<SendMessageResult> results
+    public static void handleSendMessageResults(
+            Map<RecipientIdentifier, List<SendMessageResult>> mapResults
+    ) throws CommandException {
+        List<String> errors = getErrorMessagesFromSendMessageResults(mapResults);
+        handleSendMessageResultErrors(errors);
+    }
+
+    public static void handleSendMessageResults(
+            Collection<SendMessageResult> results
     ) throws CommandException {
-        if (timestamp != 0) {
-            writer.println("{}", timestamp);
-        }
         var errors = getErrorMessagesFromSendMessageResults(results);
         handleSendMessageResultErrors(errors);
     }
 
-    public static List<String> getErrorMessagesFromSendMessageResults(List<SendMessageResult> results) {
+    public static List<String> getErrorMessagesFromSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
+        return mapResults.values()
+                .stream()
+                .flatMap(results -> getErrorMessagesFromSendMessageResults(results).stream())
+                .collect(Collectors.toList());
+    }
+
+    public static List<String> getErrorMessagesFromSendMessageResults(Collection<SendMessageResult> results) {
         var errors = new ArrayList<String>();
         for (var result : results) {
             var error = getErrorMessageFromSendMessageResult(result);
@@ -42,17 +57,28 @@ public class ErrorUtils {
     }
 
     public static String getErrorMessageFromSendMessageResult(SendMessageResult result) {
+        var identifier = getLegacyIdentifier(result.getAddress());
         if (result.isNetworkFailure()) {
-            return String.format("Network failure for \"%s\"", result.getAddress().getLegacyIdentifier());
+            return String.format("Network failure for \"%s\"", identifier);
         } else if (result.isUnregisteredFailure()) {
-            return String.format("Unregistered user \"%s\"", result.getAddress().getLegacyIdentifier());
+            return String.format("Unregistered user \"%s\"", identifier);
         } else if (result.getIdentityFailure() != null) {
-            return String.format("Untrusted Identity for \"%s\"", result.getAddress().getLegacyIdentifier());
+            return String.format("Untrusted Identity for \"%s\"", identifier);
         } else if (result.getProofRequiredFailure() != null) {
             final var failure = result.getProofRequiredFailure();
             return String.format(
-                    "CAPTCHA proof required for sending to \"%s\", available options \"%s\" with token \"%s\", or wait \"%d\" seconds",
-                    result.getAddress().getLegacyIdentifier(),
+                    "CAPTCHA proof required for sending to \"%s\", available options \"%s\" with challenge token \"%s\", or wait \"%d\" seconds.\n"
+                            + (
+                            failure.getOptions().contains(ProofRequiredException.Option.RECAPTCHA)
+                                    ?
+                                    "To get the captcha token, go to https://signalcaptchas.org/registration/generate.html\n"
+                                            + "Check the developer tools (F12) console for a failed redirect to signalcaptcha://\n"
+                                            + "Everything after signalcaptcha:// is the captcha token.\n"
+                                            + "Use the following command to submit the captcha token:\n"
+                                            + "signal-cli submitRateLimitChallenge --challenge CHALLENGE_TOKEN --captcha CAPTCHA_TOKEN"
+                                    : ""
+                    ),
+                    identifier,
                     failure.getOptions()
                             .stream()
                             .map(ProofRequiredException.Option::toString)
@@ -72,6 +98,6 @@ public class ErrorUtils {
         for (var error : errors) {
             message.append(error).append("\n");
         }
-        throw new IOErrorException(message.toString());
+        throw new IOErrorException(message.toString(), null);
     }
 }