forked from flashmob/go-guerrilla
/
p_hasher.go
58 lines (52 loc) · 1.9 KB
/
p_hasher.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
package backends
import (
"crypto/md5"
"fmt"
"io"
"strings"
"time"
"github.com/flashmob/go-guerrilla/mail"
)
// ----------------------------------------------------------------------------------
// Processor Name: hasher
// ----------------------------------------------------------------------------------
// Description : Generates a unique md5 checksum id for an email
// ----------------------------------------------------------------------------------
// Config Options: None
// --------------:-------------------------------------------------------------------
// Input : e.MailFrom, e.Subject, e.RcptTo
// : assuming e.Subject was generated by "headersparser" processor
// ----------------------------------------------------------------------------------
// Output : Checksum stored in e.Hash
// ----------------------------------------------------------------------------------
func init() {
processors["hasher"] = func() Decorator {
return Hasher()
}
}
// The hasher decorator computes a hash of the email for each recipient
// It appends the hashes to envelope's Hashes slice.
func Hasher() Decorator {
return func(p Processor) Processor {
return ProcessWith(func(e *mail.Envelope, task SelectTask) (Result, error) {
if task == TaskSaveMail {
// base hash, use subject from and timestamp-nano
h := md5.New()
ts := fmt.Sprintf("%d", time.Now().UnixNano())
_, _ = io.Copy(h, strings.NewReader(e.MailFrom.String()))
_, _ = io.Copy(h, strings.NewReader(e.Subject))
_, _ = io.Copy(h, strings.NewReader(ts))
// using the base hash, calculate a unique hash for each recipient
for i := range e.RcptTo {
h2 := h
_, _ = io.Copy(h2, strings.NewReader(e.RcptTo[i].String()))
sum := h2.Sum([]byte{})
e.Hashes = append(e.Hashes, fmt.Sprintf("%x", sum))
}
return p.Process(e, task)
} else {
return p.Process(e, task)
}
})
}
}