email-forward-parser 1.0.21 → 1.1.0
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/parser.js +75 -51
- package/lib/utils.js +41 -8
- 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 =
|
|
587
|
-
_name =
|
|
588
|
-
|
|
589
|
-
return {
|
|
590
|
-
address : _address,
|
|
590
|
+
_address = _match.groups.from_address;
|
|
591
|
+
_name = _match.groups.from_name;
|
|
591
592
|
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
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 =
|
|
604
|
-
_name =
|
|
605
|
-
|
|
606
|
-
return {
|
|
607
|
-
address : _address,
|
|
604
|
+
_address = _match[3];
|
|
605
|
+
_name = _match[2];
|
|
608
606
|
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
607
|
+
return this.__prepareMailbox(
|
|
608
|
+
_address,
|
|
609
|
+
_name
|
|
610
|
+
);
|
|
613
611
|
}
|
|
614
612
|
|
|
615
|
-
return
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
613
|
+
return this.__prepareMailbox(
|
|
614
|
+
_address,
|
|
615
|
+
_name
|
|
616
|
+
);
|
|
619
617
|
}
|
|
620
618
|
|
|
621
619
|
|
|
@@ -636,9 +634,7 @@ class Parser {
|
|
|
636
634
|
);
|
|
637
635
|
|
|
638
636
|
// Recipient(s) found?
|
|
639
|
-
if (Array.isArray(_recipients) &&
|
|
640
|
-
!(_recipients[0].address === null && _recipients[0].name === null)
|
|
641
|
-
) {
|
|
637
|
+
if (Array.isArray(_recipients) && _recipients.length > 0) {
|
|
642
638
|
return _recipients;
|
|
643
639
|
}
|
|
644
640
|
|
|
@@ -692,9 +688,7 @@ class Parser {
|
|
|
692
688
|
);
|
|
693
689
|
|
|
694
690
|
// Recipient(s) found?
|
|
695
|
-
if (Array.isArray(_recipients) &&
|
|
696
|
-
!(_recipients[0].address === null && _recipients[0].name === null)
|
|
697
|
-
) {
|
|
691
|
+
if (Array.isArray(_recipients) && _recipients.length > 0) {
|
|
698
692
|
return _recipients;
|
|
699
693
|
}
|
|
700
694
|
|
|
@@ -754,19 +748,18 @@ class Parser {
|
|
|
754
748
|
|
|
755
749
|
// Address and name available?
|
|
756
750
|
if (_mailboxMatch.length === 3) {
|
|
757
|
-
_address =
|
|
758
|
-
_name =
|
|
751
|
+
_address = _mailboxMatch[2];
|
|
752
|
+
_name = _mailboxMatch[1];
|
|
759
753
|
} else {
|
|
760
|
-
_address =
|
|
754
|
+
_address = _mailboxMatch[1];
|
|
761
755
|
}
|
|
762
756
|
|
|
763
|
-
_mailboxes.push(
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
});
|
|
757
|
+
_mailboxes.push(
|
|
758
|
+
this.__prepareMailbox(
|
|
759
|
+
_address,
|
|
760
|
+
_name
|
|
761
|
+
)
|
|
762
|
+
);
|
|
770
763
|
|
|
771
764
|
// Remove matched mailbox from mailboxes line
|
|
772
765
|
_mailboxesLine = trimString(
|
|
@@ -777,7 +770,7 @@ class Parser {
|
|
|
777
770
|
// Remove leading mailboxes separator \
|
|
778
771
|
// (", Nicholas <nicholas@globex.corp>")
|
|
779
772
|
for (var _i = 0; _i < MAILBOXES_SEPARATORS.length; _i++) {
|
|
780
|
-
|
|
773
|
+
var _separator = MAILBOXES_SEPARATORS[_i];
|
|
781
774
|
|
|
782
775
|
if (_mailboxesLine[0] === _separator) {
|
|
783
776
|
_mailboxesLine = trimString(_mailboxesLine.substring(1));
|
|
@@ -787,10 +780,12 @@ class Parser {
|
|
|
787
780
|
}
|
|
788
781
|
}
|
|
789
782
|
} else {
|
|
790
|
-
_mailboxes.push(
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
783
|
+
_mailboxes.push(
|
|
784
|
+
this.__prepareMailbox(
|
|
785
|
+
_mailboxesLine,
|
|
786
|
+
null
|
|
787
|
+
)
|
|
788
|
+
);
|
|
794
789
|
|
|
795
790
|
// No more matches
|
|
796
791
|
_mailboxesLine = "";
|
|
@@ -807,12 +802,8 @@ class Parser {
|
|
|
807
802
|
}
|
|
808
803
|
}
|
|
809
804
|
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
name : null
|
|
813
|
-
};
|
|
814
|
-
|
|
815
|
-
return (force_array === true) ? [_mailbox] : _mailbox;
|
|
805
|
+
// No mailbox found
|
|
806
|
+
return (force_array === true) ? [] : null;
|
|
816
807
|
}
|
|
817
808
|
|
|
818
809
|
|
|
@@ -885,6 +876,39 @@ class Parser {
|
|
|
885
876
|
|
|
886
877
|
return null;
|
|
887
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
|
+
}
|
|
888
912
|
}
|
|
889
913
|
|
|
890
914
|
|
package/lib/utils.js
CHANGED
|
@@ -13,12 +13,15 @@ var RE2 = require("re2");
|
|
|
13
13
|
/**
|
|
14
14
|
* Executes multiple regular expressions until one succeeds
|
|
15
15
|
* @public
|
|
16
|
-
* @param {object}
|
|
17
|
-
* @param {string}
|
|
18
|
-
* @param {string}
|
|
19
|
-
* @
|
|
16
|
+
* @param {object} regexes
|
|
17
|
+
* @param {string} str
|
|
18
|
+
* @param {string} [mode]
|
|
19
|
+
* @param {boolean} [highestPosition]
|
|
20
|
+
* @return {object} The result of the first successfull regular expression
|
|
20
21
|
*/
|
|
21
|
-
var loopRegexes = function(
|
|
22
|
+
var loopRegexes = function(
|
|
23
|
+
regexes, str, mode = "match", highestPosition = true
|
|
24
|
+
) {
|
|
22
25
|
var _match;
|
|
23
26
|
|
|
24
27
|
for (var _i = 0; _i < regexes.length; _i++) {
|
|
@@ -29,11 +32,13 @@ var loopRegexes = function(regexes, str, mode = "match") {
|
|
|
29
32
|
_regex = new RE2(regexes[_i]);
|
|
30
33
|
}
|
|
31
34
|
|
|
32
|
-
|
|
35
|
+
var _currentMatch = (
|
|
33
36
|
_regex[mode](str || "", mode === "replace" ? "" : undefined)
|
|
34
37
|
);
|
|
35
38
|
|
|
36
39
|
if (mode === "replace") {
|
|
40
|
+
_match = _currentMatch;
|
|
41
|
+
|
|
37
42
|
// A successfull replace must return a smaller string than the input
|
|
38
43
|
var _maxLength = str.length;
|
|
39
44
|
|
|
@@ -44,8 +49,36 @@ var loopRegexes = function(regexes, str, mode = "match") {
|
|
|
44
49
|
// A successfull split must return an array with at least two items
|
|
45
50
|
var _minLength = mode === "split" ? 1 : 0;
|
|
46
51
|
|
|
47
|
-
if ((
|
|
48
|
-
|
|
52
|
+
if ((_currentMatch || []).length > _minLength) {
|
|
53
|
+
if (highestPosition === true) {
|
|
54
|
+
// No previous match?
|
|
55
|
+
if (!_match) {
|
|
56
|
+
// Save match and continue looking for other matches
|
|
57
|
+
_match = _currentMatch;
|
|
58
|
+
} else {
|
|
59
|
+
var _higher;
|
|
60
|
+
|
|
61
|
+
if (mode === "match") {
|
|
62
|
+
_higher = _match.index > _currentMatch.index;
|
|
63
|
+
} else if (mode === "split") {
|
|
64
|
+
_higher = (
|
|
65
|
+
_match.length === _currentMatch.length &&
|
|
66
|
+
_match[0].length > _currentMatch[0].length
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Match higher positioned than previous one?
|
|
71
|
+
if (_higher) {
|
|
72
|
+
// Replace match and continue looking for other matches
|
|
73
|
+
_match = _currentMatch;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
// Save match and stop
|
|
78
|
+
_match = _currentMatch;
|
|
79
|
+
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
49
82
|
}
|
|
50
83
|
}
|
|
51
84
|
}
|