- public int handleCommand(final Namespace ns, final Signal signal, DBusConnection dbusconnection) {
- if (dbusconnection != null) {
- try {
- dbusconnection.addSigHandler(Signal.MessageReceived.class, new DBusSigHandler<Signal.MessageReceived>() {
- @Override
- public void handle(Signal.MessageReceived s) {
- System.out.print(String.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
- s.getSender(), DateUtils.formatTimestamp(s.getTimestamp()), s.getMessage()));
- if (s.getGroupId().length > 0) {
- System.out.println("Group info:");
- System.out.println(" Id: " + Base64.encodeBytes(s.getGroupId()));
- }
- if (s.getAttachments().size() > 0) {
- System.out.println("Attachments: ");
- for (String attachment : s.getAttachments()) {
- System.out.println("- Stored plaintext in: " + attachment);
- }
+ @Override
+ public Set<OutputType> getSupportedOutputTypes() {
+ return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON);
+ }
+
+ public void handleCommand(
+ final Namespace ns, final Signal signal, DBusConnection dbusconnection
+ ) throws CommandException {
+ var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
+
+ // TODO delete later when "json" variable is removed
+ if (ns.getBoolean("json")) {
+ logger.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
+ }
+
+ try {
+ if (inJson) {
+ final var jsonWriter = new JsonWriter(System.out);
+
+ dbusconnection.addSigHandler(Signal.MessageReceived.class, signal, messageReceived -> {
+ var envelope = new JsonMessageEnvelope(messageReceived);
+ final var object = Map.of("envelope", envelope);
+ jsonWriter.write(object);
+ });
+
+ dbusconnection.addSigHandler(Signal.ReceiptReceived.class, signal, receiptReceived -> {
+ var envelope = new JsonMessageEnvelope(receiptReceived);
+ final var object = Map.of("envelope", envelope);
+ jsonWriter.write(object);
+ });
+
+ dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, signal, syncReceived -> {
+ var envelope = new JsonMessageEnvelope(syncReceived);
+ final var object = Map.of("envelope", envelope);
+ jsonWriter.write(object);
+ });
+ } else {
+ final var writer = new PlainTextWriterImpl(System.out);
+
+ dbusconnection.addSigHandler(Signal.MessageReceived.class, signal, messageReceived -> {
+ 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);