email-forward-parser 1.0.3 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -55,8 +55,8 @@ class EmailForwardParser {
55
55
  body : _email.body,
56
56
 
57
57
  from : {
58
- email : (_email.from || {}).email,
59
- name : (_email.from || {}).name
58
+ address : (_email.from || {}).address,
59
+ name : (_email.from || {}).name
60
60
  },
61
61
 
62
62
  to : _email.to,
package/lib/parser.js CHANGED
@@ -10,13 +10,13 @@ var MULTIPLE_MAILBOXES_SEPARATORS = [
10
10
  ";" // Outlook Live, Yahoo Mail
11
11
  ];
12
12
 
13
- var QUOTE_LINE_BREAK_REGEX = /^(>+)\s?$/gm; // Apple Mail
14
- var QUOTE_REGEX = /^(>+)\s?/gm; // Apple Mail
15
- var FOUR_SPACES_REGEX = /^(\ {4})\s?/gm; // Outlook 2019
16
- var CARRIAGE_RETURN_REGEX = /\r\n/gm; // Outlook 2019
17
- var BYTE_ORDER_MARK_REGEX = /[\uFEFF]/gm; // Outlook 2019
18
-
19
13
  var REGEXES = {
14
+ quote_line_break : /^(>+)\s?$/gm, // Apple Mail
15
+ quote : /^(>+)\s?/gm, // Apple Mail
16
+ four_spaces : /^(\ {4})\s?/gm, // Outlook 2019
17
+ carriage_return : /\r\n/gm, // Outlook 2019
18
+ byte_order_mark : /[\uFEFF]/gm, // Outlook 2019
19
+
20
20
  subject : [
21
21
  /^Fw:(.+)/m, // Outlook Live (cs, en, hr, hu, sk), Yahoo Mail (all locales)
22
22
  /^VS:(.+)/m, // Outlook Live (da), New Outlook 2019 (da)
@@ -60,23 +60,23 @@ var REGEXES = {
60
60
  /^>?\s*Початок листа, що пересилається\s?:/m, // Apple Mail (uk)
61
61
  /^\s*-{1,20}\s*Forwarded message\s*-{1,20}\s*/m, // Gmail (all locales)
62
62
  /^\s*_{20,40}\s*$/m, // Outlook Live (all locales)
63
- /^\s?Dne\s?(?<date>.+)\,\s?(?<from_name>.+)\s*[\[|<](?<from_email>.+)[\]|>]\s?napsal\(a\)\s?:/m, // Outlook 2019 (cz)
64
- /^\s?D.\s?(?<date>.+)\s?skrev\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_email>.+)[\]|>]\s?:/m, // Outlook 2019 (da)
65
- /^\s?Am\s?(?<date>.+)\s?schrieb\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_email>.+)[\]|>]\s?:/m, // Outlook 2019 (de)
66
- /^\s?On\s?(?<date>.+)\,\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_email>.+)[\]|>]\s?wrote\s?:/m, // Outlook 2019 (en)
67
- /^\s?El\s?(?<date>.+)\,\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_email>.+)[\]|>]\s?escribió\s?:/m, // Outlook 2019 (es)
68
- /^\s?Le\s?(?<date>.+)\,\s?«(?<from_name>.+)»\s*[\[|<](?<from_email>.+)[\]|>]\s?a écrit\s?:/m, // Outlook 2019 (fr)
69
- /^\s?(?<from_name>.+)\s*[\[|<](?<from_email>.+)[\]|>]\s?kirjoitti\s?(?<date>.+)\s?:/m, // Outlook 2019 (fi)
70
- /^\s?(?<date>.+)\s?időpontban\s?(?<from_name>.+)\s*[\[|<|(](?<from_email>.+)[\]|>|)]\s?ezt írta\s?:/m, // Outlook 2019 (hu)
71
- /^\s?Il giorno\s?(?<date>.+)\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_email>.+)[\]|>]\s?ha scritto\s?:/m, // Outlook 2019 (it)
72
- /^\s?Op\s?(?<date>.+)\s?heeft\s?(?<from_name>.+)\s*[\[|<](?<from_email>.+)[\]|>]\s?geschreven\s?:/m, // Outlook 2019 (nl)
73
- /^\s?(?<from_name>.+)\s*[\[|<](?<from_email>.+)[\]|>]\s?skrev følgende den\s?(?<date>.+)\s?:/m, // Outlook 2019 (no)
74
- /^\s?Dnia\s?(?<date>.+)\s?„(?<from_name>.+)”\s*[\[|<](?<from_email>.+)[\]|>]\s?napisał\s?:/m, // Outlook 2019 (pl)
75
- /^\s?Em\s?(?<date>.+)\,\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_email>.+)[\]|>]\s?escreveu\s?:/m, // Outlook 2019 (pt)
76
- /^\s?(?<date>.+)\s?пользователь\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_email>.+)[\]|>]\s?написал\s?:/m, // Outlook 2019 (ru)
77
- /^\s?(?<date>.+)\s?používateľ\s?(?<from_name>.+)\s*\([\[|<](?<from_email>.+)[\]|>]\)\s?napísal\s?:/m, // Outlook 2019 (sk)
78
- /^\s?Den\s?(?<date>.+)\s?skrev\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_email>.+)[\]|>]\s?följande\s?:/m, // Outlook 2019 (sv)
79
- /^\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_email>.+)[\]|>]\,\s?(?<date>.+)\s?tarihinde şunu yazdı\s?:/m, // Outlook 2019 (tr)
63
+ /^\s?Dne\s?(?<date>.+)\,\s?(?<from_name>.+)\s*[\[|<](?<from_address>.+)[\]|>]\s?napsal\(a\)\s?:/m, // Outlook 2019 (cz)
64
+ /^\s?D.\s?(?<date>.+)\s?skrev\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_address>.+)[\]|>]\s?:/m, // Outlook 2019 (da)
65
+ /^\s?Am\s?(?<date>.+)\s?schrieb\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_address>.+)[\]|>]\s?:/m, // Outlook 2019 (de)
66
+ /^\s?On\s?(?<date>.+)\,\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_address>.+)[\]|>]\s?wrote\s?:/m, // Outlook 2019 (en)
67
+ /^\s?El\s?(?<date>.+)\,\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_address>.+)[\]|>]\s?escribió\s?:/m, // Outlook 2019 (es)
68
+ /^\s?Le\s?(?<date>.+)\,\s?«(?<from_name>.+)»\s*[\[|<](?<from_address>.+)[\]|>]\s?a écrit\s?:/m, // Outlook 2019 (fr)
69
+ /^\s?(?<from_name>.+)\s*[\[|<](?<from_address>.+)[\]|>]\s?kirjoitti\s?(?<date>.+)\s?:/m, // Outlook 2019 (fi)
70
+ /^\s?(?<date>.+)\s?időpontban\s?(?<from_name>.+)\s*[\[|<|(](?<from_address>.+)[\]|>|)]\s?ezt írta\s?:/m, // Outlook 2019 (hu)
71
+ /^\s?Il giorno\s?(?<date>.+)\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_address>.+)[\]|>]\s?ha scritto\s?:/m, // Outlook 2019 (it)
72
+ /^\s?Op\s?(?<date>.+)\s?heeft\s?(?<from_name>.+)\s*[\[|<](?<from_address>.+)[\]|>]\s?geschreven\s?:/m, // Outlook 2019 (nl)
73
+ /^\s?(?<from_name>.+)\s*[\[|<](?<from_address>.+)[\]|>]\s?skrev følgende den\s?(?<date>.+)\s?:/m, // Outlook 2019 (no)
74
+ /^\s?Dnia\s?(?<date>.+)\s?„(?<from_name>.+)”\s*[\[|<](?<from_address>.+)[\]|>]\s?napisał\s?:/m, // Outlook 2019 (pl)
75
+ /^\s?Em\s?(?<date>.+)\,\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_address>.+)[\]|>]\s?escreveu\s?:/m, // Outlook 2019 (pt)
76
+ /^\s?(?<date>.+)\s?пользователь\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_address>.+)[\]|>]\s?написал\s?:/m, // Outlook 2019 (ru)
77
+ /^\s?(?<date>.+)\s?používateľ\s?(?<from_name>.+)\s*\([\[|<](?<from_address>.+)[\]|>]\)\s?napísal\s?:/m, // Outlook 2019 (sk)
78
+ /^\s?Den\s?(?<date>.+)\s?skrev\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_address>.+)[\]|>]\s?följande\s?:/m, // Outlook 2019 (sv)
79
+ /^\s?\"(?<from_name>.+)\"\s*[\[|<](?<from_address>.+)[\]|>]\,\s?(?<date>.+)\s?tarihinde şunu yazdı\s?:/m, // Outlook 2019 (tr)
80
80
  /\s*-{5,8} Přeposlaná zpráva -{5,8}\s*/m, // Yahoo Mail (cs), Thunderbird (cs)
