// Wildduck Database Type Definitions // Internal MongoDB document structures and database models import { ObjectId } from 'mongodb'; import { Readable } from 'stream'; export namespace Wildduck { // ============================================================================ // Core Database Models // ============================================================================ /** * User document stored in the 'users' collection * Represents a mail user account with all internal fields */ export interface WildduckUser { _id: ObjectId; username: string; password: string; address?: string; name?: string; quota?: number; storageUsed: number; messages: number; pubKey?: string; encryptMessages?: boolean; encryptForwarded?: boolean; created: Date; lastLogin?: { time: Date; ip: string; sess: string; }; disabled?: boolean; suspended?: boolean; activated?: boolean; metadata?: Record; internalData?: Record; tags?: string[]; targets?: Array<{ id: string; type: string; value: string; }>; enabled2fa?: string[]; autoreply?: boolean; } /** * Mailbox document stored in the 'mailboxes' collection * Represents a user's mail folder */ export interface WildduckMailbox { _id: ObjectId; user: ObjectId; path: string; name?: string; uidValidity: number; uidNext: number; modifyIndex: number; specialUse?: '\\Sent' | '\\Trash' | '\\Junk' | '\\Drafts' | '\\Archive' | '\\All' | '\\Flagged'; subscribed?: boolean; retention?: number; encryptMessages?: boolean; hidden?: boolean; total: number; unseen: number; } /** * Message document stored in the 'messages' collection * Represents an email message */ export interface WildduckMessage { _id: ObjectId; mailbox: ObjectId; user: ObjectId; uid: number; modseq: number; size: number; flags: string[]; flagsUpdated?: Date; unseen?: boolean; searchable?: boolean; junk?: boolean; thread?: string; threadRoot?: string; idate: Date; hdate?: Date; mimeTree: WildduckMimeTree; envelope?: WildduckEnvelope; bodystructure?: string; headers?: Record; text?: string; html?: string[]; intro?: string; attachments?: WildduckAttachment[]; ha?: boolean; exp?: boolean; rdate?: number; copied?: boolean; meta?: { events?: Array<{ action: string; time: Date; }>; }; } /** * Address document stored in the 'addresses' collection * Represents an email address (can be alias or forwarding address) */ export interface WildduckAddress { _id: ObjectId; user?: ObjectId; address: string; name?: string; created: Date; tags?: string[]; metadata?: Record; internalData?: Record; forwarded?: boolean; forwardedDisabled?: boolean; targets?: string[]; main?: boolean; } /** * Filter document stored in the 'filters' collection * Represents a message filtering rule */ export interface WildduckFilter { _id: ObjectId; user: ObjectId; name: string; query: { from?: string; to?: string; subject?: string; listId?: string; text?: string; ha?: boolean; size?: number; }; action: { seen?: boolean; flag?: boolean; delete?: boolean; spam?: boolean; mailbox?: ObjectId; targets?: string[]; }; disabled?: boolean; created: Date; metadata?: Record; } /** * Task document stored in the 'tasks' collection * Represents a background task */ export interface WildduckTask { _id: ObjectId; user?: ObjectId; type: string; locked?: boolean; lockedUntil?: Date; created: Date; status: 'queued' | 'processing' | 'completed' | 'failed'; data: Record; result?: any; error?: string; } /** * Webhook document stored in the 'webhooks' collection * Represents a webhook subscription */ export interface WildduckWebhook { _id: ObjectId; user?: ObjectId; type: string[]; url: string; active: boolean; created: Date; failures?: number; lastError?: { message: string; time: Date; }; } /** * Settings document stored in the 'settings' collection * Represents a system-wide configuration setting */ export interface WildduckSettings { _id: ObjectId; key: string; value: any; name?: string; description?: string; updated?: Date; } /** * Application-Specific Password document stored in the 'asps' collection */ export interface WildduckASP { _id: ObjectId; user: ObjectId; description: string; password: string; scopes: string[]; created: Date; lastUse?: { time: Date; event: string; }; } /** * DKIM key document stored in the 'dkim' collection */ export interface WildduckDKIM { _id: ObjectId; domain: string; selector: string; privateKey: string; publicKey: string; fingerprint: string; created: Date; } /** * Certificate document stored in the 'certs' collection */ export interface WildduckCertificate { _id: ObjectId; servername: string; privateKey: string; cert: string; ca?: string[]; fingerprint: string; altNames?: string[]; validFrom: Date; validTo: Date; created: Date; } // ============================================================================ // MIME and Email Structure Types // ============================================================================ /** * MIME tree structure representing parsed email structure */ export interface WildduckMimeTree { parsedHeader?: Record; attachmentMap?: Record; header?: string[]; body?: boolean | string; size?: number; childNodes?: WildduckMimeTree[]; } /** * Email envelope (MIME headers, not SMTP envelope) */ export interface WildduckEnvelope { from?: WildduckAddressObject[]; to?: WildduckAddressObject[]; cc?: WildduckAddressObject[]; bcc?: WildduckAddressObject[]; sender?: WildduckAddressObject[]; replyTo?: WildduckAddressObject[]; subject?: string; messageId?: string; inReplyTo?: string; date?: Date; } /** * Email address object (name + address) */ export interface WildduckAddressObject { name?: string; address?: string; } /** * Attachment metadata */ export interface WildduckAttachment { id: string; filename?: string; contentType?: string; disposition?: string; transferEncoding?: string; related?: boolean; size: number; sizeKb?: number; cid?: string; } // ============================================================================ // GridFS Attachment Storage Types // ============================================================================ /** * GridFS attachment file metadata (stored in attachments.files) */ export interface WildduckAttachmentFile { _id: ObjectId; length: number; chunkSize: number; uploadDate: Date; filename: string; md5: string; metadata: { magic: number; decoded?: boolean; c?: number; // Reference count m?: string; // MIME type [key: string]: any; }; } // ============================================================================ // Session and Connection Types (Internal) // ============================================================================ /** * IMAP session state (in-memory, not stored in database) */ export interface WildduckIMAPSession { id: string; user: { id: ObjectId; username: string; }; state: 'Not Authenticated' | 'Authenticated' | 'Selected'; selected?: WildduckSelectedMailbox; socket?: any; ip: string; secure: boolean; clientInfo?: { name?: string; version?: string; vendor?: string; }; } /** * Selected mailbox state in IMAP session */ export interface WildduckSelectedMailbox { mailbox: ObjectId; path: string; uidList: number[]; modifyIndex: number; uidNext: number; uidValidity: number; exists: number; unseen: number; } // ============================================================================ // IMAP Protocol Response Types // ============================================================================ /** * IMAP command response codes */ export type WildduckIMAPResponse = | 'OK' | 'NO' | 'BAD' | 'NONEXISTENT' | 'ALREADYEXISTS' | 'TRYCREATE' | 'OVERQUOTA' | 'CANNOT'; /** * IMAP COPY command response */ export interface WildduckCopyResponse { uidValidity: number; sourceUid: number[]; destinationUid: number[]; } }