From 0d88de9f77d6843c26909be4a13d778908eb82a7 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Sat, 27 Aug 2022 17:46:47 +0300 Subject: [PATCH] Show option values in help Before: > * **`!pm help`** - Show this help message > * **`!pm stop`** - Disable bridge for the room and clear all configuration > * **`!pm mailbox`** - Get or set mailbox of the room > * **`!pm owner`** - Get or set owner of the room > * **`!pm nosender`** - Get or set `nosender` of the room (`true` - hide email sender; `false` - show email sender) > * **`!pm nosubject`** - Get or set `nosubject` of the room (`true` - hide email subject; `false` - show email subject) > * **`!pm nohtml`** - Get or set `nohtml` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails) > * **`!pm nothreads`** - Get or set `nothreads` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads) > * **`!pm nofiles`** - Get or set `nofiles` of the room (`true` - ignore email attachments; `false` - upload email attachments After: > * **`!pm help`** - Show this help message > * **`!pm stop`** - Disable bridge for the room and clear all configuration > * **`!pm mailbox`** (currently `something@example.com`) - Get or set mailbox of the room > * **`!pm owner`** (currently `@someone:example.com`) - Get or set owner of the room > * **`!pm nosender`** (currently `false`) - Get or set `nosender` of the room (`true` - hide email sender; `false` - show email sender) > * **`!pm nosubject`** (currently `true`) - Get or set `nosubject` of the room (`true` - hide email subject; `false` - show email subject) > * **`!pm nohtml`** (currently `false`) - Get or set `nohtml` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails) > * **`!pm nothreads`** (currently `false`) - Get or set `nothreads` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads) > * **`!pm nofiles`** (currently `false`) - Get or set `nofiles` of the room (`true` - ignore email attachments; `false` - upload email attachments) --- bot/command.go | 45 ++++++++++++++++++++++++++++++++------------- bot/settings.go | 19 +++++++++++++++++++ 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/bot/command.go b/bot/command.go index ea19740..d514c2f 100644 --- a/bot/command.go +++ b/bot/command.go @@ -16,6 +16,7 @@ type sanitizerFunc func(string) string type commandDefinition struct { key string description string + isOption bool } type commandList []commandDefinition @@ -45,10 +46,12 @@ var ( { key: optionMailbox, description: "Get or set mailbox of the room", + isOption: true, }, { key: optionOwner, description: "Get or set owner of the room", + isOption: true, }, { key: optionNoSender, @@ -56,6 +59,7 @@ var ( "Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)", optionNoSender, ), + isOption: true, }, { key: optionNoSubject, @@ -63,6 +67,7 @@ var ( "Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)", optionNoSubject, ), + isOption: true, }, { key: optionNoHTML, @@ -70,6 +75,7 @@ var ( "Get or set `%s` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails)", optionNoHTML, ), + isOption: true, }, { key: optionNoThreads, @@ -77,6 +83,7 @@ var ( "Get or set `%s` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads)", optionNoThreads, ), + isOption: true, }, { key: optionNoFiles, @@ -84,6 +91,7 @@ var ( "Get or set `%s` of the room (`true` - ignore email attachments; `false` - upload email attachments)", optionNoFiles, ), + isOption: true, }, } @@ -152,6 +160,8 @@ func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) { } func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) { + evt := eventFromContext(ctx) + var msg strings.Builder msg.WriteString("The following commands are supported:\n\n") for _, command := range commands { @@ -159,7 +169,23 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) { msg.WriteString(b.prefix) msg.WriteString(" ") msg.WriteString(command.key) - msg.WriteString("`** - ") + msg.WriteString("`**") + + if command.isOption { + value, err := b.getSettingsOption(evt.RoomID, command.key) + if err != nil { + b.Error(ctx, evt.RoomID, err.Error()) + } else { + if value == nil { + msg.WriteString(" (currently not set)") + } else { + msg.WriteString(fmt.Sprintf(" (currently `%v`)", value)) + } + } + } + + msg.WriteString(" - ") + msg.WriteString(command.description) msg.WriteString("\n") } @@ -206,26 +232,19 @@ func (b *Bot) handleOption(ctx context.Context, command []string) { } func (b *Bot) getOption(ctx context.Context, name string) { - msg := "`%s` of this room is `%s`" - evt := eventFromContext(ctx) - cfg, err := b.getSettings(evt.RoomID) + + value, err := b.getSettingsOption(evt.RoomID, name) if err != nil { - b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err) + b.Error(ctx, evt.RoomID, err.Error()) return } - - value := cfg.Get(name) - if value == "" { + if value == nil { b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` is not set, kupo.", name)) return } - if name == optionMailbox { - value = fmt.Sprintf("%s@%s", value, b.domain) - } - - b.Notice(ctx, evt.RoomID, fmt.Sprintf(msg, name, value)) + b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room is `%s`", name, value)) } func (b *Bot) setOption(ctx context.Context, name, value string) { diff --git a/bot/settings.go b/bot/settings.go index ad37899..332d74f 100644 --- a/bot/settings.go +++ b/bot/settings.go @@ -1,6 +1,7 @@ package bot import ( + "fmt" "strconv" "strings" @@ -115,6 +116,24 @@ func (b *Bot) getSettings(roomID id.RoomID) (settings, error) { return config, err } +func (b *Bot) getSettingsOption(roomID id.RoomID, name string) (any, error) { + cfg, err := b.getSettings(roomID) + if err != nil { + return nil, fmt.Errorf("failed to retrieve settings: %v", err) + } + + value := cfg.Get(name) + if value == "" { + return nil, nil + } + + if name == optionMailbox { + value = fmt.Sprintf("%s@%s", value, b.domain) + } + + return value, nil +} + func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error { return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg) }