Source file email_simple.ml

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
module Stable = struct
  open Core.Core_stable
  include Email_message_kernel.Simple.Stable

  module Attachment = struct
    module Id = struct
      module V1 = struct
        type t =
          { filename : string
          ; path : int list
          }
        [@@deriving bin_io, compare, sexp]
      end
    end
  end
end

open! Core
open! Import
module Unix = Core_unix
module Crypto = Crypto.Cryptokit
module Hash = Crypto.Hash

(* break dependency cycle *)
module Email = Email_message_kernel

include (
  Email_message_kernel.Simple :
    Email_message_kernel.Private.Email_simple_intf.Email_simple
      with module Stable := Email_message_kernel.Simple.Stable)

let make_id () =
  if Ppx_inline_test_lib.am_running
  then "{AUTO-GENERATED-ID}"
  else
    (* Trust that UUID does a good enough job at avoiding colision risks.
       Only UUID to avoid leaking any interesting information. Use [X-JS-*] headers instead, see [tracing_headers] below. *)
    sprintf !"<%{Uuid}@ocaml.async_smtp>" (Uuid_unix.create ())
;;

let local_address () = Email_address.create ?domain:None (Core_unix.getlogin ())

let bigstring_shared_to_file data file =
  let open Async in
  Deferred.Or_error.try_with ~run:`Schedule ~rest:`Log (fun () ->
    Writer.with_file file ~f:(fun w ->
      String_monoid.output_unix (Bigstring_shared.to_string_monoid data) w;
      Writer.flushed w))
;;

