From da41bd31fb1bac9de36fa42e812d299fd4db648d Mon Sep 17 00:00:00 2001 From: Aine Date: Thu, 28 Sep 2023 08:30:37 +0300 Subject: [PATCH] add `!pm banlist:totals`, fix notices on reactions --- bot/bot.go | 7 ++++- bot/command.go | 8 ++++++ bot/command_admin.go | 29 ++++++++++++++++++-- bot/command_owner.go | 7 ++++- bot/reaction.go | 2 ++ go.mod | 2 +- go.sum | 4 +-- vendor/gitlab.com/etke.cc/linkpearl/justfile | 2 +- vendor/gitlab.com/etke.cc/linkpearl/send.go | 11 +++++++- vendor/modules.txt | 2 +- 10 files changed, 64 insertions(+), 10 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 0d493c2..2bef1c3 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -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 diff --git a/bot/command.go b/bot/command.go index 0134b8d..5205da2 100644 --- a/bot/command.go +++ b/bot/command.go @@ -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: diff --git a/bot/command_admin.go b/bot/command_admin.go index 4f4f6fb..f469784 100644 --- a/bot/command_admin.go +++ b/bot/command_admin.go @@ -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") diff --git a/bot/command_owner.go b/bot/command_owner.go index b26ce21..47997c7 100644 --- a/bot/command_owner.go +++ b/bot/command_owner.go @@ -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) { diff --git a/bot/reaction.go b/bot/reaction.go index 0bf131c..d931227 100644 --- a/bot/reaction.go +++ b/bot/reaction.go @@ -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 { diff --git a/go.mod b/go.mod index 55047bc..315fecf 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 63b3c33..1caeb59 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/vendor/gitlab.com/etke.cc/linkpearl/justfile b/vendor/gitlab.com/etke.cc/linkpearl/justfile index ded99ca..1bd70fd 100644 --- a/vendor/gitlab.com/etke.cc/linkpearl/justfile +++ b/vendor/gitlab.com/etke.cc/linkpearl/justfile @@ -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 diff --git a/vendor/gitlab.com/etke.cc/linkpearl/send.go b/vendor/gitlab.com/etke.cc/linkpearl/send.go index fd75a3a..68713de 100644 --- a/vendor/gitlab.com/etke.cc/linkpearl/send.go +++ b/vendor/gitlab.com/etke.cc/linkpearl/send.go @@ -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") + } + } } } diff --git a/vendor/modules.txt b/vendor/modules.txt index 8e2903f..e551601 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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