diff --git a/config.toml b/config.toml index 6591802..362f514 100644 --- a/config.toml +++ b/config.toml @@ -1,3 +1,5 @@ +Port = 11025 + [[Recipients]] Addresses = ["something@something.com"] Target = "ntfy://ntfy.sh/fmartingr-dev" diff --git a/main.go b/main.go index ed11bee..d2a25f2 100644 --- a/main.go +++ b/main.go @@ -8,10 +8,12 @@ import ( "log/slog" "net/mail" "net/url" + "os" "slices" "strings" "time" + "git.nakama.town/fmartingr/gotoolkit/encoding" "git.nakama.town/fmartingr/gotoolkit/model" "git.nakama.town/fmartingr/gotoolkit/service" "github.com/containrrr/shoutrrr" @@ -44,21 +46,28 @@ func (re *ReceivedEmail) Body() string { } 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 + Target string // shoutrrr address targetURL *url.URL } -func (cr ConfigRecipient) Target() *url.URL { +func (cr ConfigRecipient) GetTargetURL() *url.URL { if cr.targetURL == nil { var err error - cr.targetURL, err = url.Parse(cr.target) + 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())) + slog.Error("failed to parse shoutrrr target URL", slog.String("target", cr.Target), slog.String("err", err.Error())) } } return cr.targetURL @@ -85,9 +94,22 @@ func (s *smtpServer) Stop(ctx context.Context) error { return s.backend.Shutdown(ctx) } -func NewSMTPServer(backend *smtp.Server) model.Server { +func NewSMTPServer(config *Config) model.Server { + be := &Backend{ + config: config, + } + + smtpBackend := smtp.NewServer(be) + smtpBackend.Addr = fmt.Sprintf(":%d", config.Port) + // smtpBackend.Domain = "localhost" + smtpBackend.WriteTimeout = 10 * time.Second + smtpBackend.ReadTimeout = 10 * time.Second + smtpBackend.MaxMessageBytes = 1024 * 1024 + smtpBackend.MaxRecipients = 50 + smtpBackend.AllowInsecureAuth = true + return &smtpServer{ - backend: backend, + backend: smtpBackend, } } @@ -104,7 +126,7 @@ func (bkd *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) { } func (bkd *Backend) forwardEmail(email ReceivedEmail) error { - slog.Info("forwading message", slog.String("email", email.String())) + slog.Info("forwading message", slog.String("to", strings.Join(email.Recipients, ","))) for _, r := range bkd.config.Recipients { for _, a := range email.Recipients { @@ -113,7 +135,7 @@ func (bkd *Backend) forwardEmail(email ReceivedEmail) error { "title": {email.Msg.Header.Get("Subject")}, } - destinationURL := r.Target() + destinationURL := r.GetTargetURL() destinationURL.RawQuery = urlParams.Encode() if err := shoutrrr.Send(destinationURL.String(), email.Body()); err != nil { @@ -191,32 +213,27 @@ func (s *Session) Logout() error { } func main() { - config := &Config{ - Recipients: []ConfigRecipient{{ - Addresses: []string{"test@fmartingr.com", "caca@caca.com"}, - target: "ntfy://ntfy.sh/fmartingr-dev", - }, { - Addresses: []string{"something@something.com"}, - target: "ntfy://ntfy.sh/fmartingr-dev", - }}, + var config *Config + configPath := "config.toml" + + f, err := os.Open(configPath) + if err != nil { + slog.Error("Error opening config file", slog.String("err", err.Error()), slog.String("path", configPath)) + return } - be := &Backend{ - config: config, + enc := encoding.NewTOMLEncoding() + if err := enc.DecodeReader(f, &config); err != nil { + slog.Error("Error decoding config file", slog.String("err", err.Error()), slog.String("path", configPath)) + return } - smtpBackend := smtp.NewServer(be) + config.SetDefaults() - smtpBackend.Addr = "localhost:11025" - smtpBackend.Domain = "localhost" - smtpBackend.WriteTimeout = 10 * time.Second - smtpBackend.ReadTimeout = 10 * time.Second - smtpBackend.MaxMessageBytes = 1024 * 1024 - smtpBackend.MaxRecipients = 50 - smtpBackend.AllowInsecureAuth = true + slog.Info("config loaded", slog.Int("recipients", len(config.Recipients))) ctx := context.Background() - smtpServer := NewSMTPServer(smtpBackend) + smtpServer := NewSMTPServer(config) svc, err := service.NewService([]model.Server{ smtpServer,