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,
Bot: mxb,
Callers: []smtp.Caller{mxb, q},
Relay: smtp.RelayConfig{
Relay: &smtp.RelayConfig{
Host: cfg.Relay.Host,
Port: cfg.Relay.Port,
Usename: cfg.Relay.Username,

View File

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

View File

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

View File

@@ -26,10 +26,10 @@ var (
)
type mailServer struct {
bot matrixbot
log *logger.Logger
domains []string
mailSender MailSender
bot matrixbot
log *logger.Logger
domains []string
sender MailSender
}
// 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{
ctx: sentry.SetHubOnContext(context.Background(), sentry.CurrentHub().Clone()),
sendmail: m.SendEmail,
sendmail: m.sender.Send,
privkey: m.bot.GetDKIMprivkey(),
from: username,
log: m.log,
@@ -87,11 +87,6 @@ func (m *mailServer) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session,
}, 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
func (m *mailServer) ReceiveEmail(ctx context.Context, eml *email.Email) error {
return m.bot.IncomingEmail(ctx, eml)