-
Notifications
You must be signed in to change notification settings - Fork 2
/
Session.go
93 lines (80 loc) · 1.75 KB
/
Session.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
88
89
90
91
92
93
package gospam
import (
"bytes"
"io"
"io/ioutil"
"log"
"mime"
"net"
"net/mail"
"time"
"github.com/emersion/go-smtp"
)
type Session struct {
backend Backend
remote net.Addr
from string
to []string
}
func (s *Session) AuthPlain(username, password string) error {
return smtp.ErrAuthUnsupported
}
func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
log.Printf("MAIL FROM [%s]: %s\n", s.remote.String(), from)
s.from = from
return nil
}
func (s *Session) Rcpt(to string) error {
log.Printf("RCPT TO [%s]: %s\n", s.remote.String(), to)
if !s.backend.IsAcceptedDomain(to) {
log.Printf("not in AcceptedDomains\n")
return &smtp.SMTPError{
Code: 550,
EnhancedCode: smtp.EnhancedCode{5, 1, 1},
Message: "Invalid recipient",
}
}
if s.to == nil {
s.to = make([]string, 0)
}
s.to = append(s.to, to)
return nil
}
func (s *Session) Data(r io.Reader) error {
log.Printf("DATA [%s]\n", s.remote.String())
if m, err := ioutil.ReadAll(r); err != nil {
return err
} else {
parsedMail, err := mail.ReadMessage(bytes.NewReader(m))
if err != nil {
return err
}
b, err := ioutil.ReadAll(parsedMail.Body)
if err != nil {
return err
}
mimeDecoder := new(mime.WordDecoder)
subject, err := mimeDecoder.Decode(parsedMail.Header.Get("Subject"))
if err != nil {
subject = parsedMail.Header.Get("Subject")
}
s.backend.SaveEmail(&EMail{
Time: time.Now(),
From: s.from,
To: s.to,
Header: parsedMail.Header,
Body: b,
Data: m,
Subject: subject,
})
log.Printf("Saved E-Mail Message with %d bytes of body data\n", len(b))
}
return nil
}
func (s *Session) Reset() {
s.from = ""
s.to = make([]string, 0)
}
func (s *Session) Logout() error {
return nil
}