email-forward-parser 1.0.20 → 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 +77 -52
  2. package/package.json +1 -1
package/lib/parser.js CHANGED
@@ -354,7 +354,12 @@ var REGEXES = {
354
354
  /^\"(.+?)\"\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>"
355
355
  /^([^,;]+?)\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>"
356
356
  /^(.?)\s?\n?\s*[\[|<](.+?)[\]|>]/, // "<walter.sheltan@acme.com>"
357
- /^([^\s@]+@[^\s@]+\.[^\s@,]+)/ // "walter.sheltan@acme.com"
357
+ /^([^\s@]+@[^\s@]+\.[^\s@,]+)/, // "walter.sheltan@acme.com"
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@]+))$/
358
363
  ]
359
364
  };
360
365
 
@@ -567,12 +572,12 @@ class Parser {
567
572
  var _author = this.__parseMailbox(this.__regexes.original_from, text);
568
573
 
569
574
  // Author found?
570
- if (_author.address) {
575
+ if ((_author || {}).address || (_author || {}).name) {
571
576
  return _author;
572
577
  }
573
578
 
574
579
  // Multiple authors found?
575
- if (Array.isArray(_author) && _author[0].address) {
580
+ if (Array.isArray(_author) && (_author[0].address || _author[0].name)) {
576
581
  return _author[0];
577
582
  }
578
583
 
@@ -582,16 +587,13 @@ class Parser {
582
587
  if (_match && _match.length === 4 && _match.groups) {
583
588
  // Notice: the order of parts may change depending on the localization, \
584
589
  // hence the use of named groups
585
- _address = trimString(_match.groups.from_address);
586
- _name = trimString(_match.groups.from_name);
587
-
588
- return {
589
- address : _address,
590
+ _address = _match.groups.from_address;
591
+ _name = _match.groups.from_name;
590
592
 
591
- // Some clients fill the name with the address \
592
- // ("bessie.berry@acme.com <bessie.berry@acme.com>")
593
- name : (_address !== _name) ? _name : null
594
- };
593
+ return this.__prepareMailbox(
594
+ _address,
595
+ _name
596
+ );
595
597
  }
596
598
 
597
599
  // Third method: extract the author via the From part, using lax regexes \
@@ -599,22 +601,19 @@ class Parser {
599
601
  _match = loopRegexes(this.__regexes.original_from_lax, text);
600
602
 
601
603
  if (_match && _match.length > 1) {
602
- _address = trimString(_match[3]);
603
- _name = trimString(_match[2]);
604
-
605
- return {
606
- address : _address,
604
+ _address = _match[3];
605
+ _name = _match[2];
607
606
 
608
- // Some clients fill the name with the address \
609
- // ("bessie.berry@acme.com <bessie.berry@acme.com>")
610
- name : (_address !== _name) ? _name : null
611
- };
607
+ return this.__prepareMailbox(
608
+ _address,
609
+ _name
610
+ );
612
611
  }
613
612
 
614
- return {
615
- address : _address,
616
- name : _name
617
- };
613
+ return this.__prepareMailbox(
614
+ _address,
615
+ _name
616
+ );
618
617
  }
619
618
 
620
619
 
@@ -635,9 +634,7 @@ class Parser {
635
634
  );
636
635
 
637
636
  // Recipient(s) found?
638
- if (Array.isArray(_recipients) &&
639
- !(_recipients[0].address === null && _recipients[0].name === null)
640
- ) {
637
+ if (Array.isArray(_recipients) && _recipients.length > 0) {
641
638
  return _recipients;
642
639
  }
643
640
 
@@ -691,9 +688,7 @@ class Parser {
691
688
  );
692
689
 
693
690
  // Recipient(s) found?
694
- if (Array.isArray(_recipients) &&
695
- !(_recipients[0].address === null && _recipients[0].name === null)
696
- ) {
691
+ if (Array.isArray(_recipients) && _recipients.length > 0) {
697
692
  return _recipients;
698
693
  }
699
694
 
@@ -753,19 +748,18 @@ class Parser {
753
748
 
754
749
  // Address and name available?
755
750
  if (_mailboxMatch.length === 3) {
756
- _address = trimString(_mailboxMatch[2]);
757
- _name = trimString(_mailboxMatch[1]);
751
+ _address = _mailboxMatch[2];
752
+ _name = _mailboxMatch[1];
758
753
  } else {
759
- _address = trimString(_mailboxMatch[1]);
754
+ _address = _mailboxMatch[1];
760
755
  }
761
756
 
762
- _mailboxes.push({
763
- address : _address,
764
-
765
- // Some clients fill the name with the address \
766
- // ("bessie.berry@acme.com <bessie.berry@acme.com>")
767
- name : (_address !== _name) ? _name : null
768
- });
757
+ _mailboxes.push(
758
+ this.__prepareMailbox(
759
+ _address,
760
+ _name
761
+ )
762
+ );
769
763
 
770
764
  // Remove matched mailbox from mailboxes line
771
765
  _mailboxesLine = trimString(
@@ -776,7 +770,7 @@ class Parser {
776
770
  // Remove leading mailboxes separator \
777
771
  // (", Nicholas <nicholas@globex.corp>")
778
772
  for (var _i = 0; _i < MAILBOXES_SEPARATORS.length; _i++) {
779
- let _separator = MAILBOXES_SEPARATORS[_i];
773
+ var _separator = MAILBOXES_SEPARATORS[_i];
780
774
 
781
775
  if (_mailboxesLine[0] === _separator) {
782
776
  _mailboxesLine = trimString(_mailboxesLine.substring(1));
@@ -786,10 +780,12 @@ class Parser {
786
780
  }
787
781
  }
788
782
  } else {
789
- _mailboxes.push({
790
- address : trimString(_mailboxesLine),
791
- name : null
792
- });
783
+ _mailboxes.push(
784
+ this.__prepareMailbox(
785
+ _mailboxesLine,
786
+ null
787
+ )
788
+ );
793
789
 
794
790
  // No more matches
795
791
  _mailboxesLine = "";
@@ -806,12 +802,8 @@ class Parser {
806
802
  }
807
803
  }
808
804
 
809
- var _mailbox = {
810
- address : null,
811
- name : null
812
- };
813
-
814
- return (force_array === true) ? [_mailbox] : _mailbox;
805
+ // No mailbox found
806
+ return (force_array === true) ? [] : null;
815
807
  }
816
808
 
817
809
 
@@ -884,6 +876,39 @@ class Parser {
884
876
 
885
877
  return null;
886
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
+ }
887
912
  }
888
913
 
889
914
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "email-forward-parser",
3
- "version": "1.0.20",
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",