email-forward-parser 1.0.22 → 1.0.23

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 +71 -39
  2. package/package.json +1 -1
package/lib/parser.js CHANGED
@@ -356,6 +356,10 @@ var REGEXES = {
356
356
  /^(.?)\s?\n?\s*[\[|<](.+?)[\]|>]/, // "<walter.sheltan@acme.com>"
357
357
  /^([^\s@]+@[^\s@]+\.[^\s@,]+)/, // "walter.sheltan@acme.com"
358
358
  /^([^;].+?)\s?\n?\s*[\[|<](.+?)[\]|>]/, // "Walter, Sheltan <walter.sheltan@acme.com>" or "Walter, Sheltan [walter.sheltan@acme.com]"
359
+ ],
360
+
361
+ mailbox_address : [
362
+ /^(([^\s@]+)@([^\s@]+)\.([^\s@]+))$/
359
363
  ]
360
364
  };
361
365
 
@@ -568,12 +572,12 @@ class Parser {
568
572
  var _author = this.__parseMailbox(this.__regexes.original_from, text);
569
573
 
570
574
  // Author found?
571
- if ((_author || {}).address) {
575
+ if ((_author || {}).address || (_author || {}).name) {
572
576
  return _author;
573
577
  }
574
578
 
575
579
  // Multiple authors found?
576
- if (Array.isArray(_author) && _author[0].address) {
580
+ if (Array.isArray(_author) && (_author[0].address || _author[0].name)) {
577
581
  return _author[0];
578
582
  }
579
583
 
@@ -583,16 +587,13 @@ class Parser {
583
587
  if (_match && _match.length === 4 && _match.groups) {
584
588
  // Notice: the order of parts may change depending on the localization, \
585
589
  // hence the use of named groups
586
- _address = trimString(_match.groups.from_address);
587
- _name = trimString(_match.groups.from_name);
588
-
589
- return {
590
- address : _address,
590
+ _address = _match.groups.from_address;
591
+ _name = _match.groups.from_name;
591
592
 
592
- // Some clients fill the name with the address \
593
- // ("bessie.berry@acme.com <bessie.berry@acme.com>")
594
- name : (_address !== _name) ? _name : null
595
- };
593
+ return this.__prepareMailbox(
594
+ _address,
595
+ _name
596
+ );
596
597
  }
597
598
 
598
599
  // Third method: extract the author via the From part, using lax regexes \
@@ -600,22 +601,19 @@ class Parser {
600
601
  _match = loopRegexes(this.__regexes.original_from_lax, text);
601
602
 
602
603
  if (_match && _match.length > 1) {
603
- _address = trimString(_match[3]);
604
- _name = trimString(_match[2]);
605
-
606
- return {
607
- address : _address,
604
+ _address = _match[3];
605
+ _name = _match[2];
608
606
 
609
- // Some clients fill the name with the address \
610
- // ("bessie.berry@acme.com <bessie.berry@acme.com>")
611
- name : (_address !== _name) ? _name : null
612
- };
607
+ return this.__prepareMailbox(
608
+ _address,
609
+ _name
610
+ );
613
611
  }
614
612
 
615
- return {
616
- address : _address,
617
- name : _name
618
- };
613
+ return this.__prepareMailbox(
614
+ _address,
615
+ _name
616
+ );
619
617
  }
620
618
 
621
619
 
@@ -750,19 +748,18 @@ class Parser {
750
748
 
751
749
  // Address and name available?
752
750
  if (_mailboxMatch.length === 3) {
753
- _address = trimString(_mailboxMatch[2]);
754
- _name = trimString(_mailboxMatch[1]);
751
+ _address = _mailboxMatch[2];
752
+ _name = _mailboxMatch[1];
755
753
  } else {
756
- _address = trimString(_mailboxMatch[1]);
754
+ _address = _mailboxMatch[1];
757
755
  }
758
756
 
759
- _mailboxes.push({
760
- address : _address,
761
-
762
- // Some clients fill the name with the address \
763
- // ("bessie.berry@acme.com <bessie.berry@acme.com>")
764
- name : (_address !== _name) ? _name : null
765
- });
757
+ _mailboxes.push(
758
+ this.__prepareMailbox(
759
+ _address,
760
+ _name
761
+ )
762
+ );
766
763
 
767
764
  // Remove matched mailbox from mailboxes line
768
765
  _mailboxesLine = trimString(
@@ -773,7 +770,7 @@ class Parser {
773
770
  // Remove leading mailboxes separator \
774
771
  // (", Nicholas <nicholas@globex.corp>")
775
772
  for (var _i = 0; _i < MAILBOXES_SEPARATORS.length; _i++) {
776
- let _separator = MAILBOXES_SEPARATORS[_i];
773
+ var _separator = MAILBOXES_SEPARATORS[_i];
777
774
 
778
775
  if (_mailboxesLine[0] === _separator) {
779
776
  _mailboxesLine = trimString(_mailboxesLine.substring(1));
@@ -783,10 +780,12 @@ class Parser {
783
780
  }
784
781
  }
785
782
  } else {
786
- _mailboxes.push({
787
- address : trimString(_mailboxesLine),
788
- name : null
789
- });
783
+ _mailboxes.push(
784
+ this.__prepareMailbox(
785
+ _mailboxesLine,
786
+ null
787
+ )
788
+ );
790
789
 
791
790
  // No more matches
792
791
  _mailboxesLine = "";
@@ -877,6 +876,39 @@ class Parser {
877
876
 
878
877
  return null;
879
878
  }
879
+
880
+
881
+ /**
882
+ * Prepares mailbox
883
+ * @private
884
+ * @param {string} address
885
+ * @param {string} name
886
+ * @return {string} The prepared mailboc
887
+ */
888
+ __prepareMailbox(address, name) {
889
+ var _address = trimString(address);
890
+ var _name = trimString(name);
891
+
892
+ // Make sure mailbox address is valid
893
+ var _mailboxAddressMatch = loopRegexes(
894
+ this.__regexes.mailbox_address,
895
+ _address
896
+ );
897
+
898
+ // Invalid mailbox address? Some clients only include the name
899
+ if ((_mailboxAddressMatch || []).length === 0) {
900
+ _name = _address;
901
+ _address = null;
902
+ }
903
+
904
+ return {
905
+ address : _address,
906
+
907
+ // Some clients fill the name with the address \
908
+ // ("bessie.berry@acme.com <bessie.berry@acme.com>")
909
+ name : (_address !== _name) ? _name : null
910
+ };
911
+ }
880
912
  }
881
913
 
882
914
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "email-forward-parser",
3
- "version": "1.0.22",
3
+ "version": "1.0.23",
4
4
  "description": "Parses forwarded emails and extract content",
5
5
  "author": "Eliott Vincent <eliott@crisp.chat>",
6
6
  "main": "lib/index.js",