add catch-all mailbox, closes #25
This commit is contained in:
@@ -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 dkim** - Get DKIM signature
|
||||||
|
* **!pm catch-all** - Configure catch-all mailbox
|
||||||
* **!pm users** - Get or set allowed users patterns
|
* **!pm users** - Get or set allowed users patterns
|
||||||
* **!pm mailboxes** - Show the list of all mailboxes
|
* **!pm mailboxes** - Show the list of all mailboxes
|
||||||
* **!pm delete** <mailbox> - Delete specific mailbox
|
* **!pm delete** <mailbox> - Delete specific mailbox
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ const (
|
|||||||
commandStop = "stop"
|
commandStop = "stop"
|
||||||
commandSend = "send"
|
commandSend = "send"
|
||||||
commandDKIM = "dkim"
|
commandDKIM = "dkim"
|
||||||
|
commandCatchAll = botOptionCatchAll
|
||||||
commandUsers = botOptionUsers
|
commandUsers = botOptionUsers
|
||||||
commandDelete = "delete"
|
commandDelete = "delete"
|
||||||
commandMailboxes = "mailboxes"
|
commandMailboxes = "mailboxes"
|
||||||
@@ -144,6 +145,11 @@ func (b *Bot) initCommands() commandList {
|
|||||||
description: "Get DKIM signature",
|
description: "Get DKIM signature",
|
||||||
allowed: b.allowAdmin,
|
allowed: b.allowAdmin,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: commandCatchAll,
|
||||||
|
description: "Get or set catch-all mailbox",
|
||||||
|
allowed: b.allowAdmin,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
key: commandMailboxes,
|
key: commandMailboxes,
|
||||||
description: "Show the list of all mailboxes",
|
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)
|
b.runDKIM(ctx, commandSlice)
|
||||||
case commandUsers:
|
case commandUsers:
|
||||||
b.runUsers(ctx, commandSlice)
|
b.runUsers(ctx, commandSlice)
|
||||||
|
case commandCatchAll:
|
||||||
|
b.runCatchAll(ctx, commandSlice)
|
||||||
case commandDelete:
|
case commandDelete:
|
||||||
b.runDelete(ctx, commandSlice)
|
b.runDelete(ctx, commandSlice)
|
||||||
case commandMailboxes:
|
case commandMailboxes:
|
||||||
|
|||||||
@@ -166,3 +166,41 @@ func (b *Bot) runDKIM(ctx context.Context, commandSlice []string) {
|
|||||||
"To reset the signature, send `%s dkim reset`",
|
"To reset the signature, send `%s dkim reset`",
|
||||||
signature, signature, b.prefix))
|
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))
|
||||||
|
}
|
||||||
|
|||||||
18
bot/email.go
18
bot/email.go
@@ -31,12 +31,12 @@ func (b *Bot) SetMTA(mta utils.MTA) {
|
|||||||
b.mta = 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)
|
v, ok := b.rooms.Load(mailbox)
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", ok
|
return "", ok
|
||||||
}
|
}
|
||||||
|
|
||||||
roomID, ok := v.(id.RoomID)
|
roomID, ok := v.(id.RoomID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", ok
|
return "", ok
|
||||||
@@ -45,6 +45,20 @@ func (b *Bot) GetMapping(mailbox string) (id.RoomID, bool) {
|
|||||||
return roomID, ok
|
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
|
// Send email to matrix room
|
||||||
func (b *Bot) Send2Matrix(ctx context.Context, email *utils.Email, incoming bool) error {
|
func (b *Bot) Send2Matrix(ctx context.Context, email *utils.Email, incoming bool) error {
|
||||||
roomID, ok := b.GetMapping(email.Mailbox(incoming))
|
roomID, ok := b.GetMapping(email.Mailbox(incoming))
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const acBotSettingsKey = "cc.etke.postmoogle.config"
|
|||||||
// bot options keys
|
// bot options keys
|
||||||
const (
|
const (
|
||||||
botOptionUsers = "users"
|
botOptionUsers = "users"
|
||||||
|
botOptionCatchAll = "catch-all"
|
||||||
botOptionDKIMSignature = "dkim.pub"
|
botOptionDKIMSignature = "dkim.pub"
|
||||||
botOptionDKIMPrivateKey = "dkim.pem"
|
botOptionDKIMPrivateKey = "dkim.pem"
|
||||||
)
|
)
|
||||||
@@ -42,6 +43,11 @@ func (s botSettings) Users() []string {
|
|||||||
return []string{value}
|
return []string{value}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CatchAll option
|
||||||
|
func (s botSettings) CatchAll() string {
|
||||||
|
return s.Get(botOptionCatchAll)
|
||||||
|
}
|
||||||
|
|
||||||
// DKIMSignature (DNS TXT record)
|
// DKIMSignature (DNS TXT record)
|
||||||
func (s botSettings) DKIMSignature() string {
|
func (s botSettings) DKIMSignature() string {
|
||||||
return s.Get(botOptionDKIMSignature)
|
return s.Get(botOptionDKIMSignature)
|
||||||
|
|||||||
Reference in New Issue
Block a user