From d6b6a5dc4484be7598baa666828ec7962c6bebdf Mon Sep 17 00:00:00 2001 From: Aine Date: Tue, 4 Oct 2022 21:45:52 +0300 Subject: [PATCH] add catch-all mailbox, closes #25 --- README.md | 1 + bot/command.go | 8 ++++++++ bot/command_admin.go | 38 ++++++++++++++++++++++++++++++++++++++ bot/email.go | 18 ++++++++++++++++-- bot/settings_bot.go | 6 ++++++ 5 files changed, 69 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2a5a697..6183af3 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,7 @@ If you want to change them - check available options in the help message (`!pm h --- * **!pm dkim** - Get DKIM signature +* **!pm catch-all** - Configure catch-all mailbox * **!pm users** - Get or set allowed users patterns * **!pm mailboxes** - Show the list of all mailboxes * **!pm delete** <mailbox> - Delete specific mailbox diff --git a/bot/command.go b/bot/command.go index 61003df..d933775 100644 --- a/bot/command.go +++ b/bot/command.go @@ -17,6 +17,7 @@ const ( commandStop = "stop" commandSend = "send" commandDKIM = "dkim" + commandCatchAll = botOptionCatchAll commandUsers = botOptionUsers commandDelete = "delete" commandMailboxes = "mailboxes" @@ -144,6 +145,11 @@ func (b *Bot) initCommands() commandList { description: "Get DKIM signature", allowed: b.allowAdmin, }, + { + key: commandCatchAll, + description: "Get or set catch-all mailbox", + allowed: b.allowAdmin, + }, { key: commandMailboxes, description: "Show the list of all mailboxes", @@ -184,6 +190,8 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, commandSlice b.runDKIM(ctx, commandSlice) case commandUsers: b.runUsers(ctx, commandSlice) + case commandCatchAll: + b.runCatchAll(ctx, commandSlice) case commandDelete: b.runDelete(ctx, commandSlice) case commandMailboxes: diff --git a/bot/command_admin.go b/bot/command_admin.go index 22f9e63..89dbd0e 100644 --- a/bot/command_admin.go +++ b/bot/command_admin.go @@ -166,3 +166,41 @@ func (b *Bot) runDKIM(ctx context.Context, commandSlice []string) { "To reset the signature, send `%s dkim reset`", signature, signature, b.prefix)) } + +func (b *Bot) runCatchAll(ctx context.Context, commandSlice []string) { + evt := eventFromContext(ctx) + cfg := b.getBotSettings() + if len(commandSlice) < 2 { + var msg strings.Builder + msg.WriteString("Currently: `") + if cfg.CatchAll() != "" { + msg.WriteString(cfg.CatchAll()) + } else { + msg.WriteString("not set") + } + msg.WriteString("`\n\n") + msg.WriteString("Usage: `") + msg.WriteString(b.prefix) + msg.WriteString(" catch-all MAILBOX`") + msg.WriteString("where mailbox is valid and existing mailbox name\n") + + b.SendNotice(ctx, evt.RoomID, msg.String()) + return + } + + mailbox := utils.Mailbox(commandSlice[1]) + _, ok := b.GetMapping(mailbox) + if !ok { + b.SendError(ctx, evt.RoomID, "mailbox does not exist, kupo.") + return + } + + cfg.Set(botOptionCatchAll, mailbox) + err := b.setBotSettings(cfg) + if err != nil { + b.Error(ctx, evt.RoomID, "cannot save bot options: %v", err) + return + } + + b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Catch-all is set to: `%s@%s`.", mailbox, b.domain)) +} diff --git a/bot/email.go b/bot/email.go index 6418215..b946795 100644 --- a/bot/email.go +++ b/bot/email.go @@ -31,12 +31,12 @@ func (b *Bot) SetMTA(mta utils.MTA) { b.mta = mta } -// GetMapping returns mapping of mailbox = room -func (b *Bot) GetMapping(mailbox string) (id.RoomID, bool) { +func (b *Bot) getMapping(mailbox string) (id.RoomID, bool) { v, ok := b.rooms.Load(mailbox) if !ok { return "", ok } + roomID, ok := v.(id.RoomID) if !ok { return "", ok @@ -45,6 +45,20 @@ func (b *Bot) GetMapping(mailbox string) (id.RoomID, bool) { return roomID, ok } +// GetMapping returns mapping of mailbox = room +func (b *Bot) GetMapping(mailbox string) (id.RoomID, bool) { + roomID, ok := b.getMapping(mailbox) + if !ok { + catchAll := b.getBotSettings().CatchAll() + if catchAll == "" { + return roomID, ok + } + return b.getMapping(catchAll) + } + + return roomID, ok +} + // Send email to matrix room func (b *Bot) Send2Matrix(ctx context.Context, email *utils.Email, incoming bool) error { roomID, ok := b.GetMapping(email.Mailbox(incoming)) diff --git a/bot/settings_bot.go b/bot/settings_bot.go index 94fa98e..269d6d0 100644 --- a/bot/settings_bot.go +++ b/bot/settings_bot.go @@ -12,6 +12,7 @@ const acBotSettingsKey = "cc.etke.postmoogle.config" // bot options keys const ( botOptionUsers = "users" + botOptionCatchAll = "catch-all" botOptionDKIMSignature = "dkim.pub" botOptionDKIMPrivateKey = "dkim.pem" ) @@ -42,6 +43,11 @@ func (s botSettings) Users() []string { return []string{value} } +// CatchAll option +func (s botSettings) CatchAll() string { + return s.Get(botOptionCatchAll) +} + // DKIMSignature (DNS TXT record) func (s botSettings) DKIMSignature() string { return s.Get(botOptionDKIMSignature)