Refactored ensure...

pull/189/head
M66B 4 years ago
parent 04f75e1eca
commit f0f14f66e4

@ -108,8 +108,8 @@ import biweekly.ICalendar;
public class MessageHelper { public class MessageHelper {
private boolean ensuredEnvelope = false; private boolean ensuredEnvelope = false;
private boolean ensuredEnvelopeAll = false; private boolean ensuredHeaders = false;
private boolean ensuredBody = false; private boolean ensuredStructure = false;
private MimeMessage imessage; private MimeMessage imessage;
private static File cacheDir = null; private static File cacheDir = null;
@ -823,7 +823,7 @@ public class MessageHelper {
} }
String getMessageID() throws MessagingException { String getMessageID() throws MessagingException {
ensureMessage(false, false); ensureEnvelope();
// Outlook outbox -> sent // Outlook outbox -> sent
String header = imessage.getHeader(HEADER_CORRELATION_ID, null); String header = imessage.getHeader(HEADER_CORRELATION_ID, null);
@ -833,7 +833,7 @@ public class MessageHelper {
} }
String[] getReferences() throws MessagingException { String[] getReferences() throws MessagingException {
ensureMessage(false); ensureHeaders();
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
String refs = imessage.getHeader("References", null); String refs = imessage.getHeader("References", null);
@ -895,7 +895,7 @@ public class MessageHelper {
} }
String getDeliveredTo() throws MessagingException { String getDeliveredTo() throws MessagingException {
ensureMessage(false); ensureHeaders();
String header = imessage.getHeader("Delivered-To", null); String header = imessage.getHeader("Delivered-To", null);
if (header == null) if (header == null)
@ -911,15 +911,13 @@ public class MessageHelper {
} }
String getInReplyTo() throws MessagingException { String getInReplyTo() throws MessagingException {
ensureMessage(false); ensureHeaders();
String header = imessage.getHeader("In-Reply-To", null); String header = imessage.getHeader("In-Reply-To", null);
return (header == null ? null : MimeUtility.unfold(header)); return (header == null ? null : MimeUtility.unfold(header));
} }
String getThreadId(Context context, long account, long uid) throws MessagingException { String getThreadId(Context context, long account, long uid) throws MessagingException {
ensureMessage(false);
if (imessage instanceof GmailMessage) { if (imessage instanceof GmailMessage) {
// https://developers.google.com/gmail/imap/imap-extensions#access_to_the_gmail_thread_id_x-gm-thrid // https://developers.google.com/gmail/imap/imap-extensions#access_to_the_gmail_thread_id_x-gm-thrid
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
@ -995,7 +993,7 @@ public class MessageHelper {
Integer getPriority() throws MessagingException { Integer getPriority() throws MessagingException {
Integer priority = null; Integer priority = null;
ensureMessage(false); ensureHeaders();
// https://tools.ietf.org/html/rfc2156 // https://tools.ietf.org/html/rfc2156
// https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxcmail/2bb19f1b-b35e-4966-b1cb-1afd044e83ab // https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxcmail/2bb19f1b-b35e-4966-b1cb-1afd044e83ab
@ -1068,20 +1066,20 @@ public class MessageHelper {
} }
boolean getReceiptRequested() throws MessagingException { boolean getReceiptRequested() throws MessagingException {
ensureMessage(false); ensureHeaders();
return (imessage.getHeader("Return-Receipt-To") != null || return (imessage.getHeader("Return-Receipt-To") != null ||
imessage.getHeader("Disposition-Notification-To") != null); imessage.getHeader("Disposition-Notification-To") != null);
} }
Address[] getReceiptTo() throws MessagingException { Address[] getReceiptTo() throws MessagingException {
ensureMessage(false); ensureHeaders();
return getAddressHeader("Disposition-Notification-To"); return getAddressHeader("Disposition-Notification-To");
} }
String[] getAuthentication() throws MessagingException { String[] getAuthentication() throws MessagingException {
ensureMessage(false); ensureHeaders();
String[] headers = imessage.getHeader("Authentication-Results"); String[] headers = imessage.getHeader("Authentication-Results");
if (headers == null) if (headers == null)
@ -1121,7 +1119,7 @@ public class MessageHelper {
} }
String getReceivedFromHost() throws MessagingException { String getReceivedFromHost() throws MessagingException {
ensureMessage(false); ensureHeaders();
String[] received = imessage.getHeader("Received"); String[] received = imessage.getHeader("Received");
if (received == null || received.length == 0) if (received == null || received.length == 0)
@ -1156,7 +1154,7 @@ public class MessageHelper {
} }
private Address[] getAddressHeader(String name) throws MessagingException { private Address[] getAddressHeader(String name) throws MessagingException {
ensureMessage(false); ensureHeaders();
String header = imessage.getHeader(name, ","); String header = imessage.getHeader(name, ",");
if (header == null) if (header == null)
@ -1217,7 +1215,7 @@ public class MessageHelper {
} }
Address[] getListPost() throws MessagingException { Address[] getListPost() throws MessagingException {
ensureMessage(false); ensureHeaders();
String list; String list;
try { try {
@ -1257,7 +1255,7 @@ public class MessageHelper {
} }
String getListUnsubscribe() throws MessagingException { String getListUnsubscribe() throws MessagingException {
ensureMessage(false); ensureHeaders();
String list; String list;
try { try {
@ -1305,7 +1303,7 @@ public class MessageHelper {
} }
String getAutocrypt() throws MessagingException { String getAutocrypt() throws MessagingException {
ensureMessage(false); ensureHeaders();
String autocrypt = imessage.getHeader("Autocrypt", null); String autocrypt = imessage.getHeader("Autocrypt", null);
if (autocrypt == null) if (autocrypt == null)
@ -1315,7 +1313,7 @@ public class MessageHelper {
} }
String getSubject() throws MessagingException { String getSubject() throws MessagingException {
ensureMessage(false); ensureHeaders();
String subject = imessage.getHeader("Subject", null); String subject = imessage.getHeader("Subject", null);
if (subject == null) if (subject == null)
@ -1333,14 +1331,14 @@ public class MessageHelper {
} }
Long getSize() throws MessagingException { Long getSize() throws MessagingException {
ensureMessage(false); ensureEnvelope();
long size = imessage.getSize(); long size = imessage.getSize();
return (size < 0 ? null : size); return (size < 0 ? null : size);
} }
Long getReceived() throws MessagingException { Long getReceived() throws MessagingException {
ensureMessage(false); ensureEnvelope();
Date received = imessage.getReceivedDate(); Date received = imessage.getReceivedDate();
if (received == null) if (received == null)
@ -1350,7 +1348,7 @@ public class MessageHelper {
} }
Long getReceivedHeader() throws MessagingException { Long getReceivedHeader() throws MessagingException {
ensureMessage(false); ensureHeaders();
// https://tools.ietf.org/html/rfc5321#section-4.4 // https://tools.ietf.org/html/rfc5321#section-4.4
// https://tools.ietf.org/html/rfc5322#section-3.6.7 // https://tools.ietf.org/html/rfc5322#section-3.6.7
@ -1372,7 +1370,7 @@ public class MessageHelper {
} }
Long getSent() throws MessagingException { Long getSent() throws MessagingException {
ensureMessage(false); ensureEnvelope();
Date sent = imessage.getSentDate(); Date sent = imessage.getSentDate();
if (sent == null) if (sent == null)
@ -1382,7 +1380,7 @@ public class MessageHelper {
} }
String getHeaders() throws MessagingException { String getHeaders() throws MessagingException {
ensureMessage(false); ensureHeaders();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Enumeration<Header> headers = imessage.getAllHeaders(); Enumeration<Header> headers = imessage.getAllHeaders();
@ -2209,7 +2207,7 @@ public class MessageHelper {
MessageParts getMessageParts() throws IOException, MessagingException { MessageParts getMessageParts() throws IOException, MessagingException {
MessageParts parts = new MessageParts(); MessageParts parts = new MessageParts();
ensureMessage(true); ensureStructure();
try { try {
MimePart part = imessage; MimePart part = imessage;
@ -2436,28 +2434,38 @@ public class MessageHelper {
} }
} }
private void ensureMessage(boolean body) throws MessagingException { private void ensureEnvelope() throws MessagingException {
ensureMessage(body, true); _ensureMessage(false, false);
} }
private void ensureMessage(boolean body, boolean all) throws MessagingException { private void ensureHeaders() throws MessagingException {
if (body ? ensuredBody : ensuredEnvelopeAll || (ensuredEnvelope && !all)) _ensureMessage(false, true);
return; }
if (body) private void ensureStructure() throws MessagingException {
ensuredBody = true; _ensureMessage(true, true);
else { }
if (all)
ensuredEnvelopeAll = true; private void _ensureMessage(boolean structure, boolean headers) throws MessagingException {
else if (structure) {
if (ensuredStructure)
return;
ensuredStructure = true;
} else if (headers) {
if (ensuredHeaders)
return;
ensuredHeaders = true;
} else {
if (ensuredEnvelope)
return;
ensuredEnvelope = true; ensuredEnvelope = true;
} }
Log.i("Ensure body=" + body + " all=" + all); Log.i("Ensure structure=" + structure + " headers=" + headers);
try { try {
if (imessage instanceof IMAPMessage) { if (imessage instanceof IMAPMessage) {
if (body) { if (structure) {
String contentType = imessage.getContentType(); // force loadBODYSTRUCTURE String contentType = imessage.getContentType(); // force loadBODYSTRUCTURE
// Workaround protocol parameter missing // Workaround protocol parameter missing
@ -2483,11 +2491,10 @@ public class MessageHelper {
throw new MessagingException("Failed to load IMAP envelope"); throw new MessagingException("Failed to load IMAP envelope");
} }
} else { } else {
// force loadEnvelope if (headers)
if (all) imessage.getAllHeaders(); // force loadHeaders
imessage.getAllHeaders();
else else
imessage.getMessageID(); imessage.getMessageID(); // force loadEnvelope
} }
} }
} catch (MessagingException ex) { } catch (MessagingException ex) {

Loading…
Cancel
Save