big refactoring

This commit is contained in:
Aine
2022-11-25 23:33:38 +02:00
parent 14bad9f479
commit 8d6c4aeafe
23 changed files with 933 additions and 816 deletions

29
smtp/logger.go Normal file
View File

@@ -0,0 +1,29 @@
package smtp
import (
"strings"
)
// loggerWrapper is a wrapper around logger.Logger to implement smtp.Logger interface
type loggerWrapper struct {
log func(string, ...interface{})
}
func (l loggerWrapper) Printf(format string, v ...interface{}) {
l.log(format, v...)
}
func (l loggerWrapper) Println(v ...interface{}) {
msg := strings.Repeat("%v ", len(v))
l.log(msg, v...)
}
// loggerWriter is a wrapper around io.Writer to implement io.Writer interface
type loggerWriter struct {
log func(string)
}
func (l loggerWriter) Write(p []byte) (n int, err error) {
l.log(string(p))
return len(p), nil
}

View File

@@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"net"
"os"
"time"
"github.com/emersion/go-smtp"
@@ -26,6 +25,7 @@ type Config struct {
LogLevel string
MaxSize int
Bot matrixbot
Callers []Caller
}
type Manager struct {
@@ -47,10 +47,14 @@ type matrixbot interface {
GetMapping(string) (id.RoomID, bool)
GetIFOptions(id.RoomID) email.IncomingFilteringOptions
IncomingEmail(context.Context, *email.Email) error
SetSendmail(func(string, string, string) error)
GetDKIMprivkey() string
}
// Caller is Sendmail caller
type Caller interface {
SetSendmail(func(string, string, string) error)
}
// NewManager creates new SMTP server manager
func NewManager(cfg *Config) *Manager {
log := logger.New("smtp.", cfg.LogLevel)
@@ -59,9 +63,12 @@ func NewManager(cfg *Config) *Manager {
bot: cfg.Bot,
domains: cfg.Domains,
}
cfg.Bot.SetSendmail(mailsrv.SendEmail)
for _, caller := range cfg.Callers {
caller.SetSendmail(mailsrv.SendEmail)
}
s := smtp.NewServer(mailsrv)
s.ErrorLog = loggerWrapper{func(s string, i ...interface{}) { log.Error(s, i...) }}
s.ReadTimeout = 10 * time.Second
s.WriteTimeout = 10 * time.Second
s.MaxMessageBytes = cfg.MaxSize * 1024 * 1024
@@ -73,7 +80,7 @@ func NewManager(cfg *Config) *Manager {
s.Domain = cfg.Domains[0]
}
if log.GetLevel() == "DEBUG" || log.GetLevel() == "TRACE" {
s.Debug = os.Stdout
s.Debug = loggerWriter{func(s string) { log.Debug(s) }}
}
m := &Manager{