-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (104 loc) · 2.62 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"bytes"
"errors"
"fmt"
"log"
"regexp"
"strings"
"time"
"io"
"io/ioutil"
geemail "geegle.org/infra/geemail-client"
"github.com/emersion/go-smtp"
"github.com/jhillyerd/enmime"
)
var emailRe = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
type Backend struct{}
func (bkd *Backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
return &Session{}, nil
}
func (bkd *Backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
return &Session{}, nil
}
type Session struct {
From string
To map[string]struct{}
}
func (s *Session) Mail(from string) error {
if !emailRe.MatchString(from) {
return errors.New("Invalid email address")
}
if strings.Contains(from, "geegle.org") {
return errors.New("If you are a geegler, please use https://mail.corp.geegle.org instead")
}
s.Reset()
s.From = from
return nil
}
func (s *Session) Rcpt(to string) error {
if !emailRe.MatchString(to) {
return errors.New("Invalid email address")
}
if !strings.HasSuffix(to, "@geegle.org") {
return errors.New("Only geegle.org email addresses are allowed.")
}
if _, e := s.To[to]; e {
return errors.New("Duplicate email address")
}
var m struct{}
s.To[to] = m
return nil
}
func (s *Session) Data(r io.Reader) error {
// NOTE: for some reason, if we directly pass r to enmime, result is not correct
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
env, err := enmime.ReadEnvelope(bytes.NewReader(data))
if err != nil {
return err
}
body := env.HTML
if body == "" {
body = env.Text
}
// NOTE: all other headers are discarded
// NOTE: attachments are discarded
// NOTE: spf, dkim, dmarc are not checked
go sendMail(s.From, s.To, env.GetHeader("Subject"), []byte(body))
return nil
}
func (s *Session) Reset() {
s.To = make(map[string]struct{})
s.From = ""
}
func (s *Session) Logout() error {
return nil
}
func sendMail(from string, to map[string]struct{}, subject string, data []byte) {
fmt.Println("sending email")
fmt.Println(from)
fmt.Println(to)
fmt.Println(subject)
fmt.Println(string(data))
for r := range to {
geemail.SendEmailNow(from, r, subject, data)
}
}
func main() {
be := &Backend{}
s := smtp.NewServer(be)
s.Addr = ":25"
s.Domain = "geegle.org"
s.ReadTimeout = 10 * time.Second
s.WriteTimeout = 10 * time.Second
s.MaxMessageBytes = 1024 * 1024
s.MaxRecipients = 50
s.AllowInsecureAuth = true
log.Println("Starting server at", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
}
}