Dbus enhancements (#543)

* Dbus enhancements

* reverted sendMessage, completed isContactBlocked, fixed formatting

* Updates based on feedback and fixes

* Added trim

* fixed getContactName Exception handling
This commit is contained in:
Adimarantis 2021-03-01 20:31:17 +01:00 committed by GitHub
parent 8d0de67530
commit 1d2c7a479d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 146 additions and 28 deletions

View file

@ -529,7 +529,7 @@ public class Manager implements Closeable {
ServiceConfig.AUTOMATIC_NETWORK_RETRY); ServiceConfig.AUTOMATIC_NETWORK_RETRY);
} }
private SignalProfile getRecipientProfile( public SignalProfile getRecipientProfile(
SignalServiceAddress address SignalServiceAddress address
) { ) {
return getRecipientProfile(address, false); return getRecipientProfile(address, false);

View file

@ -76,8 +76,8 @@ public class SignalProfile {
} }
public String getDisplayName() { public String getDisplayName() {
// First name and last name (if set) are separated by a NULL char // First name and last name (if set) are separated by a NULL char + trim space in case only one is filled
return name == null ? null : name.replace("\0", " "); return name == null ? null : name.replace("\0", " ").trim();
} }
public String getAbout() { public String getAbout() {

View file

@ -54,6 +54,22 @@ public interface Signal extends DBusInterface {
void updateProfile( void updateProfile(
String name, String about, String aboutEmoji, String avatarPath, boolean removeAvatar String name, String about, String aboutEmoji, String avatarPath, boolean removeAvatar
) throws Error.Failure; ) throws Error.Failure;
public String version();
public List<String> listNumbers();
public List<String> getContactNumber(final String name) throws Error.Failure;
public void quitGroup(final byte[] groupId) throws Error.GroupNotFound,Error.Failure;
public boolean isContactBlocked(final String number);
public boolean isGroupBlocked(final byte[] groupId);
public boolean isMember(final byte[] groupId);
public void joinGroup(final String groupLink) throws Error.Failure;
class MessageReceived extends DBusSignal { class MessageReceived extends DBusSignal {

View file

@ -6,17 +6,24 @@ import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
import org.asamk.signal.manager.storage.protocol.IdentityInfo;
import org.asamk.signal.util.ErrorUtils; import org.asamk.signal.util.ErrorUtils;
import org.asamk.signal.manager.util.Utils;
import org.asamk.signal.BaseConfig;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.messages.SendMessageResult; import org.whispersystems.signalservice.api.messages.SendMessageResult;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.InvalidNumberException;
import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class DbusSignalImpl implements Signal { public class DbusSignalImpl implements Signal {
@ -60,9 +67,8 @@ public class DbusSignalImpl implements Signal {
} }
} }
private static void checkSendMessageResults( private static void checkSendMessageResults(long timestamp, List<SendMessageResult> results)
long timestamp, List<SendMessageResult> results throws DBusExecutionException {
) throws DBusExecutionException {
if (results.size() == 1) { if (results.size() == 1) {
checkSendMessageResult(timestamp, results.get(0)); checkSendMessageResult(timestamp, results.get(0));
return; return;
@ -99,9 +105,8 @@ public class DbusSignalImpl implements Signal {
} }
@Override @Override
public long sendNoteToSelfMessage( public long sendNoteToSelfMessage(final String message, final List<String> attachments)
final String message, final List<String> attachments throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
try { try {
final var results = m.sendSelfMessage(message, attachments); final var results = m.sendSelfMessage(message, attachments);
checkSendMessageResult(results.first(), results.second()); checkSendMessageResult(results.first(), results.second());
@ -140,13 +145,17 @@ public class DbusSignalImpl implements Signal {
} }
} }
// Since contact names might be empty if not defined, also potentially return
// the profile name
@Override @Override
public String getContactName(final String number) { public String getContactName(final String number) {
String name="";
try { try {
return m.getContactName(number); name = m.getContactOrProfileName(number);
} catch (InvalidNumberException e) { } catch (Exception e) {
throw new Error.InvalidNumber(e.getMessage()); throw new Error.InvalidNumber(e.getMessage());
} }
return name;
} }
@Override @Override
@ -202,11 +211,8 @@ public class DbusSignalImpl implements Signal {
if (group == null) { if (group == null) {
return List.of(); return List.of();
} else { } else {
return group.getMembers() return group.getMembers().stream().map(m::resolveSignalServiceAddress)
.stream() .map(SignalServiceAddress::getLegacyIdentifier).collect(Collectors.toList());
.map(m::resolveSignalServiceAddress)
.map(SignalServiceAddress::getLegacyIdentifier)
.collect(Collectors.toList());
} }
} }
@ -225,9 +231,7 @@ public class DbusSignalImpl implements Signal {
if (avatar.isEmpty()) { if (avatar.isEmpty()) {
avatar = null; avatar = null;
} }
final var results = m.updateGroup(groupId == null ? null : GroupId.unknownVersion(groupId), final var results = m.updateGroup(groupId == null ? null : GroupId.unknownVersion(groupId), name, members,
name,
members,
avatar == null ? null : new File(avatar)); avatar == null ? null : new File(avatar));
checkSendMessageResults(0, results.second()); checkSendMessageResults(0, results.second());
return results.first().serialize(); return results.first().serialize();
@ -248,19 +252,13 @@ public class DbusSignalImpl implements Signal {
} }
@Override @Override
public void updateProfile( public void updateProfile(final String name, final String about, final String aboutEmoji, String avatarPath,
final String name, final boolean removeAvatar) {
final String about,
final String aboutEmoji,
String avatarPath,
final boolean removeAvatar
) {
try { try {
if (avatarPath.isEmpty()) { if (avatarPath.isEmpty()) {
avatarPath = null; avatarPath = null;
} }
Optional<File> avatarFile = removeAvatar Optional<File> avatarFile = removeAvatar ? Optional.absent()
? Optional.absent()
: avatarPath == null ? null : Optional.of(new File(avatarPath)); : avatarPath == null ? null : Optional.of(new File(avatarPath));
m.setProfile(name, about, aboutEmoji, avatarFile); m.setProfile(name, about, aboutEmoji, avatarFile);
} catch (IOException e) { } catch (IOException e) {
@ -268,4 +266,108 @@ 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<String> 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<String> getContactNumber(final String name) {
// Contact names have precendence.
List<String> 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 (IdentityInfo identity : m.getIdentities()) {
String number = identity.getAddress().getNumber().orNull();
if (number != null) {
var address = Utils.getSignalServiceAddressFromIdentifier(number);
var profile = m.getRecipientProfile(address);
String profileName = profile.getDisplayName();
if (profileName.equals(name)) {
numbers.add(number);
}
}
}
if (numbers.size()==0) {
throw new Error.Failure("Contact name not found");
}
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) {
final GroupInviteLinkUrl linkUrl;
try {
linkUrl = GroupInviteLinkUrl.fromUri(groupLink);
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());
}
}
} }