aboutsummaryrefslogtreecommitdiff
path: root/deno/mail-relay/mail-parsing.ts
blob: 8edaf773800b62547abb0258f3eacd1411e6df9c (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import emailAddresses from "email-addresses";

function parseHeaderSection(section: string) {
  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 Error(`Expect ':' in the header field line: ${field}`);
    }
    headers.push([field.slice(0, sepPos).trim(), field.slice(sepPos + 1)]);
    field = null;
  };

  for (const line of section.trimEnd().split(/\r?\n|\r/)) {
    if (line.match(/^\s/)) {
      if (field == null) {
        throw new Error("Header section starts with a space.");
      }
      field += line;
    } else {
      handleField();
      field = line;
    }
    lineNumber += 1;
  }

  handleField();

  return headers;
}

function findFirst(fields: readonly [string, string][], key: string) {
  for (const [k, v] of fields) {
    if (key.toLowerCase() === k.toLowerCase()) return v;
  }
  return undefined;
}

function findMessageId(fields: readonly [string, string][]) {
  const messageIdField = findFirst(fields, "message-id");
  if (messageIdField == null) return undefined;

  const match = messageIdField.match(/\<(.*?)\>/);
  if (match != null) {
    return match[1];
  } else {
    console.warn(`Invalid syntax in header 'message-id': ${messageIdField}`);
    return undefined;
  }
}

function findDate(fields: readonly [string, string][]) {
  const dateField = findFirst(fields, "date");
  if (dateField == null) return undefined;

  const date = new Date(dateField);
  if (isNaN(date.getTime())) {
    console.warn(`Invalid date string in header 'date': ${dateField}`);
    return undefined;
  }
  return date;
}

function findFrom(fields: readonly [string, string][]) {
  const fromField = findFirst(fields, "from");
  if (fromField == null) return undefined;

  const addr = emailAddresses.parseOneAddress(fromField);
  return addr?.type === "mailbox" ? addr.address : undefined;
}

function findRecipients(fields: readonly [string, string][]) {
  const headers = ["to", "cc", "bcc", "x-original-to"];
  const recipients = new Set<string>();
  for (const [key, value] of fields) {
    if (headers.includes(key.toLowerCase())) {
      emailAddresses
        .parseAddressList(value)
        ?.flatMap((a) => (a.type === "mailbox" ? a : a.addresses))
        ?.forEach(({ address }) => recipients.add(address));
    }
  }
  return recipients;
}

function parseSections(raw: string) {
  const twoEolMatch = raw.match(/(\r?\n)(\r?\n)/);
  if (twoEolMatch == null) {
    throw new Error(
      "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: raw.slice(0, twoEolMatch.index!),
    body: raw.slice(twoEolMatch.index! + eol.length + sep.length),
    eol,
    sep,
  };
}

export type ParsedMail = Readonly<{
  header: string;
  body: string;
  sep: string;
  eol: string;
  headers: readonly [string, string][];
  messageId: string | undefined;
  date: Date | undefined;
  from: string | undefined;
  recipients: readonly string[];
}>;

export function simpleParseMail(raw: string): ParsedMail {
  const sections = Object.freeze(parseSections(raw));
  const headers = Object.freeze(parseHeaderSection(sections.header));
  const messageId = findMessageId(headers);
  const date = findDate(headers);
  const from = findFrom(headers);
  const recipients = Object.freeze([...findRecipients(headers)]);
  return Object.freeze({
    ...sections,
    headers,
    messageId,
    date,
    from,
    recipients,
  });
}