adjust auto-retry, fix banned response code, rewrite email composing to enmime, add more logs

This commit is contained in:
Aine
2022-11-16 22:22:19 +02:00
parent fce6593cd7
commit 225ba2ee9b
4 changed files with 44 additions and 77 deletions

View File

@@ -2,7 +2,6 @@ package smtp
import (
"context"
"errors"
"io"
"strings"
@@ -14,6 +13,13 @@ import (
"gitlab.com/etke.cc/postmoogle/utils"
)
// ErrBanned returned to banned hosts
var ErrBanned = &smtp.SMTPError{
Code: 554,
EnhancedCode: smtp.EnhancedCode{5, 5, 4},
Message: "please, don't bother me anymore, kupo.",
}
type mailServer struct {
bot matrixbot
log *logger.Logger
@@ -24,17 +30,19 @@ type mailServer struct {
func (m *mailServer) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
m.log.Debug("Login state=%+v username=%+v", state, username)
if m.bot.IsBanned(state.RemoteAddr) {
return nil, errors.New("please, don't bother me anymore")
return nil, ErrBanned
}
if !utils.AddressValid(username) {
m.log.Debug("address %s is invalid", username)
m.bot.Ban(state.RemoteAddr)
return nil, errors.New("please, provide an email address")
return nil, ErrBanned
}
if !m.bot.AllowAuth(username, password) {
m.log.Debug("username=%s or password=<redacted> is invalid", username)
m.bot.Ban(state.RemoteAddr)
return nil, errors.New("email or password is invalid")
return nil, ErrBanned
}
return &outgoingSession{
@@ -51,7 +59,7 @@ func (m *mailServer) Login(state *smtp.ConnectionState, username, password strin
func (m *mailServer) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
m.log.Debug("AnonymousLogin state=%+v", state)
if m.bot.IsBanned(state.RemoteAddr) {
return nil, errors.New("please, don't bother me anymore")
return nil, ErrBanned
}
return &incomingSession{

View File

@@ -35,8 +35,9 @@ type incomingSession struct {
func (s *incomingSession) Mail(from string, opts smtp.MailOptions) error {
sentry.GetHubFromContext(s.ctx).Scope().SetTag("from", from)
if !utils.AddressValid(from) {
s.log.Debug("address %s is invalid", from)
s.ban(s.addr)
return errors.New("please, provide email address")
return ErrBanned
}
s.from = from
s.log.Debug("mail from %s, options: %+v", from, opts)
@@ -56,20 +57,20 @@ func (s *incomingSession) Rcpt(to string) error {
if !domainok {
s.log.Debug("wrong domain of %s", to)
s.ban(s.addr)
return smtp.ErrAuthRequired
return ErrBanned
}
roomID, ok := s.getRoomID(utils.Mailbox(to))
if !ok {
s.log.Debug("mapping for %s not found", to)
s.ban(s.addr)
return smtp.ErrAuthRequired
return ErrBanned
}
validations := s.getFilters(roomID)
if !validateEmail(s.from, s.to, s.log, validations) {
s.ban(s.addr)
return smtp.ErrAuthRequired
return ErrBanned
}
s.log.Debug("mail to %s", to)