]> nmode's Git Repositories - signal-cli/commitdiff
Improve robustness in receiving messages
authorAsamK <asamk@gmx.de>
Sat, 16 Sep 2023 10:00:55 +0000 (12:00 +0200)
committerAsamK <asamk@gmx.de>
Sat, 16 Sep 2023 10:00:55 +0000 (12:00 +0200)
lib/src/main/java/org/asamk/signal/manager/helper/IncomingMessageHandler.java
lib/src/main/java/org/asamk/signal/manager/helper/ReceiveHelper.java

index 9ffee5c8c2164e3c14213d4623fe907c40450522..cad112b52002fbf3d62a39d67e84d94f8f45aad1 100644 (file)
@@ -137,13 +137,17 @@ public final class IncomingMessageHandler {
             final Manager.ReceiveMessageHandler handler
     ) {
         final var actions = new ArrayList<HandleAction>();
-        if (envelope.hasSourceServiceId()) {
-            // Store uuid if we don't have it already
-            // uuid in envelope is sent by server
-            account.getRecipientTrustedResolver().resolveRecipientTrusted(envelope.getSourceAddress());
-        }
         SignalServiceContent content = null;
         Exception exception = null;
+        try {
+            if (envelope.hasSourceServiceId()) {
+                // Store uuid if we don't have it already
+                // uuid in envelope is sent by server
+                account.getRecipientTrustedResolver().resolveRecipientTrusted(envelope.getSourceAddress());
+            }
+        } catch (Exception e) {
+            exception = e;
+        }
         if (!envelope.isReceipt()) {
             try {
                 final var cipherResult = dependencies.getCipher()
index 2f7834673a01ab732decc3cffff99978c9eee438..0c003682b93b107b238b49e982333b5fc1cd5c6d 100644 (file)
@@ -205,41 +205,49 @@ public class ReceiveHelper {
                 backOffCounter = 0;
                 if (returnOnTimeout) return;
                 continue;
+            } catch (Exception e) {
+                logger.error("Unknown error when receiving messages", e);
+                continue;
             }
 
-            final var result = context.getIncomingMessageHandler().handleEnvelope(envelope, receiveConfig, handler);
-            for (final var h : result.first()) {
-                final var existingAction = queuedActions.get(h);
-                if (existingAction == null) {
-                    queuedActions.put(h, h);
-                } else {
-                    existingAction.mergeOther(h);
+            try {
+                final var result = context.getIncomingMessageHandler().handleEnvelope(envelope, receiveConfig, handler);
+                for (final var h : result.first()) {
+                    final var existingAction = queuedActions.get(h);
+                    if (existingAction == null) {
+                        queuedActions.put(h, h);
+                    } else {
+                        existingAction.mergeOther(h);
+                    }
                 }
-            }
-            final var exception = result.second();
+                final var exception = result.second();
 
-            if (hasCaughtUpWithOldMessages) {
-                handleQueuedActions(queuedActions.keySet());
-                queuedActions.clear();
-            }
-            if (cachedMessage[0] != null) {
-                if (exception instanceof UntrustedIdentityException) {
-                    logger.debug("Keeping message with untrusted identity in message cache");
-                    final var address = ((UntrustedIdentityException) exception).getSender();
-                    if (!envelope.hasSourceServiceId() && address.uuid().isPresent()) {
-                        final var recipientId = account.getRecipientResolver()
-                                .resolveRecipient(ACI.from(address.uuid().get()));
-                        try {
-                            cachedMessage[0] = account.getMessageCache().replaceSender(cachedMessage[0], recipientId);
-                        } catch (IOException ioException) {
-                            logger.warn("Failed to move cached message to recipient folder: {}",
-                                    ioException.getMessage(),
-                                    ioException);
+                if (hasCaughtUpWithOldMessages) {
+                    handleQueuedActions(queuedActions.keySet());
+                    queuedActions.clear();
+                }
+                if (cachedMessage[0] != null) {
+                    if (exception instanceof UntrustedIdentityException) {
+                        logger.debug("Keeping message with untrusted identity in message cache");
+                        final var address = ((UntrustedIdentityException) exception).getSender();
+                        if (!envelope.hasSourceServiceId() && address.uuid().isPresent()) {
+                            final var recipientId = account.getRecipientResolver()
+                                    .resolveRecipient(ACI.from(address.uuid().get()));
+                            try {
+                                cachedMessage[0] = account.getMessageCache()
+                                        .replaceSender(cachedMessage[0], recipientId);
+                            } catch (IOException ioException) {
+                                logger.warn("Failed to move cached message to recipient folder: {}",
+                                        ioException.getMessage(),
+                                        ioException);
+                            }
                         }
+                    } else {
+                        cachedMessage[0].delete();
                     }
-                } else {
-                    cachedMessage[0].delete();
                 }
+            } catch (Exception e) {
+                logger.error("Unknown error when handling messages", e);
             }
         }
     }