package org.asamk.signal.dbus;
import org.asamk.Signal;
+import org.asamk.Signal.Error;
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.Device;
import org.asamk.signal.manager.api.Identity;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.storage.recipients.Profile;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.asamk.signal.util.ErrorUtils;
+import org.freedesktop.dbus.DBusPath;
+import org.freedesktop.dbus.connections.impl.DBusConnection;
+import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.whispersystems.libsignal.InvalidKeyException;
import org.whispersystems.libsignal.util.Pair;
public class DbusSignalImpl implements Signal {
private final Manager m;
+ private final DBusConnection connection;
private final String objectPath;
- public DbusSignalImpl(final Manager m, final String objectPath) {
+ private DBusPath thisDevice;
+ private final List<StructDevice> devices = new ArrayList<>();
+
+ public DbusSignalImpl(final Manager m, DBusConnection connection, final String objectPath) {
this.m = m;
+ this.connection = connection;
this.objectPath = objectPath;
}
- @Override
- public boolean isRemote() {
- return false;
+ public void initObjects() {
+ updateDevices();
+ }
+
+ public void close() {
+ unExportDevices();
}
@Override
}
@Override
- public String getNumber() {
+ public String getSelfNumber() {
return m.getSelfNumber();
}
+ @Override
+ public void submitRateLimitChallenge(String challenge, String captchaString) throws IOErrorException {
+ 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);
+ }
+
+ }
+
@Override
public void addDevice(String uri) {
try {
}
@Override
- public void removeDevice(int deviceId) {
- try {
- m.removeLinkedDevices(deviceId);
- } catch (IOException e) {
- throw new Error.Failure(e.getClass().getSimpleName() + ": Error while removing device: " + e.getMessage());
- }
+ public DBusPath getDevice(long deviceId) {
+ updateDevices();
+ return new DBusPath(getDeviceObjectPath(objectPath, deviceId));
}
@Override
- public List<String> listDevices() {
- List<Device> devices;
- List<String> results = new ArrayList<String>();
-
- try {
- devices = m.getLinkedDevices();
- } catch (IOException | Error.Failure e) {
- throw new Error.Failure("Failed to get linked devices: " + e.getMessage());
- }
-
- return devices.stream().map(d -> d.getName() == null ? "" : d.getName()).collect(Collectors.toList());
+ public List<StructDevice> listDevices() {
+ updateDevices();
+ return this.devices;
}
@Override
- public void updateDeviceName(String deviceName) {
- try {
- m.updateAccountAttributes(deviceName);
- } catch (IOException | Signal.Error.Failure e) {
- throw new Error.Failure("UpdateAccount error: " + e.getMessage());
- }
+ public DBusPath getThisDevice() {
+ updateDevices();
+ return thisDevice;
}
@Override
// the profile name
@Override
public String getContactName(final String number) {
- return m.getContactOrProfileName(getSingleRecipientIdentifier(number, m.getSelfNumber()));
+ final var name = m.getContactOrProfileName(getSingleRecipientIdentifier(number, m.getSelfNumber()));
+ return name == null ? "" : name;
}
@Override
@Override
public String getGroupName(final byte[] groupId) {
var group = m.getGroup(getGroupId(groupId));
- if (group == null) {
+ if (group == null || group.getTitle() == null) {
return "";
} else {
return group.getTitle();
@Override
public byte[] updateGroup(byte[] groupId, String name, List<String> members, String avatar) {
try {
- if (groupId.length == 0) {
- groupId = null;
- }
- if (name.isEmpty()) {
- name = null;
- }
- if (avatar.isEmpty()) {
- avatar = null;
- }
+ groupId = nullIfEmpty(groupId);
+ name = nullIfEmpty(name);
+ avatar = nullIfEmpty(avatar);
final var memberIdentifiers = getSingleRecipientIdentifiers(members, m.getSelfNumber());
if (groupId == null) {
final var results = m.createGroup(name, memberIdentifiers, avatar == null ? null : new File(avatar));
@Override
public void updateProfile(
- final String givenName,
- final String familyName,
- final String about,
- final String aboutEmoji,
+ String givenName,
+ String familyName,
+ String about,
+ String aboutEmoji,
String avatarPath,
final boolean removeAvatar
) {
try {
- if (avatarPath.isEmpty()) {
- avatarPath = null;
- }
+ givenName = nullIfEmpty(givenName);
+ familyName = nullIfEmpty(familyName);
+ about = nullIfEmpty(about);
+ aboutEmoji = nullIfEmpty(aboutEmoji);
+ avatarPath = nullIfEmpty(avatarPath);
Optional<File> avatarFile = removeAvatar
? Optional.absent()
: avatarPath == null ? null : Optional.of(new File(avatarPath));
String avatarPath,
final boolean removeAvatar
) {
- try {
- if (avatarPath.isEmpty()) {
- avatarPath = null;
- }
- Optional<File> avatarFile = removeAvatar
- ? Optional.absent()
- : avatarPath == null ? null : Optional.of(new File(avatarPath));
- m.setProfile(name, null, about, aboutEmoji, avatarFile);
- } catch (IOException e) {
- throw new Error.Failure(e.getMessage());
- }
+ updateProfile(name, "", about, aboutEmoji, avatarPath, removeAvatar);
}
@Override
throw new Error.InvalidGroupId("Invalid group id: " + e.getMessage());
}
}
+
+ private byte[] nullIfEmpty(final byte[] array) {
+ return array.length == 0 ? null : array;
+ }
+
+ private String nullIfEmpty(final String name) {
+ return name.isEmpty() ? null : name;
+ }
+
+ private String emptyIfNull(final String string) {
+ return string == null ? "" : string;
+ }
+
+ private static String getDeviceObjectPath(String basePath, long deviceId) {
+ return basePath + "/Devices/" + deviceId;
+ }
+
+ private void updateDevices() {
+ List<org.asamk.signal.manager.api.Device> linkedDevices;
+ try {
+ linkedDevices = m.getLinkedDevices();
+ } catch (IOException e) {
+ throw new Error.Failure("Failed to get linked devices: " + e.getMessage());
+ }
+
+ unExportDevices();
+
+ linkedDevices.forEach(d -> {
+ final var object = new DbusSignalDeviceImpl(d);
+ final var deviceObjectPath = object.getObjectPath();
+ try {
+ connection.exportObject(object);
+ } catch (DBusException e) {
+ e.printStackTrace();
+ }
+ if (d.isThisDevice()) {
+ thisDevice = new DBusPath(deviceObjectPath);
+ }
+ this.devices.add(new StructDevice(new DBusPath(deviceObjectPath), d.getId(), emptyIfNull(d.getName())));
+ });
+ }
+
+ private void unExportDevices() {
+ this.devices.stream()
+ .map(StructDevice::getObjectPath)
+ .map(DBusPath::getPath)
+ .forEach(connection::unExportObject);
+ this.devices.clear();
+ }
+
+ public class DbusSignalDeviceImpl extends DbusProperties implements Signal.Device {
+
+ private final org.asamk.signal.manager.api.Device device;
+
+ 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))));
+ this.device = device;
+ }
+
+ @Override
+ public String getObjectPath() {
+ return getDeviceObjectPath(objectPath, device.getId());
+ }
+
+ @Override
+ public void removeDevice() throws Error.Failure {
+ try {
+ m.removeLinkedDevices(device.getId());
+ updateDevices();
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ }
+ }
+
+ private void setDeviceName(String name) {
+ if (!device.isThisDevice()) {
+ throw new Error.Failure("Only the name of this device can be changed");
+ }
+ try {
+ m.updateAccountAttributes(name);
+ // update device list
+ updateDevices();
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ }
+ }
+ }
}