-
Notifications
You must be signed in to change notification settings - Fork 61
/
rfc_2822.ex
460 lines (353 loc) · 15.9 KB
/
rfc_2822.ex
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
defmodule Mail.Parsers.RFC2822 do
@moduledoc """
RFC2822 Parser
Will attempt to parse a valid RFC2822 message back into
a `%Mail.Message{}` data model.
Mail.Parsers.RFC2822.parse(message)
%Mail.Message{body: "Some message", headers: %{to: ["user@example.com"], from: "other@example.com", subject: "Read this!"}}
"""
@months ~w(jan feb mar apr may jun jul aug sep oct nov dec)
@spec parse(binary() | nonempty_maybe_improper_list()) :: Mail.Message.t()
def parse(content)
def parse([_ | _] = lines) do
[headers, lines] = extract_headers(lines)
%Mail.Message{}
|> parse_headers(headers)
|> mark_multipart
|> parse_body(lines)
end
def parse(content),
do: content |> String.split("\r\n") |> Enum.map(&String.trim_trailing/1) |> parse
defp extract_headers(list, headers \\ [])
defp extract_headers(["" | tail], headers),
do: [Enum.reverse(headers), tail]
defp extract_headers([<<" ", _::binary>> = folded_body | tail], [previous_header | headers]),
do: extract_headers(tail, [previous_header <> folded_body | headers])
defp extract_headers([<<"\t", _::binary>> = folded_body | tail], [previous_header | headers]),
do: extract_headers(tail, [previous_header <> folded_body | headers])
defp extract_headers([header | tail], headers),
do: extract_headers(tail, [header | headers])
@doc """
Parses a RFC2822 timestamp to an Erlang timestamp
[RFC2822 3.3 - Date and Time Specification](https://tools.ietf.org/html/rfc2822#section-3.3)
Timezone information is ignored
"""
def erl_from_timestamp(<<" ", rest::binary>>), do: erl_from_timestamp(rest)
def erl_from_timestamp(<<"\t", rest::binary>>), do: erl_from_timestamp(rest)
def erl_from_timestamp(<<_day::binary-size(3), ", ", rest::binary>>) do
erl_from_timestamp(rest)
end
def erl_from_timestamp(<<date::binary-size(1), " ", rest::binary>>) do
erl_from_timestamp("0" <> date <> " " <> rest)
end
# This caters for an invalid date with no 0 before the hour, e.g. 5:21:43 instead of 05:21:43
def erl_from_timestamp(<<date::binary-size(11), " ", hour::binary-size(1), ":", rest::binary>>) do
erl_from_timestamp("#{date} 0#{hour}:#{rest}")
end
# This caters for an invalid date with dashes between the date/month/year parts
def erl_from_timestamp(
<<date::binary-size(2), "-", month::binary-size(3), "-", year::binary-size(4),
rest::binary>>
) do
erl_from_timestamp("#{date} #{month} #{year}#{rest}")
end
def erl_from_timestamp(
<<date::binary-size(11), " ", hour::binary-size(2), ":", minute::binary-size(2), " ",
rest::binary>>
) do
erl_from_timestamp("#{date} #{hour}:#{minute}:00 #{rest}")
end
def erl_from_timestamp(
<<date::binary-size(2), " ", month::binary-size(3), " ", year::binary-size(4), " ",
hour::binary-size(2), ":", minute::binary-size(2), ":", second::binary-size(2), " ",
_timezone::binary-size(5), _rest::binary>>
) do
year = year |> String.to_integer()
month_name = String.downcase(month)
month = Enum.find_index(@months, &(&1 == month_name)) + 1
date = date |> String.to_integer()
hour = hour |> String.to_integer()
minute = minute |> String.to_integer()
second = second |> String.to_integer()
{{year, month, date}, {hour, minute, second}}
end
def erl_from_timestamp(
<<date::binary-size(2), " ", month::binary-size(3), " ", year::binary-size(4), " ",
hour::binary-size(2), ":", minute::binary-size(2), ":", second::binary-size(2)>>
) do
erl_from_timestamp("#{date} #{month} #{year} #{hour}:#{minute}:#{second} (+00:00)")
end
# This adds support for a now obsolete format
# https://tools.ietf.org/html/rfc2822#section-4.3
def erl_from_timestamp(
<<date::binary-size(2), " ", month::binary-size(3), " ", year::binary-size(4), " ",
hour::binary-size(2), ":", minute::binary-size(2), ":", second::binary-size(2), " ",
timezone::binary-size(3), _rest::binary>>
) do
erl_from_timestamp("#{date} #{month} #{year} #{hour}:#{minute}:#{second} (#{timezone})")
end
# Fixes invalid value: Tue Aug 8 12:05:31 CAT 2017
def erl_from_timestamp(
<<_day::binary-size(3), " ", month::binary-size(3), " ", date::binary-size(2), " ",
hour::binary-size(2), ":", minute::binary-size(2), ":", second::binary-size(2), " ",
_tz::binary-size(3), " ", year::binary-size(4), _rest::binary>>
) do
erl_from_timestamp("#{date} #{month} #{year} #{hour}:#{minute}:#{second} (+00:00)")
end
# Fixes invalid value with milliseconds Tue, 20 Jun 2017 09:44:58.568 +0000 (UTC)
def erl_from_timestamp(
<<date::binary-size(2), " ", month::binary-size(3), " ", year::binary-size(4), " ",
hour::binary-size(2), ":", minute::binary-size(2), ":", second::binary-size(2), ".",
_milliseconds::binary-size(3), rest::binary>>
) do
erl_from_timestamp("#{date} #{month} #{year} #{hour}:#{minute}:#{second}#{rest}}")
end
# Fixes invalid value: Tue May 30 15:29:15 2017
def erl_from_timestamp(
<<_day::binary-size(3), " ", month::binary-size(3), " ", date::binary-size(2), " ",
hour::binary-size(2), ":", minute::binary-size(2), ":", second::binary-size(2), " ",
year::binary-size(4), _rest::binary>>
) do
erl_from_timestamp("#{date} #{month} #{year} #{hour}:#{minute}:#{second} (+00:00)")
end
# Fixes invalid value: Tue Aug 8 12:05:31 2017
def erl_from_timestamp(
<<_day::binary-size(3), " ", month::binary-size(3), " ", date::binary-size(1), " ",
hour::binary-size(2), ":", minute::binary-size(2), ":", second::binary-size(2), " ",
year::binary-size(4), _rest::binary>>
) do
erl_from_timestamp("#{date} #{month} #{year} #{hour}:#{minute}:#{second} (+00:00)")
end
# Fixes invalid value: Wed, 14 10 2015 12:34:17
def erl_from_timestamp(
<<date::binary-size(2), " ", month_digits::binary-size(2), " ", year::binary-size(4), " ",
hour::binary-size(2), ":", minute::binary-size(2), ":", second::binary-size(2),
rest::binary>>
) do
month_name = get_month_name(month_digits)
erl_from_timestamp("#{date} #{month_name} #{year} #{hour}:#{minute}:#{second}#{rest}")
end
@doc """
Retrieves the "name" and "address" parts from an email message recipient
(To, CC, etc.). The following is an example of recipient value:
Full Name <fullname@company.tld>, another@company.tld
In this example, `Full Name` is the "name" part and `fullname@company.tld` is
the "address" part. `another@company.tld` does not have a "name" part, only
an "address" part.
The return value is a mixed list of tuples and strings, which should be
interpreted in the following way:
- When the element is just a string, it represents the "address" part only
- When the element is a tuple, the format is `{name, address}`. Both "name"
and "address" are strings
"""
@spec parse_recipient_value(value :: String.t()) ::
[{String.t(), String.t()} | String.t()]
def parse_recipient_value(value) do
Regex.scan(~r/\s*("?)(.*?)\1\s*?<?([^<\s]+@[^\s>,]+)>?,?/, value)
|> Enum.map(fn
[_, _, "", address] -> address
[_, _, name, address] -> {name, address}
end)
end
@months
|> Enum.with_index(1)
|> Enum.each(fn {month_name, number} ->
defp get_month_name(unquote(String.pad_leading(to_string(number), 2, "0"))),
do: unquote(month_name)
end)
defp parse_headers(message, []), do: message
defp parse_headers(message, [header | tail]) do
[name, body] = String.split(header, ":", parts: 2)
key = String.downcase(name)
decoded = parse_encoded_word(body)
headers = put_header(message.headers, key, parse_header_value(name, decoded))
message = %{message | headers: headers}
parse_headers(message, tail)
end
defp put_header(headers, "received" = key, value),
do: Map.update(headers, key, [value], &[value | &1])
defp put_header(headers, key, value),
do: Map.put(headers, key, value)
defp mark_multipart(message),
do: Map.put(message, :multipart, multipart?(message.headers))
defp parse_header_value(key, " " <> value),
do: parse_header_value(key, value)
defp parse_header_value(key, "\r" <> value),
do: parse_header_value(key, value)
defp parse_header_value(key, "\n" <> value),
do: parse_header_value(key, value)
defp parse_header_value(key, "\t" <> value),
do: parse_header_value(key, value)
defp parse_header_value("To", value),
do: parse_recipient_value(value)
defp parse_header_value("CC", value),
do: parse_recipient_value(value)
defp parse_header_value("From", value),
do:
parse_recipient_value(value)
|> List.first()
defp parse_header_value("Reply-To", value),
do:
parse_recipient_value(value)
|> List.first()
defp parse_header_value("Date", timestamp),
do: erl_from_timestamp(timestamp)
defp parse_header_value("Received", value),
do: parse_received_value(value)
defp parse_header_value("Content-Type", value),
do: parse_structured_header_value(value)
defp parse_header_value("Content-Disposition", value),
do: parse_structured_header_value(value)
defp parse_header_value(_key, value),
do: value
# See https://tools.ietf.org/html/rfc2047
defp parse_encoded_word(""), do: ""
defp parse_encoded_word(<<"=?", value::binary>>) do
[_charset, encoding, encoded_string, <<"=", remainder::binary>>] =
String.split(value, "?", parts: 4)
decoded_string =
case String.upcase(encoding) do
"Q" ->
Mail.Encoders.QuotedPrintable.decode(encoded_string)
"B" ->
Mail.Encoders.Base64.decode(encoded_string)
end
decoded_string <> parse_encoded_word(remainder)
end
defp parse_encoded_word(<<char::utf8, rest::binary>>),
do: <<char::utf8, parse_encoded_word(rest)::binary>>
defp parse_structured_header_value(string, value \\ nil, sub_types \\ [], acc \\ "")
defp parse_structured_header_value("", value, [{key, nil} | sub_types], acc),
do: [value | Enum.reverse([{key, acc} | sub_types])]
defp parse_structured_header_value("", nil, [], acc),
do: acc
defp parse_structured_header_value("", value, sub_types, ""),
do: [value | Enum.reverse(sub_types)]
defp parse_structured_header_value("", value, [], acc),
do: [value, String.trim(acc)]
defp parse_structured_header_value("", value, sub_types, acc),
do: parse_structured_header_value("", value, sub_types, String.trim(acc))
defp parse_structured_header_value(<<"\"", rest::binary>>, value, sub_types, acc) do
{string, rest} = parse_quoted_string(rest)
parse_structured_header_value(rest, value, sub_types, <<acc::binary, string::binary>>)
end
defp parse_structured_header_value(<<";", rest::binary>>, nil, sub_types, acc),
do: parse_structured_header_value(rest, acc, sub_types, "")
defp parse_structured_header_value(<<";", rest::binary>>, value, [{key, nil} | sub_types], acc),
do: parse_structured_header_value(rest, value, [{key, acc} | sub_types], "")
defp parse_structured_header_value(<<"=", rest::binary>>, value, sub_types, acc),
do: parse_structured_header_value(rest, value, [{key_to_atom(acc), nil} | sub_types], "")
defp parse_structured_header_value(<<char::utf8, rest::binary>>, value, sub_types, acc),
do: parse_structured_header_value(rest, value, sub_types, <<acc::binary, char::utf8>>)
defp parse_quoted_string(string, acc \\ "")
defp parse_quoted_string(<<"\\", char::utf8, rest::binary>>, acc),
do: parse_quoted_string(rest, <<acc::binary, char::utf8>>)
defp parse_quoted_string(<<"\"", rest::binary>>, acc), do: {acc, rest}
defp parse_quoted_string(<<char::utf8, rest::binary>>, acc),
do: parse_quoted_string(rest, <<acc::binary, char::utf8>>)
defp parse_received_value(value) do
case String.split(value, ";") do
[value, ""] ->
[value]
[value, date] ->
{value, date} =
case extract_comment(remove_timezone_comment(date)) do
{date, nil} -> {value, date}
{date, comment} -> {"#{value} #{comment}", date}
end
[value, {"date", erl_from_timestamp(remove_excess_whitespace(date))}]
value ->
value
end
end
defp remove_timezone_comment(date_string) do
string_size = date_string |> String.trim_trailing() |> byte_size()
if string_size > 6 do
case binary_part(date_string, string_size - 6, 6) do
<<" (", _::binary-size(3), ")">> -> binary_part(date_string, 0, string_size - 6)
_ -> date_string
end
else
date_string
end
end
defp extract_comment(string, state \\ :value, value \\ "", comment \\ nil)
defp extract_comment("", _, value, comment), do: {value, comment}
defp extract_comment(<<"(", rest::binary>>, :value, value, nil),
do: extract_comment(rest, :comment, value, "(")
defp extract_comment(<<")", rest::binary>>, :comment, value, comment),
do: extract_comment(rest, :value, value, comment <> ")")
defp extract_comment(<<char::utf8, rest::binary>>, :value, value, comment),
do: extract_comment(rest, :value, <<value::binary, char::utf8>>, comment)
defp extract_comment(<<char::utf8, rest::binary>>, :comment, value, comment),
do: extract_comment(rest, :comment, value, <<comment::binary, char::utf8>>)
defp remove_excess_whitespace(<<>>), do: <<>>
defp remove_excess_whitespace(<<" ", rest::binary>>),
do: remove_excess_whitespace(<<" ", rest::binary>>)
defp remove_excess_whitespace(<<"\t", rest::binary>>),
do: remove_excess_whitespace(<<" ", rest::binary>>)
defp remove_excess_whitespace(<<char::utf8, rest::binary>>),
do: <<char::utf8, remove_excess_whitespace(rest)::binary>>
defp parse_body(%Mail.Message{multipart: true} = message, lines) do
content_type = message.headers["content-type"]
boundary = Mail.Proplist.get(content_type, "boundary")
parts =
lines
|> extract_parts(boundary)
|> Enum.map(fn part ->
parse(part)
end)
Map.put(message, :parts, parts)
end
defp parse_body(%Mail.Message{} = message, []) do
message
end
defp parse_body(%Mail.Message{} = message, lines) do
decoded =
lines
|> join_body
|> decode(message)
Map.put(message, :body, decoded)
end
defp join_body(lines, acc \\ [])
defp join_body([], acc), do: acc |> Enum.reverse() |> Enum.join("\r\n")
defp join_body([""], acc), do: acc |> Enum.reverse() |> Enum.join("\r\n")
defp join_body([head | tail], acc), do: join_body(tail, [head | acc])
defp extract_parts(lines, boundary, acc \\ [], parts \\ nil)
defp extract_parts([], _boundary, _acc, parts),
do: Enum.reverse(List.wrap(parts))
defp extract_parts(["--" <> boundary | tail], boundary, acc, nil),
do: extract_parts(tail, boundary, acc, [])
defp extract_parts(["--" <> boundary | tail], boundary, acc, parts),
do: extract_parts(tail, boundary, [], [Enum.reverse(acc) | parts])
defp extract_parts([<<"--" <> rest>> = line | tail], boundary, acc, parts) do
if rest == boundary <> "--" do
extract_parts([], boundary, [], [Enum.reverse(acc) | parts])
else
extract_parts(tail, boundary, [line | acc], parts)
end
end
defp extract_parts([_line | tail], boundary, acc, nil),
do: extract_parts(tail, boundary, acc, nil)
defp extract_parts([head | tail], boundary, acc, parts),
do: extract_parts(tail, boundary, [head | acc], parts)
defp key_to_atom(key) do
key
|> String.trim()
|> String.downcase()
|> String.replace("-", "_")
end
defp multipart?(headers) do
content_type = headers["content-type"]
!!case content_type do
nil -> nil
type when is_binary(type) -> nil
content_type -> Mail.Proplist.get(content_type, "boundary")
end
end
defp decode(body, message) do
body = String.trim_trailing(body)
transfer_encoding = Mail.Message.get_header(message, "content-transfer-encoding")
Mail.Encoder.decode(body, transfer_encoding)
end
end