]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java
Use exit 4 when sending a single recipient message fails due to untrusted identity key
[signal-cli] / src / main / java / org / asamk / signal / dbus / DbusSignalImpl.java
index e662ab2ac9d4237d4a90d16f04902098d7f28575..a2f6cace9933b95685dc0bf6de5c9296798a695d 100644 (file)
@@ -2,10 +2,11 @@ package org.asamk.signal.dbus;
 
 import org.asamk.Signal;
 import org.asamk.signal.manager.AttachmentInvalidException;
-import org.asamk.signal.manager.GroupNotFoundException;
 import org.asamk.signal.manager.Manager;
-import org.asamk.signal.manager.NotAGroupMemberException;
-import org.asamk.signal.storage.groups.GroupInfo;
+import org.asamk.signal.manager.groups.GroupId;
+import org.asamk.signal.manager.groups.GroupNotFoundException;
+import org.asamk.signal.manager.groups.NotAGroupMemberException;
+import org.asamk.signal.manager.storage.groups.GroupInfo;
 import org.asamk.signal.util.ErrorUtils;
 import org.freedesktop.dbus.exceptions.DBusExecutionException;
 import org.whispersystems.libsignal.util.Pair;
@@ -13,9 +14,9 @@ import org.whispersystems.signalservice.api.messages.SendMessageResult;
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 import org.whispersystems.signalservice.api.util.InvalidNumberException;
 
+import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -44,10 +45,30 @@ public class DbusSignalImpl implements Signal {
         return sendMessage(message, attachments, recipients);
     }
 
+    private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException {
+        String error = ErrorUtils.getErrorMessageFromSendMessageResult(result);
+
+        if (error == null) {
+            return;
+        }
+
+        final String message = timestamp + "\nFailed to send message:\n" + error + '\n';
+
+        if (result.getIdentityFailure() != null) {
+            throw new Error.UntrustedIdentity(message);
+        } else {
+            throw new Error.Failure(message);
+        }
+    }
+
     private static void checkSendMessageResults(
-            long timestamp,
-            List<SendMessageResult> results
+            long timestamp, List<SendMessageResult> results
     ) throws DBusExecutionException {
+        if (results.size() == 1) {
+            checkSendMessageResult(timestamp, results.get(0));
+            return;
+        }
+
         List<String> errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results);
         if (errors.size() == 0) {
             return;
@@ -78,6 +99,21 @@ public class DbusSignalImpl implements Signal {
         }
     }
 
+    @Override
+    public long sendNoteToSelfMessage(
+            final String message, final List<String> attachments
+    ) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
+        try {
+            final Pair<Long, SendMessageResult> results = m.sendSelfMessage(message, attachments);
+            checkSendMessageResult(results.first(), results.second());
+            return results.first();
+        } catch (AttachmentInvalidException e) {
+            throw new Error.AttachmentInvalid(e.getMessage());
+        } catch (IOException e) {
+            throw new Error.Failure(e.getMessage());
+        }
+    }
+
     @Override
     public void sendEndSessionMessage(final List<String> recipients) {
         try {
@@ -93,7 +129,9 @@ public class DbusSignalImpl implements Signal {
     @Override
     public long sendGroupMessage(final String message, final List<String> attachments, final byte[] groupId) {
         try {
-            Pair<Long, List<SendMessageResult>> results = m.sendGroupMessage(message, attachments, groupId);
+            Pair<Long, List<SendMessageResult>> results = m.sendGroupMessage(message,
+                    attachments,
+                    GroupId.unknownVersion(groupId));
             checkSendMessageResults(results.first(), results.second());
             return results.first();
         } catch (IOException e) {
@@ -135,7 +173,7 @@ public class DbusSignalImpl implements Signal {
     @Override
     public void setGroupBlocked(final byte[] groupId, final boolean blocked) {
         try {
-            m.setGroupBlocked(groupId, blocked);
+            m.setGroupBlocked(GroupId.unknownVersion(groupId), blocked);
         } catch (GroupNotFoundException e) {
             throw new Error.GroupNotFound(e.getMessage());
         }
@@ -146,14 +184,14 @@ public class DbusSignalImpl implements Signal {
         List<GroupInfo> groups = m.getGroups();
         List<byte[]> ids = new ArrayList<>(groups.size());
         for (GroupInfo group : groups) {
-            ids.add(group.groupId);
+            ids.add(group.getGroupId().serialize());
         }
         return ids;
     }
 
     @Override
     public String getGroupName(final byte[] groupId) {
-        GroupInfo group = m.getGroup(groupId);
+        GroupInfo group = m.getGroup(GroupId.unknownVersion(groupId));
         if (group == null) {
             return "";
         } else {
@@ -163,9 +201,9 @@ public class DbusSignalImpl implements Signal {
 
     @Override
     public List<String> getGroupMembers(final byte[] groupId) {
-        GroupInfo group = m.getGroup(groupId);
+        GroupInfo group = m.getGroup(GroupId.unknownVersion(groupId));
         if (group == null) {
-            return Collections.emptyList();
+            return List.of();
         } else {
             return group.getMembers()
                     .stream()
@@ -190,9 +228,11 @@ public class DbusSignalImpl implements Signal {
             if (avatar.isEmpty()) {
                 avatar = null;
             }
-            final Pair<byte[], List<SendMessageResult>> results = m.updateGroup(groupId, name, members, avatar);
+            final Pair<GroupId, List<SendMessageResult>> results = m.updateGroup(groupId == null
+                    ? null
+                    : GroupId.unknownVersion(groupId), name, members, avatar == null ? null : new File(avatar));
             checkSendMessageResults(0, results.second());
-            return results.first();
+            return results.first().serialize();
         } catch (IOException e) {
             throw new Error.Failure(e.getMessage());
         } catch (GroupNotFoundException | NotAGroupMemberException e) {