X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/af292d8f0ea897ea13470489d51c40acca50fc3e..cd29144e81701698092f3334bee0c99c0f77f202:/src/main/java/org/asamk/signal/commands/ReceiveCommand.java diff --git a/src/main/java/org/asamk/signal/commands/ReceiveCommand.java b/src/main/java/org/asamk/signal/commands/ReceiveCommand.java index 82bd5d8f..fe062ffc 100644 --- a/src/main/java/org/asamk/signal/commands/ReceiveCommand.java +++ b/src/main/java/org/asamk/signal/commands/ReceiveCommand.java @@ -58,23 +58,22 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand { final Namespace ns, final Signal signal, DBusConnection dbusconnection, final OutputWriter outputWriter ) throws CommandException { try { - if (outputWriter instanceof JsonWriter) { - final var jsonWriter = (JsonWriter) outputWriter; + if (outputWriter instanceof JsonWriter jsonWriter) { dbusconnection.addSigHandler(Signal.MessageReceived.class, signal, messageReceived -> { - var envelope = new JsonMessageEnvelope(messageReceived); + var envelope = JsonMessageEnvelope.from(messageReceived); final var object = Map.of("envelope", envelope); jsonWriter.write(object); }); dbusconnection.addSigHandler(Signal.ReceiptReceived.class, signal, receiptReceived -> { - var envelope = new JsonMessageEnvelope(receiptReceived); + var envelope = JsonMessageEnvelope.from(receiptReceived); final var object = Map.of("envelope", envelope); jsonWriter.write(object); }); dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, signal, syncReceived -> { - var envelope = new JsonMessageEnvelope(syncReceived); + var envelope = JsonMessageEnvelope.from(syncReceived); final var object = Map.of("envelope", envelope); jsonWriter.write(object); }); @@ -126,13 +125,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 +148,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); } } }