X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/631f10c916652bf3fc521b2f799e8311282462b1..a91e3f762e0e5d6a9cf9e208fe85207d7829e22d:/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 552c80ea..6606e966 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -4,14 +4,15 @@ import org.asamk.Signal; import org.asamk.signal.BaseConfig; import org.asamk.signal.manager.AttachmentInvalidException; import org.asamk.signal.manager.Manager; +import org.asamk.signal.manager.NotMasterDeviceException; import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupInviteLinkUrl; import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.NotAGroupMemberException; -import org.asamk.signal.manager.storage.protocol.IdentityInfo; -import org.asamk.signal.manager.util.Utils; +import org.asamk.signal.manager.storage.identities.IdentityInfo; import org.asamk.signal.util.ErrorUtils; import org.freedesktop.dbus.exceptions.DBusExecutionException; +import org.whispersystems.libsignal.util.Pair; import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException; import org.whispersystems.signalservice.api.messages.SendMessageResult; @@ -105,6 +106,77 @@ public class DbusSignalImpl implements Signal { } } + @Override + public long sendRemoteDeleteMessage( + final long targetSentTimestamp, final String recipient + ) { + var recipients = new ArrayList(1); + recipients.add(recipient); + return sendRemoteDeleteMessage(targetSentTimestamp, recipients); + } + + @Override + public long sendRemoteDeleteMessage( + final long targetSentTimestamp, final List recipients + ) { + try { + final var results = m.sendRemoteDeleteMessage(targetSentTimestamp, recipients); + checkSendMessageResults(results.first(), results.second()); + return results.first(); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } catch (InvalidNumberException e) { + throw new Error.InvalidNumber(e.getMessage()); + } + } + + @Override + public long sendGroupRemoteDeleteMessage( + final long targetSentTimestamp, final byte[] groupId + ) { + try { + final var results = m.sendGroupRemoteDeleteMessage(targetSentTimestamp, GroupId.unknownVersion(groupId)); + checkSendMessageResults(results.first(), results.second()); + return results.first(); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } catch (GroupNotFoundException | NotAGroupMemberException e) { + throw new Error.GroupNotFound(e.getMessage()); + } + } + + @Override + public long sendMessageReaction( + final String emoji, + final boolean remove, + final String targetAuthor, + final long targetSentTimestamp, + final String recipient + ) { + var recipients = new ArrayList(1); + recipients.add(recipient); + return sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, recipients); + } + + @Override + public long sendMessageReaction( + final String emoji, + final boolean remove, + final String targetAuthor, + final long targetSentTimestamp, + final List recipients + ) { + try { + final var results = m.sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, recipients); + checkSendMessageResults(results.first(), results.second()); + return results.first(); + } catch (InvalidNumberException e) { + throw new Error.InvalidNumber(e.getMessage()); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } + } + @Override public long sendNoteToSelfMessage( final String message, final List attachments @@ -147,17 +219,40 @@ public class DbusSignalImpl implements Signal { } } + @Override + public long sendGroupMessageReaction( + final String emoji, + final boolean remove, + final String targetAuthor, + final long targetSentTimestamp, + final byte[] groupId + ) { + try { + final var results = m.sendGroupMessageReaction(emoji, + remove, + targetAuthor, + targetSentTimestamp, + GroupId.unknownVersion(groupId)); + checkSendMessageResults(results.first(), results.second()); + return results.first(); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } catch (InvalidNumberException e) { + throw new Error.InvalidNumber(e.getMessage()); + } catch (GroupNotFoundException | NotAGroupMemberException e) { + throw new Error.GroupNotFound(e.getMessage()); + } + } + // Since contact names might be empty if not defined, also potentially return // the profile name @Override public String getContactName(final String number) { - String name = ""; try { - name = m.getContactOrProfileName(number); - } catch (Exception e) { + return m.getContactOrProfileName(number); + } catch (InvalidNumberException e) { throw new Error.InvalidNumber(e.getMessage()); } - return name; } @Override @@ -166,6 +261,8 @@ public class DbusSignalImpl implements Signal { m.setContactName(number, name); } catch (InvalidNumberException e) { throw new Error.InvalidNumber(e.getMessage()); + } catch (NotMasterDeviceException e) { + throw new Error.Failure("This command doesn't work on linked devices."); } } @@ -175,6 +272,8 @@ public class DbusSignalImpl implements Signal { m.setContactBlocked(number, blocked); } catch (InvalidNumberException e) { throw new Error.InvalidNumber(e.getMessage()); + } catch (NotMasterDeviceException e) { + throw new Error.Failure("This command doesn't work on linked devices."); } } @@ -236,12 +335,20 @@ public class DbusSignalImpl implements Signal { if (avatar.isEmpty()) { avatar = null; } - final var results = m.updateGroup(groupId == null ? null : GroupId.unknownVersion(groupId), - name, - members, - avatar == null ? null : new File(avatar)); - checkSendMessageResults(0, results.second()); - return results.first().serialize(); + if (groupId == null) { + final var results = m.createGroup(name, members, avatar == null ? null : new File(avatar)); + checkSendMessageResults(0, results.second()); + return results.first().serialize(); + } else { + final var results = m.updateGroup(GroupId.unknownVersion(groupId), + name, + null, + members, + null, + avatar == null ? null : new File(avatar)); + checkSendMessageResults(results.first(), results.second()); + return groupId; + } } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException e) { @@ -273,7 +380,7 @@ public class DbusSignalImpl implements Signal { Optional avatarFile = removeAvatar ? Optional.absent() : avatarPath == null ? null : Optional.of(new File(avatarPath)); - m.setProfile(name, about, aboutEmoji, avatarFile); + m.setProfile(name, null, about, aboutEmoji, avatarFile); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } @@ -290,8 +397,10 @@ public class DbusSignalImpl implements Signal { // all numbers the system knows @Override public List listNumbers() { - return Stream.concat(m.getIdentities().stream().map(i -> i.getAddress().getNumber().orNull()), - m.getContacts().stream().map(c -> c.number)) + return Stream.concat(m.getIdentities().stream().map(IdentityInfo::getRecipientId), + m.getContacts().stream().map(Pair::first)) + .map(m::resolveSignalServiceAddress) + .map(a -> a.getNumber().orNull()) .filter(Objects::nonNull) .distinct() .collect(Collectors.toList()); @@ -299,29 +408,26 @@ public class DbusSignalImpl implements Signal { @Override public List getContactNumber(final String name) { - // Contact names have precendence. - List numbers = new ArrayList<>(); + // Contact names have precedence. + var numbers = new ArrayList(); var contacts = m.getContacts(); for (var c : contacts) { - if (c.name != null && c.name.equals(name)) { - numbers.add(c.number); + if (name.equals(c.second().getName())) { + numbers.add(m.resolveSignalServiceAddress(c.first()).getLegacyIdentifier()); } } // Try profiles if no contact name was found - for (IdentityInfo identity : m.getIdentities()) { - String number = identity.getAddress().getNumber().orNull(); + for (var identity : m.getIdentities()) { + final var recipientId = identity.getRecipientId(); + final var address = m.resolveSignalServiceAddress(recipientId); + var number = address.getNumber().orNull(); if (number != null) { - var address = Utils.getSignalServiceAddressFromIdentifier(number); - var profile = m.getRecipientProfile(address); - String profileName = profile.getDisplayName(); - if (profileName.equals(name)) { + var profile = m.getRecipientProfile(recipientId); + if (profile != null && profile.getDisplayName().equals(name)) { numbers.add(number); } } } - if (numbers.size() == 0) { - throw new Error.Failure("Contact name not found"); - } return numbers; } @@ -339,9 +445,11 @@ public class DbusSignalImpl implements Signal { @Override public void joinGroup(final String groupLink) { - final GroupInviteLinkUrl linkUrl; try { - linkUrl = GroupInviteLinkUrl.fromUri(groupLink); + final var linkUrl = GroupInviteLinkUrl.fromUri(groupLink); + if (linkUrl == null) { + throw new Error.Failure("Group link is invalid:"); + } m.joinGroup(linkUrl); } catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupLinkNotActiveException e) { throw new Error.Failure("Group link is invalid: " + e.getMessage()); @@ -354,13 +462,11 @@ public class DbusSignalImpl implements Signal { @Override public boolean isContactBlocked(final String number) { - var contacts = m.getContacts(); - for (var c : contacts) { - if (c.number.equals(number)) { - return c.blocked; - } + try { + return m.isContactBlocked(number); + } catch (InvalidNumberException e) { + throw new Error.InvalidNumber(e.getMessage()); } - return false; } @Override @@ -379,8 +485,7 @@ public class DbusSignalImpl implements Signal { if (group == null) { return false; } else { - return group.isMember(m.getSelfAddress()); + return group.isMember(m.getSelfRecipientId()); } } - }