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 {

2
go.mod
View File

@@ -26,7 +26,7 @@ require (
gitlab.com/etke.cc/go/secgen v1.1.1
gitlab.com/etke.cc/go/trysmtp v1.1.3
gitlab.com/etke.cc/go/validator v1.0.6
gitlab.com/etke.cc/linkpearl v0.0.0-20230927084751-e9a37b134a8a
gitlab.com/etke.cc/linkpearl v0.0.0-20230928051620-7f1b7d54e9a8
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
maunium.net/go/mautrix v0.16.1
)

4
go.sum
View File

@@ -111,8 +111,8 @@ gitlab.com/etke.cc/go/trysmtp v1.1.3 h1:e2EHond77onMaecqCg6mWumffTSEf+ycgj88nbee
gitlab.com/etke.cc/go/trysmtp v1.1.3/go.mod h1:lOO7tTdAE0a3ETV3wN3GJ7I1Tqewu7YTpPWaOmTteV0=
gitlab.com/etke.cc/go/validator v1.0.6 h1:w0Muxf9Pqw7xvF7NaaswE6d7r9U3nB2t2l5PnFMrecQ=
gitlab.com/etke.cc/go/validator v1.0.6/go.mod h1:Id0SxRj0J3IPhiKlj0w1plxVLZfHlkwipn7HfRZsDts=
gitlab.com/etke.cc/linkpearl v0.0.0-20230927084751-e9a37b134a8a h1:DRSWdLpi1s9MLlXCbrQ6ymJJCqQYemi2wFZP3u9ROb8=
gitlab.com/etke.cc/linkpearl v0.0.0-20230927084751-e9a37b134a8a/go.mod h1:IZ0TE+ZnIdJLb538owDMxhtpWH7blfW+oR7e5XRXxNY=
gitlab.com/etke.cc/linkpearl v0.0.0-20230928051620-7f1b7d54e9a8 h1:iCNH8eFwmqqU4OSXtR+OTi5OkElqB+AlyOrgm9YCluk=
gitlab.com/etke.cc/linkpearl v0.0.0-20230928051620-7f1b7d54e9a8/go.mod h1:IZ0TE+ZnIdJLb538owDMxhtpWH7blfW+oR7e5XRXxNY=
go.mau.fi/util v0.1.0 h1:BwIFWIOEeO7lsiI2eWKFkWTfc5yQmoe+0FYyOFVyaoE=
go.mau.fi/util v0.1.0/go.mod h1:AxuJUMCxpzgJ5eV9JbPWKRH8aAJJidxetNdUj7qcb84=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=

View File

@@ -21,6 +21,6 @@ vuln:
# run unit tests
test:
@go test ${BUILDFLAGS} -coverprofile=cover.out ./...
@go test -coverprofile=cover.out ./...
@go tool cover -func=cover.out
-@rm -f cover.out

View File

@@ -21,15 +21,24 @@ func (l *Linkpearl) Send(roomID id.RoomID, content interface{}) (id.EventID, err
// SendNotice to a room with optional relations, markdown supported
func (l *Linkpearl) SendNotice(roomID id.RoomID, message string, relates ...*event.RelatesTo) {
var withRelatesTo bool
content := format.RenderMarkdown(message, true, true)
content.MsgType = event.MsgNotice
if len(relates) > 0 {
withRelatesTo = true
content.RelatesTo = relates[0]
}
_, err := l.Send(roomID, &content)
if err != nil {
l.log.Error().Err(UnwrapError(err)).Str("roomID", roomID.String()).Msg("cannot send a notice int the room")
l.log.Error().Err(UnwrapError(err)).Str("roomID", roomID.String()).Str("retries", "1/2").Msg("cannot send a notice into the room")
if withRelatesTo {
content.RelatesTo = nil
_, err = l.Send(roomID, &content)
if err != nil {
l.log.Error().Err(UnwrapError(err)).Str("roomID", roomID.String()).Str("retries", "2/2").Msg("cannot send a notice into the room even without relations")
}
}
}
}

2
vendor/modules.txt vendored
View File

@@ -144,7 +144,7 @@ gitlab.com/etke.cc/go/trysmtp
# gitlab.com/etke.cc/go/validator v1.0.6
## explicit; go 1.18
gitlab.com/etke.cc/go/validator
# gitlab.com/etke.cc/linkpearl v0.0.0-20230927084751-e9a37b134a8a
# gitlab.com/etke.cc/linkpearl v0.0.0-20230928051620-7f1b7d54e9a8
## explicit; go 1.18
gitlab.com/etke.cc/linkpearl
# go.mau.fi/util v0.1.0