-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
57 lines (49 loc) · 1.28 KB
/
filter.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
package spam
import (
"fmt"
"net/mail"
"regexp"
"github.com/zen-en-tonal/mtw/session"
)
// RcptMismatchFilter returns a filter that compares `rcpt` and `to`.
func RcptMismatchFilter() rcptMismatchFilter {
return rcptMismatchFilter{}
}
type rcptMismatchFilter struct{}
func (r rcptMismatchFilter) Validate(e session.Transaction) error {
rcpt := e.RcptAddress()
to, err := mail.ParseAddress(e.To())
if err != nil {
return session.ErrNilEnvelope
}
if rcpt != to.Address {
return fmt.Errorf("rcpt %s and to %s is mismatched", rcpt, to.Address)
}
return nil
}
type blackList []string
// BlackListFilter sets filters defined as regexp pattern.
// If at least one pattern matches, the filter returns an error.
func BlackListFilter(patterns ...string) blackList {
return blackList(patterns)
}
func (patterns blackList) Validate(e session.Transaction) error {
rcpt := e.RcptAddress()
to, err := mail.ParseAddress(e.To())
if err != nil {
return session.ErrNilEnvelope
}
for _, pattern := range patterns {
r, err := regexp.Compile(pattern)
if err != nil {
return err
}
if r.Match([]byte(rcpt)) {
return fmt.Errorf("addr %s contains blacklist", rcpt)
}
if r.Match([]byte(to.Address)) {
return fmt.Errorf("addr %s contains blacklist", to.Address)
}
}
return nil
}