Skip to content

Commit

Permalink
\* is not a valid IMAP flag (fixes #633)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelmay committed Nov 19, 2023
1 parent 0adae9c commit 1ab3cc2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
*/
package com.icegreen.greenmail.imap;

import java.util.*;
import jakarta.mail.Flags;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.UIDFolder;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.search.SearchTerm;

import com.icegreen.greenmail.foedus.util.MsgRangeFilter;
import com.icegreen.greenmail.imap.commands.IdRange;
import com.icegreen.greenmail.mail.MovingMessage;
import com.icegreen.greenmail.store.FolderException;
import com.icegreen.greenmail.store.FolderListener;
import com.icegreen.greenmail.store.MailFolder;
import com.icegreen.greenmail.store.StoredMessage;
import jakarta.mail.Flags;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.UIDFolder;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.search.SearchTerm;

import java.util.*;

public class ImapSessionFolder implements MailFolder, FolderListener, UIDFolder {
private MailFolder folder;
Expand Down Expand Up @@ -139,6 +139,11 @@ public String getFullName() {
return folder.getFullName();
}

@Override
public Flags getAvailableFlags() {
return folder.getAvailableFlags();
}

@Override
public Flags getPermanentFlags() {
return folder.getPermanentFlags();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected void doProcess(ImapRequestLineReader request,
}

ImapSessionFolder mailbox = session.getSelected();
response.flagsResponse(mailbox.getPermanentFlags());
response.flagsResponse(mailbox.getAvailableFlags());
response.existsResponse(mailbox.getMessageCount());
final boolean resetRecent = !isExamine;
response.recentResponse(mailbox.getRecentCount(resetRecent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@
* @author Raimund Klein <raimund.klein@gmx.de>
*/
class HierarchicalFolder implements MailFolder, UIDFolder {
private static final Flags DEFAULT_FLAGS = new Flags();
static {
DEFAULT_FLAGS.add(Flags.Flag.ANSWERED);
DEFAULT_FLAGS.add(Flags.Flag.DELETED);
DEFAULT_FLAGS.add(Flags.Flag.DRAFT);
DEFAULT_FLAGS.add(Flags.Flag.FLAGGED);
DEFAULT_FLAGS.add(Flags.Flag.SEEN);
}
private static final Flags PERMANENT_FLAGS = new Flags();

static {
PERMANENT_FLAGS.add(Flags.Flag.ANSWERED);
PERMANENT_FLAGS.add(Flags.Flag.DELETED);
PERMANENT_FLAGS.add(Flags.Flag.DRAFT);
PERMANENT_FLAGS.add(Flags.Flag.FLAGGED);
PERMANENT_FLAGS.add(Flags.Flag.SEEN);
PERMANENT_FLAGS.add(DEFAULT_FLAGS);
PERMANENT_FLAGS.add(Flags.Flag.USER);
}

Expand Down Expand Up @@ -125,6 +128,11 @@ public String getFullName() {
return parent.getFullName() + ImapConstants.HIERARCHY_DELIMITER_CHAR + name;
}

@Override
public Flags getAvailableFlags() {
return DEFAULT_FLAGS;
}

@Override
public Flags getPermanentFlags() {
return PERMANENT_FLAGS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ public interface MailFolder {

String getFullName();

/**
* The flags (at a minimum, the system-defined flags) that are applicable for this mailbox.
*
* @see <a href="https://datatracker.ietf.org/doc/html/rfc3501#section-7.2.6">rfc3501#section-7.2.6</a>
*
* @return the applicable mailbox flags
*/
Flags getAvailableFlags();

/**
* Permanent flags are those which the client can add or remove from the
* message flags permanently; that is, concurrent and subsequent
* sessions will see any change in permanent flags
*
* @see <a href="https://datatracker.ietf.org/doc/html/rfc3501#page-12">rfc3501#page-12</a>
*
* @return the permanent (persisting across sessions) flags
*/
Flags getPermanentFlags();

int getMessageCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.icegreen.greenmail.user.GreenMailUser;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetupTest;
import jakarta.mail.*;
import jakarta.mail.internet.MimeMessage;
import org.eclipse.angus.mail.iap.Argument;
import org.eclipse.angus.mail.iap.ByteArray;
import org.eclipse.angus.mail.iap.Response;
Expand All @@ -15,8 +17,6 @@
import org.junit.Rule;
import org.junit.Test;

import jakarta.mail.*;
import jakarta.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -453,6 +453,29 @@ public void testUidSearchAll() throws MessagingException {
}
}

@Test
public void testSelectMailFolder() throws MessagingException {
greenMail.setUser("foo2@localhost", "pwd");
store.connect("foo2@localhost", "pwd");
try {
IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);

final IMAPFolder.ProtocolCommand selectInbox = protocol -> protocol.command("EXAMINE INBOX", null);

// Search empty
Response[] ret = (Response[]) folder.doCommand(selectInbox);
IMAPResponse response = (IMAPResponse) ret[0];
assertThat(response.isBAD()).isFalse();
assertThat(response).hasToString("* FLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen)");
assertThat(ret).satisfiesOnlyOnce(r -> assertThat(r).
hasToString("* OK [PERMANENTFLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen \\*)]")
);
} finally {
store.close();
}
}

private String msnListToUidString(Map<Integer, Long> uids, int... msnList) {
StringBuilder buf = new StringBuilder();
for (int msn : msnList) {
Expand Down

0 comments on commit 1ab3cc2

Please sign in to comment.