Add license information.
[mailpopbox.git] / smtp / server.go
1 // mailpopbox
2 // Copyright 2020 Blue Static <https://www.bluestatic.org>
3 // This program is free software licensed under the GNU General Public License,
4 // version 3.0. The full text of the license can be found in LICENSE.txt.
5 // SPDX-License-Identifier: GPL-3.0-only
6
7 package smtp
8
9 import (
10 "crypto/tls"
11 "fmt"
12 "io"
13 "net"
14 "net/mail"
15 "time"
16 )
17
18 type ReplyLine struct {
19 Code int
20 Message string
21 }
22
23 func (l ReplyLine) String() string {
24 return fmt.Sprintf("%d %s", l.Code, l.Message)
25 }
26
27 var (
28 ReplyOK = ReplyLine{250, "OK"}
29 ReplyBadSyntax = ReplyLine{501, "syntax error"}
30 ReplyBadSequence = ReplyLine{503, "bad sequence of commands"}
31 ReplyBadMailbox = ReplyLine{550, "mailbox unavailable"}
32 )
33
34 type Envelope struct {
35 RemoteAddr net.Addr
36 EHLO string
37 MailFrom mail.Address
38 RcptTo []mail.Address
39 Data []byte
40 Received time.Time
41 ID string
42 }
43
44 func WriteEnvelopeForDelivery(w io.Writer, e Envelope) {
45 fmt.Fprintf(w, "Delivered-To: <%s>\r\n", e.RcptTo[0].Address)
46 fmt.Fprintf(w, "Return-Path: <%s>\r\n", e.MailFrom.Address)
47 w.Write(e.Data)
48 }
49
50 type Server interface {
51 Name() string
52 TLSConfig() *tls.Config
53 OnEHLO() *ReplyLine
54 VerifyAddress(mail.Address) ReplyLine
55 OnMessageDelivered(Envelope) *ReplyLine
56 }
57
58 type EmptyServerCallbacks struct{}
59
60 func (*EmptyServerCallbacks) TLSConfig() *tls.Config {
61 return nil
62 }
63
64 func (*EmptyServerCallbacks) OnEHLO() *ReplyLine {
65 return nil
66 }
67
68 func (*EmptyServerCallbacks) VerifyAddress(mail.Address) ReplyLine {
69 return ReplyOK
70 }
71
72 func (*EmptyServerCallbacks) OnMessageDelivered(Envelope) *ReplyLine {
73 return nil
74 }