From d4838bd646c736a5fe1d40b45aa12be239b01ed3 Mon Sep 17 00:00:00 2001 From: John Freed Date: Thu, 7 Oct 2021 07:46:15 +0200 Subject: [PATCH 1/5] implement DBus submitRateLimitChallenge method (#763) update documentation --- man/signal-cli-dbus.5.adoc | 8 +++++++- src/main/java/org/asamk/Signal.java | 4 ++++ .../java/org/asamk/signal/dbus/DbusSignalImpl.java | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/man/signal-cli-dbus.5.adoc b/man/signal-cli-dbus.5.adoc index e7cd083f..594c2941 100755 --- a/man/signal-cli-dbus.5.adoc +++ b/man/signal-cli-dbus.5.adoc @@ -363,8 +363,14 @@ uploadStickerPack(stickerPackPath) -> url:: Exception: Failure -== Signals +submitRateLimitChallenge(challenge, captcha) -> <>:: +* challenge : The challenge token taken from the proof required error. +* captcha : The captcha token from the solved captcha on the Signal website.. +Can be used to lift some rate-limits by solving a captcha. +Exception: IOErrorException + +== Signalss SyncMessageReceived (timestamp, sender, destination, groupId,message, attachments):: The sync message is received when the user sends a message from a linked device. diff --git a/src/main/java/org/asamk/Signal.java b/src/main/java/org/asamk/Signal.java index b8800085..a7832714 100644 --- a/src/main/java/org/asamk/Signal.java +++ b/src/main/java/org/asamk/Signal.java @@ -1,5 +1,7 @@ package org.asamk; +import org.asamk.signal.commands.exceptions.IOErrorException; + import org.freedesktop.dbus.DBusPath; import org.freedesktop.dbus.annotations.DBusProperty; import org.freedesktop.dbus.exceptions.DBusException; @@ -141,6 +143,8 @@ public interface Signal extends DBusInterface { String uploadStickerPack(String stickerPackPath) throws Error.Failure; + void submitRateLimitChallenge(String challenge, String captchaString) throws IOErrorException; + class MessageReceived extends DBusSignal { private final long timestamp; diff --git a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java index ab9c89b2..698ce7c1 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; @@ -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 { From 7829a8d631c22a6c06f4ce176d8bccabd65f9de9 Mon Sep 17 00:00:00 2001 From: AsamK Date: Sun, 3 Oct 2021 17:41:31 +0200 Subject: [PATCH 2/5] Fix device id type --- src/main/java/org/asamk/Signal.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/asamk/Signal.java b/src/main/java/org/asamk/Signal.java index a7832714..1d8d04a3 100644 --- a/src/main/java/org/asamk/Signal.java +++ b/src/main/java/org/asamk/Signal.java @@ -262,7 +262,7 @@ public interface Signal extends DBusInterface { } } - @DBusProperty(name = "Id", type = Integer.class, access = DBusProperty.Access.READ) + @DBusProperty(name = "Id", type = Long.class, access = DBusProperty.Access.READ) @DBusProperty(name = "Name", type = String.class) @DBusProperty(name = "Created", type = String.class, access = DBusProperty.Access.READ) @DBusProperty(name = "LastSeen", type = String.class, access = DBusProperty.Access.READ) From c56a8df9b286eee786c7e81764c5394eced51295 Mon Sep 17 00:00:00 2001 From: AsamK Date: Thu, 7 Oct 2021 20:51:33 +0200 Subject: [PATCH 3/5] Return struct instead of object path directly for dbus list devices --- src/main/java/org/asamk/Signal.java | 34 ++++++++- .../asamk/signal/dbus/DbusManagerImpl.java | 7 +- .../org/asamk/signal/dbus/DbusSignalImpl.java | 76 ++++++++++--------- 3 files changed, 77 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/asamk/Signal.java b/src/main/java/org/asamk/Signal.java index 1d8d04a3..2f81c196 100644 --- a/src/main/java/org/asamk/Signal.java +++ b/src/main/java/org/asamk/Signal.java @@ -3,7 +3,9 @@ package org.asamk; import org.asamk.signal.commands.exceptions.IOErrorException; import org.freedesktop.dbus.DBusPath; +import org.freedesktop.dbus.Struct; import org.freedesktop.dbus.annotations.DBusProperty; +import org.freedesktop.dbus.annotations.Position; import org.freedesktop.dbus.exceptions.DBusException; import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.interfaces.DBusInterface; @@ -104,7 +106,7 @@ public interface Signal extends DBusInterface { DBusPath getDevice(long deviceId); - List listDevices() throws Error.Failure; + List listDevices() throws Error.Failure; DBusPath getThisDevice(); @@ -262,6 +264,36 @@ public interface Signal extends DBusInterface { } } + class StructDevice extends Struct { + + @Position(0) + DBusPath objectPath; + + @Position(1) + Long id; + + @Position(2) + String name; + + public StructDevice(final DBusPath objectPath, final Long id, final String name) { + this.objectPath = objectPath; + this.id = id; + this.name = name; + } + + public DBusPath getObjectPath() { + return objectPath; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + } + @DBusProperty(name = "Id", type = Long.class, access = DBusProperty.Access.READ) @DBusProperty(name = "Name", type = String.class) @DBusProperty(name = "Created", type = String.class, access = DBusProperty.Access.READ) diff --git a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java index 3124a5b0..53148c01 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java @@ -145,13 +145,14 @@ public class DbusManagerImpl implements Manager { @Override public List getLinkedDevices() throws IOException { final var thisDevice = signal.getThisDevice(); - return signal.listDevices().stream().map(devicePath -> { - final var device = getRemoteObject(devicePath, Signal.Device.class).GetAll("org.asamk.Signal.Device"); + return signal.listDevices().stream().map(d -> { + final var device = getRemoteObject(d.getObjectPath(), + Signal.Device.class).GetAll("org.asamk.Signal.Device"); return new Device((long) device.get("Id").getValue(), (String) device.get("Name").getValue(), (long) device.get("Created").getValue(), (long) device.get("LastSeen").getValue(), - thisDevice.equals(devicePath)); + thisDevice.equals(d.getObjectPath())); }).collect(Collectors.toList()); } diff --git a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java index 698ce7c1..d0e33a40 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -57,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; @@ -115,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(); @@ -802,21 +772,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; From 179855272a9eab56bfd8514ec71e684b541feb88 Mon Sep 17 00:00:00 2001 From: AsamK Date: Thu, 7 Oct 2021 20:52:10 +0200 Subject: [PATCH 4/5] Fix dbus properties GetAll method for variants --- src/main/java/org/asamk/signal/dbus/DbusProperties.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/asamk/signal/dbus/DbusProperties.java b/src/main/java/org/asamk/signal/dbus/DbusProperties.java index 37cc35e3..bbe01d6b 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusProperties.java +++ b/src/main/java/org/asamk/signal/dbus/DbusProperties.java @@ -51,6 +51,7 @@ public abstract class DbusProperties implements Properties { } @Override + @SuppressWarnings("unchecked") public Map> GetAll(final String interface_name) { final var handler = getHandlerOptional(interface_name); if (handler.isEmpty()) { @@ -61,6 +62,9 @@ public abstract class DbusProperties implements Properties { .getProperties() .stream() .filter(p -> p.getGetter() != null) - .collect(Collectors.toMap(DbusProperty::getName, p -> new Variant<>(p.getGetter().get()))); + .collect(Collectors.toMap(DbusProperty::getName, p -> { + final Object o = p.getGetter().get(); + return o instanceof Variant ? (Variant) o : new Variant<>(o); + })); } } From cadcc6c8ef13b679d6a542b0712aad202d4a64a7 Mon Sep 17 00:00:00 2001 From: John Freed Date: Sat, 9 Oct 2021 13:04:07 +0200 Subject: [PATCH 5/5] update docs for DBus listDevices method (#768) --- man/signal-cli-dbus.5.adoc | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/man/signal-cli-dbus.5.adoc b/man/signal-cli-dbus.5.adoc index 594c2941..8168c421 100755 --- a/man/signal-cli-dbus.5.adoc +++ b/man/signal-cli-dbus.5.adoc @@ -29,15 +29,18 @@ method(arg1, arg2, ...) -> return Where is according to DBus specification: -* : String -* : Byte Array -* : Array of Byte Arrays -* : String Array -* : Array of signed 64 bit integer -* : Boolean (0|1) -* : Signed 64 bit integer +* : Array of ... (comma-separated list) +* (...) : Struct (cannot be sent via `dbus-send`) +* : Boolean (false|true) (boolean:) +* : Signed 32-bit (int) integer (int32:) +* : DBusPath object (objpath:) +* : String (string:) +* : Signed 64-bit (long) integer (int64:) +* : Unsigned 8-bit (byte) integer (byte:) * <> : no return value +The final parenthetical value (such as "boolean:") is the type indicator used by `dbus-send`. + Exceptions are the names of the Java Exceptions returned in the body field. They typically contain an additional message with details. All Exceptions begin with "org.asamk.Signal.Error." which is omitted here for better readability. Phone numbers always have the format + @@ -340,8 +343,11 @@ addDevice(deviceUri) -> <>:: Exception: InvalidUri -listDevices() -> devices:: -* devices : String array of linked devices +listDevices() -> devices:: +* devices : Array of structs (objectPath, id, name) +** objectPath : DBusPath representing the device's object path +** id : Long representing the deviceId +** name : String representing the device's name Exception: Failure @@ -370,7 +376,7 @@ Can be used to lift some rate-limits by solving a captcha. Exception: IOErrorException -== Signalss +== Signals SyncMessageReceived (timestamp, sender, destination, groupId,message, attachments):: The sync message is received when the user sends a message from a linked device.