Add basic mailboxes command
This can be improved in the future, to show some additional information about each mailbox like: - "how many users are in that room" - "which users are in that room" - "who is the owner of the mailbox" This can all be done later though.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
39
bot/admin_command.go
Normal file
39
bot/admin_command.go
Normal file
@@ -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())
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user