From 9cfe0a6d4f104bb933af70e6a579df74254484b2 Mon Sep 17 00:00:00 2001 From: Aine Date: Tue, 8 Nov 2022 21:21:06 +0200 Subject: [PATCH] show multi-domain aliases everywhere --- README.md | 7 ++++--- bot/command.go | 9 +++++---- bot/command_admin.go | 11 ++++++----- bot/command_owner.go | 6 ++++-- utils/utils.go | 16 ++++++++++++++++ 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 2beefd1..4964fb0 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ so you can use it to send emails from your apps and scripts as well. - [x] Receive attachments - [x] Catch-all mailbox - [x] Map email threads to matrix threads +- [x] Multi-domain aliases #### deep dive @@ -45,15 +46,15 @@ env vars * **POSTMOOGLE_HOMESERVER** - homeserver url, eg: `https://matrix.example.com` * **POSTMOOGLE_LOGIN** - user login/localpart, eg: `moogle` * **POSTMOOGLE_PASSWORD** - user password -* **POSTMOOGLE_DOMAINS** - space separated list of SMTP domains to listen for new emails. The first domain acts as actual domain, all other as aliases +* **POSTMOOGLE_DOMAINS** - space separated list of SMTP domains to listen for new emails. The first domain acts as the main (actual) domain, all other as aliases
other optional config parameters * **POSTMOOGLE_PORT** - SMTP port to listen for new emails * **POSTMOOGLE_TLS_PORT** - secure SMTP port to listen for new emails. Requires valid cert and key as well -* **POSTMOOGLE_TLS_CERT** - path to your SSL certificate (chain) -* **POSTMOOGLE_TLS_KEY** - path to your SSL certificate's private key +* **POSTMOOGLE_TLS_CERT** - path to the SSL certificate (chain) of your main domain +* **POSTMOOGLE_TLS_KEY** - path to the SSL certificate's private key of your main domain * **POSTMOOGLE_TLS_REQUIRED** - require TLS connection, **even** on the non-TLS port (`POSTMOOGLE_PORT`). TLS connections are always required on the TLS port (`POSTMOOGLE_TLS_PORT`) regardless of this setting. * **POSTMOOGLE_DATA_SECRET** - secure key (password) to encrypt account data, must be 16, 24, or 32 bytes long * **POSTMOOGLE_NOENCRYPTION** - disable matrix encryption (libolm) support diff --git a/bot/command.go b/bot/command.go index a973635..5685318 100644 --- a/bot/command.go +++ b/bot/command.go @@ -261,8 +261,8 @@ func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) { msg.WriteString(roomOptionMailbox) msg.WriteString(" SOME_INBOX` command.\n") - msg.WriteString("You will then be able to send emails to `SOME_INBOX@") - msg.WriteString(b.domains[0]) + msg.WriteString("You will then be able to send emails to ") + msg.WriteString(utils.EmailsList("SOME_INBOX", b.domains)) msg.WriteString("` and have them appear in this room.") b.SendNotice(ctx, roomID, msg.String()) @@ -300,8 +300,9 @@ func (b *Bot) sendHelp(ctx context.Context) { msg.WriteString("(currently `") msg.WriteString(value) if cmd.key == roomOptionMailbox { - msg.WriteString("@") - msg.WriteString(b.domains[0]) + msg.WriteString(" (") + msg.WriteString(utils.EmailsList(value, b.domains)) + msg.WriteString(")") } msg.WriteString("`)") } diff --git a/bot/command_admin.go b/bot/command_admin.go index ee7615e..3432640 100644 --- a/bot/command_admin.go +++ b/bot/command_admin.go @@ -53,9 +53,7 @@ func (b *Bot) sendMailboxes(ctx context.Context) { for _, mailbox := range slice { cfg := mailboxes[mailbox] msg.WriteString("* `") - msg.WriteString(mailbox) - msg.WriteString("@") - msg.WriteString(b.domains[0]) + msg.WriteString(utils.EmailsList(mailbox, b.domains)) msg.WriteString("` by ") msg.WriteString(cfg.Owner()) msg.WriteString("\n") @@ -160,7 +158,7 @@ func (b *Bot) runDKIM(ctx context.Context, commandSlice []string) { b.SendNotice(ctx, evt.RoomID, fmt.Sprintf( "DKIM signature is: `%s`.\n"+ - "You need to add it to your DNS records (if not already):\n"+ + "You need to add it to DNS records of all domains added to postmoogle (if not already):\n"+ "Add new DNS record with type = `TXT`, key (subdomain/from): `postmoogle._domainkey` and value (to):\n ```\n%s\n```\n"+ "Without that record other email servers may reject your emails as spam, kupo.\n"+ "To reset the signature, send `%s dkim reset`", @@ -175,6 +173,9 @@ func (b *Bot) runCatchAll(ctx context.Context, commandSlice []string) { msg.WriteString("Currently: `") if cfg.CatchAll() != "" { msg.WriteString(cfg.CatchAll()) + msg.WriteString(" (") + msg.WriteString(utils.EmailsList(cfg.CatchAll(), b.domains)) + msg.WriteString(")") } else { msg.WriteString("not set") } @@ -202,5 +203,5 @@ func (b *Bot) runCatchAll(ctx context.Context, commandSlice []string) { return } - b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Catch-all is set to: `%s@%s`.", mailbox, b.domains[0])) + b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Catch-all is set to: `%s` (%s).", mailbox, utils.EmailsList(mailbox, b.domains))) } diff --git a/bot/command_owner.go b/bot/command_owner.go index 3c56002..ff8c7e7 100644 --- a/bot/command_owner.go +++ b/bot/command_owner.go @@ -5,6 +5,8 @@ import ( "fmt" "github.com/raja/argon2pw" + + "gitlab.com/etke.cc/postmoogle/utils" ) func (b *Bot) runStop(ctx context.Context) { @@ -58,7 +60,7 @@ func (b *Bot) getOption(ctx context.Context, name string) { } if name == roomOptionMailbox { - value = value + "@" + b.domains[0] + value = utils.EmailsList(value, b.domains) } msg := fmt.Sprintf("`%s` of this room is `%s`\n"+ @@ -85,7 +87,7 @@ func (b *Bot) setOption(ctx context.Context, name, value string) { if name == roomOptionMailbox { existingID, ok := b.getMapping(value) if ok && existingID != "" && existingID != evt.RoomID { - b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken, kupo", value, b.domains[0])) + b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s` (%s) already taken, kupo", value, utils.EmailsList(value, b.domains))) return } } diff --git a/utils/utils.go b/utils/utils.go index f49939a..5b010b5 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -14,6 +14,22 @@ func Mailbox(email string) string { return email[:index] } +// EmailsList returns human-readable list of mailbox's emails for all available domains +func EmailsList(mailbox string, domains []string) string { + var msg strings.Builder + count := len(domains) - 1 + for i, domain := range domains { + msg.WriteString(mailbox) + msg.WriteString("@") + msg.WriteString(domain) + if i < count { + msg.WriteString(", ") + } + } + + return msg.String() +} + // Hostname returns hostname part from email address func Hostname(email string) string { return email[strings.LastIndex(email, "@")+1:]