IMAP SEARCH evaluates NOT over a parenthesised group per term

Issue Description

IMAP SEARCH evaluates a negated parenthesised group per term.

The parser flattens a multi-term AND group under NOT into the negated operand
list, so NOT (A B) is evaluated as NOT A AND NOT B instead of negating the
conjunction.

A second defect is in the evaluator: when an AND operand is empty, the short
circuit skips to the first End, which can belong to a nested group. It then
closes the wrong level and drops the rest of the query.

Observed on Stalwart 0.16.24 (OSS build, sqlite and postgres stores). The parser
and evaluator code is unchanged on main.

I have a candidate fix for both paths with regression tests:

I would like to be vouched as a contributor so I can submit it as a pull
request.

Expected Behavior

Only messages whose entire parenthesised group is false match the negated form.
With two messages, one carrying Message-ID [email protected] and no X-Missing
header, the other carrying neither, NOT (HEADER Message-ID "tag-42" HEADER X-Missing "") must return both UIDs.

For the nested case, NOT (FLAGGED OR DELETED SEEN) ANSWERED must still apply
the trailing ANSWERED term, so a mailbox whose messages are not answered
returns nothing.

Actual Behavior

The grouped form returns only the message that fails both terms: one UID
instead of two, which is per-term negation.

The nested form returns the whole mailbox instead of an empty result, because
the short circuit jumps to the inner group’s End and the trailing ANSWERED
predicate is never evaluated.

Reproduction Steps

  1. Create a mailbox and append two messages: one with
    Message-ID: <[email protected]> and no X-Missing header, one with
    neither header.
  2. Run UID SEARCH NOT (HEADER Message-ID "tag-42" HEADER X-Missing "").
  3. Observe one UID instead of two. UID SEARCH NOT HEADER Message-ID "tag-42"
    returns the second message, as expected.
  4. Seed messages with no FLAGGED, DELETED, SEEN or ANSWERED flags and run
    UID SEARCH NOT (FLAGGED OR DELETED SEEN) ANSWERED; the whole mailbox is
    returned instead of an empty result.

Relevant Log Output

A1 UID SEARCH NOT (HEADER Message-ID “tag-42” HEADER X-Missing “”)

  • SEARCH 2
    A1 OK UID SEARCH completed

A2 UID SEARCH NOT HEADER Message-ID “tag-42”

  • SEARCH 2
    A2 OK UID SEARCH completed

A3 UID SEARCH NOT (FLAGGED OR DELETED SEEN) ANSWERED

  • SEARCH 1 2 3
    A3 OK UID SEARCH completed

Stalwart Version

v0.16.x

Installation Method

Docker

Database Backend

PostgreSQL

Blob Storage

Filesystem

Search Engine

Internal

Directory Backend

Internal

Additional Context

Client: Python imaplib over IMAPS 993, no proxy; the same result with any IMAP
client because the defect is in the server-side search parser.

Version 0.16.24 OSS. The parser and evaluator code is unchanged on main.

The defect is independent of the storage backend: it reproduces with both the
SQLite and PostgreSQL stores, and with the default internal search engine and
directory.

Steps 1-3 in Reproduction Steps cover the parser defect; step 4 covers the
evaluator short-circuit defect.

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

This is a detailed and well-documented bug report, and contributor vouching/PR acceptance decisions aren’t something I can handle. A staff member or maintainer will need to review the parser/evaluator behavior and your proposed fix directly.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

Fixed in v1.0.0 (expected next week).