refactoring, created email package

This commit is contained in:
Aine
2022-11-19 17:00:57 +02:00
parent 9e532a6007
commit 052fd5bb25
12 changed files with 176 additions and 164 deletions

View File

@@ -11,7 +11,7 @@ import (
"gitlab.com/etke.cc/go/logger"
"maunium.net/go/mautrix/id"
"gitlab.com/etke.cc/postmoogle/utils"
"gitlab.com/etke.cc/postmoogle/email"
)
type Config struct {
@@ -45,8 +45,8 @@ type matrixbot interface {
IsBanned(net.Addr) bool
Ban(net.Addr)
GetMapping(string) (id.RoomID, bool)
GetIFOptions(id.RoomID) utils.IncomingFilteringOptions
IncomingEmail(context.Context, *utils.Email) error
GetIFOptions(id.RoomID) email.IncomingFilteringOptions
IncomingEmail(context.Context, *email.Email) error
SetSendmail(func(string, string, string) error)
GetDKIMprivkey() string
}

View File

@@ -10,7 +10,7 @@ import (
"gitlab.com/etke.cc/go/logger"
"gitlab.com/etke.cc/go/trysmtp"
"gitlab.com/etke.cc/postmoogle/utils"
"gitlab.com/etke.cc/postmoogle/email"
)
var (
@@ -41,7 +41,7 @@ func (m *mailServer) Login(state *smtp.ConnectionState, username, password strin
return nil, ErrBanned
}
if !utils.AddressValid(username) {
if !email.AddressValid(username) {
m.log.Debug("address %s is invalid", username)
m.bot.Ban(state.RemoteAddr)
return nil, ErrBanned
@@ -114,6 +114,6 @@ func (m *mailServer) SendEmail(from, to, data string) error {
}
// ReceiveEmail - incoming mail into matrix room
func (m *mailServer) ReceiveEmail(ctx context.Context, email *utils.Email) error {
return m.bot.IncomingEmail(ctx, email)
func (m *mailServer) ReceiveEmail(ctx context.Context, eml *email.Email) error {
return m.bot.IncomingEmail(ctx, eml)
}

View File

@@ -13,6 +13,7 @@ import (
"gitlab.com/etke.cc/go/validator"
"maunium.net/go/mautrix/id"
"gitlab.com/etke.cc/postmoogle/email"
"gitlab.com/etke.cc/postmoogle/utils"
)
@@ -20,8 +21,8 @@ import (
type incomingSession struct {
log *logger.Logger
getRoomID func(string) (id.RoomID, bool)
getFilters func(id.RoomID) utils.IncomingFilteringOptions
receiveEmail func(context.Context, *utils.Email) error
getFilters func(id.RoomID) email.IncomingFilteringOptions
receiveEmail func(context.Context, *email.Email) error
greylisted func(net.Addr) bool
ban func(net.Addr)
domains []string
@@ -34,7 +35,7 @@ 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) {
if !email.AddressValid(from) {
s.log.Debug("address %s is invalid", from)
s.ban(s.addr)
return ErrBanned
@@ -84,15 +85,15 @@ func (s *incomingSession) Data(r io.Reader) error {
}
}
parser := enmime.NewParser()
eml, err := parser.ReadEnvelope(r)
envelope, err := parser.ReadEnvelope(r)
if err != nil {
return err
}
email := utils.FromEnvelope(s.tos[0], eml)
eml := email.FromEnvelope(s.tos[0], envelope)
for _, to := range s.tos {
email.RcptTo = to
err := s.receiveEmail(s.ctx, email)
eml.RcptTo = to
err := s.receiveEmail(s.ctx, eml)
if err != nil {
return err
}
@@ -116,7 +117,7 @@ type outgoingSession struct {
func (s *outgoingSession) Mail(from string, opts smtp.MailOptions) error {
sentry.GetHubFromContext(s.ctx).Scope().SetTag("from", from)
if !utils.AddressValid(from) {
if !email.AddressValid(from) {
return errors.New("please, provide email address")
}
return nil
@@ -132,14 +133,14 @@ func (s *outgoingSession) Rcpt(to string) error {
func (s *outgoingSession) Data(r io.Reader) error {
parser := enmime.NewParser()
eml, err := parser.ReadEnvelope(r)
envelope, err := parser.ReadEnvelope(r)
if err != nil {
return err
}
email := utils.FromEnvelope(s.tos[0], eml)
eml := email.FromEnvelope(s.tos[0], envelope)
for _, to := range s.tos {
email.RcptTo = to
err := s.sendmail(email.From, to, email.Compose(s.privkey))
eml.RcptTo = to
err := s.sendmail(eml.From, to, eml.Compose(s.privkey))
if err != nil {
return err
}
@@ -150,7 +151,7 @@ func (s *outgoingSession) Data(r io.Reader) error {
func (s *outgoingSession) Reset() {}
func (s *outgoingSession) Logout() error { return nil }
func validateEmail(from, to string, log *logger.Logger, options utils.IncomingFilteringOptions) bool {
func validateEmail(from, to string, log *logger.Logger, options email.IncomingFilteringOptions) bool {
enforce := validator.Enforce{
Email: true,
MX: options.SpamcheckMX(),