Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions policyeval/eventhandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ func (pe *PolicyEvaluator) HandleMember(ctx context.Context, evt *event.Event) {
if checkRules {
pe.EvaluateUser(ctx, userID, false)
}
if evt.Unsigned.PrevContent != nil && evt.Sender != pe.Bot.UserID {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should probably be an option to disable these prompts altogether (since a bot might be configured to be read-only, for example). This should probably be configurable per-bot though

_ = evt.Unsigned.PrevContent.ParseRaw(event.StateMember)
prevContent := evt.Unsigned.PrevContent.AsMember()
if content.Membership == event.MembershipBan && prevContent.Membership != event.MembershipBan {
pe.Bot.Log.Debug().
Stringer("user_id", userID).
Stringer("room_id", evt.RoomID).
Msg("Prompting ban propagation")
pe.propagateBan(ctx, evt)

@timedoutuk timedoutuk Dec 20, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Being banned in several rooms can trigger this prompt multiple times, should it be deduplicated?
  • The ban being undone does not short-circuit the menu which can be annoying
  • Should the user be able to ban to multiple lists from one prompt (via multi-use)?

} else if content.Membership == event.MembershipLeave && prevContent.Membership == event.MembershipBan {
pe.Bot.Log.Debug().
Stringer("user_id", userID).
Stringer("room_id", evt.RoomID).
Msg("Prompting unban propagation")
pe.propagateUnban(ctx, evt)
Comment thread
timedoutuk marked this conversation as resolved.
}
}
}
}

Expand Down
118 changes: 118 additions & 0 deletions policyeval/propagate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package policyeval

import (
"context"
"fmt"
"maps"
"slices"
"time"

"github.com/rs/zerolog"
"maunium.net/go/mautrix/commands"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"

"go.mau.fi/meowlnir/bot"
"go.mau.fi/meowlnir/config"
)

func (pe *PolicyEvaluator) writableLists(ctx context.Context) map[id.RoomID]*config.WatchedPolicyList {
lists := make(map[id.RoomID]*config.WatchedPolicyList)
for roomID, list := range pe.watchedListsMap {
if list.Shortcode == "" {
continue
}
pl, err := pe.Bot.StateStore.GetPowerLevels(ctx, roomID)
if err != nil || pl.GetEventLevel(event.StatePolicyUser) > pl.GetUserLevel(pe.Bot.UserID) {
continue
}
lists[roomID] = list
}
return lists
}

func (pe *PolicyEvaluator) propagateBan(ctx context.Context, banEvent *event.Event) {
content := banEvent.Content.AsMember()
userID := id.UserID(banEvent.GetStateKey())
actions := make(map[string]any, len(pe.watchedListsMap))
for _, list := range pe.writableLists(ctx) {
actions["/ban "+list.Shortcode] = fmt.Sprintf("!ban %s %s %s", list.Shortcode, userID, content.Reason)
continue
}
if len(actions) == 0 {
zerolog.Ctx(ctx).Debug().Msg("No writable policy lists to propagate ban to")
return
}

msg := fmt.Sprintf(
"%s was banned from %s by %s%s for %s. Copy to a policy list?",
format.MarkdownMention(userID),
format.MarkdownMentionRoomID("", banEvent.RoomID),
format.MarkdownMention(banEvent.Sender),
oldEventNotice(banEvent.Timestamp),
format.SafeMarkdownCode(content.Reason),
)
evtID := pe.Bot.SendNoticeOpts(ctx, pe.ManagementRoom, msg, &bot.SendNoticeOpts{
Extra: map[string]any{commands.ReactionCommandsKey: actions},
})
if evtID == "" {
return
}
pe.sendReactions(ctx, evtID, slices.Collect(maps.Keys(actions))...)
}
func (pe *PolicyEvaluator) propagateUnban(ctx context.Context, unbanEvent *event.Event) {
content := unbanEvent.Content.AsMember()
userID := id.UserID(unbanEvent.GetStateKey())

match := pe.Store.MatchUser(pe.GetWatchedLists(), userID)
if len(match) == 0 {
zerolog.Ctx(ctx).Debug().Msg("No matching policies to propagate unban to")
return
}

actions := make(map[string]any, len(match))
writeable := pe.writableLists(ctx)
msg := fmt.Sprintf(
"%s was unbanned from %s by %s%s for %s, but is still banned by %d policies. Do you want to remove any?\n",
format.MarkdownMention(userID),
format.MarkdownMentionRoomID("", unbanEvent.RoomID),
format.MarkdownMention(unbanEvent.Sender),
oldEventNotice(unbanEvent.Timestamp),
format.SafeMarkdownCode(content.Reason),
len(match),
)
n := 0
for _, policy := range match {
meta, ok := writeable[policy.RoomID]
if !ok {
continue
}
n++
msg += fmt.Sprintf(
"%d. [%s] %s set recommendation %s for %s at %s for %s\n",
n,
format.EscapeMarkdown(meta.Shortcode),
format.MarkdownMention(policy.Sender),
format.SafeMarkdownCode(policy.Recommendation),
format.SafeMarkdownCode(policy.EntityOrHash()),
format.EscapeMarkdown(time.UnixMilli(policy.Timestamp).String()),
format.SafeMarkdownCode(policy.Reason),
)
actions[fmt.Sprintf("/remove %d", n)] = fmt.Sprintf("!remove-policy %s %s", meta.Shortcode, policy.EntityOrHash())
}
if len(actions) == 0 {
return
}

evtID := pe.Bot.SendNoticeOpts(ctx, pe.ManagementRoom, msg, &bot.SendNoticeOpts{
Extra: map[string]any{
commands.ReactionCommandsKey: actions,
commands.ReactionMultiUseKey: true,
},
})
if evtID == "" {
return
}
pe.sendReactions(ctx, evtID, slices.Collect(maps.Keys(actions))...)
}