email-forward-parser 1.0.13 → 1.0.16

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.
Files changed (2) hide show
  1. package/lib/parser.js +70 -44
  2. package/package.json +1 -1
package/lib/parser.js CHANGED
@@ -5,7 +5,7 @@ var RE2 = require("re2");
5
5
  var { loopRegexes, trimString } = require("./utils");
6
6
 
7
7
 
8
- var MULTIPLE_MAILBOXES_SEPARATORS = [
8
+ var MAILBOXES_SEPARATORS = [
9
9
  ",", // Apple Mail, Gmail, New Outlook 2019, Thunderbird
10
10
  ";" // Outlook Live, Yahoo Mail
11
11
  ];
@@ -18,22 +18,22 @@ var REGEXES = {
18
18
  byte_order_mark : /[\uFEFF]/gm, // Outlook 2019
19
19
 
20
20
  subject : [
21
- /^Fw:(.+)/m, // Outlook Live (cs, en, hr, hu, sk), Yahoo Mail (all locales)
22
- /^VS:(.+)/m, // Outlook Live (da), New Outlook 2019 (da)
23
- /^WG:(.+)/m, // Outlook Live (de), New Outlook 2019 (de)
24
- /^RV:(.+)/m, // Outlook Live (es), New Outlook 2019 (es)
25
- /^TR:(.+)/m, // Outlook Live (fr), New Outlook 2019 (fr)
26
- /^I:(.+)/m, // Outlook Live (it), New Outlook 2019 (it)
27
- /^FW:(.+)/m, // Outlook Live (nl, pt), New Outlook 2019 (cs, en, hu, nl, pt, ru, sk), Outlook 2019 (all locales)
28
- /^Vs:(.+)/m, // Outlook Live (no)
29
- /^PD:(.+)/m, // Outlook Live (pl), New Outlook 2019 (pl)
30
- /^ENC:(.+)/m, // Outlook Live (pt-br), New Outlook 2019 (pt-br)
31
- /^Redir.:(.+)/m, // Outlook Live (ro)
32
- /^VB:(.+)/m, // Outlook Live (sv), New Outlook 2019 (sv)
33
- /^VL:(.+)/m, // New Outlook 2019 (fi)
34
- /^Videresend:(.+)/m, // New Outlook 2019 (no)
35
- /^İLT:(.+)/m, // New Outlook 2019 (tr)
36
- /^Fwd:(.+)/m // Gmail (all locales), Thunderbird (all locales)
21
+ /^Fw:(.*)/m, // Outlook Live (cs, en, hr, hu, sk), Yahoo Mail (all locales)
22
+ /^VS:(.*)/m, // Outlook Live (da), New Outlook 2019 (da)
23
+ /^WG:(.*)/m, // Outlook Live (de), New Outlook 2019 (de)
24
+ /^RV:(.*)/m, // Outlook Live (es), New Outlook 2019 (es)
25
+ /^TR:(.*)/m, // Outlook Live (fr), New Outlook 2019 (fr)
26
+ /^I:(.*)/m, // Outlook Live (it), New Outlook 2019 (it)
27
+ /^FW:(.*)/m, // Outlook Live (nl, pt), New Outlook 2019 (cs, en, hu, nl, pt, ru, sk), Outlook 2019 (all locales)
28
+ /^Vs:(.*)/m, // Outlook Live (no)
29
+ /^PD:(.*)/m, // Outlook Live (pl), New Outlook 2019 (pl)
30
+ /^ENC:(.*)/m, // Outlook Live (pt-br), New Outlook 2019 (pt-br)
31
+ /^Redir.:(.*)/m, // Outlook Live (ro)
32
+ /^VB:(.*)/m, // Outlook Live (sv), New Outlook 2019 (sv)
33
+ /^VL:(.*)/m, // New Outlook 2019 (fi)
34
+ /^Videresend:(.*)/m, // New Outlook 2019 (no)
35
+ /^İLT:(.*)/m, // New Outlook 2019 (tr)
36
+ /^Fwd:(.*)/m // Gmail (all locales), Thunderbird (all locales)
37
37
  ],
38
38
 
39
39
  separator : [
@@ -325,12 +325,14 @@ var REGEXES = {
325
325
  /\s*Відправлено\s?:(.+)$/m // Yahoo Mail (uk)
326
326
  ],
327
327
 
328
- name_address : [
329
- /^(.+)\s?\n?\s*[\[|<]mailto\:(.+)[\]|>]$/, // "Walter Sheltan <mailto:walter.sheltan@acme.com>" or "Walter Sheltan [mailto:walter.sheltan@acme.com]" or "walter.sheltan@acme.com <mailto:walter.sheltan@acme.com>"
330
- /^\"(.+)\"\s?\n?\s*[\[|<](.+)[\]|>]$/, // ""Walter Sheltan" <walter.sheltan@acme.com>" or ""Walter Sheltan" [walter.sheltan@acme.com]" or ""walter.sheltan@acme.com" <walter.sheltan@acme.com>"
331
- /^(.+)\s?\n?\s*[\[|<](.+)[\]|>]$/, // "Walter Sheltan <walter.sheltan@acme.com>" or "Walter Sheltan [walter.sheltan@acme.com]" or "walter.sheltan@acme.com <walter.sheltan@acme.com>"
332
- /^(.?)\s?\n?\s*[\[|<](.+)[\]|>]$/, // "<walter.sheltan@acme.com>"
333
- /(.?)\s?\n?\s*[\[|<](.+)[\]|>]/ // "<walter.sheltan@acme.com>" (lax)
328
+ mailbox : [
329
+ /^(.+?)\s?\n?\s*[\[|<]mailto\:(.+?)[\]|>]/, // "Walter Sheltan <mailto:walter.sheltan@acme.com>" or "Walter Sheltan [mailto:walter.sheltan@acme.com]" or "walter.sheltan@acme.com <mailto:walter.sheltan@acme.com>"
330
+ /^\'(.+?)\'\s?\n?\s*[\[|<](.+?)[\]|>]/, // "'Walter Sheltan' <walter.sheltan@acme.com>" or "'Walter Sheltan' [walter.sheltan@acme.com]" or "'walter.sheltan@acme.com' <walter.sheltan@acme.com>"
331
+ /^\"\'(.+?)\'\"\s?\n?\s*[\[|<](.+?)[\]|>]/, // ""'Walter Sheltan'" <walter.sheltan@acme.com>" or ""'Walter Sheltan'" [walter.sheltan@acme.com]" or ""'walter.sheltan@acme.com'" <walter.sheltan@acme.com>"
332
+ /^\"(.+?)\"\s?\n?\s*[\[|<](.+?)[\]|>]/, // ""Walter Sheltan" <walter.sheltan@acme.com>" or ""Walter Sheltan" [walter.sheltan@acme.com]" or ""walter.sheltan@acme.com" <walter.sheltan@acme.com>"
333
+ /^([^,;]+?)\s?\n?\s*[\[|<](.+?)[\]|>]/, // "Walter Sheltan <walter.sheltan@acme.com>" or "Walter Sheltan [walter.sheltan@acme.com]" or "walter.sheltan@acme.com <walter.sheltan@acme.com>"
334
+ /^(.?)\s?\n?\s*[\[|<](.+?)[\]|>]/, // "<walter.sheltan@acme.com>"
335
+ /^([^\s@]+@[^\s@]+\.[^\s@,]+)/ // "walter.sheltan@acme.com"
334
336
  ]
335
337
  };
336
338
 
@@ -708,37 +710,61 @@ class Parser {
708
710
  var _mailboxesLine = trimString(_match[_match.length - 1]);
709
711
 
710
712
  if (_mailboxesLine) {
711
- var _mailboxes = loopRegexes(
712
- MULTIPLE_MAILBOXES_SEPARATORS,
713
- _mailboxesLine,
714
-
715
- "split"
716
- );
717
-
718
- // Extract name and address (single / multiple mailboxes)
719
- for (var _i = 0; _i < _mailboxes.length; _i++) {
720
- var _nameAddressMatch = loopRegexes(
721
- this.__regexes.name_address,
722
- trimString(_mailboxes[_i])
713
+ var _mailboxes = [];
714
+
715
+ while (_mailboxesLine) {
716
+ var _mailboxMatch = loopRegexes(
717
+ this.__regexes.mailbox,
718
+ _mailboxesLine
723
719
  );
724
720
 
725
- // Name and address available?
726
- if (_nameAddressMatch && _nameAddressMatch.length > 0) {
727
- var _address = trimString(_nameAddressMatch[2]);
728
- var _name = trimString(_nameAddressMatch[1]);
721
+ // Address and / or name available?
722
+ if (_mailboxMatch && _mailboxMatch.length > 0) {
723
+ var _address = null;
724
+ var _name = null;
725
+
726
+ // Address and name available?
727
+ if (_mailboxMatch.length === 3) {
728
+ _address = trimString(_mailboxMatch[2]);
729
+ _name = trimString(_mailboxMatch[1]);
730
+ } else {
731
+ _address = trimString(_mailboxMatch[1]);
732
+ }
729
733
 
730
- _mailboxes[_i] = {
734
+ _mailboxes.push({
731
735
  address : _address,
732
736
 
733
737
  // Some clients fill the name with the address \
734
738
  // ("bessie.berry@acme.com <bessie.berry@acme.com>")
735
739
  name : (_address !== _name) ? _name : null
736
- };
740
+ });
741
+
742
+ // Remove matched mailbox from mailboxes line
743
+ _mailboxesLine = trimString(
744
+ _mailboxesLine.replace(_mailboxMatch[0], "")
745
+ );
746
+
747
+ if (_mailboxesLine) {
748
+ // Remove leading mailboxes separator \
749
+ // (", Nicholas <nicholas@globex.corp>")
750
+ for (var _i = 0; _i < MAILBOXES_SEPARATORS.length; _i++) {
751
+ let _separator = MAILBOXES_SEPARATORS[_i];
752
+
753
+ if (_mailboxesLine[0] === _separator) {
754
+ _mailboxesLine = trimString(_mailboxesLine.substring(1));
755
+
756
+ break;
757
+ }
758
+ }
759
+ }
737
760
  } else {
738
- _mailboxes[_i] = {
739
- address : trimString(_mailboxes[_i]),
761
+ _mailboxes.push({
762
+ address : trimString(_mailboxesLine),
740
763
  name : null
741
- };
764
+ });
765
+
766
+ // No more matches
767
+ _mailboxesLine = "";
742
768
  }
743
769
  }
744
770
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "email-forward-parser",
3
- "version": "1.0.13",
3
+ "version": "1.0.16",
4
4
  "description": "Parses forwarded emails and extract content",
5
5
  "author": "Eliott Vincent <eliott@crisp.chat>",
6
6
  "main": "lib/index.js",