X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/de273586b4106171a3940ce5ec2c2b3712430c8c..a7b414a8703d27a012811f5621ecab28b329351f:/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 8cbbbc03..24a90662 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -1,14 +1,17 @@ package org.asamk.signal.dbus; 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.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.util.ErrorUtils; import org.freedesktop.dbus.exceptions.DBusExecutionException; 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.SignalServiceAddress; import org.whispersystems.signalservice.api.util.InvalidNumberException; @@ -17,7 +20,9 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; +import java.util.stream.Stream; public class DbusSignalImpl implements Signal { @@ -140,11 +145,13 @@ public class DbusSignalImpl implements Signal { } } + // Since contact names might be empty if not defined, also potentially return + // the profile name @Override public String getContactName(final String number) { try { - return m.getContactName(number); - } catch (InvalidNumberException e) { + return m.getContactOrProfileName(number); + } catch (Exception e) { throw new Error.InvalidNumber(e.getMessage()); } } @@ -268,4 +275,105 @@ public class DbusSignalImpl implements Signal { } } + // Provide option to query a version string in order to react on potential + // future interface changes + @Override + public String version() { + return BaseConfig.PROJECT_VERSION; + } + + // Create a unique list of Numbers from Identities and Contacts to really get + // 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)) + .filter(Objects::nonNull) + .distinct() + .collect(Collectors.toList()); + } + + @Override + public List getContactNumber(final String name) { + // 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); + } + } + // Try profiles if no contact name was found + for (var identity : m.getIdentities()) { + final var address = identity.getAddress(); + var number = address.getNumber().orNull(); + if (number != null) { + var profile = m.getRecipientProfile(address); + if (profile != null && profile.getDisplayName().equals(name)) { + numbers.add(number); + } + } + } + return numbers; + } + + @Override + public void quitGroup(final byte[] groupId) { + var group = GroupId.unknownVersion(groupId); + try { + m.sendQuitGroupMessage(group); + } catch (GroupNotFoundException | NotAGroupMemberException e) { + throw new Error.GroupNotFound(e.getMessage()); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } + } + + @Override + public void joinGroup(final String groupLink) { + try { + 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()); + } catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) { + throw new Error.Failure("Group link was created with an incompatible version: " + e.getMessage()); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } + } + + @Override + public boolean isContactBlocked(final String number) { + var contacts = m.getContacts(); + for (var c : contacts) { + if (c.number.equals(number)) { + return c.blocked; + } + } + return false; + } + + @Override + public boolean isGroupBlocked(final byte[] groupId) { + var group = m.getGroup(GroupId.unknownVersion(groupId)); + if (group == null) { + return false; + } else { + return group.isBlocked(); + } + } + + @Override + public boolean isMember(final byte[] groupId) { + var group = m.getGroup(GroupId.unknownVersion(groupId)); + if (group == null) { + return false; + } else { + return group.isMember(m.getSelfAddress()); + } + } }