mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Improve exit code for message sending
Exit with 0 status code if the message was sent successfully to at least one recipient, otherwise exit with status code 2 or 4 (for untrusted). Fixes #828
This commit is contained in:
parent
4aa8e3f2f2
commit
9a72733c4f
4 changed files with 64 additions and 35 deletions
|
@ -300,6 +300,10 @@
|
||||||
"allDeclaredMethods":true,
|
"allDeclaredMethods":true,
|
||||||
"allDeclaredClasses":true}
|
"allDeclaredClasses":true}
|
||||||
,
|
,
|
||||||
|
{
|
||||||
|
"name":"org.asamk.Signal$Error$UntrustedIdentity",
|
||||||
|
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]}
|
||||||
|
,
|
||||||
{
|
{
|
||||||
"name":"org.asamk.Signal$Group",
|
"name":"org.asamk.Signal$Group",
|
||||||
"allDeclaredMethods":true,
|
"allDeclaredMethods":true,
|
||||||
|
@ -458,9 +462,7 @@
|
||||||
"allDeclaredFields":true,
|
"allDeclaredFields":true,
|
||||||
"queryAllDeclaredMethods":true,
|
"queryAllDeclaredMethods":true,
|
||||||
"queryAllDeclaredConstructors":true,
|
"queryAllDeclaredConstructors":true,
|
||||||
"methods":[
|
"methods":[{"name":"id","parameterTypes":[] }]}
|
||||||
{"name":"id","parameterTypes":[] }
|
|
||||||
]}
|
|
||||||
,
|
,
|
||||||
{
|
{
|
||||||
"name":"org.asamk.signal.json.JsonCallMessage$Hangup",
|
"name":"org.asamk.signal.json.JsonCallMessage$Hangup",
|
||||||
|
|
|
@ -3,4 +3,20 @@ package org.asamk.signal.manager.api;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public record SendMessageResults(long timestamp, Map<RecipientIdentifier, List<SendMessageResult>> results) {}
|
public record SendMessageResults(long timestamp, Map<RecipientIdentifier, List<SendMessageResult>> results) {
|
||||||
|
|
||||||
|
public boolean hasSuccess() {
|
||||||
|
return results.values()
|
||||||
|
.stream()
|
||||||
|
.flatMap(res -> res.stream().map(SendMessageResult::isSuccess))
|
||||||
|
.anyMatch(success -> success) || results.values().stream().mapToInt(List::size).sum() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasOnlyUntrustedIdentity() {
|
||||||
|
return results.values()
|
||||||
|
.stream()
|
||||||
|
.flatMap(res -> res.stream().map(SendMessageResult::isIdentityFailure))
|
||||||
|
.allMatch(identityFailure -> identityFailure)
|
||||||
|
&& results.values().stream().mapToInt(List::size).sum() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import org.asamk.signal.manager.api.Message;
|
||||||
import org.asamk.signal.manager.api.Pair;
|
import org.asamk.signal.manager.api.Pair;
|
||||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||||
import org.asamk.signal.manager.api.SendMessageResult;
|
import org.asamk.signal.manager.api.SendMessageResult;
|
||||||
|
import org.asamk.signal.manager.api.SendMessageResults;
|
||||||
import org.asamk.signal.manager.api.TypingAction;
|
import org.asamk.signal.manager.api.TypingAction;
|
||||||
import org.asamk.signal.manager.api.UpdateGroup;
|
import org.asamk.signal.manager.api.UpdateGroup;
|
||||||
import org.asamk.signal.manager.groups.GroupId;
|
import org.asamk.signal.manager.groups.GroupId;
|
||||||
|
@ -202,7 +203,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
.map(RecipientIdentifier.class::cast)
|
.map(RecipientIdentifier.class::cast)
|
||||||
.collect(Collectors.toSet()));
|
.collect(Collectors.toSet()));
|
||||||
|
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
return results.timestamp();
|
return results.timestamp();
|
||||||
} catch (AttachmentInvalidException e) {
|
} catch (AttachmentInvalidException e) {
|
||||||
throw new Error.AttachmentInvalid(e.getMessage());
|
throw new Error.AttachmentInvalid(e.getMessage());
|
||||||
|
@ -229,7 +230,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
|
getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
|
||||||
.map(RecipientIdentifier.class::cast)
|
.map(RecipientIdentifier.class::cast)
|
||||||
.collect(Collectors.toSet()));
|
.collect(Collectors.toSet()));
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
return results.timestamp();
|
return results.timestamp();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
|
@ -265,7 +266,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
|
getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
|
||||||
.map(RecipientIdentifier.class::cast)
|
.map(RecipientIdentifier.class::cast)
|
||||||
.collect(Collectors.toSet()));
|
.collect(Collectors.toSet()));
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
return results.timestamp();
|
return results.timestamp();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
|
@ -283,7 +284,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
getSingleRecipientIdentifiers(List.of(recipient), m.getSelfNumber()).stream()
|
getSingleRecipientIdentifiers(List.of(recipient), m.getSelfNumber()).stream()
|
||||||
.map(RecipientIdentifier.class::cast)
|
.map(RecipientIdentifier.class::cast)
|
||||||
.collect(Collectors.toSet()));
|
.collect(Collectors.toSet()));
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
|
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
|
||||||
|
@ -298,7 +299,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
try {
|
try {
|
||||||
final var results = m.sendReadReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()),
|
final var results = m.sendReadReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()),
|
||||||
messageIds);
|
messageIds);
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -311,7 +312,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
try {
|
try {
|
||||||
final var results = m.sendViewedReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()),
|
final var results = m.sendViewedReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()),
|
||||||
messageIds);
|
messageIds);
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -342,7 +343,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
try {
|
try {
|
||||||
final var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()),
|
final var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()),
|
||||||
Set.of(RecipientIdentifier.NoteToSelf.INSTANCE));
|
Set.of(RecipientIdentifier.NoteToSelf.INSTANCE));
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
return results.timestamp();
|
return results.timestamp();
|
||||||
} catch (AttachmentInvalidException e) {
|
} catch (AttachmentInvalidException e) {
|
||||||
throw new Error.AttachmentInvalid(e.getMessage());
|
throw new Error.AttachmentInvalid(e.getMessage());
|
||||||
|
@ -357,7 +358,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
public void sendEndSessionMessage(final List<String> recipients) {
|
public void sendEndSessionMessage(final List<String> recipients) {
|
||||||
try {
|
try {
|
||||||
final var results = m.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients, m.getSelfNumber()));
|
final var results = m.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients, m.getSelfNumber()));
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -386,7 +387,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
try {
|
try {
|
||||||
var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()),
|
var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()),
|
||||||
Set.of(getGroupRecipientIdentifier(groupId)));
|
Set.of(getGroupRecipientIdentifier(groupId)));
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
return results.timestamp();
|
return results.timestamp();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
|
@ -404,7 +405,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
try {
|
try {
|
||||||
final var results = m.sendTypingMessage(stop ? TypingAction.STOP : TypingAction.START,
|
final var results = m.sendTypingMessage(stop ? TypingAction.STOP : TypingAction.START,
|
||||||
Set.of(getGroupRecipientIdentifier(groupId)));
|
Set.of(getGroupRecipientIdentifier(groupId)));
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
|
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
|
||||||
|
@ -419,7 +420,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
try {
|
try {
|
||||||
final var results = m.sendRemoteDeleteMessage(targetSentTimestamp,
|
final var results = m.sendRemoteDeleteMessage(targetSentTimestamp,
|
||||||
Set.of(getGroupRecipientIdentifier(groupId)));
|
Set.of(getGroupRecipientIdentifier(groupId)));
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
return results.timestamp();
|
return results.timestamp();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
|
@ -442,7 +443,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()),
|
getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()),
|
||||||
targetSentTimestamp,
|
targetSentTimestamp,
|
||||||
Set.of(getGroupRecipientIdentifier(groupId)));
|
Set.of(getGroupRecipientIdentifier(groupId)));
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkSendMessageResults(results);
|
||||||
return results.timestamp();
|
return results.timestamp();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
|
@ -563,7 +564,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
if (groupId == null) {
|
if (groupId == null) {
|
||||||
final var results = m.createGroup(name, memberIdentifiers, avatar == null ? null : new File(avatar));
|
final var results = m.createGroup(name, memberIdentifiers, avatar == null ? null : new File(avatar));
|
||||||
updateGroups();
|
updateGroups();
|
||||||
checkSendMessageResults(results.second().timestamp(), results.second().results());
|
checkGroupSendMessageResults(results.second().timestamp(), results.second().results());
|
||||||
return results.first().serialize();
|
return results.first().serialize();
|
||||||
} else {
|
} else {
|
||||||
final var results = m.updateGroup(getGroupId(groupId),
|
final var results = m.updateGroup(getGroupId(groupId),
|
||||||
|
@ -573,7 +574,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
.withAvatarFile(avatar == null ? null : new File(avatar))
|
.withAvatarFile(avatar == null ? null : new File(avatar))
|
||||||
.build());
|
.build());
|
||||||
if (results != null) {
|
if (results != null) {
|
||||||
checkSendMessageResults(results.timestamp(), results.results());
|
checkGroupSendMessageResults(results.timestamp(), results.results());
|
||||||
}
|
}
|
||||||
return groupId;
|
return groupId;
|
||||||
}
|
}
|
||||||
|
@ -790,7 +791,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final var message = timestamp + "\nFailed to send message:\n" + error + '\n';
|
final var message = "\nFailed to send message:\n" + error + '\n' + timestamp;
|
||||||
|
|
||||||
if (result.isIdentityFailure()) {
|
if (result.isIdentityFailure()) {
|
||||||
throw new Error.UntrustedIdentity(message);
|
throw new Error.UntrustedIdentity(message);
|
||||||
|
@ -799,31 +800,29 @@ public class DbusSignalImpl implements Signal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkSendMessageResults(
|
private void checkSendMessageResults(final SendMessageResults results) {
|
||||||
long timestamp, Map<RecipientIdentifier, List<SendMessageResult>> results
|
final var sendMessageResults = results.results().values().stream().findFirst();
|
||||||
) throws DBusExecutionException {
|
if (results.results().size() == 1 && sendMessageResults.get().size() == 1) {
|
||||||
final var sendMessageResults = results.values().stream().findFirst();
|
checkSendMessageResult(results.timestamp(), sendMessageResults.get().stream().findFirst().get());
|
||||||
if (results.size() == 1 && sendMessageResults.get().size() == 1) {
|
|
||||||
checkSendMessageResult(timestamp, sendMessageResults.get().stream().findFirst().get());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results);
|
if (results.hasSuccess()) {
|
||||||
if (errors.size() == 0) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var message = new StringBuilder();
|
var message = new StringBuilder();
|
||||||
message.append(timestamp).append('\n');
|
message.append("Failed to send messages:\n");
|
||||||
message.append("Failed to send (some) messages:\n");
|
var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results.results());
|
||||||
for (var error : errors) {
|
for (var error : errors) {
|
||||||
message.append(error).append('\n');
|
message.append(error).append('\n');
|
||||||
}
|
}
|
||||||
|
message.append(results.timestamp());
|
||||||
|
|
||||||
throw new Error.Failure(message.toString());
|
throw new Error.Failure(message.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkSendMessageResults(
|
private static void checkGroupSendMessageResults(
|
||||||
long timestamp, Collection<SendMessageResult> results
|
long timestamp, Collection<SendMessageResult> results
|
||||||
) throws DBusExecutionException {
|
) throws DBusExecutionException {
|
||||||
if (results.size() == 1) {
|
if (results.size() == 1) {
|
||||||
|
@ -832,16 +831,16 @@ public class DbusSignalImpl implements Signal {
|
||||||
}
|
}
|
||||||
|
|
||||||
var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results);
|
var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results);
|
||||||
if (errors.size() == 0) {
|
if (errors.size() < results.size()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var message = new StringBuilder();
|
var message = new StringBuilder();
|
||||||
message.append(timestamp).append('\n');
|
message.append("Failed to send message:\n");
|
||||||
message.append("Failed to send (some) messages:\n");
|
|
||||||
for (var error : errors) {
|
for (var error : errors) {
|
||||||
message.append(error).append('\n');
|
message.append(error).append('\n');
|
||||||
}
|
}
|
||||||
|
message.append(timestamp);
|
||||||
|
|
||||||
throw new Error.Failure(message.toString());
|
throw new Error.Failure(message.toString());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package org.asamk.signal.util;
|
package org.asamk.signal.util;
|
||||||
|
|
||||||
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
|
import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
|
||||||
|
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||||
import org.asamk.signal.json.JsonSendMessageResult;
|
import org.asamk.signal.json.JsonSendMessageResult;
|
||||||
import org.asamk.signal.manager.api.ProofRequiredException;
|
import org.asamk.signal.manager.api.ProofRequiredException;
|
||||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||||
|
@ -33,7 +36,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) {
|
if (outputWriter instanceof PlainTextWriter writer) {
|
||||||
var errors = getErrorMessagesFromSendMessageResults(sendMessageResults.results());
|
var errors = getErrorMessagesFromSendMessageResults(sendMessageResults.results());
|
||||||
printSendMessageResultErrors(writer, errors);
|
printSendMessageResultErrors(writer, errors);
|
||||||
|
@ -43,6 +48,13 @@ public class SendMessageResultUtils {
|
||||||
var results = getJsonSendMessageResults(sendMessageResults.results());
|
var results = getJsonSendMessageResults(sendMessageResults.results());
|
||||||
writer.write(Map.of("timestamp", sendMessageResults.timestamp(), "results", 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 {
|
||||||
|
throw new UserErrorException("Failed to send message");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> getErrorMessagesFromSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
|
public static List<String> getErrorMessagesFromSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
|
||||||
|
@ -106,7 +118,7 @@ public class SendMessageResultUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<JsonSendMessageResult> getJsonSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
|
private static List<JsonSendMessageResult> getJsonSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
|
||||||
return mapResults.entrySet().stream().flatMap(entry -> {
|
return mapResults.entrySet().stream().flatMap(entry -> {
|
||||||
final var groupId = entry.getKey() instanceof RecipientIdentifier.Group g ? g.groupId() : null;
|
final var groupId = entry.getKey() instanceof RecipientIdentifier.Group g ? g.groupId() : null;
|
||||||
return entry.getValue().stream().map(r -> JsonSendMessageResult.from(r, groupId));
|
return entry.getValue().stream().map(r -> JsonSendMessageResult.from(r, groupId));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue