12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- 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)
- }
- })
- }
- }
|