X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/778adacb80bae7d6ecc1d70fa87f9217c7bc1c71..3c40b11:/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 ab9c89b2..41eca5da 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -1,7 +1,9 @@ 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; @@ -55,7 +57,7 @@ public class DbusSignalImpl implements Signal { private final String objectPath; private DBusPath thisDevice; - private final List devices = new ArrayList<>(); + private final List devices = new ArrayList<>(); public DbusSignalImpl(final Manager m, DBusConnection connection, final String objectPath) { this.m = m; @@ -81,6 +83,18 @@ public class DbusSignalImpl implements Signal { 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 { @@ -101,41 +115,11 @@ public class DbusSignalImpl implements Signal { } @Override - public List listDevices() { + public List listDevices() { updateDevices(); return this.devices; } - private void updateDevices() { - List linkedDevices; - try { - linkedDevices = m.getLinkedDevices(); - } catch (IOException | Error.Failure 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 DBusPath(deviceObjectPath)); - }); - } - - private void unExportDevices() { - this.devices.stream().map(DBusPath::getPath).forEach(connection::unExportObject); - this.devices.clear(); - } - @Override public DBusPath getThisDevice() { updateDevices(); @@ -683,12 +667,36 @@ public class DbusSignalImpl implements Signal { try { return m.uploadStickerPack(path).toString(); } catch (IOException e) { - throw new Error.Failure("Upload error (maybe image size is too large):" + e.getMessage()); + throw new Error.IOError("Upload error (maybe image size is too large):" + e.getMessage()); } catch (StickerPackInvalidException e) { throw new Error.Failure("Invalid sticker pack: " + e.getMessage()); } } + @Override + public void setConfiguration(boolean readReceipts, boolean unidentifiedDeliveryIndicators, boolean typingIndicators, boolean linkPreviews) { + try { + m.updateConfiguration(readReceipts, unidentifiedDeliveryIndicators, typingIndicators, linkPreviews); + } catch (IOException e) { + throw new Error.IOError("UpdateAccount error: " + e.getMessage()); + } catch (NotMasterDeviceException e) { + throw new Error.UserError("This command doesn't work on linked devices."); + } + } + + @Override + public List getConfiguration() { + List config = new ArrayList<>(4); + try { + config = m.getConfiguration(); + } catch (IOException e) { + throw new Error.IOError("Configuration storage error: " + e.getMessage()); + } catch (NotMasterDeviceException e) { + throw new Error.UserError("This command doesn't work on linked devices."); + } + return config; + } + private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException { var error = ErrorUtils.getErrorMessageFromSendMessageResult(result); @@ -788,21 +796,55 @@ public class DbusSignalImpl implements Signal { 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 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(); super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device", List.of(new DbusProperty<>("Id", device::getId), - new DbusProperty<>("Name", - () -> device.getName() == null ? "" : device.getName(), - this::setDeviceName), + new DbusProperty<>("Name", () -> emptyIfNull(device.getName()), this::setDeviceName), new DbusProperty<>("Created", device::getCreated), new DbusProperty<>("LastSeen", device::getLastSeen)))); this.device = device;