diff --git a/bot/email.go b/bot/email.go index e95440d..c69ad87 100644 --- a/bot/email.go +++ b/bot/email.go @@ -40,16 +40,19 @@ func (b *Bot) SetSendmail(sendmail func(string, string, string) error) { } func (b *Bot) shouldQueue(msg string) bool { - errs := strings.Split(msg, ";") - for _, err := range errs { - errParts := strings.Split(strings.TrimSpace(err), ":") - if len(errParts) < 2 { - continue - } - if strings.HasPrefix(strings.TrimSpace(errParts[1]), "4") { - return true - } + msg = strings.TrimSpace(msg) + if strings.HasPrefix(msg, "4") { // any temporary issue (4xx SMTP code) + return true } + + if strings.Contains(msg, "450") || strings.Contains(msg, "451") { // greylisting + return true + } + + if strings.Contains(msg, "greylisted") { // greylisting + return true + } + return false } diff --git a/go.mod b/go.mod index 71bb267..036ab15 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,6 @@ require ( gitlab.com/etke.cc/go/healthchecks v1.0.1 gitlab.com/etke.cc/go/mxidwc v1.0.0 gitlab.com/etke.cc/go/secgen v1.1.1 - gitlab.com/etke.cc/go/trysmtp v1.1.3 gitlab.com/etke.cc/go/validator v1.0.6 gitlab.com/etke.cc/linkpearl v0.0.0-20231007103859-01907e2b75f2 golang.org/x/exp v0.0.0-20230905200255-921286631fa9 @@ -52,6 +51,7 @@ require ( github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/sjson v1.2.5 // indirect github.com/yuin/goldmark v1.5.6 // indirect + gitlab.com/etke.cc/go/trysmtp v1.1.3 // indirect go.mau.fi/util v0.1.0 // indirect golang.org/x/crypto v0.13.0 // indirect golang.org/x/net v0.15.0 // indirect diff --git a/smtp/client.go b/smtp/client.go index 1128c00..c6667a4 100644 --- a/smtp/client.go +++ b/smtp/client.go @@ -2,12 +2,13 @@ package smtp import ( "crypto/tls" + "fmt" "io" + "net" "net/smtp" "strings" "github.com/rs/zerolog" - "gitlab.com/etke.cc/go/trysmtp" ) type MailSender interface { @@ -20,6 +21,8 @@ type Client struct { log *zerolog.Logger } +var errNoSMTP = fmt.Errorf("cannot connect to any SMTP server") + func newClient(cfg *RelayConfig, log *zerolog.Logger) *Client { return &Client{ config: cfg, @@ -36,10 +39,10 @@ func (c Client) Send(from, to, data string) error { var err error if c.config.Host != "" { log.Debug().Msg("creating relay client...") - conn, err = c.createDirectClient(from, to) + conn, err = c.createRelayClient(from, to) } else { log.Debug().Msg("trying direct SMTP connection...") - conn, err = trysmtp.Connect(from, to) + conn, err = c.createDirectClient(from, to) } if conn == nil { @@ -58,7 +61,7 @@ func (c Client) Send(from, to, data string) error { return err } defer w.Close() - c.log.Debug().Str("DATA", data).Msg("sending command") + log.Debug().Str("DATA", data).Msg("sending command") _, err = strings.NewReader(data).WriteTo(w) if err != nil { log.Error().Err(err).Msg("cannot write DATA") @@ -69,8 +72,8 @@ func (c Client) Send(from, to, data string) error { return nil } -// createDirectClient connects directly to the provided smtp host -func (c *Client) createDirectClient(from, to string) (*smtp.Client, error) { +// createRelayClientconnects directly to the provided smtp host +func (c *Client) createRelayClient(from, to string) (*smtp.Client, error) { localname := strings.SplitN(from, "@", 2)[1] target := c.config.Host + ":" + c.config.Port conn, err := smtp.Dial(target) @@ -110,3 +113,76 @@ func (c *Client) createDirectClient(from, to string) (*smtp.Client, error) { return conn, nil } + +func (c *Client) createDirectClient(from, to string) (*smtp.Client, error) { + localname := strings.SplitN(from, "@", 2)[1] + hostname := strings.SplitN(to, "@", 2)[1] + client, cerr := c.trySMTP(localname, hostname) + if client == nil { + c.log.Warn().Err(cerr).Str("from", from).Str("to", to).Msg("cannot create direct SMTP client") + return nil, cerr + } + + err := client.Mail(from) + if err != nil { + c.log.Warn().Err(err).Str("from", from).Str("to", to).Msg("cannot send MAIL command") + client.Close() + return nil, err + } + + err = client.Rcpt(to) + if err != nil { + c.log.Warn().Err(err).Str("from", from).Str("to", to).Msg("cannot send RCPT command") + client.Close() + return nil, err + } + + return client, cerr +} + +func (c *Client) trySMTP(localname, hostname string) (*smtp.Client, error) { + mxs, err := net.LookupMX(hostname) + if err != nil { + return nil, err + } + + var client *smtp.Client + for _, mx := range mxs { + if mx.Host == "." { + continue // no records case + } + client = c.connect(localname, hostname, strings.TrimSuffix(mx.Host, ".")) + if client != nil { + return client, nil + } + } + + // If there are no MX records, according to https://datatracker.ietf.org/doc/html/rfc5321#section-5.1, + // we're supposed to try talking directly to the host. + client = c.connect(localname, hostname, hostname) + if client != nil { + return client, nil + } + + return nil, errNoSMTP +} + +func (c *Client) connect(localname, serverOf, mxhost string) *smtp.Client { + target := mxhost + ":25" + conn, err := smtp.Dial(target) + if err != nil { + c.log.Warn().Err(err).Str("target", serverOf).Str("host", mxhost).Msg("cannot dial SMTP server") + return nil + } + err = conn.Hello(localname) + if err != nil { + c.log.Warn().Err(err).Str("target", serverOf).Str("host", mxhost).Msg("cannot hello SMTP server") + return nil + } + if ok, _ := conn.Extension("STARTTLS"); ok { + config := &tls.Config{ServerName: mxhost} //nolint:gosec // it's smtp, even that is too strict sometimes + conn.StartTLS(config) //nolint:errcheck // if it doesn't work - we can't do anything anyway + } + + return conn +}