]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java
Implement configuration properties for dbus
[signal-cli] / src / main / java / org / asamk / signal / dbus / DbusSignalImpl.java
index 25c80ffbfb926ae2e907d49691293e854b6eee80..047b4f995e5a825182fee5adfed99036e7133e8a 100644 (file)
@@ -2,15 +2,19 @@ package org.asamk.signal.dbus;
 
 import org.asamk.Signal;
 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.Identity;
+import org.asamk.signal.manager.api.InactiveGroupLinkException;
+import org.asamk.signal.manager.api.InvalidDeviceLinkException;
+import org.asamk.signal.manager.api.InvalidNumberException;
 import org.asamk.signal.manager.api.Message;
+import org.asamk.signal.manager.api.Pair;
 import org.asamk.signal.manager.api.RecipientIdentifier;
+import org.asamk.signal.manager.api.SendMessageResult;
 import org.asamk.signal.manager.api.TypingAction;
 import org.asamk.signal.manager.api.UpdateGroup;
 import org.asamk.signal.manager.groups.GroupId;
@@ -29,13 +33,8 @@ import org.freedesktop.dbus.connections.impl.DBusConnection;
 import org.freedesktop.dbus.exceptions.DBusException;
 import org.freedesktop.dbus.exceptions.DBusExecutionException;
 import org.freedesktop.dbus.types.Variant;
