Files
postmoogle/bot/command.go
Slavi Pantaleev 60d3fbbba5 Improve introduction and getters usability
When someone first joins a room, they see some commands (`mailbox`,
`owner`, ..) and they know they are getters and setters, but they have
no good example as to how to use them.

Is it `!pm mailbox SOMETHING` or `!pm mailbox=SOMETHING` or something
else?

It's better if the introduction text gives you the full command you need
to get started (e.g. `!pm mailbox SOME_MAILBOX`), instead of a partial
command that you don't know how to use (e.g. `!pm mailbox` - this is
merely a getter and will not set your mailbox to `SOME_MAILBOX`).

Starting from this, I thought it would be a good idea to make all
option getters tell you how the commands are to be used. If you send
`!pm mailbox` and it tells you "not yet set", it should also tell you
how to actually set it (e.g. `!pm mailbox VALUE`).
2022-08-31 09:08:49 +03:00

240 lines
5.6 KiB
Go

package bot
import (
"context"
"fmt"
"strings"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
"gitlab.com/etke.cc/postmoogle/utils"
)
const (
commandHelp = "help"
commandStop = "stop"
commandUsers = botOptionUsers
commandDelete = "delete"
commandMailboxes = "mailboxes"
)
type (
command struct {
key string
description string
sanitizer func(string) string
allowed func(id.UserID, id.RoomID) bool
}
commandList []command
)
func (c commandList) get(key string) *command {
for _, cmd := range c {
if cmd.key == key {
return &cmd
}
}
return nil
}
func (b *Bot) buildCommandList() commandList {
return commandList{
// special commands
{
key: commandHelp,
description: "Show this help message",
allowed: b.allowAnyone,
},
{
key: commandStop,
description: "Disable bridge for the room and clear all configuration",
allowed: b.allowOwner,
},
{allowed: b.allowOwner}, // delimiter
// options commands
{
key: roomOptionMailbox,
description: "Get or set mailbox of the room",
sanitizer: utils.Mailbox,
allowed: b.allowOwner,
},
{
key: roomOptionOwner,
description: "Get or set owner of the room",
sanitizer: func(s string) string { return s },
allowed: b.allowOwner,
},
{allowed: b.allowOwner}, // delimiter
{
key: roomOptionNoSender,
description: fmt.Sprintf(
"Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)",
roomOptionNoSender,
),
sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner,
},
{
key: roomOptionNoSubject,
description: fmt.Sprintf(
"Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)",
roomOptionNoSubject,
),
sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner,
},
{
key: roomOptionNoHTML,
description: fmt.Sprintf(
"Get or set `%s` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails)",
roomOptionNoHTML,
),
sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner,
},
{
key: roomOptionNoThreads,
description: fmt.Sprintf(
"Get or set `%s` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads)",
roomOptionNoThreads,
),
sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner,
},
{
key: roomOptionNoFiles,
description: fmt.Sprintf(
"Get or set `%s` of the room (`true` - ignore email attachments; `false` - upload email attachments)",
roomOptionNoFiles,
),
sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner,
},
{allowed: b.allowAdmin}, // delimiter
{
key: botOptionUsers,
description: "Get or set allowed users",
allowed: b.allowAdmin,
},
{
key: commandMailboxes,
description: "Show the list of all mailboxes",
allowed: b.allowAdmin,
},
{
key: commandDelete,
description: "Delete specific mailbox",
allowed: b.allowAdmin,
},
}
}
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, commandSlice []string) {
cmd := b.commands.get(commandSlice[0])
if cmd == nil {
return
}
if !cmd.allowed(evt.Sender, evt.RoomID) {
b.SendNotice(ctx, evt.RoomID, "not allowed to do that, kupo")
return
}
switch commandSlice[0] {
case commandHelp:
b.sendHelp(ctx)
case commandStop:
b.runStop(ctx)
case commandUsers:
b.runUsers(ctx, commandSlice)
case commandDelete:
b.runDelete(ctx, commandSlice)
case commandMailboxes:
b.sendMailboxes(ctx)
default:
b.handleOption(ctx, commandSlice)
}
}
func (b *Bot) parseCommand(message string) []string {
if message == "" {
return nil
}
index := strings.LastIndex(message, b.prefix)
if index == -1 {
return nil
}
message = strings.ToLower(strings.TrimSpace(strings.Replace(message, b.prefix, "", 1)))
return strings.Split(message, " ")
}
func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
var msg strings.Builder
msg.WriteString("Hello, kupo!\n\n")
msg.WriteString("This is Postmoogle - a bot that bridges Email to Matrix.\n\n")
msg.WriteString("To get started, assign an email address to this room by sending a `")
msg.WriteString(b.prefix)
msg.WriteString(" ")
msg.WriteString(roomOptionMailbox)
msg.WriteString(" SOME_INBOX` command.\n")
msg.WriteString("You will then be able to send emails to `SOME_INBOX@")
msg.WriteString(b.domain)
msg.WriteString("` and have them appear in this room.")
b.SendNotice(ctx, roomID, msg.String())
}
func (b *Bot) sendHelp(ctx context.Context) {
evt := eventFromContext(ctx)
cfg, serr := b.getRoomSettings(evt.RoomID)
if serr != nil {
b.log.Error("cannot retrieve settings: %v", serr)
}
var msg strings.Builder
msg.WriteString("The following commands are supported and accessible to you:\n\n")
for _, cmd := range b.commands {
if !cmd.allowed(evt.Sender, evt.RoomID) {
continue
}
if cmd.key == "" {
msg.WriteString("\n---\n")
continue
}
msg.WriteString("* **`")
msg.WriteString(b.prefix)
msg.WriteString(" ")
msg.WriteString(cmd.key)
msg.WriteString("`**")
value := cfg.Get(cmd.key)
if cmd.sanitizer != nil {
switch value != "" {
case false:
msg.WriteString("(currently not set)")
case true:
msg.WriteString("(currently `")
msg.WriteString(value)
if cmd.key == roomOptionMailbox {
msg.WriteString("@")
msg.WriteString(b.domain)
}
msg.WriteString("`)")
}
}
msg.WriteString(" - ")
msg.WriteString(cmd.description)
msg.WriteString("\n")
}
b.SendNotice(ctx, evt.RoomID, msg.String())
}