email-forward-parser 1.3.0 → 1.4.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/README.md +2 -1
- package/lib/parser.js +193 -30
- package/lib/utils.js +48 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ _👋 You use this library and you want to be listed there? [Contact us](https:/
|
|
|
24
24
|
## Features
|
|
25
25
|
|
|
26
26
|
This library is used at [Crisp](https://crisp.chat/) everyday with around 1 million inbound emails.
|
|
27
|
-
* Supported clients: Apple Mail, Gmail, Outlook Live / 365, Outlook 2013, Outlook 2019, New Outlook 2019, Yahoo Mail, Thunderbird, Missive, HubSpot
|
|
27
|
+
* Supported clients: Apple Mail, Gmail, Outlook Live / 365, Outlook 2013, Outlook 2019, New Outlook 2019, Yahoo Mail, Thunderbird, Missive, HubSpot, IONOS by 1 & 1
|
|
28
28
|
* Supported locales: Croatian, Czech, Danish, Dutch, English, French, Finnish, German, Hungarian, Italian, Japanese, Norwegian, Polish, Portuguese (Brazil), Portuguese (Portugal), Romanian, Russian, Slovak, Spanish, Swedish, Turkish, Ukrainian
|
|
29
29
|
|
|
30
30
|
## Usage
|
|
@@ -97,6 +97,7 @@ Yahoo Mail | Yes | Yes | No | Yes | Yes | Yes | The original information are all
|
|
|
97
97
|
Thunderbird | Yes | Yes | No | Yes | Yes | Yes | --
|
|
98
98
|
Missive | Yes | Yes | No | No | Yes | No | --
|
|
99
99
|
HubSpot | Yes | Yes | Yes | Yes | Yes | Yes | --
|
|
100
|
+
IONOS by 1 & 1 | ? | Yes | ? | ? | Yes | ? | --
|
|
100
101
|
|
|
101
102
|
## Contributing
|
|
102
103
|
|
package/lib/parser.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var RE2
|
|
5
|
-
var { loopRegexes, trimString } = require("./utils");
|
|
4
|
+
var RE2 = require("re2");
|
|
5
|
+
var { loopRegexes, reconciliateSplitMatch, trimString } = require("./utils");
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
var MAILBOXES_SEPARATORS = [
|
|
@@ -10,6 +10,15 @@ var MAILBOXES_SEPARATORS = [
|
|
|
10
10
|
";" // Outlook Live / 365, Yahoo Mail
|
|
11
11
|
];
|
|
12
12
|
|
|
13
|
+
var LINE_REGEXES = [
|
|
14
|
+
"separator",
|
|
15
|
+
"original_subject",
|
|
16
|
+
"original_subject_lax",
|
|
17
|
+
"original_to",
|
|
18
|
+
"original_reply_to",
|
|
19
|
+
"original_cc"
|
|
20
|
+
];
|
|
21
|
+
|
|
13
22
|
var REGEXES = {
|
|
14
23
|
quote_line_break : /^(>+)\s?$/gm, // Apple Mail, Missive
|
|
15
24
|
quote : /^(>+)\s?/gm, // Apple Mail
|
|
@@ -323,6 +332,7 @@ var REGEXES = {
|
|
|
323
332
|
/^\s*Date\s?:(.+)$/m, // Apple Mail (en, fr), Gmail (all locales), New Outlook 2019 (en, fr), Thunderbird (da, en, fr), Missive (en), HubSpot (en, fr)
|
|
324
333
|
/^\s*Datum\s?:(.+)$/m, // Apple Mail (cs, de, hr, nl, sv), New Outlook 2019 (cs, de, nl, sv), Thunderbird (cs, de, hr, nl, sv), HubSpot (de, nl, sv)
|
|
325
334
|
/^\s*Dato\s?:(.+)$/m, // Apple Mail (da, no), New Outlook 2019 (da), Thunderbird (no)
|
|
335
|
+
/^\s*Envoyé\s?:(.+)$/m, // New Outlook 2019 (fr)
|
|
326
336
|
/^\s*Fecha\s?:(.+)$/m, // Apple Mail (es), New Outlook 2019 (es), Thunderbird (es), HubSpot (es)
|
|
327
337
|
/^\s*Päivämäärä\s?:(.+)$/m, // Apple Mail (fi), New Outlook 2019 (fi), HubSpot (fi)
|
|
328
338
|
/^\s*Dátum\s?:(.+)$/m, // Apple Mail (hu, sk), New Outlook 2019 (sk), Thunderbird (hu, sk)
|
|
@@ -429,14 +439,36 @@ class Parser {
|
|
|
429
439
|
|
|
430
440
|
// First method: split via the separator (Apple Mail, Gmail, \
|
|
431
441
|
// Outlook Live / 365, Outlook 2019, Yahoo Mail, Thunderbird)
|
|
432
|
-
|
|
442
|
+
// Notice: use 'line' regex that will capture the line itself, as we may \
|
|
443
|
+
// need it to build the original email back (in case of nested emails)
|
|
444
|
+
var _match = loopRegexes(this.__regexes.separator_line, _body, "split");
|
|
445
|
+
|
|
446
|
+
if (_match && _match.length > 2) {
|
|
447
|
+
// The `split` operation creates a match with 3 substrings:
|
|
448
|
+
// * 0: anything before the line with the separator (i.e. the message)
|
|
449
|
+
// * 1: the line with the separator
|
|
450
|
+
// * 2: anything after the line with the separator (i.e. the body of \
|
|
451
|
+
// the original email)
|
|
452
|
+
// Notice: in case of nested emails, there may be several matches \
|
|
453
|
+
// against 'separator_line'. In that case, the `split` operation \
|
|
454
|
+
// creates a match with (n x 3) substrings. We need to reconciliate \
|
|
455
|
+
// those substrings.
|
|
456
|
+
var _email = reconciliateSplitMatch(
|
|
457
|
+
_match,
|
|
458
|
+
|
|
459
|
+
//-[min_substrings]
|
|
460
|
+
3,
|
|
461
|
+
|
|
462
|
+
//-[default_substrings]
|
|
463
|
+
// By default, attach anything after the line with the separator
|
|
464
|
+
[2]
|
|
465
|
+
);
|
|
433
466
|
|
|
434
|
-
if (_match && _match.length > 1) {
|
|
435
467
|
return {
|
|
436
468
|
body : _body,
|
|
437
469
|
|
|
438
470
|
message : trimString(_match[0]),
|
|
439
|
-
email : trimString(
|
|
471
|
+
email : trimString(_email)
|
|
440
472
|
};
|
|
441
473
|
}
|
|
442
474
|
|
|
@@ -446,13 +478,40 @@ class Parser {
|
|
|
446
478
|
// attempt it. The `forwarded` boolean gives the confirmation that the \
|
|
447
479
|
// email was indeed forwarded (detected from the Subject part)
|
|
448
480
|
if (forwarded === true) {
|
|
449
|
-
// Second method: split via the
|
|
481
|
+
// Second method: split via the From part (New Outlook 2019, \
|
|
450
482
|
// Outlook Live / 365)
|
|
451
483
|
_match = loopRegexes(this.__regexes.original_from, _body, "split");
|
|
452
484
|
|
|
453
485
|
if (_match && _match.length > 3) {
|
|
454
|
-
// The
|
|
455
|
-
|
|
486
|
+
// The `split` operation creates a match with 4 substrings:
|
|
487
|
+
// * 0: anything before the line with the From part (i.e. the \
|
|
488
|
+
// message before the original email)
|
|
489
|
+
// * 1: the line with the From part (in the original email)
|
|
490
|
+
// * 2: the From part itself
|
|
491
|
+
// * 3: anything after the line with the From part (i.e. \
|
|
492
|
+
// the rest of the original email)
|
|
493
|
+
// Notice: in case of nested emails, there may be several matches \
|
|
494
|
+
// against 'original_from'. In that case, the `split` operation \
|
|
495
|
+
// creates a match with (n x 4) substrings. We need to reconciliate \
|
|
496
|
+
// those substrings.
|
|
497
|
+
var _email = reconciliateSplitMatch(
|
|
498
|
+
_match,
|
|
499
|
+
|
|
500
|
+
//-[min_substrings]
|
|
501
|
+
4,
|
|
502
|
+
|
|
503
|
+
//-[default_substrings]
|
|
504
|
+
// By default, attach the line that contains the From part back to \
|
|
505
|
+
// the rest of the original email (exclude the From part itself)
|
|
506
|
+
[1, 3],
|
|
507
|
+
|
|
508
|
+
//-[fn_exlude]
|
|
509
|
+
// When reconciliating other substrings, we want to exclude the From \
|
|
510
|
+
// part itself
|
|
511
|
+
function(i) {
|
|
512
|
+
return (i % 3 === 2);
|
|
513
|
+
}
|
|
514
|
+
);
|
|
456
515
|
|
|
457
516
|
return {
|
|
458
517
|
body : _body,
|
|
@@ -507,21 +566,57 @@ class Parser {
|
|
|
507
566
|
*/
|
|
508
567
|
__initRegexes() {
|
|
509
568
|
for (var _key in REGEXES) {
|
|
510
|
-
var
|
|
569
|
+
var _key_line = `${_key}_line`;
|
|
570
|
+
var _entry = REGEXES[_key];
|
|
511
571
|
|
|
512
572
|
if (Array.isArray(_entry)) {
|
|
513
|
-
this.__regexes[_key]
|
|
573
|
+
this.__regexes[_key] = [];
|
|
574
|
+
this.__regexes[_key_line] = [];
|
|
514
575
|
|
|
515
576
|
for (var _i = 0; _i < _entry.length; _i++) {
|
|
516
|
-
|
|
577
|
+
var _regex_original = new RE2(_entry[_i]);
|
|
578
|
+
|
|
579
|
+
// Build 'line' alternative?
|
|
580
|
+
if (LINE_REGEXES.includes(_key)) {
|
|
581
|
+
var _regex_line = this.__buildLineRegex(_regex_original);
|
|
582
|
+
|
|
583
|
+
this.__regexes[_key_line].push(_regex_line);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
this.__regexes[_key].push(_regex_original);
|
|
517
587
|
}
|
|
518
588
|
} else {
|
|
519
|
-
|
|
589
|
+
var _regex_original = new RE2(_entry);
|
|
590
|
+
|
|
591
|
+
// Build 'line' alternative?
|
|
592
|
+
if (LINE_REGEXES.includes(_key)) {
|
|
593
|
+
var _regex_line = this.__buildLineRegex(_regex_original);
|
|
594
|
+
|
|
595
|
+
this.__regexes[_key_line] = _regex_line;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
this.__regexes[_key] = _regex_original;
|
|
520
599
|
}
|
|
521
600
|
}
|
|
522
601
|
}
|
|
523
602
|
|
|
524
603
|
|
|
604
|
+
/**
|
|
605
|
+
* Builds 'line' alternative regex
|
|
606
|
+
* @private
|
|
607
|
+
* @param {object} regex
|
|
608
|
+
* @return {object} 'Line' regex
|
|
609
|
+
*/
|
|
610
|
+
__buildLineRegex(regex) {
|
|
611
|
+
// A 'line' regex will capture not only inner groups, but also the line \
|
|
612
|
+
// itself
|
|
613
|
+
var _source = `(${regex.source})`;
|
|
614
|
+
var _flags = regex.flags;
|
|
615
|
+
|
|
616
|
+
return new RE2(_source, _flags);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
|
|
525
620
|
/**
|
|
526
621
|
* Parses the body part
|
|
527
622
|
* @private
|
|
@@ -531,13 +626,17 @@ class Parser {
|
|
|
531
626
|
__parseOriginalBody(text) {
|
|
532
627
|
var _match = null;
|
|
533
628
|
|
|
534
|
-
// First method: extract the text after the
|
|
535
|
-
// (
|
|
629
|
+
// First method: extract the text after the Subject part \
|
|
630
|
+
// (Outlook Live / 365) or after the Cc, To or Reply-To part \
|
|
631
|
+
// (Apple Mail, Gmail). A new line must be present.
|
|
632
|
+
// Notice: use 'line' regexes that will capture not only the Subject, Cc, \
|
|
633
|
+
// To or Reply-To part, but also the line itself, as we may need it \
|
|
634
|
+
// to build the original body back (in case of nested emails)
|
|
536
635
|
var _regexes = [
|
|
537
|
-
this.__regexes.
|
|
538
|
-
this.__regexes.
|
|
539
|
-
this.__regexes.
|
|
540
|
-
this.__regexes.
|
|
636
|
+
this.__regexes.original_subject_line,
|
|
637
|
+
this.__regexes.original_cc_line,
|
|
638
|
+
this.__regexes.original_to_line,
|
|
639
|
+
this.__regexes.original_reply_to_line
|
|
541
640
|
];
|
|
542
641
|
|
|
543
642
|
for (var _i = 0; _i < _regexes.length; _i++) {
|
|
@@ -545,25 +644,89 @@ class Parser {
|
|
|
545
644
|
|
|
546
645
|
// A new line must be present between the Cc, To, Reply-To or Subject \
|
|
547
646
|
// part and the actual body
|
|
548
|
-
if (_match && _match.length
|
|
549
|
-
|
|
647
|
+
if (_match && _match.length > 2 && _match[3].startsWith("\n\n")) {
|
|
648
|
+
// The `split` operation creates a match with 4 substrings:
|
|
649
|
+
// * 0: anything before the line with the Subject, Cc, To or Reply-To \
|
|
650
|
+
// part
|
|
651
|
+
// * 1: the line with the Subject, Cc, To or Reply-To part
|
|
652
|
+
// * 2: the Subject, Cc, To or Reply-To part itself
|
|
653
|
+
// * 3: anything after the line with the Subject, Cc, To or Reply-To \
|
|
654
|
+
// part (i.e. the body of the original email)
|
|
655
|
+
// Notice: in case of nested emails, there may be several matches \
|
|
656
|
+
// against 'original_subject_line', 'original_cc_line', \
|
|
657
|
+
// 'original_to_line' or 'original_reply_to_line'. In that case, the \
|
|
658
|
+
// `split` operation creates a match with (n x 4) substrings. We \
|
|
659
|
+
// need to reconciliate those substrings.
|
|
660
|
+
var _body = reconciliateSplitMatch(
|
|
661
|
+
_match,
|
|
662
|
+
|
|
663
|
+
//-[min_substrings]
|
|
664
|
+
4,
|
|
665
|
+
|
|
666
|
+
//-[default_substrings]
|
|
667
|
+
// By default, attach anything after the line with the Subject, Cc, \
|
|
668
|
+
// To or Reply-To part
|
|
669
|
+
[3],
|
|
670
|
+
|
|
671
|
+
//-[fn_exlude]
|
|
672
|
+
// When reconciliating other substrings, we want to exclude the \
|
|
673
|
+
// Subject, Cc, To or Reply-To part itself
|
|
674
|
+
function(i) {
|
|
675
|
+
return (i % 3 === 2);
|
|
676
|
+
}
|
|
677
|
+
);
|
|
678
|
+
|
|
679
|
+
return trimString(_body);
|
|
550
680
|
}
|
|
551
681
|
}
|
|
552
682
|
|
|
553
683
|
// Second method: extract the text after the Subject part \
|
|
554
|
-
// (New Outlook 2019, Yahoo Mail)
|
|
684
|
+
// (New Outlook 2019, Yahoo Mail). No new line must be present.
|
|
685
|
+
// Notice: use 'line' regexes that will capture not only the Subject part, \
|
|
686
|
+
// but also the line itself, as we may need it to build the original \
|
|
687
|
+
// body back (in case of nested emails)
|
|
555
688
|
_match = loopRegexes(
|
|
556
|
-
|
|
557
|
-
this.__regexes.
|
|
689
|
+
[].concat(
|
|
690
|
+
this.__regexes.original_subject_line,
|
|
691
|
+
this.__regexes.original_subject_lax_line
|
|
558
692
|
),
|
|
559
693
|
|
|
560
694
|
text,
|
|
561
695
|
"split"
|
|
562
696
|
);
|
|
563
697
|
|
|
564
|
-
//
|
|
565
|
-
|
|
566
|
-
|
|
698
|
+
// Do not bother checking for new line between the Subject part and the \
|
|
699
|
+
// actual body (specificity of New Outlook 2019 and Yahoo Mail)
|
|
700
|
+
if (_match && _match.length > 3) {
|
|
701
|
+
// The `split` operation creates a match with 4 substrings:
|
|
702
|
+
// * 0: anything before the line with the Subject part
|
|
703
|
+
// * 1: the line with the Subject part (in the original email)
|
|
704
|
+
// * 2: the Subject part itself
|
|
705
|
+
// * 3: anything after the line with the Subject part (i.e. the body of \
|
|
706
|
+
// the original email)
|
|
707
|
+
// Notice: in case of nested emails, there may be several matches \
|
|
708
|
+
// against 'original_subject_line' and 'original_subject_lax_line'. In \
|
|
709
|
+
// that case, the `split` operation creates a match with (n x 4) \
|
|
710
|
+
// substrings. We need to reconciliate those substrings.
|
|
711
|
+
var _body = reconciliateSplitMatch(
|
|
712
|
+
_match,
|
|
713
|
+
|
|
714
|
+
//-[min_substrings]
|
|
715
|
+
4,
|
|
716
|
+
|
|
717
|
+
//-[default_substrings]
|
|
718
|
+
// By default, attach anything after the line with the Subject part
|
|
719
|
+
[3],
|
|
720
|
+
|
|
721
|
+
//-[fn_exlude]
|
|
722
|
+
// When reconciliating other substrings, we want to exclude the \
|
|
723
|
+
// Subject part itself
|
|
724
|
+
function(i) {
|
|
725
|
+
return (i % 3 === 2);
|
|
726
|
+
}
|
|
727
|
+
);
|
|
728
|
+
|
|
729
|
+
return trimString(_body);
|
|
567
730
|
}
|
|
568
731
|
|
|
569
732
|
// Third method: return the raw text, as there is no original information \
|
|
@@ -646,7 +809,7 @@ class Parser {
|
|
|
646
809
|
this.__regexes.original_to,
|
|
647
810
|
|
|
648
811
|
text,
|
|
649
|
-
true //-
|
|
812
|
+
true //-[force_array]
|
|
650
813
|
);
|
|
651
814
|
|
|
652
815
|
// Recipient(s) found?
|
|
@@ -682,7 +845,7 @@ class Parser {
|
|
|
682
845
|
this.__regexes.original_to_lax,
|
|
683
846
|
|
|
684
847
|
_cleanText,
|
|
685
|
-
true //-
|
|
848
|
+
true //-[force_array]
|
|
686
849
|
);
|
|
687
850
|
}
|
|
688
851
|
|
|
@@ -700,7 +863,7 @@ class Parser {
|
|
|
700
863
|
this.__regexes.original_cc,
|
|
701
864
|
|
|
702
865
|
text,
|
|
703
|
-
true //-
|
|
866
|
+
true //-[force_array]
|
|
704
867
|
);
|
|
705
868
|
|
|
706
869
|
// Recipient(s) found?
|
|
@@ -729,7 +892,7 @@ class Parser {
|
|
|
729
892
|
this.__regexes.original_cc_lax,
|
|
730
893
|
|
|
731
894
|
_cleanText,
|
|
732
|
-
true //-
|
|
895
|
+
true //-[force_array]
|
|
733
896
|
);
|
|
734
897
|
}
|
|
735
898
|
|
package/lib/utils.js
CHANGED
|
@@ -61,13 +61,10 @@ var loopRegexes = function(
|
|
|
61
61
|
if (mode === "match") {
|
|
62
62
|
_higher = _match.index > _currentMatch.index;
|
|
63
63
|
} else if (mode === "split") {
|
|
64
|
-
_higher =
|
|
65
|
-
_match.length === _currentMatch.length &&
|
|
66
|
-
_match[0].length > _currentMatch[0].length
|
|
67
|
-
);
|
|
64
|
+
_higher = _match[0].length > _currentMatch[0].length;
|
|
68
65
|
}
|
|
69
66
|
|
|
70
|
-
// Match higher
|
|
67
|
+
// Match positioned higher than previous one?
|
|
71
68
|
if (_higher) {
|
|
72
69
|
// Replace match and continue looking for other matches
|
|
73
70
|
_match = _currentMatch;
|
|
@@ -87,6 +84,46 @@ var loopRegexes = function(
|
|
|
87
84
|
}
|
|
88
85
|
|
|
89
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Reconciliates match substrings after a "split" operation
|
|
89
|
+
* @public
|
|
90
|
+
* @param {object} match
|
|
91
|
+
* @param {number} min_substrings
|
|
92
|
+
* @param {object} default_substrings
|
|
93
|
+
* @param {function} fn_exlude
|
|
94
|
+
* @return {string} The reconciliated string
|
|
95
|
+
*/
|
|
96
|
+
var reconciliateSplitMatch = function(
|
|
97
|
+
match, min_substrings, default_substrings, fn_exlude = null
|
|
98
|
+
) {
|
|
99
|
+
var _str = "";
|
|
100
|
+
|
|
101
|
+
// Add default substrings
|
|
102
|
+
for (var _i = 0; _i < default_substrings.length; _i++) {
|
|
103
|
+
_str += match[default_substrings[_i]];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// More substrings than expected?
|
|
107
|
+
if (match.length > min_substrings) {
|
|
108
|
+
// Reconciliate them
|
|
109
|
+
for (var _i = min_substrings; _i < match.length; _i++) {
|
|
110
|
+
var _exclude = false;
|
|
111
|
+
|
|
112
|
+
// Exclude the substring?
|
|
113
|
+
if (typeof fn_exlude === "function") {
|
|
114
|
+
_exclude = fn_exlude(_i);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (_exclude === false) {
|
|
118
|
+
_str += match[_i];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return _str;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
90
127
|
/**
|
|
91
128
|
* Trims a string
|
|
92
129
|
* @public
|
|
@@ -102,10 +139,12 @@ var trimString = function(str) {
|
|
|
102
139
|
* EXPORTS
|
|
103
140
|
***************************************************************************/
|
|
104
141
|
|
|
105
|
-
exports.loopRegexes
|
|
106
|
-
exports.
|
|
142
|
+
exports.loopRegexes = loopRegexes;
|
|
143
|
+
exports.reconciliateSplitMatch = reconciliateSplitMatch;
|
|
144
|
+
exports.trimString = trimString;
|
|
107
145
|
|
|
108
146
|
exports.default = {
|
|
109
|
-
loopRegexes
|
|
110
|
-
|
|
147
|
+
loopRegexes : loopRegexes,
|
|
148
|
+
reconciliateSplitMatch : reconciliateSplitMatch,
|
|
149
|
+
trimString : trimString
|
|
111
150
|
};
|