diff options
author | Yuqian Yang <crupest@crupest.life> | 2025-06-17 19:04:16 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-06-17 21:37:43 +0800 |
commit | 874c4a8babc5aac7214e71dfef7743bae23893a3 (patch) | |
tree | d7d39313a442bac49859669bb8d1919267575023 /deno/mail-relay | |
parent | 0824de3bae3550674a9ea029b03c5cb8a35cd8e1 (diff) | |
download | crupest-mail.tar.gz crupest-mail.tar.bz2 crupest-mail.zip |
HALF WORK!:mail
Diffstat (limited to 'deno/mail-relay')
-rw-r--r-- | deno/mail-relay/aws/deliver.ts | 8 | ||||
-rw-r--r-- | deno/mail-relay/aws/fetch.ts | 8 | ||||
-rw-r--r-- | deno/mail-relay/aws/mail.ts | 17 | ||||
-rw-r--r-- | deno/mail-relay/dovecot.ts | 55 | ||||
-rw-r--r-- | deno/mail-relay/dumb-smtp-server.ts | 11 | ||||
-rw-r--r-- | deno/mail-relay/mail-parsing.ts | 196 | ||||
-rw-r--r-- | deno/mail-relay/mail.test.ts | 23 | ||||
-rw-r--r-- | deno/mail-relay/mail.ts | 202 |
8 files changed, 264 insertions, 256 deletions
diff --git a/deno/mail-relay/aws/deliver.ts b/deno/mail-relay/aws/deliver.ts index ae010a7..52438cb 100644 --- a/deno/mail-relay/aws/deliver.ts +++ b/deno/mail-relay/aws/deliver.ts @@ -46,13 +46,13 @@ export class AwsMailDeliverer extends SyncMailDeliverer { context.result.smtpMessage = `AWS Message ID: ${context.result.awsMessageId}`; context.result.recipients.set("*", { - kind: "done", - message: `Successfully called aws send-email.`, + kind: "success", + message: `Succeeded to call aws send-email api.`, }); } catch (cause) { context.result.recipients.set("*", { - kind: "fail", - message: "An error was thrown when calling aws send-email." + cause, + kind: "failure", + message: "A JS error was thrown when calling aws send-email." + cause, cause, }); } diff --git a/deno/mail-relay/aws/fetch.ts b/deno/mail-relay/aws/fetch.ts index 9278e63..b9954c8 100644 --- a/deno/mail-relay/aws/fetch.ts +++ b/deno/mail-relay/aws/fetch.ts @@ -50,8 +50,6 @@ export class AwsMailFetcher { } async listLiveMails(): Promise<string[]> { - console.info("Begin to retrieve live mails."); - const listCommand = new ListObjectsV2Command({ Bucket: this.#bucket, Prefix: this.#livePrefix, @@ -99,11 +97,7 @@ export class AwsMailFetcher { await consumer(rawMail, s3Key); console.info(`Done consuming s3 mail ${s3Key}.`); - const date = new Mail(rawMail) - .startSimpleParse() - .sections() - .headers() - .date(); + const { date } = new Mail(rawMail).parsed; const dateString = date != null ? toFileNameString(date, true) : "invalid-date"; diff --git a/deno/mail-relay/aws/mail.ts b/deno/mail-relay/aws/mail.ts index 7ac2332..0f86705 100644 --- a/deno/mail-relay/aws/mail.ts +++ b/deno/mail-relay/aws/mail.ts @@ -8,17 +8,14 @@ export class AwsMailMessageIdRewriteHook implements MailDeliverHook { } async callback(context: MailDeliverContext): Promise<void> { - console.info("Rewrite message ids..."); const addresses = context.mail.simpleFindAllAddresses(); - console.info(`Addresses found in mail: ${addresses.join(", ")}.`); for (const address of addresses) { const awsMessageId = await this.#lookup(address); if (awsMessageId != null && awsMessageId.length !== 0) { - console.info(`Rewrite ${address} to ${awsMessageId}.`); + console.info(`Rewrite address-line string in mail: ${address} => ${awsMessageId}.`); context.mail.raw = context.mail.raw.replaceAll(address, awsMessageId); } } - console.info("Done rewrite message ids."); } } @@ -36,24 +33,18 @@ export class AwsMailMessageIdSaveHook implements MailDeliverHook { } async callback(context: MailDeliverContext): Promise<void> { - console.info("Save aws message ids..."); - const messageId = context.mail - .startSimpleParse() - .sections() - .headers() - .messageId(); + const messageId = context.mail.parsed.messageId; if (messageId == null) { - console.info("Original mail does not have message id. Skip saving."); + console.warn("Original mail does not have message id, skip saving message id map."); return; } if (context.result.awsMessageId != null) { - console.info(`Saving ${messageId} => ${context.result.awsMessageId}.`); + console.info(`Save message id map: ${messageId} => ${context.result.awsMessageId}.`); context.mail.raw = context.mail.raw.replaceAll( messageId, context.result.awsMessageId, ); await this.#record(messageId, context.result.awsMessageId, context); } - console.info("Done save message ids."); } } diff --git a/deno/mail-relay/dovecot.ts b/deno/mail-relay/dovecot.ts index 55e1e9b..8e6e754 100644 --- a/deno/mail-relay/dovecot.ts +++ b/deno/mail-relay/dovecot.ts @@ -16,10 +16,11 @@ async function runCommand( options: { args: string[]; stdin?: Uint8Array; + suppressResultLog?: boolean; errorCodeMessageMap?: Map<number, string>; }, ): Promise<CommandResult> { - const { args, stdin, errorCodeMessageMap } = options; + const { args, stdin, suppressResultLog, errorCodeMessageMap } = options; console.info(`Run external command ${bin} ${args.join(" ")}`); @@ -33,7 +34,6 @@ async function runCommand( // Write stdin if any. if (stdin != null) { - console.info("Write stdin..."); const writer = process.stdin.getWriter(); await writer.write(stdin); writer.close(); @@ -43,13 +43,13 @@ async function runCommand( const status = await process.status; // Build log message string. - let message = `Command exited with code ${status.code}`; + let message = `External command exited with code ${status.code}`; if (status.signal != null) message += ` (signal: ${status.signal})`; if (errorCodeMessageMap != null && errorCodeMessageMap.has(status.code)) { message += `, ${errorCodeMessageMap.get(status.code)}`; } message += "."; - console.log(message); + suppressResultLog || console.log(message); // Return result. return { @@ -58,8 +58,8 @@ async function runCommand( logMessage: message, }; } catch (cause) { - const message = "Running command threw an error:"; - console.log(message, cause); + const message = `A JS error was thrown when invoking external command:`; + suppressResultLog || console.log(message, cause); return { kind: "throw", cause, logMessage: message + " " + cause }; } } @@ -87,36 +87,34 @@ export class DovecotMailDeliverer extends MailDeliverer { const recipients = [...context.recipients]; if (recipients.length === 0) { - context.result.smtpMessage = - "Failed to deliver to dovecot, no recipients are specified."; - return; + throw new Error( + "Failed to deliver to dovecot, no recipients are specified.", + ); } - console.info(`Deliver to dovecot users: ${recipients.join(", ")}.`); - for (const recipient of recipients) { const result = await runCommand( this.#ldaPath, { args: ["-d", recipient], stdin: utf8Bytes, + suppressResultLog: true, + errorCodeMessageMap: ldaExitCodeMessageMap, }, ); if (result.kind === "exit-success") { context.result.recipients.set(recipient, { - kind: "done", + kind: "success", message: result.logMessage, }); } else { context.result.recipients.set(recipient, { - kind: "fail", + kind: "failure", message: result.logMessage, }); } } - - console.info("Done handling all recipients."); } #queryArgs(mailbox: string, messageId: string) { @@ -128,16 +126,12 @@ export class DovecotMailDeliverer extends MailDeliverer { mailbox: string, messageId: string, ): Promise<void> { - console.info( - `Find and delete mails (user: ${user}, message-id: ${messageId}, mailbox: ${mailbox}).`, - ); await runCommand(this.#doveadmPath, { args: ["expunge", "-u", user, ...this.#queryArgs(mailbox, messageId)], }); } async #saveMail(user: string, mailbox: string, mail: Uint8Array) { - console.info(`Save a mail (user: ${user}, mailbox: ${mailbox}).`); await runCommand(this.#doveadmPath, { args: ["save", "-u", user, "-m", mailbox], stdin: mail, @@ -145,9 +139,6 @@ export class DovecotMailDeliverer extends MailDeliverer { } async #markAsRead(user: string, mailbox: string, messageId: string) { - console.info( - `Mark mails as \\Seen(user: ${user}, message-id: ${messageId}, mailbox: ${mailbox}, user: ${user}).`, - ); await runCommand(this.#doveadmPath, { args: [ "flags", @@ -164,45 +155,33 @@ export class DovecotMailDeliverer extends MailDeliverer { console.info("Save sent mails and delete ones with old message id."); // Try to get from and recipients from headers. - const headers = mail.startSimpleParse().sections().headers(); - const from = headers.from(), - recipients = headers.recipients(), - messageId = headers.messageId(); + const { messageId, from, recipients } = mail.parsed; if (from == null) { - console.warn("Failed to determine from from headers, skip saving."); + console.warn("Failed to get sender (from) in headers, skip saving."); return; } - console.info("Parsed from: ", from); - if (recipients.has(from)) { // So the mail should lie in the Inbox. console.info( - "The mail has the sender itself as one of recipients, skip saving.", + "One recipient of the mail is the sender itself, skip saving.", ); return; } await this.#saveMail(from, "Sent", mail.toUtf8Bytes()); if (messageId != null) { - console.info("Mark sent mail as read."); await this.#markAsRead(from, "Sent", messageId); } else { console.warn( - "New message id of the mail is not found, skip marking as read.", + "Message id of the mail is not found, skip marking as read.", ); } console.info("Schedule deletion of old mails at 15,30,60 seconds later."); [15, 30, 60].forEach((seconds) => setTimeout(() => { - console.info( - `Try to delete mails in Sent. (message-id: ${messageIdToDelete}, ` + - `attempt delay: ${seconds}s) ` + - "Note that the mail may have already been deleted," + - " in which case failures of deletion can be just ignored.", - ); void this.#deleteMail(from, "Sent", messageIdToDelete); }, 1000 * seconds) ); diff --git a/deno/mail-relay/dumb-smtp-server.ts b/deno/mail-relay/dumb-smtp-server.ts index 94502c4..818cc88 100644 --- a/deno/mail-relay/dumb-smtp-server.ts +++ b/deno/mail-relay/dumb-smtp-server.ts @@ -47,7 +47,7 @@ export class DumbSmtpServer { const [decoder, encoder] = [new TextDecoder(), new TextEncoder()]; const decode = (data: Uint8Array) => decoder.decode(data); const send = async (s: string) => { - console.info(LOG_TAG, "Send line: " + s); + console.info(LOG_TAG, "Send line:" + s); await writer.write(encoder.encode(s + CRLF)); }; @@ -70,7 +70,7 @@ export class DumbSmtpServer { buffer = buffer.slice(eolPos + CRLF.length); if (rawMail == null) { - console.info(LOG_TAG, "Received line: " + line); + console.info(LOG_TAG, "Received line:" + line); const upperLine = line.toUpperCase(); if (upperLine.startsWith("EHLO") || upperLine.startsWith("HELO")) { await send(this.#responses["EHLO"]); @@ -86,18 +86,17 @@ export class DumbSmtpServer { await send(this.#responses["QUIT"]); return; } else { - console.warn(LOG_TAG, "Unrecognized command from client: " + line); + console.warn(LOG_TAG, "Unrecognized command from client:" + line); await send(this.#responses["INVALID"]); return; } } else { if (line === ".") { try { - console.info(LOG_TAG, "Mail data Received, begin to relay..."); + console.info(LOG_TAG, "Mail data received, begin to relay..."); const { smtpMessage } = await this.#deliverer.deliverRaw(rawMail); await send(`250 2.6.0 ${smtpMessage}`); rawMail = null; - console.info(LOG_TAG, "Relay succeeded."); } catch (err) { console.error(LOG_TAG, "Relay failed.", err); await send("554 5.3.0 Error: check server log"); @@ -124,7 +123,7 @@ export class DumbSmtpServer { try { await this.#handleConnection(conn); } catch (cause) { - console.error(LOG_TAG, "Tcp connection throws an error.", cause); + console.error(LOG_TAG, "Tcp connection throws an error:", cause); } } } diff --git a/deno/mail-relay/mail-parsing.ts b/deno/mail-relay/mail-parsing.ts new file mode 100644 index 0000000..7e76257 --- /dev/null +++ b/deno/mail-relay/mail-parsing.ts @@ -0,0 +1,196 @@ +import emailAddresses from "email-addresses"; + +class MailSimpleParseError extends Error {} + +function lazy<T>(calculator: () => T): () => T { + const tag = new Object(); + let cache: typeof tag | T = tag; + return () => { + if (cache === tag) cache = calculator(); + return cache as T; + }; +} + +class MailSimpleParsedHeaders { + #headerSection; + + #headers = lazy(() => { + const headers = [] as [key: string, value: string][]; + + let field: string | null = null; + let lineNumber = 1; + + const handleField = () => { + if (field == null) return; + const sepPos = field.indexOf(":"); + if (sepPos === -1) { + throw new MailSimpleParseError(`No ':' in the header line: ${field}`); + } + headers.push([field.slice(0, sepPos).trim(), field.slice(sepPos + 1)]); + field = null; + }; + + for (const line of this.#headerSection.trimEnd().split(/\r?\n|\r/)) { + if (line.match(/^\s/)) { + if (field == null) { + throw new MailSimpleParseError("Header section starts with a space."); + } + field += line; + } else { + handleField(); + field = line; + } + lineNumber += 1; + } + + handleField(); + + return headers; + }); + + #messageId = lazy(() => { + const messageIdField = this.#getFirst("message-id"); + if (messageIdField == null) return undefined; + + const match = messageIdField.match(/\<(.*?)\>/); + if (match != null) { + return match[1]; + } else { + console.warn("Invalid message-id header of mail: " + messageIdField); + return undefined; + } + }); + + #date = lazy(() => { + const dateField = this.#getFirst("date"); + if (dateField == null) return undefined; + + const date = new Date(dateField); + if (isNaN(date.getTime())) { + console.warn(`Invalid date string (${dateField}) found in header.`); + return undefined; + } + return date; + }); + + #from = lazy(() => { + const fromField = this.#getFirst("from"); + if (fromField == null) return undefined; + + const addr = emailAddresses.parseOneAddress(fromField); + return addr?.type === "mailbox" ? addr.address : undefined; + }); + + #recipients = lazy(() => { + const headers = ["to", "cc", "bcc", "x-original-to"]; + const recipients = new Set<string>(); + for (const [key, value] of this.#headers()) { + if (headers.includes(key.toLowerCase())) { + emailAddresses + .parseAddressList(value) + ?.flatMap((a) => (a.type === "mailbox" ? a : a.addresses)) + ?.forEach(({ address }) => recipients.add(address)); + } + } + return recipients; + }); + + constructor(headerSection: string) { + this.#headerSection = headerSection; + } + + #getFirst(fieldKey: string): string | undefined { + for (const [key, value] of this.#headers()) { + if (key.toLowerCase() === fieldKey.toLowerCase()) return value; + } + return undefined; + } + + get messageId() { + return this.#messageId(); + } + get date() { + return this.#date(); + } + get from() { + return this.#from(); + } + get recipients() { + return this.#recipients(); + } + + toList(): [string, string][] { + return [...this.#headers()]; + } +} + +class MailSimpleParsed { + #raw; + + #sections = lazy(() => { + const twoEolMatch = this.#raw.match(/(\r?\n)(\r?\n)/); + if (twoEolMatch == null) { + throw new MailSimpleParseError( + "No header/body section separator (2 successive EOLs) found.", + ); + } + + const [eol, sep] = [twoEolMatch[1], twoEolMatch[2]]; + + if (eol !== sep) { + console.warn("Different EOLs (\\r\\n, \\n) found."); + } + + return { + header: this.#raw.slice(0, twoEolMatch.index!), + body: this.#raw.slice(twoEolMatch.index! + eol.length + sep.length), + eol, + sep, + }; + }); + + #headers = lazy(() => { + return new MailSimpleParsedHeaders(this.header); + }); + + constructor(raw: string) { + this.#raw = raw; + } + + get header() { + return this.#sections().header; + } + get body() { + return this.#sections().body; + } + get sep() { + return this.#sections().sep; + } + get eol() { + return this.#sections().eol; + } + + get headers() { + return this.#headers(); + } + + get date() { + return this.headers.date; + } + + get messageId() { + return this.headers.messageId; + } + + get from() { + return this.headers.from; + } + + get recipients() { + return this.headers.recipients; + } +} + +export function simpleParseMail(raw: string): MailSimpleParsed { + return new MailSimpleParsed(raw); +} diff --git a/deno/mail-relay/mail.test.ts b/deno/mail-relay/mail.test.ts index cd0c38d..a864ea7 100644 --- a/deno/mail-relay/mail.test.ts +++ b/deno/mail-relay/mail.test.ts @@ -51,7 +51,7 @@ const mockToAddresses = [ describe("Mail", () => { it("simple parse", () => { - const parsed = new Mail(mockMailStr).startSimpleParse().sections(); + const { parsed } = new Mail(mockMailStr); expect(parsed.header).toEqual(mockHeaderStr); expect(parsed.body).toEqual(mockBodyStr); expect(parsed.sep).toBe("\n"); @@ -59,37 +59,26 @@ describe("Mail", () => { }); it("simple parse crlf", () => { - const parsed = new Mail(mockCrlfMailStr).startSimpleParse().sections(); + const { parsed } = new Mail(mockCrlfMailStr); expect(parsed.sep).toBe("\r\n"); expect(parsed.eol).toBe("\r\n"); }); it("simple parse date", () => { expect( - new Mail(mockMailStr).startSimpleParse().sections().headers().date(), + new Mail(mockMailStr).parsed.date, ).toEqual(new Date(mockDate)); }); it("simple parse headers", () => { expect( - new Mail(mockMailStr).startSimpleParse().sections().headers().fields, + new Mail(mockMailStr).parsed.headers.toList(), ).toEqual(mockHeaders.map((h) => [h[0], " " + h[1].replaceAll("\n", "")])); }); it("parse recipients", () => { const mail = new Mail(mockMailStr); - expect([ - ...mail.startSimpleParse().sections().headers().recipients(), - ]).toEqual([...mockToAddresses, mockCcAddress]); - expect([ - ...mail.startSimpleParse().sections().headers().recipients({ - domain: "example.com", - }), - ]).toEqual( - [...mockToAddresses, mockCcAddress].filter((a) => - a.endsWith("example.com") - ), - ); + expect([...mail.parsed.recipients]).toEqual([...mockToAddresses, mockCcAddress]); }); it("find all addresses", () => { @@ -113,7 +102,7 @@ describe("MailDeliverer", () => { class MockMailDeliverer extends MailDeliverer { name = "mock"; override doDeliver = fn((_: Mail, ctx: MailDeliverContext) => { - ctx.result.recipients.set("*", { kind: "done", message: "success" }); + ctx.result.recipients.set("*", { kind: "success", message: "success message" }); return Promise.resolve(); }) as MailDeliverer["doDeliver"]; } diff --git a/deno/mail-relay/mail.ts b/deno/mail-relay/mail.ts index f1fc892..f4e3e6d 100644 --- a/deno/mail-relay/mail.ts +++ b/deno/mail-relay/mail.ts @@ -1,135 +1,28 @@ import { encodeBase64 } from "@std/encoding/base64"; import { parse } from "@std/csv/parse"; -import emailAddresses from "email-addresses"; +import { simpleParseMail } from "./mail-parsing.ts"; -class MailSimpleParseError extends Error {} - -class MailSimpleParsedHeaders { - constructor(public fields: [key: string, value: string][]) {} - - getFirst(fieldKey: string): string | undefined { - for (const [key, value] of this.fields) { - if (key.toLowerCase() === fieldKey.toLowerCase()) return value; - } - return undefined; - } - - messageId(): string | undefined { - const messageIdField = this.getFirst("message-id"); - if (messageIdField == null) return undefined; +export class Mail { + #raw; + #parsed; - const match = messageIdField.match(/\<(.*?)\>/); - if (match != null) { - return match[1]; - } else { - console.warn("Invalid message-id header of mail: " + messageIdField); - return undefined; - } + constructor(raw: string) { + this.#raw = raw; + this.#parsed = simpleParseMail(raw); } - date(invalidToUndefined: boolean = true): Date | undefined { - const dateField = this.getFirst("date"); - if (dateField == null) return undefined; - - const date = new Date(dateField); - if (invalidToUndefined && isNaN(date.getTime())) { - console.warn(`Invalid date string (${dateField}) found in header.`); - return undefined; - } - return date; + get raw() { + return this.#raw; } - from(): string | undefined { - const fromField = this.getFirst("from"); - if (fromField == null) return undefined; - - const addr = emailAddresses.parseOneAddress(fromField); - return addr?.type === "mailbox" ? addr.address : undefined; + set raw(value) { + this.#raw = value; + this.#parsed = simpleParseMail(value); } - recipients(options?: { domain?: string; headers?: string[] }): Set<string> { - const domain = options?.domain; - const headers = options?.headers ?? ["to", "cc", "bcc", "x-original-to"]; - const recipients = new Set<string>(); - for (const [key, value] of this.fields) { - if (headers.includes(key.toLowerCase())) { - emailAddresses - .parseAddressList(value) - ?.flatMap((a) => (a.type === "mailbox" ? a : a.addresses)) - ?.forEach(({ address }) => { - if (domain == null || address.endsWith(domain)) { - recipients.add(address); - } - }); - } - } - return recipients; + get parsed() { + return this.#parsed; } -} - -class MailSimpleParsedSections { - header: string; - body: string; - eol: string; - sep: string; - - constructor(raw: string) { - const twoEolMatch = raw.match(/(\r?\n)(\r?\n)/); - if (twoEolMatch == null) { - throw new MailSimpleParseError( - "No header/body section separator (2 successive EOLs) found.", - ); - } - - const [eol, sep] = [twoEolMatch[1], twoEolMatch[2]]; - - if (eol !== sep) { - console.warn("Different EOLs (\\r\\n, \\n) found."); - } - - this.header = raw.slice(0, twoEolMatch.index!); - this.body = raw.slice(twoEolMatch.index! + eol.length + sep.length); - this.eol = eol; - this.sep = sep; - } - - headers(): MailSimpleParsedHeaders { - const headers = [] as [key: string, value: string][]; - - let field: string | null = null; - let lineNumber = 1; - - const handleField = () => { - if (field == null) return; - const sepPos = field.indexOf(":"); - if (sepPos === -1) { - throw new MailSimpleParseError(`No ':' in the header line: ${field}`); - } - headers.push([field.slice(0, sepPos).trim(), field.slice(sepPos + 1)]); - field = null; - }; - - for (const line of this.header.trimEnd().split(/\r?\n|\r/)) { - if (line.match(/^\s/)) { - if (field == null) { - throw new MailSimpleParseError("Header section starts with a space."); - } - field += line; - } else { - handleField(); - field = line; - } - lineNumber += 1; - } - - handleField(); - - return new MailSimpleParsedHeaders(headers); - } -} - -export class Mail { - constructor(public raw: string) {} toUtf8Bytes(): Uint8Array { const utf8Encoder = new TextEncoder(); @@ -140,44 +33,23 @@ export class Mail { return encodeBase64(this.raw); } - startSimpleParse() { - return { sections: () => new MailSimpleParsedSections(this.raw) }; - } - simpleFindAllAddresses(): string[] { const re = /,?\<?([a-z0-9_'+\-\.]+\@[a-z0-9_'+\-\.]+)\>?,?/gi; return [...this.raw.matchAll(re)].map((m) => m[1]); } } -export type MailDeliverResultKind = "done" | "fail"; - export interface MailDeliverRecipientResult { - kind: MailDeliverResultKind; - message: string; + kind: "success" | "failure"; + message?: string; cause?: unknown; } export class MailDeliverResult { - smtpMessage: string = ""; - recipients: Map<string, MailDeliverRecipientResult> = new Map(); - + message?: string; + smtpMessage?: string; + recipients = new Map<string, MailDeliverRecipientResult>(); constructor(public mail: Mail) {} - - hasError(): boolean { - return ( - this.recipients.size === 0 || - this.recipients.values().some((r) => r.kind !== "done") - ); - } - - [Symbol.for("Deno.customInspect")]() { - return [ - ...this.recipients.entries().map(([recipient, result]) => - `${recipient} [${result.kind}]: ${result.message}` - ), - ].join("\n"); - } } export class MailDeliverContext { @@ -194,7 +66,6 @@ export interface MailDeliverHook { } export abstract class MailDeliverer { - abstract readonly name: string; preHooks: MailDeliverHook[] = []; postHooks: MailDeliverHook[] = []; @@ -211,11 +82,11 @@ export abstract class MailDeliverer { mail: Mail; recipients?: string[]; }): Promise<MailDeliverResult> { - console.info(`Begin to deliver mail via ${this.name}...`); - const context = new MailDeliverContext(options.mail); options.recipients?.forEach((r) => context.recipients.add(r)); + console.info("Begin to deliver mail to..."); + for (const hook of this.preHooks) { await hook.callback(context); } @@ -226,12 +97,7 @@ export abstract class MailDeliverer { await hook.callback(context); } - console.info("Deliver result:"); - console.info(context.result); - - if (context.result.hasError()) { - throw new Error("Mail failed to deliver."); - } + console.info("Deliver result:", context.result); return context.result; } @@ -263,21 +129,16 @@ export class RecipientFromHeadersHook implements MailDeliverHook { callback(context: MailDeliverContext) { if (context.recipients.size !== 0) { console.warn( - "Recipients are already filled. Won't set them with ones in headers.", + "Recipients are already filled, skip inferring from headers.", ); } else { - context.mail - .startSimpleParse() - .sections() - .headers() - .recipients({ - domain: this.mailDomain, - }) - .forEach((r) => context.recipients.add(r)); + [...context.mail.parsed.recipients].filter((r) => + r.endsWith("@" + this.mailDomain) + ).forEach((r) => context.recipients.add(r)); console.info( - "Recipients found from mail headers: " + - [...context.recipients].join(", "), + "Use recipients inferred from mail headers:", + [...context.recipients].join(", "), ); } return Promise.resolve(); @@ -289,9 +150,7 @@ export class FallbackRecipientHook implements MailDeliverHook { callback(context: MailDeliverContext) { if (context.recipients.size === 0) { - console.info( - "No recipients, fill with fallback: " + [...this.fallback].join(", "), - ); + console.info("Use fallback recipients:" + [...this.fallback].join(", ")); this.fallback.forEach((a) => context.recipients.add(a)); } return Promise.resolve(); @@ -308,12 +167,13 @@ export class AliasRecipientMailHook implements MailDeliverHook { async #parseAliasFile(): Promise<Map<string, string>> { const result = new Map(); if ((await Deno.stat(this.#aliasFile)).isFile) { - console.info(`Found recipients alias file: ${this.#aliasFile}.`); const text = await Deno.readTextFile(this.#aliasFile); const csv = parse(text); for (const [real, ...aliases] of csv) { aliases.forEach((a) => result.set(a, real)); } + } else { + console.warn(`Recipient alias file ${this.#aliasFile} is not found.`); } return result; } |