]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/commands/ReceiveCommand.java
Adapt behavior of receive command as dbus client to match normal mode
[signal-cli] / src / main / java / org / asamk / signal / commands / ReceiveCommand.java
index 82bd5d8f274f5c2f21d9173d02dc08ada8ced7c7..6b2e497e10b3fd8953bf1960275de1fc497f2c7a 100644 (file)
@@ -126,13 +126,20 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
             }
         } catch (DBusException e) {
             logger.error("Dbus client failed", e);
-            throw new UnexpectedErrorException("Dbus client failed");
+            throw new UnexpectedErrorException("Dbus client failed", e);
         }
+
+        double timeout = ns.getDouble("timeout");
+        long timeoutMilliseconds = timeout < 0 ? 10000 : (long) (timeout * 1000);
+
         while (true) {
             try {
-                Thread.sleep(10000);
+                Thread.sleep(timeoutMilliseconds);
             } catch (InterruptedException ignored) {
-                return;
+                break;
+            }
+            if (timeout >= 0) {
+                break;
             }
         }
     }
@@ -142,23 +149,18 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
             final Namespace ns, final Manager m, final OutputWriter outputWriter
     ) throws CommandException {
         double timeout = ns.getDouble("timeout");
-        var returnOnTimeout = true;
-        if (timeout < 0) {
-            returnOnTimeout = false;
-            timeout = 3600;
-        }
-        boolean ignoreAttachments = ns.getBoolean("ignore-attachments");
+        boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
+        m.setIgnoreAttachments(ignoreAttachments);
         try {
             final var handler = outputWriter instanceof JsonWriter ? new JsonReceiveMessageHandler(m,
                     (JsonWriter) outputWriter) : new ReceiveMessageHandler(m, (PlainTextWriter) outputWriter);
-            m.receiveMessages((long) (timeout * 1000),
-                    TimeUnit.MILLISECONDS,
-                    returnOnTimeout,
-                    ignoreAttachments,
-                    handler);
+            if (timeout < 0) {
+                m.receiveMessages(handler);
+            } else {
+                m.receiveMessages((long) (timeout * 1000), TimeUnit.MILLISECONDS, handler);
+            }
         } catch (IOException e) {
-            throw new IOErrorException("Error while receiving messages: " + e.getMessage());
-        } catch (InterruptedException ignored) {
+            throw new IOErrorException("Error while receiving messages: " + e.getMessage(), e);
         }
     }
 }