/** * IMAP protocol parser and command builder. * Pure string logic — no I/O, no Node.js dependencies. * Works in browser, Node.js, or worker thread. */ /** Parsed IMAP response line */ export interface ImapResponse { /** Tag ("*" for untagged, "+" for continuation, or the command tag) */ tag: string; /** Status (OK, NO, BAD, BYE, PREAUTH) or response type (EXISTS, RECENT, FETCH, etc.) */ type: string; /** The full text after the tag and type */ text: string; /** Raw line */ raw: string; /** Literal data keyed by BODY section name (e.g. "BODY[]", "BODY[HEADER]") */ literals?: Map; } /** Parsed FETCH response data */ export interface FetchData { seq: number; uid?: number; flags?: Set; internalDate?: Date; size?: number; envelope?: EnvelopeData; bodyStructure?: any; headers?: string; source?: string; } /** Parsed ENVELOPE data */ export interface EnvelopeData { date: Date | null; subject: string; from: AddressData[]; sender: AddressData[]; replyTo: AddressData[]; to: AddressData[]; cc: AddressData[]; bcc: AddressData[]; inReplyTo: string; messageId: string; } export interface AddressData { name: string; address: string; } /** Parsed folder LIST data */ export interface ListData { flags: string[]; delimiter: string; path: string; } /** Parsed STATUS data */ export interface StatusData { messages?: number; recent?: number; uidNext?: number; uidValidity?: number; unseen?: number; /** Total mailbox size in octets — RFC 8438 `STATUS (SIZE)`, only when the * server advertises STATUS=SIZE and the caller asked for it. */ size?: number; } /** Generate a unique command tag */ export declare function nextTag(): string; /** Reset tag counter (for testing) */ export declare function resetTags(): void; /** Build an IMAP command string (tag + command + CRLF) */ export declare function buildCommand(tag: string, command: string): string; /** Build LOGIN command */ export declare function loginCommand(tag: string, user: string, pass: string): string; /** Build AUTHENTICATE XOAUTH2 command */ export declare function xoauth2Command(tag: string, user: string, token: string): string; /** Build LIST command */ export declare function listCommand(tag: string, ref?: string, pattern?: string): string; /** Build SELECT command. Optional `qresync` triggers RFC 7162 fast resync: * the server emits `* VANISHED (EARLIER) ` for every UID expunged * since `modSeq` and `* FETCH` for every state change since then, in lieu * of the caller having to diff UID sets. Capability-gated by caller; * requires `ENABLE QRESYNC` already issued on the session. */ export declare function selectCommand(tag: string, mailbox: string, qresync?: { uidValidity: number; modSeq: number; knownUids?: string; }): string; /** Build ENABLE command per RFC 5161. Common extensions to enable: QRESYNC, * CONDSTORE, UTF8=ACCEPT. Must be issued before any SELECT for the * extension to take effect on the session. */ export declare function enableCommand(tag: string, extensions: string[]): string; /** Expand an RFC 3501 UID set (e.g. `"1:5,8,12,20:*"`) into a flat number * list. Does NOT resolve `*` — caller passes a known upper bound only * when it has one; here `*` is treated as a sentinel and skipped so the * call site can decide what to do (typically: ignore — VANISHED never * emits `*` because that would be open-ended). */ export declare function parseUidSet(set: string, maxExpand?: number): number[]; /** Build EXAMINE command (read-only SELECT) */ export declare function examineCommand(tag: string, mailbox: string): string; /** Build STATUS command */ export declare function statusCommand(tag: string, mailbox: string, items?: string[]): string; /** Build UID FETCH command */ export declare function fetchCommand(tag: string, range: string, items: string[]): string; /** Build sequence-number FETCH command (no UID prefix). Used by fetchLatestN * to avoid SEARCH SINCE on a cold mailbox — sequence ranges are O(1) on the * server because they're just message numbers in the current mailbox. */ export declare function seqFetchCommand(tag: string, range: string, items: string[]): string; /** Build UID SEARCH command */ export declare function searchCommand(tag: string, criteria: string): string; /** Build UID STORE command (set/add/remove flags) */ /** `uid` may be a single UID or an RFC 3501 sequence set ("1:500,502") — * 2026-09-16 Claude Code (Fable 5.1): needed so one STORE can flag a whole * folder for deletion instead of one round trip per message. */ export declare function storeCommand(tag: string, uid: number | string, action: string, flags: string[]): string; /** Compress sorted UIDs into an RFC 3501 sequence set: [1,2,3,7,9,10] → "1:3,7,9:10". */ export declare function uidSequenceSet(uids: number[]): string; /** Build UID COPY command */ export declare function copyCommand(tag: string, uid: number, destination: string): string; /** Build UID MOVE command */ export declare function moveCommand(tag: string, uid: number, destination: string): string; /** Build APPEND command header (body follows as literal) */ export declare function appendCommand(tag: string, mailbox: string, flags: string[], size: number): string; /** Build IDLE command */ export declare function idleCommand(tag: string): string; /** Build NOTIFY SET command (RFC 5465). `spec` is the full event group list, * e.g. "(SELECTED (MessageNew MessageExpunge FlagChange)) (PERSONAL * (MessageNew MessageExpunge FlagChange MailboxName))" — the server then * pushes unsolicited STATUS responses for non-selected mailboxes in the * spec while the connection is idle. Capability gated: only issue when * CAPABILITY response contains NOTIFY. */ export declare function notifyCommand(tag: string, spec: string): string; /** Build DONE command (ends IDLE) */ export declare function doneCommand(): string; /** Build STARTTLS command */ export declare function starttlsCommand(tag: string): string; /** Build LOGOUT command */ export declare function logoutCommand(tag: string): string; /** Build CAPABILITY command */ export declare function capabilityCommand(tag: string): string; /** Build NOOP command */ export declare function noopCommand(tag: string): string; /** Build CREATE command */ export declare function createCommand(tag: string, mailbox: string): string; /** Build DELETE command */ export declare function deleteMailboxCommand(tag: string, mailbox: string): string; /** Build RENAME command */ export declare function renameCommand(tag: string, from: string, to: string): string; /** Parse a single IMAP response line */ export declare function parseResponseLine(line: string): ImapResponse; /** Parse a LIST response line: * LIST (\flags) "delimiter" "path" */ export declare function parseListResponse(text: string): ListData | null; /** Parse STATUS response: * STATUS "mailbox" (MESSAGES n UIDNEXT n ...) */ export declare function parseStatusResponse(text: string): StatusData | null; /** Parse a STATUS response keeping the mailbox name. Used by NOTIFY's * unsolicited STATUS pushes where the mailbox identifies which folder * changed — `parseStatusResponse` discards that. Mailbox may be quoted * ("Sent") or atom-shaped (Sent); the regex handles both. */ export declare function parseStatusResponseFull(text: string): { mailbox: string; data: StatusData; } | null; /** Parse UID SEARCH response: * SEARCH 1 2 3 4 5 */ export declare function parseSearchResponse(text: string): number[]; /** Parse FLAGS from a FETCH or SELECT response */ export declare function parseFlags(flagStr: string): Set; /** Parse ENVELOPE from FETCH — simplified parser for the common fields */ export declare function parseEnvelope(envStr: string): EnvelopeData; /** Build SEARCH criteria string from common search parameters */ export declare function buildSearchCriteria(criteria: { since?: Date; before?: Date; from?: string; to?: string; subject?: string; body?: string; uid?: string; all?: boolean; }): string; //# sourceMappingURL=imap-protocol.d.ts.map