aboutsummaryrefslogtreecommitdiff
path: root/services/docker/mail-server/aws-sendmail/delivers/dovecot.ts
blob: e30c558a5b5542fd004b57c901f8399d9b8abbe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Logger } from "../logger.ts";
import { MailDeliverContext, MailDeliverer } from "../deliver.ts";

export class DovecotMailDeliverer extends MailDeliverer {
  constructor(private readonly logger: Logger) {
    super("dovecot");
  }

  readonly ldaBin = "dovecot-lda";

  protected override doPrepare(
    rawMail: string,
  ): Promise<MailDeliverContext> {
    return Promise.resolve(new MailDeliverContext(rawMail));
  }

  protected override async doDeliver(
    context: MailDeliverContext,
  ): Promise<void> {
    const { logger, ldaBin } = this;
    const { rawMail } = context;
    let status;

    try {
      const utf8Encoder = new TextEncoder();
      // TODO: A problem here is if mail is VERY long, this will block for a long time.
      // Maybe some task queue can be used.
      const utf8Stream = utf8Encoder.encode(rawMail);

      const ldaCommand = new Deno.Command(ldaBin, {
        stdin: "piped",
        stdout: "piped",
        stderr: "piped",
      });
      const ldaProcess = ldaCommand.spawn();
      logger.logProgramOutput(ldaProcess, ldaBin);

      const stdinWriter = ldaProcess.stdin.getWriter();
      await stdinWriter.ready;
      await stdinWriter.write(utf8Stream);
      await stdinWriter.ready;

      status = await ldaProcess.status;
    } catch (cause) {
      this.throwError(
        "external error.",
        rawMail,
        cause,
      );
    }

    if (!status.success) {
      this.throwError(`${ldaBin} exited with non-zero.`, rawMail);
    }
  }
}