-import org.whispersystems.libsignal.InvalidKeyException;
-import org.whispersystems.libsignal.util.Pair;
-import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
-import org.whispersystems.signalservice.api.messages.SendMessageResult;
-import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
-import org.whispersystems.signalservice.api.util.InvalidNumberException;
-import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.io.IOException;
@@ -65,6 +64,8 @@ public class DbusSignalImpl implements Signal {
     private final List<StructDevice> devices = new ArrayList<>();
     private final List<StructGroup> groups = new ArrayList<>();
 
+    private final static Logger logger = LoggerFactory.getLogger(DbusSignalImpl.class);
+
     public DbusSignalImpl(final Manager m, DBusConnection connection, final String objectPath) {
         this.m = m;
         this.connection = connection;
@@ -74,11 +75,13 @@ public class DbusSignalImpl implements Signal {
     public void initObjects() {
         updateDevices();
         updateGroups();
+        updateConfiguration();
     }
 
     public void close() {
         unExportDevices();
         unExportGroups();
+        unExportConfiguration();
     }
 
     @Override
@@ -92,13 +95,13 @@ public class DbusSignalImpl implements Signal {
     }
 
     @Override
-    public void submitRateLimitChallenge(String challenge, String captchaString) throws IOErrorException {
+    public void submitRateLimitChallenge(String challenge, String captchaString) {
         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);
+            throw new Error.Failure("Submit challenge error: " + e.getMessage());
         }
 
     }
@@ -107,7 +110,7 @@ public class DbusSignalImpl implements Signal {
     public void addDevice(String uri) {
         try {
             m.addDeviceLink(new URI(uri));
-        } catch (IOException | InvalidKeyException e) {
+        } catch (IOException | InvalidDeviceLinkException e) {
             throw new Error.Failure(e.getClass().getSimpleName() + " Add device link failed. " + e.getMessage());
         } catch (URISyntaxException e) {
             throw new Error.InvalidUri(e.getClass().getSimpleName()
@@ -390,7 +393,7 @@ public class DbusSignalImpl implements Signal {
             m.setContactName(getSingleRecipientIdentifier(number, m.getSelfNumber()), name);
         } catch (NotMasterDeviceException e) {
             throw new Error.Failure("This command doesn't work on linked devices.");
-        } catch (UnregisteredUserException e) {
+        } catch (IOException e) {
             throw new Error.Failure("Contact is not registered.");
         }
     }
@@ -584,8 +587,6 @@ public class DbusSignalImpl implements Signal {
     public void removePin() {
         try {
             m.setRegistrationLockPin(Optional.empty());
-        } catch (UnauthenticatedResponseException e) {
-            throw new Error.Failure("Remove pin failed with unauthenticated response: " + e.getMessage());
         } catch (IOException e) {
             throw new Error.Failure("Remove pin error: " + e.getMessage());
         }
@@ -595,8 +596,6 @@ public class DbusSignalImpl implements Signal {
     public void setPin(String registrationLockPin) {
         try {
             m.setRegistrationLockPin(Optional.of(registrationLockPin));
-        } catch (UnauthenticatedResponseException e) {
-            throw new Error.Failure("Set pin error failed with unauthenticated response: " + e.getMessage());
         } catch (IOException e) {
             throw new Error.Failure("Set pin error: " + e.getMessage());
         }
@@ -639,7 +638,7 @@ public class DbusSignalImpl implements Signal {
                 Profile profile = null;
                 try {
                     profile = m.getRecipientProfile(RecipientIdentifier.Single.fromAddress(address));
-                } catch (UnregisteredUserException ignored) {
+                } catch (IOException ignored) {
                 }
                 if (profile != null && profile.getDisplayName().equals(name)) {
                     numbers.add(number);
@@ -670,7 +669,7 @@ public class DbusSignalImpl implements Signal {
             }
             final var result = m.joinGroup(linkUrl);
             return result.first().serialize();
-        } catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupLinkNotActiveException e) {
+        } catch (GroupInviteLinkUrl.InvalidGroupLinkException | InactiveGroupLinkException 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());
@@ -725,7 +724,7 @@ public class DbusSignalImpl implements Signal {
 
         final var message = timestamp + "\nFailed to send message:\n" + error + '\n';
 
-        if (result.getIdentityFailure() != null) {
+        if (result.isIdentityFailure()) {
             throw new Error.UntrustedIdentity(message);
         } else {
             throw new Error.Failure(message);
@@ -842,6 +841,7 @@ public class DbusSignalImpl implements Signal {
             final var deviceObjectPath = object.getObjectPath();
             try {
                 connection.exportObject(object);
+                logger.debug("Exported dbus object: " + deviceObjectPath);
             } catch (DBusException e) {
                 e.printStackTrace();
             }
@@ -878,6 +878,7 @@ public class DbusSignalImpl implements Signal {
             final var object = new DbusSignalGroupImpl(g.groupId());
             try {
                 connection.exportObject(object);
+                logger.debug("Exported dbus object: " + object.getObjectPath());
             } catch (DBusException e) {
                 e.printStackTrace();
             }
@@ -892,6 +893,26 @@ public class DbusSignalImpl implements Signal {
         this.groups.clear();
     }
 
+    private static String getConfigurationObjectPath(String basePath) {
+        return basePath + "/Configuration";
+    }
+
+    private void updateConfiguration() {
+        try {
+            unExportConfiguration();
+            final var object = new DbusSignalConfigurationImpl();
+            connection.exportObject(object);
+            logger.debug("Exported dbus object: " + objectPath + "/Configuration");
+        } catch (DBusException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void unExportConfiguration() {
+        final var objectPath = getConfigurationObjectPath(this.objectPath);
+        connection.unExportObject(objectPath);
+    }
+
     public class DbusSignalDeviceImpl extends DbusProperties implements Signal.Device {
 
         private final org.asamk.signal.manager.api.Device device;
@@ -934,6 +955,78 @@ public class DbusSignalImpl implements Signal {
         }
     }
 
+    public class DbusSignalConfigurationImpl extends DbusProperties implements Signal.Configuration {
+
+        public DbusSignalConfigurationImpl(
+        ) {
+            super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Configuration",
+                    List.of(new DbusProperty<>("ReadReceipts", this::getReadReceipts, this::setReadReceipts),
+                            new DbusProperty<>("UnidentifiedDeliveryIndicators",
+                                    this::getUnidentifiedDeliveryIndicators,
+                                    this::setUnidentifiedDeliveryIndicators),
+                            new DbusProperty<>("TypingIndicators",
+                                    this::getTypingIndicators,
+                                    this::setTypingIndicators),
+                            new DbusProperty<>("LinkPreviews", this::getLinkPreviews, this::setLinkPreviews))));
+
+        }
+
+        @Override
+        public String getObjectPath() {
+            return getConfigurationObjectPath(objectPath);
+        }
+
+        public void setReadReceipts(Boolean readReceipts) {
+            setConfiguration(readReceipts, null, null, null);
+        }
+
+        public void setUnidentifiedDeliveryIndicators(Boolean unidentifiedDeliveryIndicators) {
+            setConfiguration(null, unidentifiedDeliveryIndicators, null, null);
+        }
+
+        public void setTypingIndicators(Boolean typingIndicators) {
+            setConfiguration(null, null, typingIndicators, null);
+        }
+
+        public void setLinkPreviews(Boolean linkPreviews) {
+            setConfiguration(null, null, null, linkPreviews);
+        }
+
+        private void setConfiguration(
+                Boolean readReceipts,
+                Boolean unidentifiedDeliveryIndicators,
+                Boolean typingIndicators,
+                Boolean linkPreviews
+        ) {
+            try {
+                m.updateConfiguration(new org.asamk.signal.manager.api.Configuration(Optional.ofNullable(readReceipts),
+                        Optional.ofNullable(unidentifiedDeliveryIndicators),
+                        Optional.ofNullable(typingIndicators),
+                        Optional.ofNullable(linkPreviews)));
+            } catch (IOException e) {
+                throw new Error.Failure("UpdateAccount error: " + e.getMessage());
+            } catch (NotMasterDeviceException e) {
+                throw new Error.Failure("This command doesn't work on linked devices.");
+            }
+        }
+
+        private boolean getReadReceipts() {
+            return m.getConfiguration().readReceipts().orElse(false);
+        }
+
+        private boolean getUnidentifiedDeliveryIndicators() {
+            return m.getConfiguration().unidentifiedDeliveryIndicators().orElse(false);
+        }
+
+        private boolean getTypingIndicators() {
+            return m.getConfiguration().typingIndicators().orElse(false);
+        }
+
+        private boolean getLinkPreviews() {
+            return m.getConfiguration().linkPreviews().orElse(false);
+        }
+    }
+
     public class DbusSignalGroupImpl extends DbusProperties implements Signal.Group {
 
         private final GroupId groupId;