diff --git a/bot/access.go b/bot/access.go index bbf48a3..f54f66d 100644 --- a/bot/access.go +++ b/bot/access.go @@ -35,3 +35,7 @@ func (b *Bot) allowOwner(actorID id.UserID, targetRoomID id.RoomID) (bool, error return owner == actorID.String(), nil } + +func (b *Bot) allowAdmin(actorID id.UserID, targetRoomID id.RoomID) (bool, error) { + return utils.Match(actorID.String(), b.allowedAdmins), nil +} diff --git a/bot/admin_command.go b/bot/admin_command.go new file mode 100644 index 0000000..eff4bf4 --- /dev/null +++ b/bot/admin_command.go @@ -0,0 +1,39 @@ +package bot + +import ( + "context" + "fmt" + "strings" + + "maunium.net/go/mautrix/id" +) + +func (b *Bot) sendMailboxes(ctx context.Context) { + evt := eventFromContext(ctx) + + mailboxes := map[string]id.RoomID{} + + b.rooms.Range(func(mailbox any, roomID any) bool { + mailboxes[mailbox.(string)] = roomID.(id.RoomID) + return true + }) + + if len(mailboxes) == 0 { + b.Notice(ctx, evt.RoomID, "No mailboxes are managed by the bot so far, kupo!") + return + } + + var msg strings.Builder + msg.WriteString("The following mailboxes are managed by the bot:\n") + for mailbox, roomID := range mailboxes { + email := fmt.Sprintf("%s@%s", mailbox, b.domain) + msg.WriteString("* `") + msg.WriteString(email) + msg.WriteString("` - `") + msg.WriteString(roomID.String()) + msg.WriteString("`") + msg.WriteString("\n") + } + + b.Notice(ctx, evt.RoomID, msg.String()) +} diff --git a/bot/command.go b/bot/command.go index 58a3cbe..4fc12aa 100644 --- a/bot/command.go +++ b/bot/command.go @@ -109,6 +109,12 @@ func (b *Bot) buildCommandList() commandList { sanitizer: utils.SanitizeBoolString, accessChecker: b.allowOwner, }, + {}, // delimiter + { + key: commandMailboxes, + description: "Show the list of all mailboxes", + accessChecker: b.allowAdmin, + }, } } @@ -138,6 +144,8 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, commandSlice b.sendHelp(ctx, evt.RoomID) case commandStop: b.runStop(ctx) + case commandMailboxes: + b.sendMailboxes(ctx) default: b.handleOption(ctx, commandSlice) }