- if (inJson) {
- final var jsonWriter = new JsonWriter(System.out);
-
- dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
- var envelope = new JsonMessageEnvelope(messageReceived);
- final var object = Map.of("envelope", envelope);
- try {
- jsonWriter.write(object);
- } catch (IOException e) {
- logger.error("Failed to write json object: {}", e.getMessage());
- }
- });
-
- dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> {
- var envelope = new JsonMessageEnvelope(receiptReceived);
- final var object = Map.of("envelope", envelope);
- try {
- jsonWriter.write(object);
- } catch (IOException e) {
- logger.error("Failed to write json object: {}", e.getMessage());
- }
- });
-
- dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
- var envelope = new JsonMessageEnvelope(syncReceived);
- final var object = Map.of("envelope", envelope);
- try {
- jsonWriter.write(object);
- } catch (IOException e) {
- logger.error("Failed to write json object: {}", e.getMessage());
- }
- });
- } else {
- final var writer = new PlainTextWriterImpl(System.out);
-
- dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
- try {
- writer.println("Envelope from: {}", messageReceived.getSender());
- writer.println("Timestamp: {}", DateUtils.formatTimestamp(messageReceived.getTimestamp()));
- writer.println("Body: {}", messageReceived.getMessage());
- if (messageReceived.getGroupId().length > 0) {
- writer.println("Group info:");
- writer.indentedWriter()
- .println("Id: {}",
- Base64.getEncoder().encodeToString(messageReceived.getGroupId()));
- }
- if (messageReceived.getAttachments().size() > 0) {
- writer.println("Attachments:");
- for (var attachment : messageReceived.getAttachments()) {
- writer.println("- Stored plaintext in: {}", attachment);
- }
- }
- writer.println();
- } catch (IOException e) {
- e.printStackTrace();
- }
- });
-
- dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> {
- try {
- writer.println("Receipt from: {}", receiptReceived.getSender());
- writer.println("Timestamp: {}", DateUtils.formatTimestamp(receiptReceived.getTimestamp()));
- } catch (IOException e) {
- e.printStackTrace();
- }
- });
-
- dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
- try {
- writer.println("Sync Envelope from: {} to: {}",
- syncReceived.getSource(),
- syncReceived.getDestination());
- writer.println("Timestamp: {}", DateUtils.formatTimestamp(syncReceived.getTimestamp()));
- writer.println("Body: {}", syncReceived.getMessage());
- if (syncReceived.getGroupId().length > 0) {
- writer.println("Group info:");
- writer.indentedWriter()
- .println("Id: {}", Base64.getEncoder().encodeToString(syncReceived.getGroupId()));
- }
- if (syncReceived.getAttachments().size() > 0) {
- writer.println("Attachments:");
- for (var attachment : syncReceived.getAttachments()) {
- writer.println("- Stored plaintext in: {}", attachment);
- }
- }
- writer.println();
- } catch (IOException e) {
- e.printStackTrace();
- }
- });
- }
- } catch (DBusException e) {
- e.printStackTrace();
- return 2;
- }
- while (true) {
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- return 0;
- }
+ final var handler = switch (outputWriter) {
+ case JsonWriter writer -> new JsonReceiveMessageHandler(m, writer);
+ case PlainTextWriter writer -> new ReceiveMessageHandler(m, writer);
+ };
+ final var duration = timeout < 0 ? null : Duration.ofMillis((long) (timeout * 1000));
+ final var maxMessages = maxMessagesRaw < 0 ? null : maxMessagesRaw;
+ Shutdown.registerShutdownListener(m::stopReceiveMessages);
+ m.receiveMessages(Optional.ofNullable(duration), Optional.ofNullable(maxMessages), handler);
+ } catch (IOException e) {
+ throw new IOErrorException("Error while receiving messages: " + e.getMessage(), e);
+ } catch (AlreadyReceivingException e) {
+ throw new UserErrorException("Receive command cannot be used if messages are already being received.", e);