aboutsummaryrefslogtreecommitdiff
path: root/deno/mail-relay/dumb-smtp-server.ts
blob: 6c63f5c9348db307940bd89a8577eef4b04b9546 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import config from "./config.ts";
import log from "./log.ts";
import { MailDeliverer } from "./mail.ts";

const CRLF = "\r\n";

const SERVER_NAME = `[${config.SMTP_HOST}]:${config.SMTP_PORT}`;

const RESPONSES = {
  "READY": `220 ${SERVER_NAME} SMTP Ready`,
  "EHLO": `250 ${SERVER_NAME}`,
  "MAIL": "250 2.1.0 Sender OK",
  "RCPT": "250 2.1.5 Recipient OK",
  "DATA": "354 Start mail input; end with <CRLF>.<CRLF>",
  "QUIT": `211 2.0.0 ${SERVER_NAME} closing connection`,
  "INVALID": "500 5.5.1 Error: command not recognized",
} as const;

export class DumbSmtpServer {
  #deliverer: MailDeliverer;

  constructor(deliverer: MailDeliverer) {
    this.#deliverer = deliverer;
  }

  async #handleConnection(conn: Deno.Conn) {
    using disposeStack = new DisposableStack();
    disposeStack.defer(() => {
      log.info("Close smtp session tcp connection.");
      conn.close();
    });
    const writer = conn.writable.getWriter();
    disposeStack.defer(() => writer.releaseLock());
    const reader = conn.readable.getReader();
    disposeStack.defer(() => reader.releaseLock());

    const [decoder, encoder] = [new TextDecoder(), new TextEncoder()];
    const decode = (data: Uint8Array) => decoder.decode(data);
    const send = async (s: string) =>
      await writer.write(encoder.encode(s + CRLF));

    let buffer: string = "";
    let rawMail: string | null = null;

    await send(RESPONSES["READY"]);

    while (true) {
      const { value, done } = await reader.read();
      if (done) break;

      buffer += decode(value);

      while (true) {
        const eolPos = buffer.indexOf(CRLF);
        if (eolPos === -1) break;

        const line = buffer.slice(0, eolPos);
        buffer = buffer.slice(eolPos + CRLF.length);

        if (rawMail == null) {
          log.info("Smtp server received line:", line);
          const upperLine = line.toUpperCase();
          if (upperLine.startsWith("EHLO") || upperLine.startsWith("HELO")) {
            await send(RESPONSES["EHLO"]);
          } else if (upperLine.startsWith("MAIL FROM:")) {
            await send(RESPONSES["MAIL"]);
          } else if (upperLine.startsWith("RCPT TO:")) {
            await send(RESPONSES["RCPT"]);
          } else if (upperLine === "DATA") {
            await send(RESPONSES["DATA"]);
            log.info("Begin to receive mail data...");
            rawMail = "";
          } else if (upperLine === "QUIT") {
            await send(RESPONSES["QUIT"]);
            return;
          } else {
            log.warn("Smtp server command unrecognized:", line);
            await send(RESPONSES["INVALID"]);
            return;
          }
        } else {
          if (line === ".") {
            try {
              log.info("Done receiving mail data, begin to relay...");
              const { message } = await this.#deliverer.deliverRaw(rawMail);
              await send(`250 2.6.0 ${message}`);
              rawMail = null;
              log.info("Done SMTP mail session.");
            } catch (err) {
              log.info(err);
              await send("554 5.3.0 Error: check server log");
              return;
            }
          } else {
            const dataLine = line.startsWith("..") ? line.slice(1) : line;
            rawMail += dataLine + CRLF;
          }
        }
      }
    }
  }

  async serve() {
    const listener = Deno.listen({
      hostname: config.SMTP_HOST,
      port: config.SMTP_PORT,
    });
    listener.unref();
    log.info(`Dumb SMTP server starts running on port ${config.SMTP_PORT}.`);

    for await (const conn of listener) {
      try {
        await this.#handleConnection(conn);
      } catch (cause) {
        log.error("One smtp connection session throws an error " + cause);
      }
    }
  }
}