new commands: spam:list, spam:add, spam:remove, spam:reset; updated readme with the full list of commands; rearranged sections in help

This commit is contained in:
Aine
2023-09-22 19:35:28 +03:00
parent c4576869ab
commit e3557f5522
5 changed files with 244 additions and 69 deletions

View File

@@ -16,20 +16,24 @@ import (
)
const (
commandHelp = "help"
commandStop = "stop"
commandSend = "send"
commandDKIM = "dkim"
commandCatchAll = config.BotCatchAll
commandUsers = config.BotUsers
commandQueueBatch = config.BotQueueBatch
commandQueueRetries = config.BotQueueRetries
commandDelete = "delete"
commandBanlist = "banlist"
commandBanlistAdd = "banlist:add"
commandBanlistRemove = "banlist:remove"
commandBanlistReset = "banlist:reset"
commandMailboxes = "mailboxes"
commandHelp = "help"
commandStop = "stop"
commandSend = "send"
commandDKIM = "dkim"
commandCatchAll = config.BotCatchAll
commandUsers = config.BotUsers
commandQueueBatch = config.BotQueueBatch
commandQueueRetries = config.BotQueueRetries
commandSpamlist = "spam:list"
commandSpamlistAdd = "spam:add"
commandSpamlistRemove = "spam:remove"
commandSpamlistReset = "spam:reset"
commandDelete = "delete"
commandBanlist = "banlist"
commandBanlistAdd = "banlist:add"
commandBanlistRemove = "banlist:remove"
commandBanlistReset = "banlist:reset"
commandMailboxes = "mailboxes"
)
type (
@@ -185,7 +189,7 @@ func (b *Bot) initCommands() commandList {
sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner,
},
{allowed: b.allowOwner, description: "mailbox antispam"}, // delimiter
{allowed: b.allowOwner, description: "mailbox security checks"}, // delimiter
{
key: config.RoomSpamcheckMX,
description: "only accept email from servers which seem prepared to receive it (those having valid MX records) (`true` - enable, `false` - disable)",
@@ -210,14 +214,27 @@ func (b *Bot) initCommands() commandList {
sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner,
},
{allowed: b.allowOwner, description: "mailbox anti-spam"}, // delimiter
{
key: config.RoomSpamlist,
description: fmt.Sprintf(
"Get or set `%s` of the room (comma-separated list), eg: `spammer@example.com,*@spammer.org,spam@*`",
config.RoomSpamlist,
),
sanitizer: utils.SanitizeStringSlice,
allowed: b.allowOwner,
key: commandSpamlist,
description: "Show comma-separated spamlist of the room, eg: `spammer@example.com,*@spammer.org,spam@*`",
sanitizer: utils.SanitizeStringSlice,
allowed: b.allowOwner,
},
{
key: commandSpamlistAdd,
description: "Mark an email address (or pattern) as spam",
allowed: b.allowOwner,
},
{
key: commandSpamlistRemove,
description: "Unmark an email address (or pattern) as spam",
allowed: b.allowOwner,
},
{
key: commandSpamlistReset,
description: "Reset spamlist",
allowed: b.allowOwner,
},
{allowed: b.allowAdmin, description: "server options"}, // delimiter
{
@@ -340,6 +357,12 @@ func (b *Bot) handle(ctx context.Context) {
b.runSend(ctx)
case commandDKIM:
b.runDKIM(ctx, commandSlice)
case commandSpamlistAdd:
b.runSpamlistAdd(ctx, commandSlice)
case commandSpamlistRemove:
b.runSpamlistRemove(ctx, commandSlice)
case commandSpamlistReset:
b.runSpamlistReset(ctx)
case config.BotAdminRoom:
b.runAdminRoom(ctx, commandSlice)
case commandUsers:
@@ -427,7 +450,12 @@ func (b *Bot) sendHelp(ctx context.Context) {
msg.WriteString(" ")
msg.WriteString(cmd.key)
msg.WriteString("`**")
value := cfg.Get(cmd.key)
name := cmd.key
if name == commandSpamlist {
name = config.RoomSpamlist
}
value := cfg.Get(name)
if cmd.sanitizer != nil {
switch value != "" {
case false:

View File

@@ -3,7 +3,9 @@ package bot
import (
"context"
"fmt"
"slices"
"strconv"
"strings"
"github.com/raja/argon2pw"
@@ -52,6 +54,10 @@ func (b *Bot) getOption(ctx context.Context, name string) {
return
}
if name == commandSpamlist {
name = config.RoomSpamlist
}
value := cfg.Get(name)
if value == "" {
msg := fmt.Sprintf("`%s` is not set, kupo.\n"+
@@ -138,3 +144,100 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
}
b.SendNotice(ctx, evt.RoomID, msg)
}
func (b *Bot) runSpamlistAdd(ctx context.Context, commandSlice []string) {
evt := eventFromContext(ctx)
if len(commandSlice) < 2 {
b.getOption(ctx, config.RoomSpamlist)
return
}
roomCfg, err := b.cfg.GetRoom(evt.RoomID)
if err != nil {
b.Error(ctx, evt.RoomID, "cannot get room settings: %v", err)
return
}
spamlist := utils.StringSlice(roomCfg[config.RoomSpamlist])
for _, newItem := range commandSlice[1:] {
newItem = strings.TrimSpace(newItem)
if slices.Contains(spamlist, newItem) {
continue
}
spamlist = append(spamlist, newItem)
}
roomCfg.Set(config.RoomSpamlist, utils.SliceString(spamlist))
err = b.cfg.SetRoom(evt.RoomID, roomCfg)
if err != nil {
b.Error(ctx, evt.RoomID, "cannot store room settings: %v", err)
return
}
b.SendNotice(ctx, evt.RoomID, "spamlist has been updated, kupo")
}
func (b *Bot) runSpamlistRemove(ctx context.Context, commandSlice []string) {
evt := eventFromContext(ctx)
if len(commandSlice) < 2 {
b.getOption(ctx, config.RoomSpamlist)
return
}
roomCfg, err := b.cfg.GetRoom(evt.RoomID)
if err != nil {
b.Error(ctx, evt.RoomID, "cannot get room settings: %v", err)
return
}
toRemove := map[int]struct{}{}
spamlist := utils.StringSlice(roomCfg[config.RoomSpamlist])
for _, item := range commandSlice[1:] {
item = strings.TrimSpace(item)
idx := slices.Index(spamlist, item)
if idx < 0 {
continue
}
toRemove[idx] = struct{}{}
}
if len(toRemove) == 0 {
b.SendNotice(ctx, evt.RoomID, "nothing new, kupo.")
return
}
updatedSpamlist := []string{}
for i, item := range spamlist {
if _, ok := toRemove[i]; ok {
continue
}
updatedSpamlist = append(updatedSpamlist, item)
}
roomCfg.Set(config.RoomSpamlist, utils.SliceString(updatedSpamlist))
err = b.cfg.SetRoom(evt.RoomID, roomCfg)
if err != nil {
b.Error(ctx, evt.RoomID, "cannot store room settings: %v", err)
return
}
b.SendNotice(ctx, evt.RoomID, "spamlist has been updated, kupo")
}
func (b *Bot) runSpamlistReset(ctx context.Context) {
evt := eventFromContext(ctx)
roomCfg, err := b.cfg.GetRoom(evt.RoomID)
if err != nil {
b.Error(ctx, evt.RoomID, "cannot get room settings: %v", err)
return
}
spamlist := utils.StringSlice(roomCfg[config.RoomSpamlist])
if len(spamlist) == 0 {
b.SendNotice(ctx, evt.RoomID, "spamlist is empty, kupo.")
return
}
roomCfg.Set(config.RoomSpamlist, "")
err = b.cfg.SetRoom(evt.RoomID, roomCfg)
if err != nil {
b.Error(ctx, evt.RoomID, "cannot store room settings: %v", err)
return
}
b.SendNotice(ctx, evt.RoomID, "spamlist has been reset, kupo.")
}

View File

@@ -168,7 +168,7 @@ func (s Room) MigrateSpamlistSettings() {
for item := range uniq {
spamlist = append(spamlist, item)
}
s.Set(RoomSpamlist, strings.Join(spamlist, ","))
s.Set(RoomSpamlist, utils.SliceString(spamlist))
}
// ContentOptions converts room display settings to content options