Merge branch 'show-option-value-in-help' into 'main'
Show option values in help See merge request etke.cc/postmoogle!21
This commit is contained in:
125
bot/command.go
125
bot/command.go
@@ -11,26 +11,25 @@ import (
|
|||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sanitizerFunc func(string) string
|
type (
|
||||||
|
command struct {
|
||||||
type commandDefinition struct {
|
|
||||||
key string
|
key string
|
||||||
description string
|
description string
|
||||||
|
sanitizer func(string) string
|
||||||
|
}
|
||||||
|
commandList []command
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c commandList) get(key string) *command {
|
||||||
|
for _, cmd := range c {
|
||||||
|
if cmd.key == key {
|
||||||
|
return &cmd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type commandList []commandDefinition
|
var commands = commandList{
|
||||||
|
|
||||||
func (c commandList) get(key string) (*commandDefinition, bool) {
|
|
||||||
for _, command := range c {
|
|
||||||
if command.key == key {
|
|
||||||
return &command, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
commands = commandList{
|
|
||||||
// special commands
|
// special commands
|
||||||
{
|
{
|
||||||
key: "help",
|
key: "help",
|
||||||
@@ -40,22 +39,26 @@ var (
|
|||||||
key: "stop",
|
key: "stop",
|
||||||
description: "Disable bridge for the room and clear all configuration",
|
description: "Disable bridge for the room and clear all configuration",
|
||||||
},
|
},
|
||||||
|
{}, // delimiter
|
||||||
// options commands
|
// options commands
|
||||||
{
|
{
|
||||||
key: optionMailbox,
|
key: optionMailbox,
|
||||||
description: "Get or set mailbox of the room",
|
description: "Get or set mailbox of the room",
|
||||||
|
sanitizer: utils.Mailbox,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: optionOwner,
|
key: optionOwner,
|
||||||
description: "Get or set owner of the room",
|
description: "Get or set owner of the room",
|
||||||
|
sanitizer: func(s string) string { return s },
|
||||||
},
|
},
|
||||||
|
{}, // delimiter
|
||||||
{
|
{
|
||||||
key: optionNoSender,
|
key: optionNoSender,
|
||||||
description: fmt.Sprintf(
|
description: fmt.Sprintf(
|
||||||
"Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)",
|
"Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)",
|
||||||
optionNoSender,
|
optionNoSender,
|
||||||
),
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: optionNoSubject,
|
key: optionNoSubject,
|
||||||
@@ -63,6 +66,7 @@ var (
|
|||||||
"Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)",
|
"Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)",
|
||||||
optionNoSubject,
|
optionNoSubject,
|
||||||
),
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: optionNoHTML,
|
key: optionNoHTML,
|
||||||
@@ -70,6 +74,7 @@ var (
|
|||||||
"Get or set `%s` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails)",
|
"Get or set `%s` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails)",
|
||||||
optionNoHTML,
|
optionNoHTML,
|
||||||
),
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: optionNoThreads,
|
key: optionNoThreads,
|
||||||
@@ -77,6 +82,7 @@ var (
|
|||||||
"Get or set `%s` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads)",
|
"Get or set `%s` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads)",
|
||||||
optionNoThreads,
|
optionNoThreads,
|
||||||
),
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: optionNoFiles,
|
key: optionNoFiles,
|
||||||
@@ -84,22 +90,12 @@ var (
|
|||||||
"Get or set `%s` of the room (`true` - ignore email attachments; `false` - upload email attachments)",
|
"Get or set `%s` of the room (`true` - ignore email attachments; `false` - upload email attachments)",
|
||||||
optionNoFiles,
|
optionNoFiles,
|
||||||
),
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanitizers is map of option name => sanitizer function
|
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, commandSlice []string) {
|
||||||
sanitizers = map[string]sanitizerFunc{
|
if cmd := commands.get(commandSlice[0]); cmd == nil {
|
||||||
optionMailbox: utils.Mailbox,
|
|
||||||
optionNoSender: utils.SanitizeBoolString,
|
|
||||||
optionNoSubject: utils.SanitizeBoolString,
|
|
||||||
optionNoHTML: utils.SanitizeBoolString,
|
|
||||||
optionNoThreads: utils.SanitizeBoolString,
|
|
||||||
optionNoFiles: utils.SanitizeBoolString,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) {
|
|
||||||
if _, ok := commands.get(command[0]); !ok {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,13 +104,13 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []str
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch command[0] {
|
switch commandSlice[0] {
|
||||||
case "help":
|
case "help":
|
||||||
b.sendHelp(ctx, evt.RoomID)
|
b.sendHelp(ctx, evt.RoomID)
|
||||||
case "stop":
|
case "stop":
|
||||||
b.runStop(ctx, true)
|
b.runStop(ctx, true)
|
||||||
default:
|
default:
|
||||||
b.handleOption(ctx, command)
|
b.handleOption(ctx, commandSlice)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,15 +148,44 @@ func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
|
func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
|
||||||
|
evt := eventFromContext(ctx)
|
||||||
|
|
||||||
|
cfg, serr := b.getSettings(evt.RoomID)
|
||||||
|
if serr != nil {
|
||||||
|
b.log.Error("cannot retrieve settings: %v", serr)
|
||||||
|
}
|
||||||
|
|
||||||
var msg strings.Builder
|
var msg strings.Builder
|
||||||
msg.WriteString("The following commands are supported:\n\n")
|
msg.WriteString("The following commands are supported:\n\n")
|
||||||
for _, command := range commands {
|
for _, cmd := range commands {
|
||||||
|
if cmd.key == "" {
|
||||||
|
msg.WriteString("\n---\n")
|
||||||
|
continue
|
||||||
|
}
|
||||||
msg.WriteString("* **`")
|
msg.WriteString("* **`")
|
||||||
msg.WriteString(b.prefix)
|
msg.WriteString(b.prefix)
|
||||||
msg.WriteString(" ")
|
msg.WriteString(" ")
|
||||||
msg.WriteString(command.key)
|
msg.WriteString(cmd.key)
|
||||||
msg.WriteString("`** - ")
|
msg.WriteString("`**")
|
||||||
msg.WriteString(command.description)
|
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 == optionMailbox {
|
||||||
|
msg.WriteString("@")
|
||||||
|
msg.WriteString(b.domain)
|
||||||
|
}
|
||||||
|
msg.WriteString("`)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.WriteString(" - ")
|
||||||
|
|
||||||
|
msg.WriteString(cmd.description)
|
||||||
msg.WriteString("\n")
|
msg.WriteString("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,17 +222,15 @@ func (b *Bot) runStop(ctx context.Context, checkAllowed bool) {
|
|||||||
b.Notice(ctx, evt.RoomID, "mailbox has been disabled")
|
b.Notice(ctx, evt.RoomID, "mailbox has been disabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) handleOption(ctx context.Context, command []string) {
|
func (b *Bot) handleOption(ctx context.Context, cmd []string) {
|
||||||
if len(command) == 1 {
|
if len(cmd) == 1 {
|
||||||
b.getOption(ctx, command[0])
|
b.getOption(ctx, cmd[0])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
b.setOption(ctx, command[0], command[1])
|
b.setOption(ctx, cmd[0], cmd[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) getOption(ctx context.Context, name string) {
|
func (b *Bot) getOption(ctx context.Context, name string) {
|
||||||
msg := "`%s` of this room is `%s`"
|
|
||||||
|
|
||||||
evt := eventFromContext(ctx)
|
evt := eventFromContext(ctx)
|
||||||
cfg, err := b.getSettings(evt.RoomID)
|
cfg, err := b.getSettings(evt.RoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -222,18 +245,16 @@ func (b *Bot) getOption(ctx context.Context, name string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if name == optionMailbox {
|
if name == optionMailbox {
|
||||||
value = fmt.Sprintf("%s@%s", value, b.domain)
|
value = 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) {
|
func (b *Bot) setOption(ctx context.Context, name, value string) {
|
||||||
msg := "`%s` of this room set to `%s`"
|
cmd := commands.get(name)
|
||||||
|
if cmd != nil {
|
||||||
sanitizer, ok := sanitizers[name]
|
value = cmd.sanitizer(value)
|
||||||
if ok {
|
|
||||||
value = sanitizer(value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
evt := eventFromContext(ctx)
|
evt := eventFromContext(ctx)
|
||||||
@@ -274,5 +295,9 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Notice(ctx, evt.RoomID, fmt.Sprintf(msg, name, value))
|
if name == optionMailbox {
|
||||||
|
value = value + "@" + b.domain
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room set to `%s`", name, value))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ func (b *Bot) handle(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
message := strings.TrimSpace(content.Body)
|
message := strings.TrimSpace(content.Body)
|
||||||
command := b.parseCommand(message)
|
cmd := b.parseCommand(message)
|
||||||
if command == nil {
|
if cmd == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b.handleCommand(ctx, evt, command)
|
b.handleCommand(ctx, evt, cmd)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,13 +21,7 @@ type settingsOld struct {
|
|||||||
|
|
||||||
// Get option
|
// Get option
|
||||||
func (s settings) Get(key string) string {
|
func (s settings) Get(key string) string {
|
||||||
value := s[strings.ToLower(strings.TrimSpace(key))]
|
return s[strings.ToLower(strings.TrimSpace(key))]
|
||||||
|
|
||||||
sanitizer, ok := sanitizers[key]
|
|
||||||
if ok {
|
|
||||||
return sanitizer(value)
|
|
||||||
}
|
|
||||||
return value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s settings) Mailbox() string {
|
func (s settings) Mailbox() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user