email-forward-parser 1.2.2 → 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 +232 -64
- 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,13 +10,23 @@ 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
|
-
quote_line_break : /^(>+)\s?$/gm, // Apple Mail
|
|
23
|
+
quote_line_break : /^(>+)\s?$/gm, // Apple Mail, Missive
|
|
15
24
|
quote : /^(>+)\s?/gm, // Apple Mail
|
|
16
25
|
four_spaces : /^(\ {4})\s?/gm, // Outlook 2019
|
|
17
26
|
carriage_return : /\r\n/gm, // Outlook 2019
|
|
18
|
-
byte_order_mark :
|
|
19
|
-
|
|
27
|
+
byte_order_mark : /\uFEFF/gm, // Outlook 2019
|
|
28
|
+
trailing_non_breaking_space : /\u00A0$/gm, // IONOS by 1 & 1
|
|
29
|
+
non_breaking_space : /\u00A0/gm, // HubSpot
|
|
20
30
|
|
|
21
31
|
subject : [
|
|
22
32
|
/^Fw:(.*)/m, // Outlook Live / 365 (cs, en, hr, hu, sk), Yahoo Mail (all locales)
|
|
@@ -79,37 +89,38 @@ var REGEXES = {
|
|
|
79
89
|
/^\s?.+\s?používateľ\s?.+\s*\([\[|<].+[\]|>]\)\s?napísal\s?:/m, // Outlook 2019 (sk)
|
|
80
90
|
/^\s?Den\s?.+\s?skrev\s?\".+\"\s*[\[|<].+[\]|>]\s?följande\s?:/m, // Outlook 2019 (sv)
|
|
81
91
|
/^\s?\".+\"\s*[\[|<].+[\]|>]\,\s?.+\s?tarihinde şunu yazdı\s?:/m, // Outlook 2019 (tr)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
92
|
+
/^\s*-{5,8} Přeposlaná zpráva -{5,8}\s*/m, // Yahoo Mail (cs), Thunderbird (cs)
|
|
93
|
+
/^\s*-{5,8} Videresendt meddelelse -{5,8}\s*/m, // Yahoo Mail (da), Thunderbird (da)
|
|
94
|
+
/^\s*-{5,10} Weitergeleitete Nachricht -{5,10}\s*/m, // Yahoo Mail (de), Thunderbird (de), HubSpot (de)
|
|
95
|
+
/^\s*-{5,8} Forwarded Message -{5,8}\s*/m, // Yahoo Mail (en), Thunderbird (en)
|
|
96
|
+
/^\s*-{5,10} Mensaje reenviado -{5,10}\s*/m, // Yahoo Mail (es), Thunderbird (es), HubSpot (es)
|
|
97
|
+
/^\s*-{5,10} Edelleenlähetetty viesti -{5,10}\s*/m, // Yahoo Mail (fi), HubSpot (fi)
|
|
98
|
+
/^\s*-{5} Message transmis -{5}\s*/m, // Yahoo Mail (fr)
|
|
99
|
+
/^\s*-{5,8} Továbbított üzenet -{5,8}\s*/m, // Yahoo Mail (hu), Thunderbird (hu)
|
|
100
|
+
/^\s*-{5,10} Messaggio inoltrato -{5,10}\s*/m, // Yahoo Mail (it), HubSpot (it)
|
|
101
|
+
/^\s*-{5,10} Doorgestuurd bericht -{5,10}\s*/m, // Yahoo Mail (nl), Thunderbird (nl), HubSpot (nl)
|
|
102
|
+
/^\s*-{5,8} Videresendt melding -{5,8}\s*/m, // Yahoo Mail (no), Thunderbird (no)
|
|
103
|
+
/^\s*-{5} Przekazana wiadomość -{5}\s*/m, // Yahoo Mail (pl)
|
|
104
|
+
/^\s*-{5,8} Mensagem reencaminhada -{5,8}\s*/m, // Yahoo Mail (pt), Thunderbird (pt)
|
|
105
|
+
/^\s*-{5,10} Mensagem encaminhada -{5,10}\s*/m, // Yahoo Mail (pt-br), Thunderbird (pt-br), HubSpot (pt-br)
|
|
106
|
+
/^\s*-{5,8} Mesaj redirecționat -{5,8}\s*/m, // Yahoo Mail (ro)
|
|
107
|
+
/^\s*-{5} Пересылаемое сообщение -{5}\s*/m, // Yahoo Mail (ru)
|
|
108
|
+
/^\s*-{5} Preposlaná správa -{5}\s*/m, // Yahoo Mail (sk)
|
|
109
|
+
/^\s*-{5,10} Vidarebefordrat meddelande -{5,10}\s*/m, // Yahoo Mail (sv), Thunderbird (sv), HubSpot (sv)
|
|
110
|
+
/^\s*-{5} İletilmiş Mesaj -{5}\s*/m, // Yahoo Mail (tr)
|
|
111
|
+
/^\s*-{5} Перенаправлене повідомлення -{5}\s*/m, // Yahoo Mail (uk)
|
|
112
|
+
/^\s*-{8} Välitetty viesti \/ Fwd.Msg -{8}\s*/m, // Thunderbird (fi)
|
|
113
|
+
/^\s*-{8,10} Message transféré -{8,10}\s*/m, // Thunderbird (fr), HubSpot (fr)
|
|
114
|
+
/^\s*-{8} Proslijeđena poruka -{8}\s*/m, // Thunderbird (hr)
|
|
115
|
+
/^\s*-{8} Messaggio Inoltrato -{8}\s*/m, // Thunderbird (it)
|
|
116
|
+
/^\s*-{3} Treść przekazanej wiadomości -{3}\s*/m, // Thunderbird (pl)
|
|
117
|
+
/^\s*-{8} Перенаправленное сообщение -{8}\s*/m, // Thunderbird (ru)
|
|
118
|
+
/^\s*-{8} Preposlaná správa --- Forwarded Message -{8}\s*/m, // Thunderbird (sk)
|
|
119
|
+
/^\s*-{8} İletilen İleti -{8}\s*/m, // Thunderbird (tr)
|
|
120
|
+
/^\s*-{8} Переслане повідомлення -{8}\s*/m, // Thunderbird (uk)
|
|
121
|
+
/^\s*-{9,10} メッセージを転送 -{9,10}\s*/m, // HubSpot (ja)
|
|
122
|
+
/^\s*-{9,10} Wiadomość przesłana dalej -{9,10}\s*/m, // HubSpot (pl)
|
|
123
|
+
/^>?\s*-{10} Original Message -{10}\s*/m // IONOS by 1 & 1 (en)
|
|
113
124
|
],
|
|
114
125
|
|
|
115
126
|
separator_with_information : [
|
|
@@ -321,6 +332,7 @@ var REGEXES = {
|
|
|
321
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)
|
|
322
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)
|
|
323
334
|
/^\s*Dato\s?:(.+)$/m, // Apple Mail (da, no), New Outlook 2019 (da), Thunderbird (no)
|
|
335
|
+
/^\s*Envoyé\s?:(.+)$/m, // New Outlook 2019 (fr)
|
|
324
336
|
/^\s*Fecha\s?:(.+)$/m, // Apple Mail (es), New Outlook 2019 (es), Thunderbird (es), HubSpot (es)
|
|
325
337
|
/^\s*Päivämäärä\s?:(.+)$/m, // Apple Mail (fi), New Outlook 2019 (fi), HubSpot (fi)
|
|
326
338
|
/^\s*Dátum\s?:(.+)$/m, // Apple Mail (hu, sk), New Outlook 2019 (sk), Thunderbird (hu, sk)
|
|
@@ -419,19 +431,44 @@ class Parser {
|
|
|
419
431
|
// Remove Byte Order Mark
|
|
420
432
|
_body = _body.replace(this.__regexes.byte_order_mark, "");
|
|
421
433
|
|
|
434
|
+
// Remove trailing Non-breaking space
|
|
435
|
+
_body = _body.replace(this.__regexes.trailing_non_breaking_space, "");
|
|
436
|
+
|
|
422
437
|
// Replace Non-breaking space with regular space
|
|
423
438
|
_body = _body.replace(this.__regexes.non_breaking_space, " ");
|
|
424
439
|
|
|
425
440
|
// First method: split via the separator (Apple Mail, Gmail, \
|
|
426
441
|
// Outlook Live / 365, Outlook 2019, Yahoo Mail, Thunderbird)
|
|
427
|
-
|
|
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
|
+
);
|
|
428
466
|
|
|
429
|
-
if (_match && _match.length > 1) {
|
|
430
467
|
return {
|
|
431
468
|
body : _body,
|
|
432
469
|
|
|
433
470
|
message : trimString(_match[0]),
|
|
434
|
-
email : trimString(
|
|
471
|
+
email : trimString(_email)
|
|
435
472
|
};
|
|
436
473
|
}
|
|
437
474
|
|
|
@@ -441,13 +478,40 @@ class Parser {
|
|
|
441
478
|
// attempt it. The `forwarded` boolean gives the confirmation that the \
|
|
442
479
|
// email was indeed forwarded (detected from the Subject part)
|
|
443
480
|
if (forwarded === true) {
|
|
444
|
-
// Second method: split via the
|
|
481
|
+
// Second method: split via the From part (New Outlook 2019, \
|
|
445
482
|
// Outlook Live / 365)
|
|
446
483
|
_match = loopRegexes(this.__regexes.original_from, _body, "split");
|
|
447
484
|
|
|
448
485
|
if (_match && _match.length > 3) {
|
|
449
|
-
// The
|
|
450
|
-
|
|
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
|
+
);
|
|
451
515
|
|
|
452
516
|
return {
|
|
453
517
|
body : _body,
|
|
@@ -502,21 +566,57 @@ class Parser {
|
|
|
502
566
|
*/
|
|
503
567
|
__initRegexes() {
|
|
504
568
|
for (var _key in REGEXES) {
|
|
505
|
-
var
|
|
569
|
+
var _key_line = `${_key}_line`;
|
|
570
|
+
var _entry = REGEXES[_key];
|
|
506
571
|
|
|
507
572
|
if (Array.isArray(_entry)) {
|
|
508
|
-
this.__regexes[_key]
|
|
573
|
+
this.__regexes[_key] = [];
|
|
574
|
+
this.__regexes[_key_line] = [];
|
|
509
575
|
|
|
510
576
|
for (var _i = 0; _i < _entry.length; _i++) {
|
|
511
|
-
|
|
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);
|
|
512
587
|
}
|
|
513
588
|
} else {
|
|
514
|
-
|
|
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;
|
|
515
599
|
}
|
|
516
600
|
}
|
|
517
601
|
}
|
|
518
602
|
|
|
519
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
|
+
|
|
520
620
|
/**
|
|
521
621
|
* Parses the body part
|
|
522
622
|
* @private
|
|
@@ -526,13 +626,17 @@ class Parser {
|
|
|
526
626
|
__parseOriginalBody(text) {
|
|
527
627
|
var _match = null;
|
|
528
628
|
|
|
529
|
-
// First method: extract the text after the
|
|
530
|
-
// (
|
|
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)
|
|
531
635
|
var _regexes = [
|
|
532
|
-
this.__regexes.
|
|
533
|
-
this.__regexes.
|
|
534
|
-
this.__regexes.
|
|
535
|
-
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
|
|
536
640
|
];
|
|
537
641
|
|
|
538
642
|
for (var _i = 0; _i < _regexes.length; _i++) {
|
|
@@ -540,25 +644,89 @@ class Parser {
|
|
|
540
644
|
|
|
541
645
|
// A new line must be present between the Cc, To, Reply-To or Subject \
|
|
542
646
|
// part and the actual body
|
|
543
|
-
if (_match && _match.length
|
|
544
|
-
|
|
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);
|
|
545
680
|
}
|
|
546
681
|
}
|
|
547
682
|
|
|
548
683
|
// Second method: extract the text after the Subject part \
|
|
549
|
-
// (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)
|
|
550
688
|
_match = loopRegexes(
|
|
551
|
-
|
|
552
|
-
this.__regexes.
|
|
689
|
+
[].concat(
|
|
690
|
+
this.__regexes.original_subject_line,
|
|
691
|
+
this.__regexes.original_subject_lax_line
|
|
553
692
|
),
|
|
554
693
|
|
|
555
694
|
text,
|
|
556
695
|
"split"
|
|
557
696
|
);
|
|
558
697
|
|
|
559
|
-
//
|
|
560
|
-
|
|
561
|
-
|
|
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);
|
|
562
730
|
}
|
|
563
731
|
|
|
564
732
|
// Third method: return the raw text, as there is no original information \
|
|
@@ -641,7 +809,7 @@ class Parser {
|
|
|
641
809
|
this.__regexes.original_to,
|
|
642
810
|
|
|
643
811
|
text,
|
|
644
|
-
true //-
|
|
812
|
+
true //-[force_array]
|
|
645
813
|
);
|
|
646
814
|
|
|
647
815
|
// Recipient(s) found?
|
|
@@ -677,7 +845,7 @@ class Parser {
|
|
|
677
845
|
this.__regexes.original_to_lax,
|
|
678
846
|
|
|
679
847
|
_cleanText,
|
|
680
|
-
true //-
|
|
848
|
+
true //-[force_array]
|
|
681
849
|
);
|
|
682
850
|
}
|
|
683
851
|
|
|
@@ -695,7 +863,7 @@ class Parser {
|
|
|
695
863
|
this.__regexes.original_cc,
|
|
696
864
|
|
|
697
865
|
text,
|
|
698
|
-
true //-
|
|
866
|
+
true //-[force_array]
|
|
699
867
|
);
|
|
700
868
|
|
|
701
869
|
// Recipient(s) found?
|
|
@@ -724,7 +892,7 @@ class Parser {
|
|
|
724
892
|
this.__regexes.original_cc_lax,
|
|
725
893
|
|
|
726
894
|
_cleanText,
|
|
727
|
-
true //-
|
|
895
|
+
true //-[force_array]
|
|
728
896
|
);
|
|
729
897
|
}
|
|
730
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
|
};
|