]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/Manager.java
Only handle jsonRpc requests, after receive thread has caught up with old messages
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / Manager.java
index 9e38853bfa4cbc97614c390bc4df1f30124386d1..c4c77b3453c4a2764a9ecc03e17da4afd1b674ec 100644 (file)
@@ -58,7 +58,6 @@ import org.asamk.signal.manager.storage.stickers.StickerPackId;
 import org.asamk.signal.manager.util.KeyUtils;
 import org.asamk.signal.manager.util.StickerUtils;
 import org.asamk.signal.manager.util.Utils;
-import org.signal.libsignal.metadata.ProtocolUntrustedIdentityException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.whispersystems.libsignal.IdentityKey;
@@ -142,6 +141,7 @@ public class Manager implements Closeable {
     private final IncomingMessageHandler incomingMessageHandler;
 
     private final Context context;
+    private boolean hasCaughtUpWithOldMessages = false;
 
     Manager(
             SignalAccount account,
@@ -165,8 +165,7 @@ public class Manager implements Closeable {
                 return LEGACY_LOCK::unlock;
             }
         };
-        this.dependencies = new SignalDependencies(account.getSelfAddress(),
-                serviceEnvironmentConfig,
+        this.dependencies = new SignalDependencies(serviceEnvironmentConfig,
                 userAgent,
                 credentialsProvider,
                 account.getSignalProtocolStore(),
@@ -187,8 +186,6 @@ public class Manager implements Closeable {
                 avatarStore,
                 account.getProfileStore()::getProfileKey,
                 unidentifiedAccessHelper::getAccessFor,
-                dependencies::getProfileService,
-                dependencies::getMessageReceiver,
                 this::resolveSignalServiceAddress);
         final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper::getRecipientProfileKeyCredential,
                 this::getRecipientProfile,
@@ -221,8 +218,7 @@ public class Manager implements Closeable {
                 this::resolveSignalServiceAddress);
 
         this.context = new Context(account,
-                dependencies.getAccountManager(),
-                dependencies.getMessageReceiver(),
+                dependencies,
                 stickerPackStore,
                 sendHelper,
                 groupHelper,
@@ -386,6 +382,13 @@ public class Manager implements Closeable {
     }
 
     public void deleteAccount() throws IOException {
+        try {
+            pinHelper.removeRegistrationLockPin();
+        } catch (UnauthenticatedResponseException e) {
+            logger.warn("Failed to remove registration lock pin");
+        }
+        account.setRegistrationLockPin(null, null);
+
         dependencies.getAccountManager().deleteAccount();
 
         account.setRegistered(false);
@@ -818,37 +821,33 @@ public class Manager implements Closeable {
     ) {
         var envelope = cachedMessage.loadEnvelope();
         if (envelope == null) {
+            cachedMessage.delete();
             return null;
         }
-        SignalServiceContent content = null;
-        List<HandleAction> actions = null;
-        if (!envelope.isReceipt()) {
-            try {
-                content = dependencies.getCipher().decrypt(envelope);
-            } catch (ProtocolUntrustedIdentityException e) {
-                if (System.currentTimeMillis() - envelope.getServerDeliveredTimestamp() > 1000L * 60 * 60 * 24 * 30) {
-                    // Envelope is more than a month old, cleaning up.
-                    cachedMessage.delete();
-                    return null;
-                }
-                if (!envelope.hasSourceUuid()) {
-                    final var identifier = e.getSender();
-                    final var recipientId = account.getRecipientStore().resolveRecipient(identifier);
-                    try {
-                        account.getMessageCache().replaceSender(cachedMessage, recipientId);
-                    } catch (IOException ioException) {
-                        logger.warn("Failed to move cached message to recipient folder: {}", ioException.getMessage());
-                    }
-                }
-                return null;
-            } catch (Exception er) {
-                // All other errors are not recoverable, so delete the cached message
+
+        final var result = incomingMessageHandler.handleRetryEnvelope(envelope, ignoreAttachments, handler);
+        final var actions = result.first();
+        final var exception = result.second();
+
+        if (exception instanceof UntrustedIdentityException) {
+            if (System.currentTimeMillis() - envelope.getServerDeliveredTimestamp() > 1000L * 60 * 60 * 24 * 30) {
+                // Envelope is more than a month old, cleaning up.
                 cachedMessage.delete();
                 return null;
             }
-            actions = incomingMessageHandler.handleMessage(envelope, content, ignoreAttachments);
+            if (!envelope.hasSourceUuid()) {
+                final var identifier = ((UntrustedIdentityException) exception).getSender();
+                final var recipientId = account.getRecipientStore().resolveRecipient(identifier);
+                try {
+                    account.getMessageCache().replaceSender(cachedMessage, recipientId);
+                } catch (IOException ioException) {
+                    logger.warn("Failed to move cached message to recipient folder: {}", ioException.getMessage());
+                }
+            }
+            return null;
         }
-        handler.handleMessage(envelope, content, null);
+
+        // If successful and for all other errors that are not recoverable, delete the cached message
         cachedMessage.delete();
         return actions;
     }
@@ -867,7 +866,7 @@ public class Manager implements Closeable {
         final var signalWebSocket = dependencies.getSignalWebSocket();
         signalWebSocket.connect();
 
-        var hasCaughtUpWithOldMessages = false;
+        hasCaughtUpWithOldMessages = false;
 
         while (!Thread.interrupted()) {
             SignalServiceEnvelope envelope;
@@ -887,11 +886,14 @@ public class Manager implements Closeable {
                     envelope = result.get();
                 } else {
                     // Received indicator that server queue is empty
-                    hasCaughtUpWithOldMessages = true;
-
                     handleQueuedActions(queuedActions);
                     queuedActions.clear();
 
+                    hasCaughtUpWithOldMessages = true;
+                    synchronized (this) {
+                        this.notifyAll();
+                    }
+
                     // Continue to wait another timeout for new messages
                     continue;
                 }
@@ -938,17 +940,27 @@ public class Manager implements Closeable {
         handleQueuedActions(queuedActions);
     }
 
+    public boolean hasCaughtUpWithOldMessages() {
+        return hasCaughtUpWithOldMessages;
+    }
+
     private void handleQueuedActions(final Collection<HandleAction> queuedActions) {
+        var interrupted = false;
         for (var action : queuedActions) {
             try {
                 action.execute(context);
             } catch (Throwable e) {
-                if (e instanceof AssertionError && e.getCause() instanceof InterruptedException) {
-                    Thread.currentThread().interrupt();
+                if ((e instanceof AssertionError || e instanceof RuntimeException)
+                        && e.getCause() instanceof InterruptedException) {
+                    interrupted = true;
+                    continue;
                 }
                 logger.warn("Message action failed.", e);
             }
         }
+        if (interrupted) {
+            Thread.currentThread().interrupt();
+        }
     }
 
     public boolean isContactBlocked(final RecipientIdentifier.Single recipient) {
@@ -1147,10 +1159,6 @@ public class Manager implements Closeable {
     }
 
     public SignalServiceAddress resolveSignalServiceAddress(SignalServiceAddress address) {
-        if (address.matches(account.getSelfAddress())) {
-            return account.getSelfAddress();
-        }
-
         return resolveSignalServiceAddress(resolveRecipient(address));
     }