Skip to content

feat(imap): implement flag-based SEARCH criteria (ANSWERED/DELETED/DRAFT/FLAGGED/SEEN/UNSEEN) #82

@ssilvius

Description

@ssilvius

Problem

The IMAP SEARCH command parser in `packages/imap/src/protocol/parser.ts` does not recognize flag-based search criteria. Any client sending `SEARCH UNSEEN`, `SEARCH SEEN`, `SEARCH ANSWERED`, `SEARCH DELETED`, `SEARCH DRAFT`, or `SEARCH FLAGGED` receives:

```
a001 BAD Unknown search criterion: UNSEEN
```

These are all RFC 3501 Section 6.4.4 criteria and are used heavily by mainstream clients:

  • Apple Mail: `SEARCH UNSEEN` on folder select to build the unread badge
  • Thunderbird: `SEARCH FLAGGED` for the "Starred" smart folder
  • K-9 Mail: `SEARCH UNSEEN SINCE ` for "new mail since last check"
  • Outlook: `SEARCH ANSWERED` for the "Replied" view

Without support, these UI elements either fail to populate or hit the error path.

Current state

Verified by reading `packages/imap/src/protocol/parser.ts` directly. The switch statement in `parseCriterion` has cases for: ALL, NEW, FROM, TO, CC, BCC, SUBJECT, BEFORE, ON, SINCE, LARGER, SMALLER, TEXT, BODY, UID, NOT, OR, plus bare sequence sets. No flag-based cases.

The `MessageAdapter.searchMessages` interface signature already accepts a criterion shape that could carry flag comparisons -- the gap is in the parser, not the adapter.

Fix scope

  1. Extend `SearchCriterion` type in parser.ts to include flag variants
  2. Add case statements in `parseCriterion` for ANSWERED, DELETED, DRAFT, FLAGGED, SEEN, UNSEEN
  3. Update `searchMessages` implementations to evaluate flag criteria against the message's `isRead`, `isStarred`, `deletedAt`, and thread-reply / folder-slug derivations
  4. Add tests referencing RFC 3501 Section 6.4.4 for each criterion
  5. Also consider: KEYWORD, UNKEYWORD, HEADER, SENTBEFORE / SENTON / SENTSINCE -- all RFC 3501 criteria that are similarly unimplemented. Could be a follow-up issue if this one gets too large.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions