X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/997b3c6a2a1f2e9a50fe579ac726677ea0d57d0c..eec7aec069307827034702f6fde4026f42b96bdd:/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 2cf3c813..8906ac47 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -2,15 +2,19 @@ package org.asamk.signal.dbus; import org.asamk.Signal; import org.asamk.signal.BaseConfig; -import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.manager.AttachmentInvalidException; import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.NotMasterDeviceException; import org.asamk.signal.manager.StickerPackInvalidException; import org.asamk.signal.manager.UntrustedIdentityException; import org.asamk.signal.manager.api.Identity; +import org.asamk.signal.manager.api.InactiveGroupLinkException; +import org.asamk.signal.manager.api.InvalidDeviceLinkException; +import org.asamk.signal.manager.api.InvalidNumberException; 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.TypingAction; import org.asamk.signal.manager.api.UpdateGroup; import org.asamk.signal.manager.groups.GroupId; @@ -29,14 +33,6 @@ import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.exceptions.DBusException; import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.types.Variant; -import org.whispersystems.libsignal.InvalidKeyException; -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; -import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException; -import org.whispersystems.signalservice.api.util.InvalidNumberException; -import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException; import java.io.File; import java.io.IOException; @@ -50,6 +46,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; @@ -78,6 +75,7 @@ public class DbusSignalImpl implements Signal { public void close() { unExportDevices(); + unExportGroups(); } @Override @@ -91,13 +89,13 @@ public class DbusSignalImpl implements Signal { } @Override - public void submitRateLimitChallenge(String challenge, String captchaString) throws IOErrorException { + public void submitRateLimitChallenge(String challenge, String captchaString) { final var captcha = captchaString == null ? null : captchaString.replace("signalcaptcha://", ""); try { m.submitRateLimitRecaptchaChallenge(challenge, captcha); } catch (IOException e) { - throw new IOErrorException("Submit challenge error: " + e.getMessage(), e); + throw new Error.Failure("Submit challenge error: " + e.getMessage()); } } @@ -106,7 +104,7 @@ public class DbusSignalImpl implements Signal { public void addDevice(String uri) { try { m.addDeviceLink(new URI(uri)); - } catch (IOException | InvalidKeyException e) { + } catch (IOException | InvalidDeviceLinkException e) { throw new Error.Failure(e.getClass().getSimpleName() + " Add device link failed. " + e.getMessage()); } catch (URISyntaxException e) { throw new Error.InvalidUri(e.getClass().getSimpleName() @@ -152,12 +150,12 @@ public class DbusSignalImpl implements Signal { .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); - checkSendMessageResults(results.getTimestamp(), results.getResults()); - return results.getTimestamp(); + checkSendMessageResults(results.timestamp(), results.results()); + return results.timestamp(); } catch (AttachmentInvalidException e) { throw new Error.AttachmentInvalid(e.getMessage()); } catch (IOException e) { - throw new Error.Failure(e.getMessage()); + throw new Error.Failure(e); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { throw new Error.GroupNotFound(e.getMessage()); } @@ -181,8 +179,8 @@ public class DbusSignalImpl implements Signal { getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); - checkSendMessageResults(results.getTimestamp(), results.getResults()); - return results.getTimestamp(); + checkSendMessageResults(results.timestamp(), results.results()); + return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { @@ -197,8 +195,8 @@ public class DbusSignalImpl implements Signal { try { final var results = m.sendRemoteDeleteMessage(targetSentTimestamp, Set.of(new RecipientIdentifier.Group(getGroupId(groupId)))); - checkSendMessageResults(results.getTimestamp(), results.getResults()); - return results.getTimestamp(); + checkSendMessageResults(results.timestamp(), results.results()); + return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { @@ -235,8 +233,8 @@ public class DbusSignalImpl implements Signal { getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); - checkSendMessageResults(results.getTimestamp(), results.getResults()); - return results.getTimestamp(); + checkSendMessageResults(results.timestamp(), results.results()); + return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { @@ -277,6 +275,19 @@ public class DbusSignalImpl implements Signal { } } + @Override + public void sendViewedReceipt( + final String recipient, final List messageIds + ) throws Error.Failure, Error.UntrustedIdentity { + try { + m.sendViewedReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()), messageIds); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } catch (UntrustedIdentityException e) { + throw new Error.UntrustedIdentity(e.getMessage()); + } + } + @Override public void sendContacts() { try { @@ -302,8 +313,8 @@ public class DbusSignalImpl implements Signal { try { final var results = m.sendMessage(new Message(message, attachments), Set.of(RecipientIdentifier.NoteToSelf.INSTANCE)); - checkSendMessageResults(results.getTimestamp(), results.getResults()); - return results.getTimestamp(); + checkSendMessageResults(results.timestamp(), results.results()); + return results.timestamp(); } catch (AttachmentInvalidException e) { throw new Error.AttachmentInvalid(e.getMessage()); } catch (IOException e) { @@ -317,7 +328,7 @@ public class DbusSignalImpl implements Signal { public void sendEndSessionMessage(final List recipients) { try { final var results = m.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients, m.getSelfNumber())); - checkSendMessageResults(results.getTimestamp(), results.getResults()); + checkSendMessageResults(results.timestamp(), results.results()); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } @@ -328,8 +339,8 @@ public class DbusSignalImpl implements Signal { try { var results = m.sendMessage(new Message(message, attachments), Set.of(new RecipientIdentifier.Group(getGroupId(groupId)))); - checkSendMessageResults(results.getTimestamp(), results.getResults()); - return results.getTimestamp(); + checkSendMessageResults(results.timestamp(), results.results()); + return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { @@ -353,8 +364,8 @@ public class DbusSignalImpl implements Signal { getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()), targetSentTimestamp, Set.of(new RecipientIdentifier.Group(getGroupId(groupId)))); - checkSendMessageResults(results.getTimestamp(), results.getResults()); - return results.getTimestamp(); + checkSendMessageResults(results.timestamp(), results.results()); + return results.timestamp(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { @@ -376,7 +387,7 @@ public class DbusSignalImpl implements Signal { m.setContactName(getSingleRecipientIdentifier(number, m.getSelfNumber()), name); } catch (NotMasterDeviceException e) { throw new Error.Failure("This command doesn't work on linked devices."); - } catch (UnregisteredUserException e) { + } catch (IOException e) { throw new Error.Failure("Contact is not registered."); } } @@ -419,7 +430,7 @@ public class DbusSignalImpl implements Signal { var groups = m.getGroups(); var ids = new ArrayList(groups.size()); for (var group : groups) { - ids.add(group.getGroupId().serialize()); + ids.add(group.groupId().serialize()); } return ids; } @@ -443,10 +454,10 @@ public class DbusSignalImpl implements Signal { @Override public String getGroupName(final byte[] groupId) { var group = m.getGroup(getGroupId(groupId)); - if (group == null || group.getTitle() == null) { + if (group == null || group.title() == null) { return ""; } else { - return group.getTitle(); + return group.title(); } } @@ -456,7 +467,7 @@ public class DbusSignalImpl implements Signal { if (group == null) { return List.of(); } else { - final var members = group.getMembers(); + final var members = group.members(); return getRecipientStrings(members); } } @@ -477,7 +488,7 @@ 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().getTimestamp(), results.second().getResults()); + checkSendMessageResults(results.second().timestamp(), results.second().results()); return results.first().serialize(); } else { final var results = m.updateGroup(getGroupId(groupId), @@ -487,7 +498,7 @@ public class DbusSignalImpl implements Signal { .withAvatarFile(avatar == null ? null : new File(avatar)) .build()); if (results != null) { - checkSendMessageResults(results.getTimestamp(), results.getResults()); + checkSendMessageResults(results.timestamp(), results.results()); } return groupId; } @@ -547,7 +558,7 @@ public class DbusSignalImpl implements Signal { aboutEmoji = nullIfEmpty(aboutEmoji); avatarPath = nullIfEmpty(avatarPath); Optional avatarFile = removeAvatar - ? Optional.absent() + ? Optional.empty() : avatarPath == null ? null : Optional.of(new File(avatarPath)); m.setProfile(givenName, familyName, about, aboutEmoji, avatarFile); } catch (IOException e) { @@ -569,9 +580,7 @@ public class DbusSignalImpl implements Signal { @Override public void removePin() { try { - m.setRegistrationLockPin(Optional.absent()); - } catch (UnauthenticatedResponseException e) { - throw new Error.Failure("Remove pin failed with unauthenticated response: " + e.getMessage()); + m.setRegistrationLockPin(Optional.empty()); } catch (IOException e) { throw new Error.Failure("Remove pin error: " + e.getMessage()); } @@ -581,8 +590,6 @@ public class DbusSignalImpl implements Signal { public void setPin(String registrationLockPin) { try { m.setRegistrationLockPin(Optional.of(registrationLockPin)); - } catch (UnauthenticatedResponseException e) { - throw new Error.Failure("Set pin error failed with unauthenticated response: " + e.getMessage()); } catch (IOException e) { throw new Error.Failure("Set pin error: " + e.getMessage()); } @@ -599,8 +606,8 @@ public class DbusSignalImpl implements Signal { // all numbers the system knows @Override public List listNumbers() { - return Stream.concat(m.getIdentities().stream().map(Identity::getRecipient), - m.getContacts().stream().map(Pair::first)) + return Stream.concat(m.getIdentities().stream().map(Identity::recipient), + m.getContacts().stream().map(Pair::first)) .map(a -> a.getNumber().orElse(null)) .filter(Objects::nonNull) .distinct() @@ -619,13 +626,13 @@ public class DbusSignalImpl implements Signal { } // Try profiles if no contact name was found for (var identity : m.getIdentities()) { - final var address = identity.getRecipient(); + final var address = identity.recipient(); var number = address.getNumber().orElse(null); if (number != null) { Profile profile = null; try { profile = m.getRecipientProfile(RecipientIdentifier.Single.fromAddress(address)); - } catch (UnregisteredUserException ignored) { + } catch (IOException ignored) { } if (profile != null && profile.getDisplayName().equals(name)) { numbers.add(number); @@ -656,7 +663,7 @@ public class DbusSignalImpl implements Signal { } final var result = m.joinGroup(linkUrl); return result.first().serialize(); - } catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupLinkNotActiveException e) { + } catch (GroupInviteLinkUrl.InvalidGroupLinkException | InactiveGroupLinkException e) { throw new Error.Failure("Group link is invalid: " + e.getMessage()); } catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) { throw new Error.Failure("Group link was created with an incompatible version: " + e.getMessage()); @@ -711,7 +718,7 @@ public class DbusSignalImpl implements Signal { final var message = timestamp + "\nFailed to send message:\n" + error + '\n'; - if (result.getIdentityFailure() != null) { + if (result.isIdentityFailure()) { throw new Error.UntrustedIdentity(message); } else { throw new Error.Failure(message); @@ -834,7 +841,7 @@ public class DbusSignalImpl implements Signal { if (d.isThisDevice()) { thisDevice = new DBusPath(deviceObjectPath); } - this.devices.add(new StructDevice(new DBusPath(deviceObjectPath), d.getId(), emptyIfNull(d.getName()))); + this.devices.add(new StructDevice(new DBusPath(deviceObjectPath), d.id(), emptyIfNull(d.name()))); }); } @@ -861,15 +868,15 @@ public class DbusSignalImpl implements Signal { unExportGroups(); groups.forEach(g -> { - final var object = new DbusSignalGroupImpl(g.getGroupId()); + final var object = new DbusSignalGroupImpl(g.groupId()); try { connection.exportObject(object); } catch (DBusException e) { e.printStackTrace(); } this.groups.add(new StructGroup(new DBusPath(object.getObjectPath()), - g.getGroupId().serialize(), - emptyIfNull(g.getTitle()))); + g.groupId().serialize(), + emptyIfNull(g.title()))); }); } @@ -884,22 +891,22 @@ public class DbusSignalImpl implements Signal { public DbusSignalDeviceImpl(final org.asamk.signal.manager.api.Device device) { super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device", - List.of(new DbusProperty<>("Id", device::getId), - new DbusProperty<>("Name", () -> emptyIfNull(device.getName()), this::setDeviceName), - new DbusProperty<>("Created", device::getCreated), - new DbusProperty<>("LastSeen", device::getLastSeen)))); + List.of(new DbusProperty<>("Id", device::id), + new DbusProperty<>("Name", () -> emptyIfNull(device.name()), this::setDeviceName), + new DbusProperty<>("Created", device::created), + new DbusProperty<>("LastSeen", device::lastSeen)))); this.device = device; } @Override public String getObjectPath() { - return getDeviceObjectPath(objectPath, device.getId()); + return getDeviceObjectPath(objectPath, device.id()); } @Override public void removeDevice() throws Error.Failure { try { - m.removeLinkedDevices(device.getId()); + m.removeLinkedDevices(device.id()); updateDevices(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); @@ -928,36 +935,36 @@ public class DbusSignalImpl implements Signal { this.groupId = groupId; super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Group", List.of(new DbusProperty<>("Id", groupId::serialize), - new DbusProperty<>("Name", () -> emptyIfNull(getGroup().getTitle()), this::setGroupName), + new DbusProperty<>("Name", () -> emptyIfNull(getGroup().title()), this::setGroupName), new DbusProperty<>("Description", - () -> emptyIfNull(getGroup().getDescription()), + () -> emptyIfNull(getGroup().description()), this::setGroupDescription), new DbusProperty<>("Avatar", this::setGroupAvatar), new DbusProperty<>("IsBlocked", () -> getGroup().isBlocked(), this::setIsBlocked), new DbusProperty<>("IsMember", () -> getGroup().isMember()), new DbusProperty<>("IsAdmin", () -> getGroup().isAdmin()), new DbusProperty<>("MessageExpirationTimer", - () -> getGroup().getMessageExpirationTimer(), + () -> getGroup().messageExpirationTimer(), this::setMessageExpirationTime), new DbusProperty<>("Members", - () -> new Variant<>(getRecipientStrings(getGroup().getMembers()), "as")), + () -> new Variant<>(getRecipientStrings(getGroup().members()), "as")), new DbusProperty<>("PendingMembers", - () -> new Variant<>(getRecipientStrings(getGroup().getPendingMembers()), "as")), + () -> new Variant<>(getRecipientStrings(getGroup().pendingMembers()), "as")), new DbusProperty<>("RequestingMembers", - () -> new Variant<>(getRecipientStrings(getGroup().getRequestingMembers()), "as")), + () -> new Variant<>(getRecipientStrings(getGroup().requestingMembers()), "as")), new DbusProperty<>("Admins", - () -> new Variant<>(getRecipientStrings(getGroup().getAdminMembers()), "as")), + () -> new Variant<>(getRecipientStrings(getGroup().adminMembers()), "as")), new DbusProperty<>("PermissionAddMember", - () -> getGroup().getPermissionAddMember().name(), + () -> getGroup().permissionAddMember().name(), this::setGroupPermissionAddMember), new DbusProperty<>("PermissionEditDetails", - () -> getGroup().getPermissionEditDetails().name(), + () -> getGroup().permissionEditDetails().name(), this::setGroupPermissionEditDetails), new DbusProperty<>("PermissionSendMessage", - () -> getGroup().getPermissionSendMessage().name(), + () -> getGroup().permissionSendMessage().name(), this::setGroupPermissionSendMessage), new DbusProperty<>("GroupInviteLink", () -> { - final var groupInviteLinkUrl = getGroup().getGroupInviteLinkUrl(); + final var groupInviteLinkUrl = getGroup().groupInviteLinkUrl(); return groupInviteLinkUrl == null ? "" : groupInviteLinkUrl.getUrl(); })))); }