81
81
  /\s*-{5,8} Videresendt meddelelse -{5,8}\s*/m, // Yahoo Mail (da), Thunderbird (da)
82
82
  /\s*-{5,8} Weitergeleitete Nachricht -{5,8}\s*/m, // Yahoo Mail (de), Thunderbird (de)
@@ -233,6 +233,29 @@ var REGEXES = {
233
233
  /\s*Kime\s?:(.+)$/m // Yahook Mail (tr)
234
234
  ],
235
235
 
236
+ original_reply_to : [
237
+ /^\s*Reply-To\s?:(.+)$/m, // Apple Mail (en)
238
+ /^\s*Odgovori na\s?:(.+)$/m, // Apple Mail (hr)
239
+ /^\s*Odpověď na\s?:(.+)$/m, // Apple Mail (cs)
240
+ /^\s*Svar til\s?:(.+)$/m, // Apple Mail (da)
241
+ /^\s*Antwoord aan\s?:(.+)$/m, // Apple Mail (nl)
242
+ /^\s*Vastaus\s?:(.+)$/m, // Apple Mail (fi)
243
+ /^\s*Répondre à\s?:(.+)$/m, // Apple Mail (fr)
244
+ /^\s*Antwort an\s?:(.+)$/m, // Apple Mail (de)
245
+ /^\s*Válaszcím\s?:(.+)$/m, // Apple Mail (hu)
246
+ /^\s*Rispondi a\s?:(.+)$/m, // Apple Mail (it)
247
+ /^\s*Svar til\s?:(.+)$/m, // Apple Mail (no)
248
+ /^\s*Odpowiedź-do\s?:(.+)$/m, // Apple Mail (pl)
249
+ /^\s*Responder A\s?:(.+)$/m, // Apple Mail (pt)
250
+ /^\s*Responder a\s?:(.+)$/m, // Apple Mail (pt-br, es)
251
+ /^\s*Răspuns către\s?:(.+)$/m, // Apple Mail (ro)
252
+ /^\s*Ответ-Кому\s?:(.+)$/m, // Apple Mail (ru)
253
+ /^\s*Odpovedať-Pre\s?:(.+)$/m, // Apple Mail (sk)
254
+ /^\s*Svara till\s?:(.+)$/m, // Apple Mail (sv)
255
+ /^\s*Yanıt Adresi\s?:(.+)$/m, // Apple Mail (tr)
256
+ /^\s*Кому відповісти\s?:(.+)$/m // Apple Mail (uk)
257
+ ],
258
+
236
259
  original_cc : [
237
260
  /^\s*Cc\s?:(.+)$/m, // Apple Mail (en, da, es, fr, hr, it, pt, pt-br, ro, sk), Gmail (all locales), Outlook Live (all locales), New Outlook 2019 (da, de, en, fr, it, pt-br)
238
261
  /^\s*CC\s?:(.+)$/m, // New Outlook 2019 (es, nl, pt), Thunderbird (da, en, es, fi, hr, hu, it, nl, no, pt, pt-br, ro, tr, uk)
@@ -301,7 +324,7 @@ var REGEXES = {
301
324
  /\s*Відправлено\s?:(.+)$/m // Yahoo Mail (uk)
302
325
  ],
303
326
 
304
- name_email : [
327
+ name_address : [
305
328
  /^\"(.+)\"\s?\n?\s*[\[|<](.+)[\]|>]$/, // ""Walter Sheltan" <walter.sheltan@acme.com>" or ""walter.sheltan@acme.com" <walter.sheltan@acme.com>"
306
329
  /^(.+)\s?\n?\s*[\[|<](.+)[\]|>]$/, // "Walter Sheltan <walter.sheltan@acme.com>" or "walter.sheltan@acme.com <walter.sheltan@acme.com>"
307
330
  /^(.?)\s?\n?\s*[\[|<](.+)[\]|>]$/, // "<walter.sheltan@acme.com>"
@@ -350,10 +373,10 @@ class Parser {
350
373
  */
351
374
  parseBody(body, forwarded = false) {
352
375
  // Replace carriage return by regular line break
353
- var _body = body.replace(CARRIAGE_RETURN_REGEX, "\n");
376
+ var _body = body.replace(this.__regexes.carriage_return, "\n");
354
377
 
355
378
  // Remove Byte Order Mark
356
- _body = _body.replace(BYTE_ORDER_MARK_REGEX, "");
379
+ _body = _body.replace(this.__regexes.byte_order_mark, "");
357
380
 
358
381
  // First method: split via the separator (Apple Mail, Gmail, Outlook Live, \
359
382
  // Outlook 2019, Yahoo Mail, Thunderbird)
@@ -403,16 +426,16 @@ class Parser {
403
426
  */
404
427
  parseOriginalEmail(text, body) {
405
428
  // Remove Byte Order Mark
406
- var _text = text.replace(BYTE_ORDER_MARK_REGEX, "");
429
+ var _text = text.replace(this.__regexes.byte_order_mark, "");
407
430
 
408
431
  // Remove ">" at the beginning of each line, while keeping line breaks
409
- _text = text.replace(QUOTE_LINE_BREAK_REGEX, "");
432
+ _text = text.replace(this.__regexes.quote_line_break, "");
410
433
 
411
434
  // Remove ">" at the beginning of other lines
412
- _text = _text.replace(QUOTE_REGEX, "");
435
+ _text = _text.replace(this.__regexes.quote, "");
413
436
 
414
437
  // Remove " " at the beginning of lines
415
- _text = _text.replace(FOUR_SPACES_REGEX, "");
438
+ _text = _text.replace(this.__regexes.four_spaces, "");
416
439
 
417
440
  return {
418
441
  body : this.__parseOriginalBody(_text),
@@ -458,19 +481,20 @@ class Parser {
458
481
  __parseOriginalBody(text) {
459
482
  var _match = null;
460
483
 
461
- // First method: extract the text after the Cc or To part (Apple Mail, \
462
- // Gmail) or after the Subject part (Outlook Live)
484
+ // First method: extract the text after the Cc, To or Reply-To part
485
+ // (Apple Mail, Gmail) or after the Subject part (Outlook Live)
463
486
  var _regexes = [
464
487
  this.__regexes.original_subject,
465
488
  this.__regexes.original_cc,
466
- this.__regexes.original_to
489
+ this.__regexes.original_to,
490
+ this.__regexes.original_reply_to
467
491
  ];
468
492
 
469
493
  for (var _i = 0; _i < _regexes.length; _i++) {
470
494
  _match = loopRegexes(_regexes[_i], text, "split");
471
495
 
472
- // A new line must be present between the Cc, To or Subject part and the \
473
- // actual body
496
+ // A new line must be present between the Cc, To, Reply-To or Subject \
497
+ // part and the actual body
474
498
  if (_match && _match.length === 3 && _match[2].startsWith("\n\n")) {
475
499
  return trimString(_match[2]);
476
500
  }
@@ -506,7 +530,7 @@ class Parser {
506
530
  * @return {object} The parsed author
507
531
  */
508
532
  __parseOriginalFrom(text, body) {
509
- var _email = null;
533
+ var _address = null;
510
534
  var _name = null;
511
535
 
512
536
  // First method: extract the author via the From part (Apple Mail, Gmail, \
@@ -514,7 +538,7 @@ class Parser {
514
538
  var _author = this.__parseMailbox(this.__regexes.original_from, text);
515
539
 
516
540
  // Author found?
517
- if (_author.email) {
541
+ if (_author.address) {
518
542
  return _author;
519
543
  }
520
544
 
@@ -524,15 +548,15 @@ class Parser {
524
548
  if (_match && _match.length === 4 && _match.groups) {
525
549
  // Notice: the order of parts may change depending on the localization, \
526
550
  // hence the use of named groups
527
- _email = trimString(_match.groups.from_email);
551
+ _address = trimString(_match.groups.from_address);
528
552
  _name = trimString(_match.groups.from_name);
529
553
 
530
554
  return {
531
- email : _email,
555
+ address : _address,
532
556
 
533
- // Some clients fill the name with the email \
557
+ // Some clients fill the name with the address \
534
558
  // ("bessie.berry@acme.com <bessie.berry@acme.com>")
535
- name : (_email !== _name) ? _name : null
559
+ name : (_address !== _name) ? _name : null
536
560
  };
537
561
  }
538
562
 
@@ -541,21 +565,21 @@ class Parser {
541
565
  _match = loopRegexes(this.__regexes.original_from_lax, text);
542
566
 
543
567
  if (_match && _match.length > 1) {
544
- _email = trimString(_match[3]);
568
+ _address = trimString(_match[3]);
545
569
  _name = trimString(_match[2]);
546
570
 
547
571
  return {
548
- email : _email,
572
+ address : _address,
549
573
 
550
- // Some clients fill the name with the email \
574
+ // Some clients fill the name with the address \
551
575
  // ("bessie.berry@acme.com <bessie.berry@acme.com>")
552
- name : (_email !== _name) ? _name : null
576
+ name : (_address !== _name) ? _name : null
553
577
  };
554
578
  }
555
579
 
556
580
  return {
557
- email : _email,
558
- name : _name
581
+ address : _address,
582
+ name : _name
559
583
  };
560
584
  }
561
585
 
@@ -569,11 +593,16 @@ class Parser {
569
593
  __parseOriginalTo(text) {
570
594
  // First method: extract the primary recipient(s) via the To part \
571
595
  // (Apple Mail, Gmail, Outlook Live, New Outlook 2019, Thunderbird)
572
- var _recipients = this.__parseMailbox(this.__regexes.original_to, text);
596
+ var _recipients = this.__parseMailbox(
597
+ this.__regexes.original_to,
598
+
599
+ text,
600
+ true //- [force_array]
601
+ );
573
602
 
574
603
  // Recipient(s) found?
575
- if (Array.isArray(_recipients) ||
576
- !(_recipients.email === null && _recipients.name === null)
604
+ if (Array.isArray(_recipients) &&
605
+ !(_recipients[0].address === null && _recipients[0].name === null)
577
606
  ) {
578
607
  return _recipients;
579
608
  }
@@ -602,8 +631,12 @@ class Parser {
602
631
  "replace"
603
632
  );
604
633
 
634
+ return this.__parseMailbox(
635
+ this.__regexes.original_to_lax,
605
636
 
606
- return this.__parseMailbox(this.__regexes.original_to_lax, _cleanText);
637
+ _cleanText,
638
+ true //- [force_array]
639
+ );
607
640
  }
608
641
 
609
642
 
@@ -616,11 +649,16 @@ class Parser {
616
649
  __parseOriginalCc(text) {
617
650
  // First method: extract the carbon-copy recipient(s) via the Cc part
618
651
  // (Apple Mail, Gmail, Outlook Live, New Outlook 2019, Thunderbird)
619
- var _recipients = this.__parseMailbox(this.__regexes.original_cc, text);
652
+ var _recipients = this.__parseMailbox(
653
+ this.__regexes.original_cc,
654
+
655
+ text,
656
+ true //- [force_array]
657
+ );
620
658
 
621
659
  // Recipient(s) found?
622
- if (Array.isArray(_recipients) ||
623
- !(_recipients.email === null && _recipients.name === null)
660
+ if (Array.isArray(_recipients) &&
661
+ !(_recipients[0].address === null && _recipients[0].name === null)
624
662
  ) {
625
663
  return _recipients;
626
664
  }
@@ -642,17 +680,24 @@ class Parser {
642
680
  "replace"
643
681
  );
644
682
 
645
- return this.__parseMailbox(this.__regexes.original_cc_lax, _cleanText);
683
+ return this.__parseMailbox(
684
+ this.__regexes.original_cc_lax,
685
+
686
+ _cleanText,
687
+ true //- [force_array]
688
+ );
646
689
  }
647
690
 
648
691
 
649
692
  /**
650
693
  * Parses mailboxes(s)
651
694
  * @private
652
- * @param {string} text
653
- * @return {object} The parsed mailboxes(s)
695
+ * @param {object} regexes
696
+ * @param {string} text
697
+ * @param {boolean} [force_array]
698
+ * @return {object} The parsed mailboxes(s)
654
699
  */
655
- __parseMailbox(regexes, text) {
700
+ __parseMailbox(regexes, text, force_array=false) {
656
701
  var _match = loopRegexes(regexes, text);
657
702
 
658
703
  if (_match && _match.length > 0) {
@@ -666,29 +711,29 @@ class Parser {
666
711
  "split"
667
712
  );
668
713
 
669
- // Extract name and email (single / multiple mailboxes)
714
+ // Extract name and address (single / multiple mailboxes)
670
715
  for (var _i = 0; _i < _mailboxes.length; _i++) {
671
- var _nameEmailMatch = loopRegexes(
672
- this.__regexes.name_email,
716
+ var _nameAddressMatch = loopRegexes(
717
+ this.__regexes.name_address,
673
718
  trimString(_mailboxes[_i])
674
719
  );
675
720
 
676
- // Name and email available?
677
- if (_nameEmailMatch && _nameEmailMatch.length > 0) {
678
- var _email = trimString(_nameEmailMatch[2]);
679
- var _name = trimString(_nameEmailMatch[1]);
721
+ // Name and address available?
722
+ if (_nameAddressMatch && _nameAddressMatch.length > 0) {
723
+ var _address = trimString(_nameAddressMatch[2]);
724
+ var _name = trimString(_nameAddressMatch[1]);
680
725
 
681
726
  _mailboxes[_i] = {
682
- email : _email,
727
+ address : _address,
683
728
 
684
- // Some clients fill the name with the email \
729
+ // Some clients fill the name with the address \
685
730
  // ("bessie.berry@acme.com <bessie.berry@acme.com>")
686
- name : (_email !== _name) ? _name : null
731
+ name : (_address !== _name) ? _name : null
687
732
  };
688
733
  } else {
689
734
  _mailboxes[_i] = {
690
- email : trimString(_mailboxes[_i]),
691
- name : null
735
+ address : trimString(_mailboxes[_i]),
736
+ name : null
692
737
  };
693
738
  }
694
739
  }
@@ -699,14 +744,16 @@ class Parser {
699
744
  }
700
745
 
701
746
  // Return single mailbox
702
- return _mailboxes[0];
747
+ return (force_array === true) ? _mailboxes : _mailboxes[0];
703
748
  }
704
749
  }
705
750
 
706
- return {
707
- email : null,
708
- name : null
751
+ var _mailbox = {
752
+ address : null,
753
+ name : null
709
754
  };
755
+
756
+ return (force_array === true) ? [_mailbox] : _mailbox;
710
757
  }
711
758
 
712
759
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "email-forward-parser",
3
- "version": "1.0.3",
3
+ "version": "1.0.7",
4
4
  "description": "Parses forwarded emails and extract content",
5
5
  "author": "Eliott Vincent <eliott@crisp.chat>",
6
6
  "main": "lib/index.js",