module Expert = struct
  include Expert

  let tracing_headers () =
    let get_value ~actual ~testing =
      if Ppx_inline_test_lib.am_running then testing else actual
    in
    let sending_header str =
      let prefix = "X-Sending" in
      [%string "%{prefix}-%{str}"]
    in
    let headers =
      [ ( sending_header "Host"
        , get_value ~actual:(Unix.gethostname ()) ~testing:"{HOSTNAME}" )
      ; sending_header "User", get_value ~actual:(Unix.getlogin ()) ~testing:"{USERNAME}"
      ; ( sending_header "Program"
        , get_value ~actual:Sys_unix.executable_name ~testing:"{EXECUTABLE_NAME}" )
      ]
    in
    headers
  ;;

  let%expect_test "tracing_headers" =
    print_s [%sexp (tracing_headers () : (string * string) list)];
    [%expect
      {|
      ((X-JS-Sending-Host {HOSTNAME}) (X-JS-Sending-User {USERNAME})
       (X-JS-Sending-Program {EXECUTABLE_NAME}))
      |}]
  ;;

  let create_raw
    ?(from = local_address () |> Email_address.to_string)
    ~to_
    ?cc
    ?reply_to
    ~subject
    ?id
    ?in_reply_to
    ?date
    ?auto_generated
    ?(extra_headers = [])
    ?attachments
    ?no_tracing_headers
    content
    =
    let id, extra_headers =
      (* there seems to be some clients that set the [id] via extra_headers, so handle
         these correctly. *)
      let id_from_extra_headers =
        List.Assoc.find extra_headers "Message-Id" ~equal:String.Caseless.equal
      in
      let remove_id_from_extra_headers () =
        List.Assoc.remove extra_headers "Message-Id" ~equal:String.Caseless.equal
      in
      match id with
      | None ->
        (match id_from_extra_headers with
         | None -> make_id (), extra_headers
         | Some id -> id, remove_id_from_extra_headers ())
      | Some id ->
        (match id_from_extra_headers with
         | None -> id, extra_headers
         | Some id' ->
           if String.equal (String.strip id) (String.strip id')
           then id, remove_id_from_extra_headers ()
           else
             (* This case is odd, it will result in two different Message-Id headers on
                the email *)
             id, extra_headers)
    in
    let date =
      match date with
      | None -> Email_date.rfc822_date (Time_float.now ())
      | Some date -> date
    in
    let extra_headers =
      extra_headers
      @
      match no_tracing_headers with
      | Some `Because_not_using_standard_email_infra -> []
      | None -> tracing_headers ()
    in
    create_raw
      ~from
      ~to_
      ?cc
      ?reply_to
      ~subject
      ~id
      ?in_reply_to
      ~date
      ?auto_generated
      ~extra_headers
      ?attachments
      content
  ;;
end

type attachment_name = string [@@deriving sexp_of]

module Path : sig
  type t

  val root : t
  val child : t -> int -> t
  val to_int_list : t -> int list
end = struct
  type t = int list

  let root = []
  let child t i = i :: t
  let to_int_list t = List.rev t
end

module Attachment = struct
  module Id = struct
    type t = Stable.Attachment.Id.V1.t =
      { filename : string
      ; path : int list
      }
    [@@deriving compare, fields ~getters, sexp_of]
  end

  type t =
    { headers : Headers.t
    ; id : Id.t
    ; embedded_email : Email.t option
        (* These are expensive operations. Ensure they are only computed once, and
       lazily. *)
    ; decoded_filename : string Lazy.t
    ; raw_data : Bigstring_shared.t Or_error.t Lazy.t
    ; md5 : string Or_error.t Lazy.t
    ; sha256 : string Or_error.t Lazy.t
    }
  [@@deriving fields ~getters, sexp_of]

  let filename t = Id.filename t.id
  let decoded_filename t = Lazy.force t.decoded_filename
  let raw_data t = Lazy.force t.raw_data
  let md5 t = Lazy.force t.md5
  let sha256 t = Lazy.force t.sha256

  let to_hex digest =
    let result = Bytes.create (String.length digest * 2) in
    let hex = "0123456789ABCDEF" in
    for i = 0 to String.length digest - 1 do
      let c = int_of_char digest.[i] in
      Bytes.set result (2 * i) hex.[c lsr 4];
      Bytes.set result ((2 * i) + 1) hex.[c land 0xF]
    done;
    Bytes.unsafe_to_string ~no_mutation_while_string_reachable:result
  ;;

  let of_content' ?embedded_email ~headers ~filename ~path content =
    let decoded_filename =
      lazy
        (Encoded_word.decode filename
         |> function
         | Ok s -> s
         | Error _ -> filename)
    in
    let raw_data =
      lazy
        (Or_error.try_with (fun () ->
           Octet_stream.decode (Lazy.force content) |> Option.value_exn))
    in
    let compute_hash ~hash =
      lazy
        (match Lazy.force raw_data with
         | Error _ as err -> err
         | Ok data ->
           Or_error.try_with (fun () ->
             Crypto.hash_string (hash ()) (Bigstring_shared.to_string data) |> to_hex))
    in
    let md5 = compute_hash ~hash:Hash.md5 in
    let sha256 = compute_hash ~hash:Hash.sha256 in
    let id = { Id.filename; path = Path.to_int_list path } in
    { headers; id; embedded_email; decoded_filename; raw_data; md5; sha256 }
  ;;

  let of_content ~headers ~filename ~path content =
    of_content' ~headers ~filename ~path (lazy content)
  ;;

  let of_embedded_email ~headers ~filename ~path embedded_email =
    let content =
      lazy
        (Email.to_bigstring_shared embedded_email
         |> Octet_stream.of_bigstring_shared
              ~encoding:(Octet_stream.Encoding.of_headers_or_default headers))
    in
    of_content' ~embedded_email ~headers ~filename ~path content
  ;;

  let to_file t file =
    match raw_data t with
    | Error _ as err -> Async.return err
    | Ok data -> bigstring_shared_to_file data file
  ;;
end

module Content = struct
  include Content

  let of_file ?content_type ?encoding ?extra_headers file =
    let open Async in
    let%map content = Reader.file_contents file in
    let content_type =
      match content_type with
      | None -> Mimetype.from_filename file
      | Some content_type -> content_type
    in
    create_custom ~content_type ?encoding ?extra_headers content
  ;;

  let to_file t file =
    let open Async in
    match content t with
    | None ->
      Deferred.Or_error.errorf
        "The payload of this email is ambigous, you\n\
        \                  you should decompose the email further"
    | Some content ->
      (match Octet_stream.decode content with
       | None -> Deferred.Or_error.errorf "The message payload used an unknown encoding"
       | Some content -> bigstring_shared_to_file content file)
  ;;
end

let create
  ?from
  ~to_
  ?cc
  ?reply_to
  ~subject
  ?id
  ?in_reply_to
  ?date
  ?auto_generated
  ?extra_headers
  ?attachments
  ?no_tracing_headers
  content
  =
  Expert.create_raw
    ?from:(Option.map from ~f:Email_address.to_string)
    ~to_:(List.map to_ ~f:Email_address.to_string)
    ?cc:(Option.map cc ~f:(List.map ~f:Email_address.to_string))
    ?reply_to:(Option.map reply_to ~f:Email_address.to_string)
    ~subject
    ?id
    ?in_reply_to
    ?date:(Option.map date ~f:Email_date.rfc822_date)
    ?auto_generated
    ?extra_headers
    ?attachments
    ?no_tracing_headers
    content
;;

let create_utf8
  ?from
  ~to_
  ?cc
  ?reply_to
  ~subject
  ?id
  ?in_reply_to
  ?date
  ?auto_generated
  ?extra_headers
  ?attachments
  ?no_tracing_headers
  content
  =
  let subject_utf8 =
    let encoded_subject = Base64.encode_string subject in
    Core.sprintf "=?%s?B?%s?=" "UTF-8" encoded_subject
  in
  Expert.create_raw
    ?from:(Option.map from ~f:Email_address.to_string_utf8)
    ~to_:(List.map to_ ~f:Email_address.to_string_utf8)
    ?cc:(Option.map cc ~f:(List.map ~f:Email_address.to_string_utf8))
    ?reply_to:(Option.map reply_to ~f:Email_address.to_string_utf8)
    ~subject:subject_utf8
    ?id
    ?in_reply_to
    ?date:(Option.map date ~f:Email_date.rfc822_date)
    ?auto_generated
    ?extra_headers
    ?attachments
    ?no_tracing_headers
    content
;;

let parse_attachment ~include_inline_parts ?container_headers ~path t =
  let headers = Email.headers t in
  let as_attachment ~even_if_multipart ~filename =
    match Email_content.parse ?container_headers t with
    | Error _ -> None
    | Ok (Email_content.Multipart _) ->
      if even_if_multipart
      then Some (Attachment.of_embedded_email ~headers ~filename ~path t)
      else None
    | Ok (Message email) ->
      Some (Attachment.of_embedded_email ~headers ~filename ~path email)
    | Ok (Data content) -> Some (Attachment.of_content ~headers ~filename ~path content)
  in
  match Content.content_disposition (Content.of_email t) with
  | `Inline ->
    (match include_inline_parts with
     | `None -> None
     | (`Named | `Named_or_has_content_id) as include_inline_parts ->
       let%bind.Option filename =
         match Content.attachment_name (Content.of_email t) with
         | Some filename -> Some filename
         | None ->
           (match include_inline_parts with
            | `Named -> None
            | `Named_or_has_content_id ->
              let%map.Option name = Content.related_part_cid (Content.of_email t) in
              (match
                 Mimetype.to_extension (Content.content_type (Content.of_email t))
               with
               | None -> name
               | Some ext -> name ^ "." ^ ext))
       in
       as_attachment ~even_if_multipart:true ~filename)
  | `Attachment filename -> as_attachment ~even_if_multipart:false ~filename
;;

let map_attachments ?(include_inline_parts = `None) t ~f =
  let handle_possible_attachment ?container_headers ~path t =
    parse_attachment ~include_inline_parts ?container_headers ~path t
    |> function
    | None -> `Unchanged
    | Some attachment ->
      (match f attachment with
       | `Keep -> `Unchanged
       | `Keep_and_don't_recurse -> `Stop
       | `Replace attachment' -> `Changed attachment')
  in
  let rec loop ?container_headers t ~path =
    match Email_content.parse ?container_headers t with
    | Error _ -> `Unchanged
    | Ok (Data _data) ->
      (match handle_possible_attachment ?container_headers ~path t with
       | `Stop -> `Unchanged
       | (`Unchanged | `Changed _) as t -> t)
    | Ok (Message message) ->
      (match handle_possible_attachment ?container_headers ~path t with
       | `Changed t' -> `Changed t'
       | `Stop -> `Unchanged
       | `Unchanged ->
         (match loop message ?container_headers:None ~path:(Path.child path 0) with
          | `Unchanged -> `Unchanged
          | `Changed message' -> `Changed (Email_content.set_content t (Message message'))))
    | Ok (Multipart (mp : Email_content.Multipart.t)) ->
      (match
         List.fold_mapi mp.parts ~init:`Unchanged ~f:(fun i change_status t ->
           match
             loop ~container_headers:mp.container_headers ~path:(Path.child path i) t
           with
           | `Unchanged -> change_status, t
           | `Changed t -> `Changed, t)
       with
       | `Unchanged, _ -> `Unchanged
       | `Changed, parts' ->
         let mp' = Email_content.Multipart.set mp ~parts:parts' () in
         `Changed (Email_content.set_content t (Multipart mp')))
  in
  match loop ?container_headers:None ~path:Path.root t with
  | `Unchanged -> t
  | `Changed t -> t
;;

let all_attachments
  ?(include_inline_parts = `None)
  ?(look_through_attached_mails = true)
  t
  =
  let all_attachments = Queue.create () in
  let (_ : t) =
    map_attachments ~include_inline_parts t ~f:(fun attachment ->
      Queue.enqueue all_attachments attachment;
      match look_through_attached_mails with
      | true -> `Keep
      | false -> `Keep_and_don't_recurse)
  in
  Queue.to_list all_attachments
;;

let find_attachment t name =
  List.find (all_attachments t) ~f:(fun attachment ->
    String.equal (Attachment.filename attachment) name)
;;