This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contentinspectionfilter.go
62 lines (52 loc) · 1.63 KB
/
contentinspectionfilter.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
package filter
import (
"analyzer"
metrics "github.com/rcrowley/go-metrics"
"log"
"mailfile"
"mailpost"
)
type ContentInspectionFilter struct {
next Filter
allPass bool
anlz analyzer.Analyzer
total metrics.Counter
malformed metrics.Counter
counters map[string]metrics.Counter
}
func (cif *ContentInspectionFilter) Filter(mail mailfile.Mail) Result {
log.Printf("Run %s, Mail:%s\n", cif, mail.Name())
cif.total.Inc(1)
post, err := mailpost.Parse(mail)
mail.Close()
if err != nil {
cif.malformed.Inc(1)
log.Printf("ContentInspectionFilter: Err:%v, Mail:%s\n", err, mail.Path())
return cif.next.Filter(mail)
}
class := cif.anlz.Test(post.Subject + " " + post.Content)
cif.counters[class].Inc(1)
if cif.allPass || analyzer.Good == class {
return cif.next.Filter(mail)
}
return Quarantine
}
func (cif *ContentInspectionFilter) Key(mail mailfile.Mail) string {
return mail.Subject()
}
func (cif *ContentInspectionFilter) String() string {
return "ContentInspectionFilter"
}
func NewContentInspectionFilter(next Filter, allPass bool, anlz analyzer.Analyzer) *ContentInspectionFilter {
total := metrics.NewCounter()
metrics.Register("ContentInspectionFilter-Total", total)
counters := make(map[string]metrics.Counter)
for _, class := range []string{analyzer.Good, analyzer.Bad, analyzer.Neutral} {
counter := metrics.NewCounter()
counters[class] = counter
metrics.Register("ContentInspectionFilter-"+class, counter)
}
malformed := metrics.NewCounter()
metrics.Register("ContentInspectionFilter-Malformed", malformed)
return &ContentInspectionFilter{next, allPass, anlz, total, malformed, counters}
}