mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 02:20:39 +00:00
Use exit 4 when sending a single recipient message fails due to untrusted identity key
Fixes #88
This commit is contained in:
parent
e1134d832a
commit
06404667a1
6 changed files with 57 additions and 25 deletions
|
@ -404,6 +404,7 @@ signal-cli -u USERNAME trust -a NUMBER
|
|||
* *1*: Error is probably caused and fixable by the user
|
||||
* *2*: Some unexpected error
|
||||
* *3*: Server or IO error
|
||||
* *4*: Sending failed due to untrusted key
|
||||
|
||||
== Files
|
||||
|
||||
|
|
|
@ -15,21 +15,21 @@ public interface Signal extends DBusInterface {
|
|||
|
||||
long sendMessage(
|
||||
String message, List<String> attachments, String recipient
|
||||
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber;
|
||||
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity;
|
||||
|
||||
long sendMessage(
|
||||
String message, List<String> attachments, List<String> recipients
|
||||
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UnregisteredUser, Error.UntrustedIdentity;
|
||||
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity;
|
||||
|
||||
long sendNoteToSelfMessage(
|
||||
String message, List<String> attachments
|
||||
) throws Error.AttachmentInvalid, Error.Failure, Error.UnregisteredUser, Error.UntrustedIdentity;
|
||||
) throws Error.AttachmentInvalid, Error.Failure;
|
||||
|
||||
void sendEndSessionMessage(List<String> recipients) throws Error.Failure, Error.InvalidNumber, Error.UnregisteredUser, Error.UntrustedIdentity;
|
||||
void sendEndSessionMessage(List<String> recipients) throws Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity;
|
||||
|
||||
long sendGroupMessage(
|
||||
String message, List<String> attachments, byte[] groupId
|
||||
) throws Error.GroupNotFound, Error.Failure, Error.AttachmentInvalid, Error.UnregisteredUser, Error.UntrustedIdentity;
|
||||
) throws Error.GroupNotFound, Error.Failure, Error.AttachmentInvalid;
|
||||
|
||||
String getContactName(String number) throws Error.InvalidNumber;
|
||||
|
||||
|
@ -47,7 +47,7 @@ public interface Signal extends DBusInterface {
|
|||
|
||||
byte[] updateGroup(
|
||||
byte[] groupId, String name, List<String> members, String avatar
|
||||
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.GroupNotFound, Error.UnregisteredUser, Error.UntrustedIdentity;
|
||||
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.GroupNotFound;
|
||||
|
||||
boolean isRegistered();
|
||||
|
||||
|
@ -198,13 +198,6 @@ public interface Signal extends DBusInterface {
|
|||
}
|
||||
}
|
||||
|
||||
class UnregisteredUser extends DBusExecutionException {
|
||||
|
||||
public UnregisteredUser(final String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
class UntrustedIdentity extends DBusExecutionException {
|
||||
|
||||
public UntrustedIdentity(final String message) {
|
||||
|
|
|
@ -66,6 +66,9 @@ public class SendCommand implements DbusCommand {
|
|||
} catch (AssertionError e) {
|
||||
handleAssertionError(e);
|
||||
return 1;
|
||||
} catch (Signal.Error.UntrustedIdentity e) {
|
||||
System.err.println("Failed to send message: " + e.getMessage());
|
||||
return 4;
|
||||
} catch (DBusExecutionException e) {
|
||||
System.err.println("Failed to send message: " + e.getMessage());
|
||||
return 2;
|
||||
|
@ -118,6 +121,9 @@ public class SendCommand implements DbusCommand {
|
|||
} catch (AssertionError e) {
|
||||
handleAssertionError(e);
|
||||
return 1;
|
||||
} catch (Signal.Error.UntrustedIdentity e) {
|
||||
System.err.println("Failed to send message: " + e.getMessage());
|
||||
return 4;
|
||||
} catch (DBusExecutionException e) {
|
||||
System.err.println("Failed to send note to self message: " + e.getMessage());
|
||||
return 2;
|
||||
|
@ -134,6 +140,9 @@ public class SendCommand implements DbusCommand {
|
|||
} catch (UnknownObject e) {
|
||||
System.err.println("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
|
||||
return 1;
|
||||
} catch (Signal.Error.UntrustedIdentity e) {
|
||||
System.err.println("Failed to send message: " + e.getMessage());
|
||||
return 4;
|
||||
} catch (DBusExecutionException e) {
|
||||
System.err.println("Failed to send message: " + e.getMessage());
|
||||
return 2;
|
||||
|
|
|
@ -45,9 +45,30 @@ public class DbusSignalImpl implements Signal {
|
|||
return sendMessage(message, attachments, recipients);
|
||||
}
|
||||
|
||||
private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException {
|
||||
String error = ErrorUtils.getErrorMessageFromSendMessageResult(result);
|
||||
|
||||
if (error == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String message = timestamp + "\nFailed to send message:\n" + error + '\n';
|
||||
|
||||
if (result.getIdentityFailure() != null) {
|
||||
throw new Error.UntrustedIdentity(message);
|
||||
} else {
|
||||
throw new Error.Failure(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkSendMessageResults(
|
||||
long timestamp, List<SendMessageResult> results
|
||||
) throws DBusExecutionException {
|
||||
if (results.size() == 1) {
|
||||
checkSendMessageResult(timestamp, results.get(0));
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results);
|
||||
if (errors.size() == 0) {
|
||||
return;
|
||||
|
@ -81,10 +102,10 @@ public class DbusSignalImpl implements Signal {
|
|||
@Override
|
||||
public long sendNoteToSelfMessage(
|
||||
final String message, final List<String> attachments
|
||||
) throws Error.AttachmentInvalid, Error.Failure, Error.UnregisteredUser, Error.UntrustedIdentity {
|
||||
) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
|
||||
try {
|
||||
final Pair<Long, List<SendMessageResult>> results = m.sendSelfMessage(message, attachments);
|
||||
checkSendMessageResults(results.first(), results.second());
|
||||
final Pair<Long, SendMessageResult> results = m.sendSelfMessage(message, attachments);
|
||||
checkSendMessageResult(results.first(), results.second());
|
||||
return results.first();
|
||||
} catch (AttachmentInvalidException e) {
|
||||
throw new Error.AttachmentInvalid(e.getMessage());
|
||||
|
|
|
@ -953,7 +953,7 @@ public class Manager implements Closeable {
|
|||
return sendMessage(messageBuilder, getSignalServiceAddresses(recipients));
|
||||
}
|
||||
|
||||
public Pair<Long, List<SendMessageResult>> sendSelfMessage(
|
||||
public Pair<Long, SendMessageResult> sendSelfMessage(
|
||||
String messageText, List<String> attachments
|
||||
) throws IOException, AttachmentInvalidException {
|
||||
final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
|
||||
|
@ -1278,7 +1278,7 @@ public class Manager implements Closeable {
|
|||
}
|
||||
}
|
||||
|
||||
private Pair<Long, List<SendMessageResult>> sendSelfMessage(
|
||||
private Pair<Long, SendMessageResult> sendSelfMessage(
|
||||
SignalServiceDataMessage.Builder messageBuilder
|
||||
) throws IOException {
|
||||
final long timestamp = System.currentTimeMillis();
|
||||
|
@ -1294,7 +1294,7 @@ public class Manager implements Closeable {
|
|||
|
||||
SignalServiceDataMessage message = messageBuilder.build();
|
||||
final SendMessageResult result = sendSelfMessage(message);
|
||||
return new Pair<>(timestamp, List.of(result));
|
||||
return new Pair<>(timestamp, result);
|
||||
} finally {
|
||||
account.save();
|
||||
}
|
||||
|
|
|
@ -33,18 +33,26 @@ public class ErrorUtils {
|
|||
public static List<String> getErrorMessagesFromSendMessageResults(List<SendMessageResult> results) {
|
||||
List<String> errors = new ArrayList<>();
|
||||
for (SendMessageResult result : results) {
|
||||
if (result.isNetworkFailure()) {
|
||||
errors.add(String.format("Network failure for \"%s\"", result.getAddress().getLegacyIdentifier()));
|
||||
} else if (result.isUnregisteredFailure()) {
|
||||
errors.add(String.format("Unregistered user \"%s\"", result.getAddress().getLegacyIdentifier()));
|
||||
} else if (result.getIdentityFailure() != null) {
|
||||
errors.add(String.format("Untrusted Identity for \"%s\"", result.getAddress().getLegacyIdentifier()));
|
||||
String error = getErrorMessageFromSendMessageResult(result);
|
||||
if (error != null) {
|
||||
errors.add(error);
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
public static String getErrorMessageFromSendMessageResult(SendMessageResult result) {
|
||||
if (result.isNetworkFailure()) {
|
||||
return String.format("Network failure for \"%s\"", result.getAddress().getLegacyIdentifier());
|
||||
} else if (result.isUnregisteredFailure()) {
|
||||
return String.format("Unregistered user \"%s\"", result.getAddress().getLegacyIdentifier());
|
||||
} else if (result.getIdentityFailure() != null) {
|
||||
return String.format("Untrusted Identity for \"%s\"", result.getAddress().getLegacyIdentifier());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int handleSendMessageResultErrors(List<String> errors) {
|
||||
if (errors.size() == 0) {
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue