X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/382d8d22d0ccad109106b140616638799cc646f6..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 7f0c3b31..003f6695 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -2,7 +2,6 @@ package org.asamk.signal.dbus; import org.asamk.Signal; import org.asamk.signal.BaseConfig; -import org.asamk.signal.DbusReceiveMessageHandler; import org.asamk.signal.manager.AttachmentInvalidException; import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.NotMasterDeviceException; @@ -15,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; @@ -27,7 +28,7 @@ import org.asamk.signal.manager.groups.LastGroupAdminException; import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.storage.recipients.Profile; import org.asamk.signal.manager.storage.recipients.RecipientAddress; -import org.asamk.signal.util.ErrorUtils; +import org.asamk.signal.util.SendMessageResultUtils; import org.freedesktop.dbus.DBusPath; import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.exceptions.DBusException; @@ -112,7 +113,7 @@ public class DbusSignalImpl implements Signal { @Override public void subscribeReceive() { if (dbusMessageHandler == null) { - dbusMessageHandler = new DbusReceiveMessageHandler(m, connection, objectPath); + dbusMessageHandler = new DbusReceiveMessageHandler(connection, objectPath); m.addReceiveHandler(dbusMessageHandler); } subscriberCount++; @@ -192,20 +193,18 @@ public class DbusSignalImpl implements Signal { @Override public long sendMessage(final String message, final List attachments, final String recipient) { - var recipients = new ArrayList(1); - recipients.add(recipient); - return sendMessage(message, attachments, recipients); + return sendMessage(message, attachments, List.of(recipient)); } @Override public long sendMessage(final String message, final List attachments, final List recipients) { try { - final var results = m.sendMessage(new Message(message, attachments, List.of()), + final var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()), getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .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()); @@ -213,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."); } } @@ -220,9 +221,7 @@ public class DbusSignalImpl implements Signal { public long sendRemoteDeleteMessage( final long targetSentTimestamp, final String recipient ) { - var recipients = new ArrayList(1); - recipients.add(recipient); - return sendRemoteDeleteMessage(targetSentTimestamp, recipients); + return sendRemoteDeleteMessage(targetSentTimestamp, List.of(recipient)); } @Override @@ -234,23 +233,7 @@ public class DbusSignalImpl implements Signal { getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); - checkSendMessageResults(results.timestamp(), results.results()); - return results.timestamp(); - } catch (IOException e) { - throw new Error.Failure(e.getMessage()); - } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { - throw new Error.GroupNotFound(e.getMessage()); - } - } - - @Override - public long sendGroupRemoteDeleteMessage( - final long targetSentTimestamp, final byte[] groupId - ) { - try { - final var results = m.sendRemoteDeleteMessage(targetSentTimestamp, - Set.of(new RecipientIdentifier.Group(getGroupId(groupId)))); - checkSendMessageResults(results.timestamp(), results.results()); + checkSendMessageResults(results); return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); @@ -267,9 +250,7 @@ public class DbusSignalImpl implements Signal { final long targetSentTimestamp, final String recipient ) { - var recipients = new ArrayList(1); - recipients.add(recipient); - return sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, recipients); + return sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, List.of(recipient)); } @Override @@ -288,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."); } } @@ -302,13 +285,11 @@ public class DbusSignalImpl implements Signal { final String recipient, final boolean stop ) throws Error.Failure, Error.GroupNotFound, Error.UntrustedIdentity { try { - var recipients = new ArrayList(1); - recipients.add(recipient); final var results = m.sendTypingMessage(stop ? TypingAction.STOP : TypingAction.START, - getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() + 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) { @@ -323,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()); } @@ -336,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()); } @@ -365,9 +346,9 @@ public class DbusSignalImpl implements Signal { final String message, final List attachments ) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity { try { - final var results = m.sendMessage(new Message(message, attachments, List.of()), + 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()); @@ -375,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."); } } @@ -382,18 +365,28 @@ 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()); } } + @Override + public void deleteRecipient(final String recipient) throws Error.Failure { + m.deleteRecipient(getSingleRecipientIdentifier(recipient, m.getSelfNumber())); + } + + @Override + public void deleteContact(final String recipient) throws Error.Failure { + m.deleteContact(getSingleRecipientIdentifier(recipient, m.getSelfNumber())); + } + @Override public long sendGroupMessage(final String message, final List attachments, final byte[] groupId) { try { - var results = m.sendMessage(new Message(message, attachments, List.of()), - Set.of(new RecipientIdentifier.Group(getGroupId(groupId)))); - checkSendMessageResults(results.timestamp(), results.results()); + var results = m.sendMessage(new Message(message, attachments, List.of(), Optional.empty()), + Set.of(getGroupRecipientIdentifier(groupId))); + checkSendMessageResults(results); return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); @@ -401,6 +394,39 @@ 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."); + } + } + + @Override + public void sendGroupTyping( + final byte[] groupId, final boolean stop + ) throws Error.Failure, Error.GroupNotFound, Error.UntrustedIdentity { + try { + final var results = m.sendTypingMessage(stop ? TypingAction.STOP : TypingAction.START, + Set.of(getGroupRecipientIdentifier(groupId))); + checkSendMessageResults(results); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { + throw new Error.GroupNotFound(e.getMessage()); + } + } + + @Override + public long sendGroupRemoteDeleteMessage( + final long targetSentTimestamp, final byte[] groupId + ) { + try { + final var results = m.sendRemoteDeleteMessage(targetSentTimestamp, + Set.of(getGroupRecipientIdentifier(groupId))); + 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()); } } @@ -417,13 +443,15 @@ public class DbusSignalImpl implements Signal { remove, getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()), targetSentTimestamp, - Set.of(new RecipientIdentifier.Group(getGroupId(groupId)))); - checkSendMessageResults(results.timestamp(), results.results()); + Set.of(getGroupRecipientIdentifier(groupId))); + 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."); } } @@ -443,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."); } } @@ -452,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."); } } @@ -463,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."); } } @@ -482,11 +516,7 @@ public class DbusSignalImpl implements Signal { @Override public List getGroupIds() { var groups = m.getGroups(); - var ids = new ArrayList(groups.size()); - for (var group : groups) { - ids.add(group.groupId().serialize()); - } - return ids; + return groups.stream().map(g -> g.groupId().serialize()).toList(); } @Override @@ -542,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), @@ -552,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; } @@ -562,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."); } } @@ -578,9 +611,8 @@ public class DbusSignalImpl implements Signal { @Override public List isRegistered(List numbers) { - var results = new ArrayList(); if (numbers.isEmpty()) { - return results; + return List.of(); } Map> registered; @@ -593,7 +625,7 @@ public class DbusSignalImpl implements Signal { return numbers.stream().map(number -> { var uuid = registered.get(number).second(); return uuid != null; - }).collect(Collectors.toList()); + }).toList(); } @Override @@ -637,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."); } } @@ -646,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."); } } @@ -662,10 +698,10 @@ public class DbusSignalImpl implements Signal { public List listNumbers() { return Stream.concat(m.getIdentities().stream().map(Identity::recipient), m.getContacts().stream().map(Pair::first)) - .map(a -> a.getNumber().orElse(null)) + .map(a -> a.number().orElse(null)) .filter(Objects::nonNull) .distinct() - .collect(Collectors.toList()); + .toList(); } @Override @@ -681,12 +717,12 @@ public class DbusSignalImpl implements Signal { // Try profiles if no contact name was found for (var identity : m.getIdentities()) { final var address = identity.recipient(); - var number = address.getNumber().orElse(null); + var number = address.number().orElse(null); if (number != null) { 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); @@ -705,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."); } } @@ -764,13 +802,13 @@ public class DbusSignalImpl implements Signal { } private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException { - var error = ErrorUtils.getErrorMessageFromSendMessageResult(result); + var error = SendMessageResultUtils.getErrorMessageFromSendMessageResult(result); if (error == null) { 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); @@ -779,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 = ErrorUtils.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) { @@ -811,23 +847,23 @@ public class DbusSignalImpl implements Signal { return; } - var errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results); - if (errors.size() == 0) { + var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results); + 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()); } private static List getRecipientStrings(final Set members) { - return members.stream().map(RecipientAddress::getLegacyIdentifier).collect(Collectors.toList()); + return members.stream().map(RecipientAddress::getLegacyIdentifier).toList(); } private static Set getSingleRecipientIdentifiers( @@ -850,6 +886,10 @@ public class DbusSignalImpl implements Signal { } } + private RecipientIdentifier.Group getGroupRecipientIdentifier(final byte[] groupId) { + return new RecipientIdentifier.Group(getGroupId(groupId)); + } + private static GroupId getGroupId(byte[] groupId) throws DBusExecutionException { try { return GroupId.unknownVersion(groupId); @@ -1126,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 @@ -1229,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."); } } }