add !pm banlist:totals, fix notices on reactions

This commit is contained in:
Aine
2023-09-28 08:30:37 +03:00
parent 7fbb279830
commit da41bd31fb
10 changed files with 64 additions and 10 deletions

View File

@@ -110,7 +110,12 @@ func (b *Bot) Error(ctx context.Context, message string, args ...interface{}) {
noThreads = cfg.NoThreads()
}
b.lp.SendNotice(evt.RoomID, "ERROR: "+err.Error(), utils.RelatesTo(!noThreads, threadID))
var relatesTo *event.RelatesTo
if threadID != "" {
relatesTo = utils.RelatesTo(!noThreads, threadID)
}
b.lp.SendNotice(evt.RoomID, "ERROR: "+err.Error(), relatesTo)
}
// Start performs matrix /sync

View File

@@ -31,6 +31,7 @@ const (
commandSpamlistReset = "spam:reset"
commandDelete = "delete"
commandBanlist = "banlist"
commandBanlistTotals = "banlist:totals"
commandBanlistAuto = "banlist:auto"
commandBanlistAuth = "banlist:auth"
commandBanlistAdd = "banlist:add"
@@ -315,6 +316,11 @@ func (b *Bot) initCommands() commandList {
description: "Enable/disable automatic banning of IP addresses when they try to send invalid emails",
allowed: b.allowAdmin,
},
{
key: commandBanlistTotals,
description: "List banlist totals only",
allowed: b.allowAdmin,
},
{
key: commandBanlistAdd,
description: "Ban an IP",
@@ -404,6 +410,8 @@ func (b *Bot) handle(ctx context.Context) {
b.runBanlistAuth(ctx, commandSlice)
case commandBanlistAuto:
b.runBanlistAuto(ctx, commandSlice)
case commandBanlistTotals:
b.runBanlistTotals(ctx)
case commandBanlistAdd:
b.runBanlistAdd(ctx, commandSlice)
case commandBanlistRemove:

View File

@@ -319,7 +319,7 @@ func (b *Bot) runBanlist(ctx context.Context, commandSlice []string) {
msg.WriteString("You can find current banlist values below:\n")
b.lp.SendNotice(evt.RoomID, msg.String(), utils.RelatesTo(true, evt.ID))
b.addBanlistTimeline(ctx)
b.addBanlistTimeline(ctx, false)
return
}
value := utils.SanitizeBoolString(commandSlice[1])
@@ -331,6 +331,25 @@ func (b *Bot) runBanlist(ctx context.Context, commandSlice []string) {
b.lp.SendNotice(evt.RoomID, "banlist has been updated", utils.RelatesTo(true, evt.ID))
}
func (b *Bot) runBanlistTotals(ctx context.Context) {
evt := eventFromContext(ctx)
banlist := b.cfg.GetBanlist()
var msg strings.Builder
size := len(banlist)
if size == 0 {
b.lp.SendNotice(evt.RoomID, "banlist is empty, kupo.", utils.RelatesTo(true, evt.ID))
return
}
msg.WriteString("Total: ")
msg.WriteString(strconv.Itoa(size))
msg.WriteString(" hosts banned\n\n")
msg.WriteString("You can find daily totals below:\n")
b.lp.SendNotice(evt.RoomID, msg.String(), utils.RelatesTo(true, evt.ID))
b.addBanlistTimeline(ctx, true)
return
}
func (b *Bot) runBanlistAuth(ctx context.Context, commandSlice []string) {
evt := eventFromContext(ctx)
cfg := b.cfg.GetBot()
@@ -447,7 +466,7 @@ func (b *Bot) runBanlistRemove(ctx context.Context, commandSlice []string) {
b.lp.SendNotice(evt.RoomID, "banlist has been updated, kupo", utils.RelatesTo(true, evt.ID))
}
func (b *Bot) addBanlistTimeline(ctx context.Context) {
func (b *Bot) addBanlistTimeline(ctx context.Context, onlyTotals bool) {
evt := eventFromContext(ctx)
banlist := b.cfg.GetBanlist()
timeline := map[string][]string{}
@@ -471,6 +490,12 @@ func (b *Bot) addBanlistTimeline(ctx context.Context) {
sort.Strings(data)
txt.WriteString("* `")
txt.WriteString(day)
if onlyTotals {
txt.WriteString("` ")
txt.WriteString(strconv.Itoa(len(data)))
txt.WriteString(" hosts banned\n")
continue
}
txt.WriteString("` `")
txt.WriteString(strings.Join(data, "`, `"))
txt.WriteString("`\n")

View File

@@ -217,7 +217,12 @@ func (b *Bot) runSpamlistAdd(ctx context.Context, commandSlice []string) {
return
}
b.lp.SendNotice(evt.RoomID, "spamlist has been updated, kupo", utils.RelatesTo(!cfg.NoThreads(), evt.ID))
threadID := threadIDFromContext(ctx)
if threadID == "" {
threadID = evt.ID
}
b.lp.SendNotice(evt.RoomID, "spamlist has been updated, kupo", utils.RelatesTo(!cfg.NoThreads(), threadID))
}
func (b *Bot) runSpamlistRemove(ctx context.Context, commandSlice []string) {

View File

@@ -28,6 +28,8 @@ func (b *Bot) handleReaction(ctx context.Context) {
b.Error(ctx, "cannot find event %s: %v", srcID, err)
return
}
threadID := linkpearl.EventParent(srcID, srcEvt.Content.AsMessage())
ctx = threadIDToContext(ctx, threadID)
linkpearl.ParseContent(evt, event.EventMessage, b.log)
switch action {