]>
src.bluestatic.org Git - mailpopbox.git/blob - smtp/server.go
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
21 type ReplyLine
struct {
26 func (l ReplyLine
) String() string {
27 return fmt
.Sprintf("%d %s", l
.Code
, l
.Message
)
30 var SendAsSubject
= regexp
.MustCompile(`(?i)\[sendas:\s*([a-zA-Z0-9\.\-_]+)\]`)
33 ReplyOK
= ReplyLine
{250, "OK"}
34 ReplyAuthOK
= ReplyLine
{235, "auth success"}
35 ReplyBadSyntax
= ReplyLine
{501, "syntax error"}
36 ReplyBadSequence
= ReplyLine
{503, "bad sequence of commands"}
37 ReplyBadMailbox
= ReplyLine
{550, "mailbox unavailable"}
38 ReplyMailboxUnallowed
= ReplyLine
{553, "mailbox name not allowed"}
41 func DomainForAddress(addr mail
.Address
) string {
42 return DomainForAddressString(addr
.Address
)
45 func DomainForAddressString(address
string) string {
46 domainIdx
:= strings
.LastIndex(address
, "@")
50 return address
[domainIdx
+1:]
53 type Envelope
struct {
63 func WriteEnvelopeForDelivery(w io
.Writer
, e Envelope
) {
64 fmt
.Fprintf(w
, "Delivered-To: <%s>\r\n", e
.RcptTo
[0].Address
)
65 fmt
.Fprintf(w
, "Return-Path: <%s>\r\n", e
.MailFrom
.Address
)
69 func generateEnvelopeId(prefix
string, t time
.Time
) string {
72 return fmt
.Sprintf("%s.%d.%x", prefix
, t
.UnixNano(), idBytes
)
75 // lookupRemoteHost attempts to reverse look-up the provided IP address. On
76 // success, it returns the hostname and the IP as formatted for a receive
77 // trace. If the lookup fails, it just returns the original IP.
78 func lookupRemoteHost(addr net
.Addr
) string {
79 rhost
, _
, err
:= net
.SplitHostPort(addr
.String())
84 rhosts
, err
:= net
.LookupAddr(rhost
)
86 rhost
= fmt
.Sprintf("%s [%s]", rhosts
[0], rhost
)
92 type Server
interface {
94 TLSConfig() *tls
.Config
95 VerifyAddress(mail
.Address
) ReplyLine
96 // Verify that the authc+passwd identity can send mail as authz.
97 Authenticate(authz
, authc
, passwd
string) bool
98 DeliverMessage(Envelope
) *ReplyLine
100 // RelayMessage instructs the server to send the Envelope to another
101 // MTA for outbound delivery.
102 RelayMessage(Envelope
)
105 type EmptyServerCallbacks
struct{}
107 func (*EmptyServerCallbacks
) TLSConfig() *tls
.Config
{
111 func (*EmptyServerCallbacks
) VerifyAddress(mail
.Address
) ReplyLine
{
115 func (*EmptyServerCallbacks
) Authenticate(authz
, authc
, passwd
string) bool {
119 func (*EmptyServerCallbacks
) DeliverMessage(Envelope
) *ReplyLine
{
123 func (*EmptyServerCallbacks
) RelayMessage(Envelope
) {