Skip to content

Commit

Permalink
Add readonly detection on fetching (closes #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
igr committed Aug 29, 2024
1 parent bf352e4 commit f178e84
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/main/java/jodd/mail/ReceivedEmails.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.mail.FetchProfile;
import jakarta.mail.Flags;
import jakarta.mail.Folder;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;

Expand Down Expand Up @@ -40,11 +41,14 @@ public ReceivedEmail[] fetch() {
}
}

@SuppressWarnings("t")
private ReceivedEmail[] _fetch() throws MessagingException {
if (messages.length == 0) {
return ReceivedEmail.EMPTY_ARRAY;
}

final boolean isReadOnly = session.folder.getMode() == Folder.READ_ONLY;

if (envelope) {
final FetchProfile fetchProfile = new FetchProfile();

Expand All @@ -64,16 +68,22 @@ private ReceivedEmail[] _fetch() throws MessagingException {

if (!EmailUtil.isEmptyFlags(flagsToSet)) {
emails[i].flags(flagsToSet);
msg.setFlags(flagsToSet, true);
if (!isReadOnly) {
msg.setFlags(flagsToSet, true);
}
}

if (!EmailUtil.isEmptyFlags(flagsToUnset)) {
emails[i].flags().remove(flagsToUnset);
msg.setFlags(flagsToUnset, false);
if (!isReadOnly) {
msg.setFlags(flagsToUnset, false);
}
}

if (EmailUtil.isEmptyFlags(flagsToSet) && !emails[i].isSeen()) {
msg.setFlag(Flags.Flag.SEEN, false);
if (!isReadOnly) {
msg.setFlag(Flags.Flag.SEEN, false);
}
}
}

Expand All @@ -82,7 +92,7 @@ private ReceivedEmail[] _fetch() throws MessagingException {
}

// if messages were marked to be deleted, we need to expunge the folder
if (!EmailUtil.isEmptyFlags(flagsToSet)) {
if (!EmailUtil.isEmptyFlags(flagsToSet) && !isReadOnly) {
if (flagsToSet.contains(Flags.Flag.DELETED)) {
session.folder.expunge();
}
Expand Down

0 comments on commit f178e84

Please sign in to comment.