Add zap logging through the servers.
[mailpopbox.git] / smtp / server.go
1 package smtp
2
3 import (
4 "crypto/tls"
5 "fmt"
6 "io"
7 "net"
8 "net/mail"
9 "time"
10 )
11
12 type ReplyLine struct {
13 Code int
14 Message string
15 }
16
17 func (l ReplyLine) String() string {
18 return fmt.Sprintf("%d %s", l.Code, l.Message)
19 }
20
21 var (
22 ReplyOK = ReplyLine{250, "OK"}
23 ReplyBadSyntax = ReplyLine{501, "syntax error"}
24 ReplyBadSequence = ReplyLine{503, "bad sequence of commands"}
25 ReplyBadMailbox = ReplyLine{550, "mailbox unavailable"}
26 )
27
28 type Envelope struct {
29 RemoteAddr net.Addr
30 EHLO string
31 MailFrom mail.Address
32 RcptTo []mail.Address
33 Data []byte
34 Received time.Time
35 ID string
36 }
37
38 func WriteEnvelopeForDelivery(w io.Writer, e Envelope) {
39 fmt.Fprintf(w, "Delivered-To: <%s>\r\n", e.RcptTo[0].Address)
40 fmt.Fprintf(w, "Return-Path: <%s>\r\n", e.MailFrom.Address)
41 w.Write(e.Data)
42 }
43
44 type Server interface {
45 Name() string
46 TLSConfig() *tls.Config
47 OnEHLO() *ReplyLine
48 VerifyAddress(mail.Address) ReplyLine
49 OnMessageDelivered(Envelope) *ReplyLine
50 }
51
52 type EmptyServerCallbacks struct{}
53
54 func (*EmptyServerCallbacks) TLSConfig() *tls.Config {
55 return nil
56 }
57
58 func (*EmptyServerCallbacks) OnEHLO() *ReplyLine {
59 return nil
60 }
61
62 func (*EmptyServerCallbacks) VerifyAddress(mail.Address) ReplyLine {
63 return ReplyOK
64 }
65
66 func (*EmptyServerCallbacks) OnMessageDelivered(Envelope) *ReplyLine {
67 return nil
68 }