X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/d9c8711eb0f5391a8f9efaecdef97dc7a7af23e6..5d83e149b3ed61683da20bba6703a37db66f93cf:/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java diff --git a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java index d306b380..003f6695 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -14,7 +14,9 @@ import org.asamk.signal.manager.api.Message; import org.asamk.signal.manager.api.Pair; import org.asamk.signal.manager.api.RecipientIdentifier; 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.UnregisteredRecipientException; import org.asamk.signal.manager.api.UpdateGroup; import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupInviteLinkUrl; @@ -202,7 +204,7 @@ public class DbusSignalImpl implements Signal { .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); return results.timestamp(); } catch (AttachmentInvalidException e) { throw new Error.AttachmentInvalid(e.getMessage()); @@ -210,6 +212,8 @@ public class DbusSignalImpl implements Signal { throw new Error.Failure(e); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { throw new Error.GroupNotFound(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } @@ -229,7 +233,7 @@ public class DbusSignalImpl implements Signal { getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); @@ -265,12 +269,14 @@ public class DbusSignalImpl implements Signal { getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { throw new Error.GroupNotFound(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } @@ -283,7 +289,7 @@ public class DbusSignalImpl implements Signal { getSingleRecipientIdentifiers(List.of(recipient), m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { @@ -298,7 +304,7 @@ public class DbusSignalImpl implements Signal { try { final var results = m.sendReadReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()), messageIds); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } @@ -311,7 +317,7 @@ public class DbusSignalImpl implements Signal { try { final var results = m.sendViewedReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()), messageIds); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } @@ -342,7 +348,7 @@ public class DbusSignalImpl implements Signal { try { final var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()), Set.of(RecipientIdentifier.NoteToSelf.INSTANCE)); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); return results.timestamp(); } catch (AttachmentInvalidException e) { throw new Error.AttachmentInvalid(e.getMessage()); @@ -350,6 +356,8 @@ public class DbusSignalImpl implements Signal { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { throw new Error.GroupNotFound(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } @@ -357,7 +365,7 @@ public class DbusSignalImpl implements Signal { public void sendEndSessionMessage(final List recipients) { try { final var results = m.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients, m.getSelfNumber())); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } @@ -365,20 +373,12 @@ public class DbusSignalImpl implements Signal { @Override public void deleteRecipient(final String recipient) throws Error.Failure { - try { - m.deleteRecipient(getSingleRecipientIdentifier(recipient, m.getSelfNumber())); - } catch (IOException e) { - throw new Error.Failure("Recipient not found"); - } + m.deleteRecipient(getSingleRecipientIdentifier(recipient, m.getSelfNumber())); } @Override public void deleteContact(final String recipient) throws Error.Failure { - try { - m.deleteContact(getSingleRecipientIdentifier(recipient, m.getSelfNumber())); - } catch (IOException e) { - throw new Error.Failure("Contact not found"); - } + m.deleteContact(getSingleRecipientIdentifier(recipient, m.getSelfNumber())); } @Override @@ -386,7 +386,7 @@ public class DbusSignalImpl implements Signal { try { var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()), Set.of(getGroupRecipientIdentifier(groupId))); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); @@ -394,6 +394,8 @@ public class DbusSignalImpl implements Signal { throw new Error.GroupNotFound(e.getMessage()); } catch (AttachmentInvalidException e) { throw new Error.AttachmentInvalid(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } @@ -404,7 +406,7 @@ public class DbusSignalImpl implements Signal { try { final var results = m.sendTypingMessage(stop ? TypingAction.STOP : TypingAction.START, Set.of(getGroupRecipientIdentifier(groupId))); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { @@ -419,7 +421,7 @@ public class DbusSignalImpl implements Signal { try { final var results = m.sendRemoteDeleteMessage(targetSentTimestamp, Set.of(getGroupRecipientIdentifier(groupId))); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); @@ -442,12 +444,14 @@ public class DbusSignalImpl implements Signal { getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()), targetSentTimestamp, Set.of(getGroupRecipientIdentifier(groupId))); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { throw new Error.GroupNotFound(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } @@ -467,6 +471,8 @@ public class DbusSignalImpl implements Signal { throw new Error.Failure("This command doesn't work on linked devices."); } catch (IOException e) { throw new Error.Failure("Contact is not registered."); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } @@ -476,6 +482,8 @@ public class DbusSignalImpl implements Signal { m.setExpirationTimer(getSingleRecipientIdentifier(number, m.getSelfNumber()), expiration); } catch (IOException e) { throw new Error.Failure(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } @@ -487,6 +495,8 @@ public class DbusSignalImpl implements Signal { throw new Error.Failure("This command doesn't work on linked devices."); } catch (IOException e) { throw new Error.Failure(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } @@ -562,7 +572,8 @@ public class DbusSignalImpl implements Signal { final var memberIdentifiers = getSingleRecipientIdentifiers(members, m.getSelfNumber()); if (groupId == null) { final var results = m.createGroup(name, memberIdentifiers, avatar == null ? null : new File(avatar)); - checkSendMessageResults(results.second().timestamp(), results.second().results()); + updateGroups(); + checkGroupSendMessageResults(results.second().timestamp(), results.second().results()); return results.first().serialize(); } else { final var results = m.updateGroup(getGroupId(groupId), @@ -572,7 +583,7 @@ public class DbusSignalImpl implements Signal { .withAvatarFile(avatar == null ? null : new File(avatar)) .build()); if (results != null) { - checkSendMessageResults(results.timestamp(), results.results()); + checkGroupSendMessageResults(results.timestamp(), results.results()); } return groupId; } @@ -582,6 +593,8 @@ public class DbusSignalImpl implements Signal { throw new Error.GroupNotFound(e.getMessage()); } catch (AttachmentInvalidException e) { throw new Error.AttachmentInvalid(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } @@ -656,6 +669,8 @@ public class DbusSignalImpl implements Signal { m.setRegistrationLockPin(Optional.empty()); } catch (IOException e) { throw new Error.Failure("Remove pin error: " + e.getMessage()); + } catch (NotMasterDeviceException e) { + throw new Error.Failure("This command doesn't work on linked devices."); } } @@ -665,6 +680,8 @@ public class DbusSignalImpl implements Signal { m.setRegistrationLockPin(Optional.of(registrationLockPin)); } catch (IOException e) { throw new Error.Failure("Set pin error: " + e.getMessage()); + } catch (NotMasterDeviceException e) { + throw new Error.Failure("This command doesn't work on linked devices."); } } @@ -705,7 +722,7 @@ public class DbusSignalImpl implements Signal { Profile profile = null; try { profile = m.getRecipientProfile(RecipientIdentifier.Single.fromAddress(address)); - } catch (IOException ignored) { + } catch (IOException | UnregisteredRecipientException ignored) { } if (profile != null && profile.getDisplayName().equals(name)) { numbers.add(number); @@ -724,6 +741,8 @@ public class DbusSignalImpl implements Signal { throw new Error.GroupNotFound(e.getMessage()); } catch (IOException | LastGroupAdminException e) { throw new Error.Failure(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } @@ -789,7 +808,7 @@ public class DbusSignalImpl implements Signal { 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()) { throw new Error.UntrustedIdentity(message); @@ -798,31 +817,29 @@ public class DbusSignalImpl implements Signal { } } - private static void checkSendMessageResults( - long timestamp, Map> results - ) throws DBusExecutionException { - final var sendMessageResults = results.values().stream().findFirst(); - if (results.size() == 1 && sendMessageResults.get().size() == 1) { - checkSendMessageResult(timestamp, sendMessageResults.get().stream().findFirst().get()); + private void checkSendMessageResults(final SendMessageResults results) { + final var sendMessageResults = results.results().values().stream().findFirst(); + if (results.results().size() == 1 && sendMessageResults.get().size() == 1) { + checkSendMessageResult(results.timestamp(), sendMessageResults.get().stream().findFirst().get()); return; } - var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results); - if (errors.size() == 0) { + if (results.hasSuccess()) { return; } var message = new StringBuilder(); - message.append(timestamp).append('\n'); - message.append("Failed to send (some) messages:\n"); + message.append("Failed to send messages:\n"); + var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results.results()); for (var error : errors) { message.append(error).append('\n'); } + message.append(results.timestamp()); throw new Error.Failure(message.toString()); } - private static void checkSendMessageResults( + private static void checkGroupSendMessageResults( long timestamp, Collection results ) throws DBusExecutionException { if (results.size() == 1) { @@ -831,16 +848,16 @@ public class DbusSignalImpl implements Signal { } var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results); - if (errors.size() == 0) { + if (errors.size() < results.size()) { return; } var message = new StringBuilder(); - message.append(timestamp).append('\n'); - message.append("Failed to send (some) messages:\n"); + message.append("Failed to send message:\n"); for (var error : errors) { message.append(error).append('\n'); } + message.append(timestamp); throw new Error.Failure(message.toString()); } @@ -1149,7 +1166,19 @@ public class DbusSignalImpl implements Signal { throw new Error.Failure(e.getMessage()); } catch (LastGroupAdminException e) { throw new Error.LastGroupAdmin(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); + } + } + + @Override + public void deleteGroup() throws Error.Failure, Error.LastGroupAdmin { + try { + m.deleteGroup(groupId); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); } + updateGroups(); } @Override @@ -1252,6 +1281,8 @@ public class DbusSignalImpl implements Signal { throw new Error.GroupNotFound(e.getMessage()); } catch (AttachmentInvalidException e) { throw new Error.AttachmentInvalid(e.getMessage()); + } catch (UnregisteredRecipientException e) { + throw new Error.UntrustedIdentity(e.getSender().getIdentifier() + " is not registered."); } } }