From c829408095faf8d9dc9f9025282627585d7b74f8 Mon Sep 17 00:00:00 2001 From: "Felipe M." Date: Sun, 24 Nov 2024 12:08:35 +0100 Subject: [PATCH] refactor: configuration into separate file --- cmd/smtp2shoutrrr/main.go | 46 ++++++++++----------------------------- config.go | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 35 deletions(-) create mode 100644 config.go diff --git a/cmd/smtp2shoutrrr/main.go b/cmd/smtp2shoutrrr/main.go index 41bc0d2..ef2c937 100644 --- a/cmd/smtp2shoutrrr/main.go +++ b/cmd/smtp2shoutrrr/main.go @@ -3,7 +3,6 @@ package main import ( "bytes" "context" - "errors" "fmt" "io" "log" @@ -23,6 +22,8 @@ import ( "github.com/containrrr/shoutrrr" "github.com/emersion/go-sasl" "github.com/emersion/go-smtp" + + "git.nakama.town/fmartingr/smtp2shoutrrr" ) type ReceivedEmail struct { @@ -82,39 +83,11 @@ func (re *ReceivedEmail) Body() (string, error) { return re.body, nil } -type Config struct { - Port int - Recipients []ConfigRecipient -} - -func (c *Config) SetDefaults() { - if c.Port == 0 { - c.Port = 11125 - } -} - -type ConfigRecipient struct { - Addresses []string // email addresses - Target string // shoutrrr address - targetURL *url.URL -} - -func (cr ConfigRecipient) GetTargetURL() *url.URL { - if cr.targetURL == nil { - var err error - cr.targetURL, err = url.Parse(cr.Target) - if err != nil { - slog.Error("failed to parse shoutrrr target URL", slog.String("target", cr.Target), slog.String("err", err.Error())) - } - } - return cr.targetURL -} - var _ model.Server = (*smtpServer)(nil) type smtpServer struct { backend *smtp.Server - config Config + config smtp2shoutrrr.Config } func (s *smtpServer) IsEnabled() bool { @@ -131,7 +104,7 @@ func (s *smtpServer) Stop(ctx context.Context) error { return s.backend.Shutdown(ctx) } -func NewSMTPServer(config *Config) model.Server { +func NewSMTPServer(config *smtp2shoutrrr.Config) model.Server { be := &Backend{ config: config, } @@ -152,13 +125,14 @@ func NewSMTPServer(config *Config) model.Server { // The Backend implements SMTP server methods. type Backend struct { - config *Config + config *smtp2shoutrrr.Config } // NewSession is called after client greeting (EHLO, HELO). func (bkd *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) { return &Session{ forwarderFunc: bkd.forwardEmail, + config: bkd.config, }, nil } @@ -200,6 +174,8 @@ type Session struct { addresses []string body string + config *smtp2shoutrrr.Config + forwarderFunc func(ReceivedEmail) error } @@ -212,8 +188,8 @@ func (s *Session) AuthMechanisms() []string { // Auth is the handler for supported authenticators. func (s *Session) Auth(mech string) (sasl.Server, error) { return sasl.NewPlainServer(func(identity, username, password string) error { - if username != "username" || password != "password" { - return errors.New("Invalid username or password") + if username != s.config.Username && password != s.config.Password { + return fmt.Errorf("invalid credentials") } return nil }), nil @@ -256,7 +232,7 @@ func (s *Session) Logout() error { } func main() { - var config *Config + var config *smtp2shoutrrr.Config configPath := "config.toml" f, err := os.Open(configPath) diff --git a/config.go b/config.go new file mode 100644 index 0000000..9a64e9f --- /dev/null +++ b/config.go @@ -0,0 +1,46 @@ +package smtp2shoutrrr + +import ( + "log/slog" + "net/url" +) + +type Config struct { + Port int + Username string + Password string + Recipients []ConfigRecipient +} + +func (c *Config) SetDefaults() { + if c.Port == 0 { + c.Port = 11125 + } + + if c.Username == "" { + slog.Warn("no username provided, using default: username") + c.Username = "username" + } + + if c.Password == "" { + slog.Warn("no password provided, using default: password") + c.Password = "password" + } +} + +type ConfigRecipient struct { + Addresses []string // email addresses + Target string // shoutrrr address + targetURL *url.URL +} + +func (cr ConfigRecipient) GetTargetURL() *url.URL { + if cr.targetURL == nil { + var err error + cr.targetURL, err = url.Parse(cr.Target) + if err != nil { + slog.Error("failed to parse shoutrrr target URL", slog.String("target", cr.Target), slog.String("err", err.Error())) + } + } + return cr.targetURL +}