-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_admit.go
87 lines (66 loc) · 1.49 KB
/
message_admit.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
package mboxrd
import (
"fmt"
"regexp"
)
type Criterion struct {
OnlyHeaders bool
RE *regexp.Regexp
}
const (
sdrFmt = `^(From:|Sender:|Reply-To:|Return-Path:).*[\s<,:]%s[\s>,\]].*`
rvrFmt = `^(To:\s|Cc:\s|Bcc:\s|(\s+)).*[<,:]%s[\s>,\]].*`
)
var (
banChat = regexp.MustCompile(`^X-Gmail-Labels: Chat`)
)
func AdmitAnyPattern(criteria []Criterion, vetos []Criterion, errors chan error) ByLineAdmit {
var (
admitted = false
banned = false
inHeaders = true
)
return func(line string, errors chan error) bool {
if banned {
return false
}
if admitted {
return true
}
if inHeaders && line == "" {
inHeaders = false
}
for _, veto := range vetos {
if veto.OnlyHeaders && !inHeaders {
continue
}
if veto.RE.MatchString(line) {
banned = true
return false
}
}
if len(criteria) == 0 {
return true
}
for _, permit := range criteria {
if permit.OnlyHeaders && !inHeaders {
continue
}
if admitted = permit.RE.MatchString(line); admitted {
return true
}
}
return false
}
}
func AllWith(addrs []string, errors chan error) ByLineAdmit {
criteria := make([]Criterion, len(addrs)*2)
vetos := make([]Criterion, 1)
for i, addr := range addrs {
pos := i * 2
criteria[pos] = Criterion{true, regexp.MustCompile(fmt.Sprintf(sdrFmt, addr))}
criteria[pos+1] = Criterion{true, regexp.MustCompile(fmt.Sprintf(rvrFmt, addr))}
}
vetos[0] = Criterion{true, banChat}
return AdmitAnyPattern(criteria, vetos, errors)
}