refactor: configuration into separate file

This commit is contained in:
Felipe M 2024-11-24 12:08:35 +01:00
parent f1733d3670
commit c829408095
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8
2 changed files with 57 additions and 35 deletions

View file

@ -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)

46
config.go Normal file
View file

@ -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
}