add !pm relay - per-mailbox relay config

This commit is contained in:
Aine
2024-05-02 11:28:37 +03:00
parent 6a63e44bfc
commit ea1533acae
13 changed files with 135 additions and 32 deletions

View File

@@ -35,6 +35,7 @@ so you can use it to send emails from your apps and scripts as well.
- [x] SMTP client - [x] SMTP client
- [x] SMTP server (you can use Postmoogle as general purpose SMTP server to send emails from your scripts or apps) - [x] SMTP server (you can use Postmoogle as general purpose SMTP server to send emails from your scripts or apps)
- [x] SMTP Relaying (postmoogle can send emails via relay host), global and per-mailbox
- [x] Send a message to matrix room with special format to send a new email, even to multiple email addresses at once - [x] Send a message to matrix room with special format to send a new email, even to multiple email addresses at once
- [x] Reply to matrix thread sends reply into email thread - [x] Reply to matrix thread sends reply into email thread
- [x] Email signatures - [x] Email signatures
@@ -76,10 +77,10 @@ env vars
* **POSTMOOGLE_MAILBOXES_ACTIVATION** - activation flow for new mailboxes, [docs/mailboxes.md](docs/mailboxes.md) * **POSTMOOGLE_MAILBOXES_ACTIVATION** - activation flow for new mailboxes, [docs/mailboxes.md](docs/mailboxes.md)
* **POSTMOOGLE_MAXSIZE** - max email size (including attachments) in megabytes * **POSTMOOGLE_MAXSIZE** - max email size (including attachments) in megabytes
* **POSTMOOGLE_ADMINS** - a space-separated list of admin users. See `POSTMOOGLE_USERS` for syntax examples * **POSTMOOGLE_ADMINS** - a space-separated list of admin users. See `POSTMOOGLE_USERS` for syntax examples
* **POSTMOOGLE_RELAY_HOST** - SMTP hostname of relay host (e.g. Sendgrid) * **POSTMOOGLE_RELAY_HOST** - (global) SMTP hostname of relay host (e.g. Sendgrid)
* **POSTMOOGLE_RELAY_PORT** - SMTP port of relay host * **POSTMOOGLE_RELAY_PORT** - (global) SMTP port of relay host
* **POSTMOOGLE_RELAY_USERNAME** - Username of relay host * **POSTMOOGLE_RELAY_USERNAME** - (global) Username of relay host
* **POSTMOOGLE_RELAY_PASSWORD** - Password of relay host * **POSTMOOGLE_RELAY_PASSWORD** - (global) Password of relay host
You can find default values in [config/defaults.go](config/defaults.go) You can find default values in [config/defaults.go](config/defaults.go)
@@ -118,7 +119,7 @@ If you want to change them - check available options in the help message (`!pm h
* **`!pm domain`** - Get or set default domain of the room * **`!pm domain`** - Get or set default domain of the room
* **`!pm owner`** - Get or set owner of the room * **`!pm owner`** - Get or set owner of the room
* **`!pm password`** - Get or set SMTP password of the room's mailbox * **`!pm password`** - Get or set SMTP password of the room's mailbox
* **`!pm relay`** - Get or set SMTP relay of that mailbox. Format: `smtp://user:password@host:port`, e.g. `smtp://54b7bfb9-b95f-44b8-9879-9b560baf4e3a:8528a3a9-bea8-4583-9912-d4357ba565eb@example.com:587`
--- ---
#### mailbox options #### mailbox options

View File

@@ -3,6 +3,7 @@ package bot
import ( import (
"context" "context"
"fmt" "fmt"
"net/url"
"regexp" "regexp"
"sync" "sync"
@@ -36,7 +37,7 @@ type Bot struct {
commands commandList commands commandList
rooms sync.Map rooms sync.Map
proxies []string proxies []string
sendmail func(string, string, string) error sendmail func(string, string, string, *url.URL) error
psdc *psd.Client psdc *psd.Client
cfg *config.Manager cfg *config.Manager
log *zerolog.Logger log *zerolog.Logger

View File

@@ -103,6 +103,12 @@ func (b *Bot) initCommands() commandList {
description: "Get or set SMTP password of the room's mailbox", description: "Get or set SMTP password of the room's mailbox",
allowed: b.allowOwner, allowed: b.allowOwner,
}, },
{
key: config.RoomRelay,
description: "Configure SMTP Relay for that mailbox, format: `smtp://user:pass@host:port`",
sanitizer: utils.SanitizeURL,
allowed: b.allowOwner,
},
{allowed: b.allowOwner, description: "mailbox options"}, // delimiter {allowed: b.allowOwner, description: "mailbox options"}, // delimiter
{ {
key: config.RoomAutoreply, key: config.RoomAutoreply,
@@ -630,7 +636,7 @@ func (b *Bot) runSendCommand(ctx context.Context, cfg config.Room, tos []string,
b.lp.SendNotice(ctx, evt.RoomID, "email body is empty", linkpearl.RelatesTo(evt.ID, cfg.NoThreads())) b.lp.SendNotice(ctx, evt.RoomID, "email body is empty", linkpearl.RelatesTo(evt.ID, cfg.NoThreads()))
return return
} }
queued, err := b.Sendmail(ctx, evt.ID, from, to, data) queued, err := b.Sendmail(ctx, evt.ID, from, to, data, cfg.Relay())
if queued { if queued {
b.log.Warn().Err(err).Msg("email has been queued") b.log.Warn().Err(err).Msg("email has been queued")
b.saveSentMetadata(ctx, queued, evt.ID, to, eml, cfg) b.saveSentMetadata(ctx, queued, evt.ID, to, eml, cfg)

View File

@@ -51,6 +51,8 @@ func (b *Bot) handleOption(ctx context.Context, cmd []string) {
b.setMailbox(ctx, cmd[1]) b.setMailbox(ctx, cmd[1])
case config.RoomPassword: case config.RoomPassword:
b.setPassword(ctx) b.setPassword(ctx)
case config.RoomRelay:
b.setRelay(ctx)
default: default:
b.setOption(ctx, cmd[0], cmd[1]) b.setOption(ctx, cmd[0], cmd[1])
} }
@@ -152,6 +154,25 @@ func (b *Bot) setPassword(ctx context.Context) {
b.lp.SendNotice(ctx, evt.RoomID, "SMTP password has been set", linkpearl.RelatesTo(evt.ID, cfg.NoThreads())) b.lp.SendNotice(ctx, evt.RoomID, "SMTP password has been set", linkpearl.RelatesTo(evt.ID, cfg.NoThreads()))
} }
func (b *Bot) setRelay(ctx context.Context) {
evt := eventFromContext(ctx)
cfg, err := b.cfg.GetRoom(ctx, evt.RoomID)
if err != nil {
b.Error(ctx, "failed to retrieve settings: %v", err)
return
}
value := b.parseCommand(evt.Content.AsMessage().Body, false)[1] // get original value, without forced lower case
cfg.Set(config.RoomRelay, value)
err = b.cfg.SetRoom(ctx, evt.RoomID, cfg)
if err != nil {
b.Error(ctx, "cannot update settings: %v", err)
return
}
b.lp.SendNotice(ctx, evt.RoomID, "Relay config has been set", linkpearl.RelatesTo(evt.ID, cfg.NoThreads()))
}
func (b *Bot) setOption(ctx context.Context, name, value string) { func (b *Bot) setOption(ctx context.Context, name, value string) {
cmd := b.commands.get(name) cmd := b.commands.get(name)
if cmd != nil && cmd.sanitizer != nil { if cmd != nil && cmd.sanitizer != nil {

View File

@@ -1,8 +1,11 @@
package config package config
import ( import (
"fmt"
"net/url"
"strings" "strings"
"gitlab.com/etke.cc/go/healthchecks/v2"
"gitlab.com/etke.cc/postmoogle/email" "gitlab.com/etke.cc/postmoogle/email"
"gitlab.com/etke.cc/postmoogle/utils" "gitlab.com/etke.cc/postmoogle/utils"
) )
@@ -21,6 +24,7 @@ const (
RoomPassword = "password" RoomPassword = "password"
RoomSignature = "signature" RoomSignature = "signature"
RoomAutoreply = "autoreply" RoomAutoreply = "autoreply"
RoomRelay = "relay"
RoomThreadify = "threadify" RoomThreadify = "threadify"
RoomStripify = "stripify" RoomStripify = "stripify"
@@ -69,6 +73,20 @@ func (s Room) Active() bool {
return utils.Bool(s.Get(RoomActive)) return utils.Bool(s.Get(RoomActive))
} }
// Relay returns the SMTP Relay configuration in a manner of URL: smtp://user:pass@host:port
func (s Room) Relay() *url.URL {
relay := s.Get(RoomRelay)
if relay == "" {
return nil
}
u, err := url.Parse(relay)
if err != nil {
healthchecks.Global().Fail(strings.NewReader(fmt.Sprintf("cannot parse relay URL %q: %v", relay, err)))
return nil
}
return u
}
func (s Room) Password() string { func (s Room) Password() string {
return s.Get(RoomPassword) return s.Get(RoomPassword)
} }

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"net/url"
"strings" "strings"
"time" "time"
@@ -36,7 +37,7 @@ const (
var ErrNoRoom = errors.New("room not found") var ErrNoRoom = errors.New("room not found")
// SetSendmail sets mail sending func to the bot // SetSendmail sets mail sending func to the bot
func (b *Bot) SetSendmail(sendmail func(string, string, string) error) { func (b *Bot) SetSendmail(sendmail func(string, string, string, *url.URL) error) {
b.sendmail = sendmail b.sendmail = sendmail
b.q.SetSendmail(sendmail) b.q.SetSendmail(sendmail)
} }
@@ -60,14 +61,14 @@ func (b *Bot) shouldQueue(msg string) bool {
// Sendmail tries to send email immediately, but if it gets 4xx error (greylisting), // Sendmail tries to send email immediately, but if it gets 4xx error (greylisting),
// the email will be added to the queue and retried several times after that // the email will be added to the queue and retried several times after that
func (b *Bot) Sendmail(ctx context.Context, eventID id.EventID, from, to, data string) (bool, error) { func (b *Bot) Sendmail(ctx context.Context, eventID id.EventID, from, to, data string, relayOverride *url.URL) (bool, error) {
log := b.log.With().Str("from", from).Str("to", to).Str("eventID", eventID.String()).Logger() log := b.log.With().Str("from", from).Str("to", to).Str("eventID", eventID.String()).Logger()
log.Info().Msg("attempting to deliver email") log.Info().Msg("attempting to deliver email")
err := b.sendmail(from, to, data) err := b.sendmail(from, to, data, relayOverride)
if err != nil { if err != nil {
if b.shouldQueue(err.Error()) { if b.shouldQueue(err.Error()) {
log.Info().Err(err).Msg("email has been added to the queue") log.Info().Err(err).Msg("email has been added to the queue")
return true, b.q.Add(ctx, eventID.String(), from, to, data) return true, b.q.Add(ctx, eventID.String(), from, to, data, relayOverride)
} }
log.Warn().Err(err).Msg("email delivery failed") log.Warn().Err(err).Msg("email delivery failed")
return false, err return false, err
@@ -82,6 +83,16 @@ func (b *Bot) GetDKIMprivkey(ctx context.Context) string {
return b.cfg.GetBot(ctx).DKIMPrivateKey() return b.cfg.GetBot(ctx).DKIMPrivateKey()
} }
// GetRelayConfig returns relay config for specific room (mailbox) if set
func (b *Bot) GetRelayConfig(ctx context.Context, roomID id.RoomID) *url.URL {
cfg, err := b.cfg.GetRoom(ctx, roomID)
if err != nil {
b.log.Error().Err(err).Str("room_id", roomID.String()).Msg("cannot get room config")
return nil
}
return cfg.Relay()
}
func (b *Bot) getMapping(mailbox string) (id.RoomID, bool) { func (b *Bot) getMapping(mailbox string) (id.RoomID, bool) {
v, ok := b.rooms.Load(mailbox) v, ok := b.rooms.Load(mailbox)
if !ok { if !ok {
@@ -277,7 +288,7 @@ func (b *Bot) sendAutoreply(ctx context.Context, roomID id.RoomID, threadID id.E
ctx = newContext(ctx, threadEvt) ctx = newContext(ctx, threadEvt)
recipients := meta.Recipients recipients := meta.Recipients
for _, to := range recipients { for _, to := range recipients {
queued, err = b.Sendmail(ctx, evt.ID, meta.From, to, data) queued, err = b.Sendmail(ctx, evt.ID, meta.From, to, data, cfg.Relay())
if queued { if queued {
b.log.Info().Err(err).Str("from", meta.From).Str("to", to).Msg("email has been queued") b.log.Info().Err(err).Str("from", meta.From).Str("to", to).Msg("email has been queued")
b.saveSentMetadata(ctx, queued, meta.ThreadID, to, eml, cfg, "Autoreply has been sent to "+to+" (queued)") b.saveSentMetadata(ctx, queued, meta.ThreadID, to, eml, cfg, "Autoreply has been sent to "+to+" (queued)")
@@ -361,7 +372,7 @@ func (b *Bot) SendEmailReply(ctx context.Context) {
var queued bool var queued bool
recipients := meta.Recipients recipients := meta.Recipients
for _, to := range recipients { for _, to := range recipients {
queued, err = b.Sendmail(ctx, evt.ID, meta.From, to, data) queued, err = b.Sendmail(ctx, evt.ID, meta.From, to, data, cfg.Relay())
if queued { if queued {
b.log.Info().Err(err).Str("from", meta.From).Str("to", to).Msg("email has been queued") b.log.Info().Err(err).Str("from", meta.From).Str("to", to).Msg("email has been queued")
b.saveSentMetadata(ctx, queued, meta.ThreadID, to, eml, cfg) b.saveSentMetadata(ctx, queued, meta.ThreadID, to, eml, cfg)

View File

@@ -2,6 +2,7 @@ package queue
import ( import (
"context" "context"
"net/url"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"gitlab.com/etke.cc/linkpearl" "gitlab.com/etke.cc/linkpearl"
@@ -22,7 +23,7 @@ type Queue struct {
lp *linkpearl.Linkpearl lp *linkpearl.Linkpearl
cfg *config.Manager cfg *config.Manager
log *zerolog.Logger log *zerolog.Logger
sendmail func(string, string, string) error sendmail func(string, string, string, *url.URL) error
} }
// New queue // New queue
@@ -36,7 +37,7 @@ func New(lp *linkpearl.Linkpearl, cfg *config.Manager, log *zerolog.Logger) *Que
} }
// SetSendmail func // SetSendmail func
func (q *Queue) SetSendmail(function func(string, string, string) error) { func (q *Queue) SetSendmail(function func(string, string, string, *url.URL) error) {
q.sendmail = function q.sendmail = function
} }

View File

@@ -2,14 +2,20 @@ package queue
import ( import (
"context" "context"
"net/url"
"strconv" "strconv"
) )
// Add to queue // Add to queue
func (q *Queue) Add(ctx context.Context, id, from, to, data string) error { func (q *Queue) Add(ctx context.Context, id, from, to, data string, relayOverride ...*url.URL) error {
itemkey := acQueueKey + "." + id itemkey := acQueueKey + "." + id
relay := ""
if len(relayOverride) > 0 {
relay = relayOverride[0].String()
}
item := map[string]string{ item := map[string]string{
"attempts": "0", "attempts": "0",
"relay": relay,
"data": data, "data": data,
"from": from, "from": from,
"to": to, "to": to,
@@ -84,7 +90,12 @@ func (q *Queue) try(ctx context.Context, itemkey string, maxRetries int) bool {
return true return true
} }
err = q.sendmail(item["from"], item["to"], item["data"]) var relayOverride *url.URL
if item["relay"] != "" {
relayOverride, _ = url.Parse(item["relay"]) //nolint:errcheck // doesn't matter
}
err = q.sendmail(item["from"], item["to"], item["data"], relayOverride)
if err == nil { if err == nil {
q.log.Info().Str("id", itemkey).Msg("email from queue was delivered") q.log.Info().Str("id", itemkey).Msg("email from queue was delivered")
return true return true

View File

@@ -148,7 +148,7 @@ func initSMTP(cfg *config.Config) {
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, Username: cfg.Relay.Username,
Password: cfg.Relay.Password, Password: cfg.Relay.Password,
}, },
}) })

View File

@@ -6,13 +6,14 @@ import (
"io" "io"
"net" "net"
"net/smtp" "net/smtp"
"net/url"
"strings" "strings"
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
type MailSender interface { type MailSender interface {
Send(from, to, data string) error Send(from, to, data string, relayOverride *url.URL) error
} }
// SMTP client // SMTP client
@@ -30,16 +31,35 @@ func newClient(cfg *RelayConfig, log *zerolog.Logger) *Client {
} }
} }
// relayFromURL creates a RelayConfig from a URL
func relayFromURL(relayURL *url.URL) *RelayConfig {
if relayURL == nil {
return nil
}
password, _ := relayURL.User.Password()
return &RelayConfig{
Host: relayURL.Hostname(),
Port: relayURL.Port(),
Username: relayURL.User.Username(),
Password: password,
}
}
// Send email // Send email
func (c Client) Send(from, to, data string) error { func (c Client) Send(from, to, data string, relayOverride *url.URL) error {
log := c.log.With().Str("from", from).Str("to", to).Logger() log := c.log.With().Str("from", from).Str("to", to).Logger()
log.Debug().Msg("sending email") log.Debug().Msg("sending email")
relay := c.config
if relayOverrideCfg := relayFromURL(relayOverride); relayOverrideCfg != nil {
relay = relayOverrideCfg
}
var conn *smtp.Client var conn *smtp.Client
var err error var err error
if c.config.Host != "" { if relay != nil && relay.Host != "" {
log.Debug().Msg("creating relay client...") log.Debug().Msg("creating relay client...")
conn, err = c.createRelayClient(from, to) conn, err = c.createRelayClient(relay, from, to)
} else { } else {
log.Debug().Msg("trying direct SMTP connection...") log.Debug().Msg("trying direct SMTP connection...")
conn, err = c.createDirectClient(from, to) conn, err = c.createDirectClient(from, to)
@@ -73,9 +93,9 @@ func (c Client) Send(from, to, data string) error {
} }
// createRelayClientconnects directly to the provided smtp host // createRelayClientconnects directly to the provided smtp host
func (c *Client) createRelayClient(from, to string) (*smtp.Client, error) { func (c *Client) createRelayClient(config *RelayConfig, from, 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 := config.Host + ":" + config.Port
conn, err := smtp.Dial(target) conn, err := smtp.Dial(target)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -87,12 +107,12 @@ func (c *Client) createRelayClient(from, to string) (*smtp.Client, error) {
} }
if ok, _ := conn.Extension("STARTTLS"); ok { if ok, _ := conn.Extension("STARTTLS"); ok {
config := &tls.Config{ServerName: c.config.Host} //nolint:gosec // it's smtp, even that is too strict sometimes tlsConfig := &tls.Config{ServerName: config.Host} //nolint:gosec // it's smtp, even that is too strict sometimes
conn.StartTLS(config) //nolint:errcheck // if it doesn't work - we can't do anything anyway conn.StartTLS(tlsConfig) //nolint:errcheck // if it doesn't work - we can't do anything anyway
} }
if c.config.Usename != "" { if config.Username != "" {
err = conn.Auth(smtp.PlainAuth("", c.config.Usename, c.config.Password, c.config.Host)) err = conn.Auth(smtp.PlainAuth("", config.Username, config.Password, config.Host))
if err != nil { if err != nil {
conn.Close() conn.Close()
return nil, err return nil, err

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"net" "net"
"net/url"
"sync" "sync"
"time" "time"
@@ -44,7 +45,7 @@ type TLSConfig struct {
type RelayConfig struct { type RelayConfig struct {
Host string Host string
Port string Port string
Usename string Username string
Password string Password string
} }
@@ -70,11 +71,12 @@ type matrixbot interface {
GetIFOptions(context.Context, id.RoomID) email.IncomingFilteringOptions GetIFOptions(context.Context, id.RoomID) email.IncomingFilteringOptions
IncomingEmail(context.Context, *email.Email) error IncomingEmail(context.Context, *email.Email) error
GetDKIMprivkey(context.Context) string GetDKIMprivkey(context.Context) string
GetRelayConfig(context.Context, id.RoomID) *url.URL
} }
// Caller is Sendmail caller // Caller is Sendmail caller
type Caller interface { type Caller interface {
SetSendmail(func(string, string, string) error) SetSendmail(func(string, string, string, *url.URL) error)
} }
// NewManager creates new SMTP server manager // NewManager creates new SMTP server manager

View File

@@ -6,6 +6,7 @@ import (
"errors" "errors"
"io" "io"
"net" "net"
"net/url"
"strconv" "strconv"
"github.com/emersion/go-msgauth/dkim" "github.com/emersion/go-msgauth/dkim"
@@ -40,7 +41,7 @@ type session struct {
ctx context.Context //nolint:containedctx // that's session ctx context.Context //nolint:containedctx // that's session
conn *smtp.Conn conn *smtp.Conn
domains []string domains []string
sendmail func(string, string, string) error sendmail func(string, string, string, *url.URL) error
dir string dir string
tos []string tos []string
@@ -124,7 +125,7 @@ func (s *session) outgoingData(r io.Reader) error {
eml := email.FromEnvelope(s.tos[0], envelope) eml := email.FromEnvelope(s.tos[0], envelope)
for _, to := range s.tos { for _, to := range s.tos {
eml.RcptTo = to eml.RcptTo = to
err := s.sendmail(eml.From, to, eml.Compose(s.privkey)) err := s.sendmail(eml.From, to, eml.Compose(s.privkey), s.bot.GetRelayConfig(s.ctx, s.fromRoom))
if err != nil { if err != nil {
return err return err
} }

View File

@@ -2,6 +2,7 @@ package utils
import ( import (
"net" "net"
"net/url"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@@ -40,6 +41,15 @@ func SanitizeDomain(domain string) string {
return domains[0] return domains[0]
} }
// SanitizeURL checks that input URL is valid
func SanitizeURL(str string) string {
parsed, err := url.Parse(str)
if err != nil {
return ""
}
return parsed.String()
}
// Bool converts string to boolean // Bool converts string to boolean
func Bool(str string) bool { func Bool(str string) bool {
str = strings.ToLower(str) str = strings.ToLower(str)