X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/06e93b84da2718c31111e820cd35a3354f22bae2..56ee173d03baf8feede7c14aea5d9d670a047ae7:/src/main/java/org/asamk/signal/util/SendMessageResultUtils.java diff --git a/src/main/java/org/asamk/signal/util/SendMessageResultUtils.java b/src/main/java/org/asamk/signal/util/SendMessageResultUtils.java index 0e13a200..ecc669e8 100644 --- a/src/main/java/org/asamk/signal/util/SendMessageResultUtils.java +++ b/src/main/java/org/asamk/signal/util/SendMessageResultUtils.java @@ -1,7 +1,12 @@ package org.asamk.signal.util; +import org.asamk.signal.commands.exceptions.CommandException; +import org.asamk.signal.commands.exceptions.RateLimitErrorException; +import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException; +import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.json.JsonSendMessageResult; import org.asamk.signal.manager.api.ProofRequiredException; +import org.asamk.signal.manager.api.RateLimitException; import org.asamk.signal.manager.api.RecipientIdentifier; import org.asamk.signal.manager.api.SendGroupMessageResults; import org.asamk.signal.manager.api.SendMessageResult; @@ -33,7 +38,9 @@ public class SendMessageResultUtils { } } - public static void outputResult(final OutputWriter outputWriter, final SendMessageResults sendMessageResults) { + public static void outputResult( + final OutputWriter outputWriter, final SendMessageResults sendMessageResults + ) throws CommandException { if (outputWriter instanceof PlainTextWriter writer) { var errors = getErrorMessagesFromSendMessageResults(sendMessageResults.results()); printSendMessageResultErrors(writer, errors); @@ -43,6 +50,16 @@ public class SendMessageResultUtils { var results = getJsonSendMessageResults(sendMessageResults.results()); writer.write(Map.of("timestamp", sendMessageResults.timestamp(), "results", results)); } + if (!sendMessageResults.hasSuccess()) { + if (sendMessageResults.hasOnlyUntrustedIdentity()) { + throw new UntrustedKeyErrorException("Failed to send message due to untrusted identities"); + } else if (sendMessageResults.hasOnlyRateLimitFailure()) { + throw new RateLimitErrorException("Failed to send message due to rate limiting", + new RateLimitException(0)); + } else { + throw new UserErrorException("Failed to send message"); + } + } } public static List getErrorMessagesFromSendMessageResults(final Map> mapResults) { @@ -53,14 +70,14 @@ public class SendMessageResultUtils { .map(SendMessageResultUtils::getErrorMessageFromSendMessageResult) .filter(Objects::nonNull) .map(error -> entry.getKey().getIdentifier() + ": " + error)) - .collect(Collectors.toList()); + .toList(); } public static List getErrorMessagesFromSendMessageResults(Collection results) { return results.stream() .map(SendMessageResultUtils::getErrorMessageFromSendMessageResult) .filter(Objects::nonNull) - .collect(Collectors.toList()); + .toList(); } public static String getErrorMessageFromSendMessageResult(SendMessageResult result) { @@ -72,11 +89,11 @@ public class SendMessageResultUtils { + ( failure.getOptions().contains(ProofRequiredException.Option.RECAPTCHA) ? """ - To get the captcha token, go to https://signalcaptchas.org/challenge/generate.html - Check the developer tools (F12) console for a failed redirect to signalcaptcha:// - Everything after signalcaptcha:// is the captcha token. - Use the following command to submit the captcha token: - signal-cli submitRateLimitChallenge --challenge CHALLENGE_TOKEN --captcha CAPTCHA_TOKEN""" + To get the captcha token, go to https://signalcaptchas.org/challenge/generate.html + Check the developer tools (F12) console for a failed redirect to signalcaptcha:// + Everything after signalcaptcha:// is the captcha token. + Use the following command to submit the captcha token: + signal-cli submitRateLimitChallenge --challenge CHALLENGE_TOKEN --captcha CAPTCHA_TOKEN""" : "" ), identifier, @@ -88,6 +105,8 @@ public class SendMessageResultUtils { failure.getRetryAfterSeconds()); } else if (result.isNetworkFailure()) { return String.format("Network failure for \"%s\"", identifier); + } else if (result.isRateLimitFailure()) { + return String.format("Rate limit failure for \"%s\"", identifier); } else if (result.isUnregisteredFailure()) { return String.format("Unregistered user \"%s\"", identifier); } else if (result.isIdentityFailure()) { @@ -97,7 +116,7 @@ public class SendMessageResultUtils { } public static void printSendMessageResultErrors(PlainTextWriter writer, List errors) { - if (errors.size() == 0) { + if (errors.isEmpty()) { return; } writer.println("Failed to send (some) messages:"); @@ -106,14 +125,14 @@ public class SendMessageResultUtils { } } - public static List getJsonSendMessageResults(final Map> mapResults) { + private static List getJsonSendMessageResults(final Map> mapResults) { return mapResults.entrySet().stream().flatMap(entry -> { final var groupId = entry.getKey() instanceof RecipientIdentifier.Group g ? g.groupId() : null; return entry.getValue().stream().map(r -> JsonSendMessageResult.from(r, groupId)); - }).collect(Collectors.toList()); + }).toList(); } public static List getJsonSendMessageResults(Collection results) { - return results.stream().map(JsonSendMessageResult::from).collect(Collectors.toList()); + return results.stream().map(JsonSendMessageResult::from).toList(); } }