From 7191e4371b7b9d355b5a5d987f33673099d0f9d1 Mon Sep 17 00:00:00 2001 From: david-harley Date: Thu, 18 Feb 2021 09:43:11 +1030 Subject: [PATCH] Removed old comments, simplified if-statement and added a 'leadingSpaces' field to the print attachments/mentions functions --- .../org/asamk/signal/manager/Manager.java | 19 +++---- .../asamk/signal/ReceiveMessageHandler.java | 53 ++++++++++++------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/lib/src/main/java/org/asamk/signal/manager/Manager.java b/lib/src/main/java/org/asamk/signal/manager/Manager.java index 885f683c..80ff4e71 100644 --- a/lib/src/main/java/org/asamk/signal/manager/Manager.java +++ b/lib/src/main/java/org/asamk/signal/manager/Manager.java @@ -1554,16 +1554,17 @@ public class Manager implements Closeable { } } } - if (message.getAttachments().isPresent() && !ignoreAttachments) { - for (SignalServiceAttachment attachment : message.getAttachments().get()) { - downloadAttachment(attachment); + if (!ignoreAttachments) { + if (message.getAttachments().isPresent()) { + for (SignalServiceAttachment attachment : message.getAttachments().get()) { + downloadAttachment(attachment); + } } - } - if (message.getSharedContacts().isPresent() && !ignoreAttachments) { - for (SharedContact contact : message.getSharedContacts().get()) { - if (contact.getAvatar().isPresent()) { - // TODO probably should save to contacts instead - downloadAttachment(contact.getAvatar().get().getAttachment()); + if (message.getSharedContacts().isPresent()) { + for (SharedContact contact : message.getSharedContacts().get()) { + if (contact.getAvatar().isPresent()) { + downloadAttachment(contact.getAvatar().get().getAttachment()); + } } } } diff --git a/src/main/java/org/asamk/signal/ReceiveMessageHandler.java b/src/main/java/org/asamk/signal/ReceiveMessageHandler.java index a6d2f217..996100f6 100644 --- a/src/main/java/org/asamk/signal/ReceiveMessageHandler.java +++ b/src/main/java/org/asamk/signal/ReceiveMessageHandler.java @@ -117,11 +117,11 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler { } else { System.out.println("Received sync contacts"); } - printAttachment(contactsMessage.getContactsStream()); + printAttachment(contactsMessage.getContactsStream(), 0); } if (syncMessage.getGroups().isPresent()) { System.out.println("Received sync groups"); - printAttachment(syncMessage.getGroups().get()); + printAttachment(syncMessage.getGroups().get(), 0); } if (syncMessage.getRead().isPresent()) { System.out.println("Received sync read messages list"); @@ -393,7 +393,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler { } if (groupInfo.getAvatar().isPresent()) { System.out.println(" Avatar:"); - printAttachment(groupInfo.getAvatar().get()); + printAttachment(groupInfo.getAvatar().get(), 2); } } else if (groupContext.getGroupV2().isPresent()) { final SignalServiceGroupV2 groupInfo = groupContext.getGroupV2().get(); @@ -421,7 +421,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler { System.out.println(" - Title: " + preview.getTitle()); System.out.println(" - Url: " + preview.getUrl()); if (preview.getImage().isPresent()) { - printAttachment(preview.getImage().get()); + printAttachment(preview.getImage().get(), 1); } } } @@ -453,7 +453,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler { if (contact.getAvatar().isPresent()) { SharedContact.Avatar avatar = contact.getAvatar().get(); System.out.println(" - Avatar:"); - printAttachment(avatar.getAttachment()); + printAttachment(avatar.getAttachment(), 3); if (avatar.isProfile()) { System.out.println(" - Is profile"); } else { @@ -575,7 +575,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler { if (quote.getMentions() != null && quote.getMentions().size() > 0) { System.out.println(" Mentions: "); for (SignalServiceDataMessage.Mention mention : quote.getMentions()) { - printMention(mention, m); + printMention(mention, m, 1); } } if (quote.getAttachments().size() > 0) { @@ -585,7 +585,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler { System.out.println(" Type: " + attachment.getContentType()); System.out.println(" Thumbnail:"); if (attachment.getThumbnail() != null) { - printAttachment(attachment.getThumbnail()); + printAttachment(attachment.getThumbnail(), 3); } } } @@ -598,45 +598,60 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler { if (message.getMentions().isPresent()) { System.out.println("Mentions: "); for (SignalServiceDataMessage.Mention mention : message.getMentions().get()) { - printMention(mention, m); + printMention(mention, m, 0); } } if (message.getAttachments().isPresent()) { System.out.println("Attachments: "); for (SignalServiceAttachment attachment : message.getAttachments().get()) { - printAttachment(attachment); + printAttachment(attachment, 0); } } } - private void printMention(SignalServiceDataMessage.Mention mention, Manager m) { - System.out.println("- " + m.resolveSignalServiceAddress(new SignalServiceAddress(mention.getUuid(), null)) + /** + * Prints the Signal mention information + * + * @param mention is the Signal mention to print + * @param m is the Manager. used to resolve UUIDs into phone numbers if possible + * @param leadingSpaces is the number of spaces you want the message to be indented by + */ + private void printMention(SignalServiceDataMessage.Mention mention, Manager m, int leadingSpaces) { + String spaces = " ".repeat(leadingSpaces); + System.out.println(spaces + "- " + m.resolveSignalServiceAddress(new SignalServiceAddress(mention.getUuid(), null)) .getLegacyIdentifier() + ": " + mention.getStart() + " (length: " + mention.getLength() + ")"); } - private void printAttachment(SignalServiceAttachment attachment) { - System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + ( + /** + * Prints the Signal attachment information + * + * @param attachment is the Signal attachment to print + * @param leadingSpaces is the number of spaces you want the message to be indented by + */ + private void printAttachment(SignalServiceAttachment attachment, int leadingSpaces) { + String spaces = " ".repeat(leadingSpaces); + System.out.println(spaces + "- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + ( attachment.isStream() ? "Stream" : "" ) + ")"); if (attachment.isPointer()) { final SignalServiceAttachmentPointer pointer = attachment.asPointer(); - System.out.println(" Id: " + pointer.getRemoteId() + " Key length: " + pointer.getKey().length); - System.out.println(" Filename: " + ( + System.out.println(spaces + " Id: " + pointer.getRemoteId() + " Key length: " + pointer.getKey().length); + System.out.println(spaces + " Filename: " + ( pointer.getFileName().isPresent() ? pointer.getFileName().get() : "-" )); - System.out.println(" Size: " + ( + System.out.println(spaces + " Size: " + ( pointer.getSize().isPresent() ? pointer.getSize().get() + " bytes" : "" ) + ( pointer.getPreview().isPresent() ? " (Preview is available: " + pointer.getPreview().get().length + " bytes)" : "" )); - System.out.println(" Voice note: " + (pointer.getVoiceNote() ? "yes" : "no")); - System.out.println(" Dimensions: " + pointer.getWidth() + "x" + pointer.getHeight()); + System.out.println(spaces + " Voice note: " + (pointer.getVoiceNote() ? "yes" : "no")); + System.out.println(spaces + " Dimensions: " + pointer.getWidth() + "x" + pointer.getHeight()); File file = m.getAttachmentFile(pointer.getRemoteId()); if (file.exists()) { - System.out.println(" Stored plaintext in: " + file); + System.out.println(spaces + " Stored plaintext in: " + file); } } }