refactor a bit

This commit is contained in:
Aine
2023-05-10 22:43:52 +03:00
parent 81c6d5abf1
commit 9bcc2d462f
4 changed files with 26 additions and 32 deletions

View File

@@ -143,7 +143,7 @@ func initSMTP(cfg *config.Config) {
MaxSize: cfg.MaxSize, MaxSize: cfg.MaxSize,
Bot: mxb, Bot: mxb,
Callers: []smtp.Caller{mxb, q}, Callers: []smtp.Caller{mxb, q},
Relay: smtp.RelayConfig{ Relay: &smtp.RelayConfig{
Host: cfg.Relay.Host, Host: cfg.Relay.Host,
Port: cfg.Relay.Port, Port: cfg.Relay.Port,
Usename: cfg.Relay.Username, Usename: cfg.Relay.Username,

View File

@@ -14,21 +14,31 @@ type MailSender interface {
Send(from string, to string, data string) error Send(from string, to string, data string) error
} }
// SMTP client
type Client struct { type Client struct {
config *RelayConfig config *RelayConfig
log *logger.Logger log *logger.Logger
} }
func newClient(cfg *RelayConfig, log *logger.Logger) Client { func newClient(cfg *RelayConfig, log *logger.Logger) *Client {
return Client{ return &Client{
config: cfg, config: cfg,
log: log, log: log,
} }
} }
// Send email
func (c Client) Send(from string, to string, data string) error { func (c Client) Send(from string, to string, data string) error {
c.log.Debug("Sending email from %s to %s", from, to) c.log.Debug("Sending email from %s to %s", from, to)
conn, err := c.createSmtpClient(from, to)
var conn *smtp.Client
var err error
if c.config.Host != "" {
conn, err = c.createDirectClient(from, to)
} else {
conn, err = trysmtp.Connect(from, to)
}
if conn == nil { if conn == nil {
c.log.Error("cannot connect to SMTP server of %s: %v", to, err) c.log.Error("cannot connect to SMTP server of %s: %v", to, err)
return err return err
@@ -56,19 +66,11 @@ func (c Client) Send(from string, to string, data string) error {
return nil return nil
} }
func (c *Client) createSmtpClient(from string, to string) (*smtp.Client, error) { // createDirectClient connects directly to the provided smtp host
if c.config.Host != "" {
return c.createDirectClient(from, to)
}
return trysmtp.Connect(from, to)
}
func (c *Client) createDirectClient(from string, to string) (*smtp.Client, error) { func (c *Client) createDirectClient(from string, to string) (*smtp.Client, error) {
localname := strings.SplitN(from, "@", 2)[1] localname := strings.SplitN(from, "@", 2)[1]
target := c.config.Host + ":" + c.config.Port target := c.config.Host + ":" + c.config.Port
conn, err := smtp.Dial(target) conn, err := smtp.Dial(target)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -29,7 +29,7 @@ type Config struct {
MaxSize int MaxSize int
Bot matrixbot Bot matrixbot
Callers []Caller Callers []Caller
Relay RelayConfig Relay *RelayConfig
} }
type TLSConfig struct { type TLSConfig struct {
@@ -79,17 +79,14 @@ type Caller interface {
// NewManager creates new SMTP server manager // NewManager creates new SMTP server manager
func NewManager(cfg *Config) *Manager { func NewManager(cfg *Config) *Manager {
log := logger.New("smtp.", cfg.LogLevel) log := logger.New("smtp.", cfg.LogLevel)
smtpClient := newClient(&cfg.Relay, log)
mailsrv := &mailServer{ mailsrv := &mailServer{
log: log, log: log,
bot: cfg.Bot, bot: cfg.Bot,
domains: cfg.Domains, domains: cfg.Domains,
mailSender: smtpClient, sender: newClient(cfg.Relay, log),
} }
for _, caller := range cfg.Callers { for _, caller := range cfg.Callers {
caller.SetSendmail(mailsrv.SendEmail) caller.SetSendmail(mailsrv.sender.Send)
} }
s := smtp.NewServer(mailsrv) s := smtp.NewServer(mailsrv)

View File

@@ -29,7 +29,7 @@ type mailServer struct {
bot matrixbot bot matrixbot
log *logger.Logger log *logger.Logger
domains []string domains []string
mailSender MailSender sender MailSender
} }
// Login used for outgoing mail submissions only (when you use postmoogle as smtp server in your scripts) // Login used for outgoing mail submissions only (when you use postmoogle as smtp server in your scripts)
@@ -54,7 +54,7 @@ func (m *mailServer) Login(state *smtp.ConnectionState, username, password strin
return &outgoingSession{ return &outgoingSession{
ctx: sentry.SetHubOnContext(context.Background(), sentry.CurrentHub().Clone()), ctx: sentry.SetHubOnContext(context.Background(), sentry.CurrentHub().Clone()),
sendmail: m.SendEmail, sendmail: m.sender.Send,
privkey: m.bot.GetDKIMprivkey(), privkey: m.bot.GetDKIMprivkey(),
from: username, from: username,
log: m.log, log: m.log,
@@ -87,11 +87,6 @@ func (m *mailServer) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session,
}, nil }, nil
} }
// SendEmail to external mail server
func (m *mailServer) SendEmail(from, to, data string) error {
return m.mailSender.Send(from, to, data)
}
// ReceiveEmail - incoming mail into matrix room // ReceiveEmail - incoming mail into matrix room
func (m *mailServer) ReceiveEmail(ctx context.Context, eml *email.Email) error { func (m *mailServer) ReceiveEmail(ctx context.Context, eml *email.Email) error {
return m.bot.IncomingEmail(ctx, eml) return m.bot.IncomingEmail(ctx, eml)