-
Notifications
You must be signed in to change notification settings - Fork 2
/
email_reply_parser.go
654 lines (559 loc) · 15.4 KB
/
email_reply_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
package email_reply_parser //nolint:stylecheck,golint
import (
"regexp"
"strings"
"unicode"
)
type Line struct {
Index int
Content string
ContentStripped string
IsQuoted bool
IsEmpty bool
PossibleSignatureLine bool
}
const (
space = ` `
enter = "\n"
)
var dot = "."
func Parse(plainMail string) string {
lines := plainMailToLines(plainMail)
var plainLines []string
if isQuoteOnTop(plainMail) {
plainLines = getPlainLinesWithQuotedReplyOnTop(lines)
} else {
plainLines = getPlainLinesWithQuotedReplyOnBottom(lines)
}
return removeWhiteSpaceBeforeAndAfter(
strings.Join(plainLines, enter),
)
}
func plainMailToLines(plainMail string) []*Line {
baseLines := strings.Split(plainMail, enter)
// first save lines with some information we will use later on while parsing
lines := make([]*Line, len(baseLines))
for i, baseLine := range baseLines {
contentStripped := removeWhitespace(baseLine)
withoutMarkdown := removeMarkdown(contentStripped)
lines[i] = &Line{
Index: i,
Content: baseLine,
ContentStripped: withoutMarkdown,
IsEmpty: isWhitespace(contentStripped),
IsQuoted: strings.HasPrefix(baseLine, ">"),
PossibleSignatureLine: isPossibleSignatureLine(withoutMarkdown),
}
}
return lines
}
func getPlainLinesWithQuotedReplyOnBottom(lines []*Line) []string {
//nolint:prealloc
var finalLines []string
for i, line := range lines {
if isSignatureStart(i, line, lines) {
break
}
multilineQuoteReply, _ := detectQuotedEmailStart(i, line, lines)
if multilineQuoteReply {
break
}
finalLines = append(finalLines, line.Content)
}
return finalLines
}
func getPlainLinesWithQuotedReplyOnTop(lines []*Line) []string {
//nolint:prealloc
var finalLines []string
var quotedStartSeen bool
var normalLineSeen bool
var skipNextLine bool
for i, line := range lines {
multiLine, singleLine := detectQuotedEmailStart(i, line, lines)
// start of quoted text can be ignored
if multiLine {
quotedStartSeen = true
if !singleLine {
skipNextLine = true
}
continue
}
// skip this line because this is still the quote start
if skipNextLine {
skipNextLine = false
continue
}
if quotedStartSeen &&
!line.IsQuoted &&
!line.IsEmpty {
normalLineSeen = true
}
if normalLineSeen && quotedStartSeen {
if isSignatureStart(i, line, lines) {
break
}
finalLines = append(finalLines, line.Content)
}
}
return finalLines
}
func isQuoteOnTop(plainMail string) bool {
plainMailStrippedWhiteSpace := removeWhitespace(plainMail)
lines := plainMailToLines(plainMailStrippedWhiteSpace)
for i, line := range lines {
isQuoteStarted, _ := detectQuotedEmailStart(i, line, lines)
if i == 0 && isQuoteStarted {
return true
}
}
return false
}
func lineBeforeAndAfter(lineIndex int, lines []*Line) (*Line, *Line) {
var before *Line
var after *Line
if lineIndex > 0 {
before = lines[lineIndex-1]
}
isLast := lineIndex != len(lines)-1
if isLast {
after = lines[lineIndex+1]
}
return before, after
}
func isSignatureStart(lineIndex int, line *Line, lines []*Line) bool {
// first line is probably not the signature
if lineIndex == 0 {
return false
}
lowerLine := strings.ToLower(line.ContentStripped)
// --
// my name
if isValidSignatureFormat(lowerLine) {
return true
}
// e.g. with best regards,
if detectGreetings(strings.ToLower(line.ContentStripped)) {
return true
}
// smart system to detect signature + disclaimers like
// Karen The Green
// Graphic Designer
// Office
// Tel: +44423423423423
// Fax: +44234234234234
// karen@webby.com
// Street 2, City, Zeeland, 4694EG, NL
// www.thing.com
//
// Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
if detectSignature(lineIndex, line, lines) {
return true
}
// Sent from .... iphone/blackberry/galaxy etc
if isSentFrom(lowerLine) {
return true
}
return false
}
func detectQuotedEmailStart(lineIndex int, line *Line, lines []*Line) (bool, bool) {
// Detect by quoted reply headers
// sometimes there are line breaks within the quoted reply header
_, after := lineBeforeAndAfter(lineIndex, lines)
lineWithBreaksInOneLine := strings.ToLower(removeEnters(joinLineContents("", line, after)))
multi := isQuotedEmailStart(lineWithBreaksInOneLine)
single := isQuotedEmailStart(strings.ToLower(line.ContentStripped)) && after != nil && containsQuotedEmail(after.ContentStripped)
if after != nil && containsQuotedEmail(after.ContentStripped) {
single = false
}
// On .. wrote ..
return multi, single
}
func isValidSignatureFormat(fullLine string) bool {
return strings.TrimSpace(fullLine) == "--"
}
func detectSignature(lineIndex int, line *Line, lines []*Line) bool {
// signatures mostly contains of numbers and short kind of labels with numbers after it
// so we try to detect these kind of lines
possible := isPossibleSignatureLine(line.ContentStripped)
if possible {
var matches int
var lastMatchLineIndex int
linesTillQuotedText := getLinesTillQuotedText(lineIndex, lines)
for i, signatureLine := range linesTillQuotedText {
if isPossibleSignatureLine(signatureLine.ContentStripped) {
lastMatchLineIndex = i
matches++
}
}
// disclaimer
possibleDisclaimer := getLinesTillQuotedText(lineIndex+lastMatchLineIndex+1, lines)
filledDisclaimerLines := countLinesFilled(possibleDisclaimer)
isDisclaimer := filledDisclaimerLines < 6
filledLines := countLinesFilled(linesTillQuotedText)
if isDisclaimer {
filledLines -= filledDisclaimerLines
}
percentMatched := (float64(matches) * 100) / float64(filledLines)
return percentMatched > 70
}
return false
}
func countLinesFilled(lines []*Line) int {
var count int
for _, line := range lines {
if !line.IsEmpty {
count++
}
}
return count
}
func detectGreetings(line string) bool {
// greetings but not
if startWithOneOfButDoesNotContainMuchAfter(line, greetings, 2) {
return true
}
// without first word e.g. best regards or
// -->met<-- vriendelijke groeten
if startWithOneOfButDoesNotContainMuchAfter(removeFirstWord(line), greetings, 2) {
return true
}
return false
}
func removeFirstWord(sentence string) string {
split := strings.Split(sentence, space)
var a []string
for i, w := range split {
if i > 0 {
a = append(a, w)
}
}
return strings.Join(a, space)
}
func getLinesTillQuotedText(lineIndex int, lines []*Line) []*Line {
var a []*Line
for i, line := range lines {
if i > lineIndex {
multilineQuoteEmailStart, _ := detectQuotedEmailStart(i, line, lines)
if multilineQuoteEmailStart {
break
}
a = append(a, line)
}
}
return a
}
func isPossibleSignatureLine(sentence string) bool {
if isName(sentence) {
return true
}
withoutStripe := strings.TrimPrefix(sentence, "-")
if isName(strings.TrimSpace(withoutStripe)) {
return true
}
noSpaceBetweenNumbers := removeSpacesBetweenNumbers(sentence)
if isLogo(sentence) {
return true
}
if areStripes(sentence) {
return true
}
if isLabelWithValue(noSpaceBetweenNumbers) {
return true
}
if isNumberSignature(noSpaceBetweenNumbers) {
return true
}
if isEmailSignature(sentence) {
return true
}
if isWebsiteSignature(sentence) {
return true
}
return false
}
func isLogo(sentence string) bool {
return strings.HasPrefix(sentence, "[image") && strings.HasSuffix(sentence, "]")
}
func removeSpacesBetweenNumbers(sentence string) string {
var newSentence string
split := strings.Split(sentence, space)
for i, word := range split {
if i > 0 {
prevWord := split[i-1]
if !isNumberWord(prevWord) {
newSentence += space
}
}
newSentence += word
}
return newSentence
}
func isNumberWord(s string) bool {
for _, c := range s {
if !unicode.IsDigit(c) {
return false
}
}
return true
}
func isWebsiteSignature(sentence string) bool {
spaces := strings.Count(sentence, space)
return containsWebsite(sentence) && spaces <= 2
}
func isEmailSignature(sentence string) bool {
spaces := strings.Count(sentence, space)
return containsEmail(sentence) && spaces <= 2
}
func isNumberSignature(sentence string) bool {
spaces := strings.Count(sentence, space)
return containsNumber(sentence) && spaces <= 3
}
func areStripes(sentence string) bool {
for _, c := range sentence {
if string(c) != `-` && string(c) != `_` {
return false
}
}
return len(sentence) > 0
}
func isLabelWithValue(v string) bool {
// is a telephone number with label or some other stuff
lowerLine := strings.ToLower(v)
withoutLabel := removeFirstWord(lowerLine)
amountOfSpaces := strings.Count(withoutLabel, space)
// Beatrixlaan 2, 4694EG Scherpenisse
// if amountOfCommas >
return amountOfSpaces <= 1 && (containsEmail(withoutLabel) ||
containsWebsite(withoutLabel) ||
isNumberSignature(withoutLabel))
}
func containsWebsite(v string) bool {
words := strings.Split(v, space)
for _, word := range words {
containsSlashes := strings.Count(word, "/")
containsDots := strings.Count(word, ".")
containsHttp := strings.Count(word, "http")
containsWww := strings.Count(word, "www")
containsExtension := hasOneOf(word, extensions, &dot, nil)
count := containsSlashes + containsDots + containsHttp + containsWww
if count >= 1 && containsExtension {
return true
}
}
return false
}
func containsEmail(v string) bool {
words := strings.Split(v, space)
for _, word := range words {
if strings.Contains(word, "@") &&
strings.Contains(word, ".") {
return true
}
}
return false
}
func containsNumber(v string) bool {
words := strings.Split(v, space)
for _, word := range words {
if amountOfDigits(word) > 5 {
return true
}
}
return false
}
func isName(sentence string) bool {
nameAndFunction := splitNameAndFunction(sentence)
splitName := strings.Split(removeWhitespace(nameAndFunction[0]), space)
// is a name e.g Kate Green, Richard Lindhout, Jan van der Doorn
if len(splitName) > 0 && len(splitName) <= 3 {
firstName := removeWhitespace(splitName[0])
lastName := removeWhitespace(splitName[len(splitName)-1])
isValidName := (len(firstName) > 3 || len(lastName) > 3) && isFirstLetterUppercase(firstName) && isFirstLetterUppercase(lastName)
invalidCharacters := containsSpecialCharacters(firstName) || containsSpecialCharacters(lastName)
if len(nameAndFunction) > 1 {
// function name should not be more than 3 words
function := removeWhitespace(nameAndFunction[1])
isFunction := len(function) > 3
validFunction := strings.Count(function, space) <= 3
if isFunction && !validFunction {
return false
}
}
return isValidName && !invalidCharacters
}
return false
}
var separators = []string{"|", "-", ","}
func splitNameAndFunction(v string) []string {
for _, separator := range separators {
if strings.Count(v, separator) == 1 {
return strings.Split(v, separator)
}
}
return []string{v}
}
var specialCharacters = []rune("[!@#$%&*()_+=|<>?{}[]~-]")
func containsSpecialCharacters(v string) bool {
for _, specialC := range specialCharacters {
for _, c := range v {
if c == specialC {
return true
}
}
}
return false
}
func amountOfDigits(v string) int {
var amount int
for _, c := range v {
if unicode.IsDigit(c) {
amount++
}
}
return amount
}
func removeMarkdown(s string) string {
newS := strings.Replace(s, "*", "", -1)
return newS
}
func isFirstLetterUppercase(v string) bool {
if len(v) > 0 {
for i, c := range v {
if i == 0 {
return unicode.IsUpper(c)
}
}
}
return false
}
func isSentFrom(fullLine string) bool {
startsWithSend := startWithOneOf(fullLine, sent, true)
containsDevice := hasOneOf(fullLine, mailPrograms, nil, nil)
return startsWithSend && containsDevice
}
var spaceStr = " "
func isQuotedEmailStart(fullLine string) bool {
// on ... wrote etc
// On Monday, November 4, 2013 4:29 PM, John Smith <john.smith@example.org> wrote:
// Op za 8 mei 2021 om 12:09 schreef Richard Lindhout <richardlindhout96@gmail.com>:
// On Oct 1, 2012, at 11:55 PM, Dave Tapley wrote:
// 2013/11/1 John Smith <john@smith.org>
startsWithOn := startWithOneOf(fullLine, on, true)
containsWrote := hasOneOf(fullLine, wrote, &spaceStr, nil)
allNumbers := findNumbers(fullLine)
containsYear := numberArrayContainsYear(allNumbers)
containsEnoughNumbers := len(allNumbers) >= 3
// TODO: more strict
containsQuotedEmail := strings.Contains(fullLine, "@") &&
strings.Contains(fullLine, "<") &&
strings.Contains(fullLine, ">")
if startsWithOn && containsWrote && containsEnoughNumbers && containsYear {
return true
} else if containsQuotedEmail && containsEnoughNumbers && containsYear {
return true
}
return false
}
func containsQuotedEmail(v string) bool {
return strings.Contains(v, "@") &&
strings.Contains(v, "<") &&
strings.Contains(v, ">")
}
func joinLineContents(sep string, lines ...*Line) string {
var a []string
for _, line := range lines {
if line != nil {
a = append(a, line.ContentStripped)
}
}
return strings.Join(a, sep)
}
//nolint:gochecknoglobals
var numberRegex = regexp.MustCompile("[0-9]+")
func findNumbers(v string) []string {
return numberRegex.FindAllString(v, -1)
}
func numberArrayContainsYear(a []string) bool {
for _, v := range a {
if len(v) == 4 {
return true
}
}
return false
}
func hasOneOf(value string, a []string, addFront *string, addBack *string) bool {
for _, c := range a {
finalContains := strings.ToLower(c)
if addFront != nil {
finalContains = *addFront + finalContains
}
if addBack != nil {
finalContains += *addBack
}
if strings.Contains(value, finalContains) {
return true
}
}
return false
}
func startWithOneOf(value string, a []string, addSpaceAfter bool) bool {
for _, prefix := range a {
finalPrefix := strings.ToLower(prefix)
if addSpaceAfter {
finalPrefix += space
}
if strings.HasPrefix(value, finalPrefix) {
return true
}
}
return false
}
func startWithOneOfButDoesNotContainMuchAfter(value string, a []string, maxCharactersAfter int) bool {
for _, prefix := range a {
finalPrefix := strings.ToLower(prefix)
if strings.HasPrefix(value, finalPrefix) {
after := strings.TrimPrefix(value, finalPrefix)
return len(after) <= maxCharactersAfter
}
}
return false
}
// isWhitespace returns true if the string consist of white space
func isWhitespace(content string) bool {
// If the node is a space it's an enter
for _, v := range content {
// Test each character to see if it is whitespace.
if !unicode.IsSpace(v) {
return false
}
}
return true
}
// removeWhitespace removes all double spaces in text
func removeWhitespace(v string) (r string) {
r = strings.ReplaceAll(v, "\t", space)
r = strings.ReplaceAll(r, ` `, space)
r = strings.ReplaceAll(r, " ", space)
r = strings.ReplaceAll(r, ` `, space)
if v != r {
r = removeWhitespace(r)
}
return strings.TrimSpace(r)
}
func removeEnters(v string) (r string) {
r = strings.ReplaceAll(v, enter, "")
if v != r {
r = removeEnters(r)
}
return strings.TrimSpace(r)
}
func removeWhiteSpaceBeforeAndAfter(v string) (r string) {
r = strings.TrimSpace(v)
r = strings.TrimSuffix(r, enter)
r = strings.TrimPrefix(r, enter)
r = strings.TrimSpace(r)
if v != r {
r = removeWhiteSpaceBeforeAndAfter(r)
}
return r
}