proposed changes
This commit is contained in:
255
bot/command.go
255
bot/command.go
@@ -11,103 +11,91 @@ 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
|
||||||
isOption bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type commandList []commandDefinition
|
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
key: "help",
|
|
||||||
description: "Show this help message",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: "stop",
|
|
||||||
description: "Disable bridge for the room and clear all configuration",
|
|
||||||
},
|
|
||||||
|
|
||||||
// options commands
|
|
||||||
{
|
|
||||||
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,
|
|
||||||
description: fmt.Sprintf(
|
|
||||||
"Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)",
|
|
||||||
optionNoSender,
|
|
||||||
),
|
|
||||||
isOption: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: optionNoSubject,
|
|
||||||
description: fmt.Sprintf(
|
|
||||||
"Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)",
|
|
||||||
optionNoSubject,
|
|
||||||
),
|
|
||||||
isOption: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: optionNoHTML,
|
|
||||||
description: fmt.Sprintf(
|
|
||||||
"Get or set `%s` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails)",
|
|
||||||
optionNoHTML,
|
|
||||||
),
|
|
||||||
isOption: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: optionNoThreads,
|
|
||||||
description: fmt.Sprintf(
|
|
||||||
"Get or set `%s` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads)",
|
|
||||||
optionNoThreads,
|
|
||||||
),
|
|
||||||
isOption: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: optionNoFiles,
|
|
||||||
description: fmt.Sprintf(
|
|
||||||
"Get or set `%s` of the room (`true` - ignore email attachments; `false` - upload email attachments)",
|
|
||||||
optionNoFiles,
|
|
||||||
),
|
|
||||||
isOption: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// sanitizers is map of option name => sanitizer function
|
|
||||||
sanitizers = map[string]sanitizerFunc{
|
|
||||||
optionMailbox: utils.Mailbox,
|
|
||||||
optionNoSender: utils.SanitizeBoolString,
|
|
||||||
optionNoSubject: utils.SanitizeBoolString,
|
|
||||||
optionNoHTML: utils.SanitizeBoolString,
|
|
||||||
optionNoThreads: utils.SanitizeBoolString,
|
|
||||||
optionNoFiles: utils.SanitizeBoolString,
|
|
||||||
}
|
}
|
||||||
|
commandList []command
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) {
|
func (c commandList) get(key string) *command {
|
||||||
if _, ok := commands.get(command[0]); !ok {
|
for _, cmd := range c {
|
||||||
|
if cmd.key == key {
|
||||||
|
return &cmd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var commands = commandList{
|
||||||
|
// special commands
|
||||||
|
{
|
||||||
|
key: "help",
|
||||||
|
description: "Show this help message",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "stop",
|
||||||
|
description: "Disable bridge for the room and clear all configuration",
|
||||||
|
},
|
||||||
|
{}, // delimiter
|
||||||
|
// options commands
|
||||||
|
{
|
||||||
|
key: optionMailbox,
|
||||||
|
description: "Get or set mailbox of the room",
|
||||||
|
sanitizer: utils.Mailbox,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: optionOwner,
|
||||||
|
description: "Get or set owner of the room",
|
||||||
|
sanitizer: func(s string) string { return s },
|
||||||
|
},
|
||||||
|
{}, // delimiter
|
||||||
|
{
|
||||||
|
key: optionNoSender,
|
||||||
|
description: fmt.Sprintf(
|
||||||
|
"Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)",
|
||||||
|
optionNoSender,
|
||||||
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: optionNoSubject,
|
||||||
|
description: fmt.Sprintf(
|
||||||
|
"Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)",
|
||||||
|
optionNoSubject,
|
||||||
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: optionNoHTML,
|
||||||
|
description: fmt.Sprintf(
|
||||||
|
"Get or set `%s` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails)",
|
||||||
|
optionNoHTML,
|
||||||
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: optionNoThreads,
|
||||||
|
description: fmt.Sprintf(
|
||||||
|
"Get or set `%s` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads)",
|
||||||
|
optionNoThreads,
|
||||||
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: optionNoFiles,
|
||||||
|
description: fmt.Sprintf(
|
||||||
|
"Get or set `%s` of the room (`true` - ignore email attachments; `false` - upload email attachments)",
|
||||||
|
optionNoFiles,
|
||||||
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, commandSlice []string) {
|
||||||
|
if cmd := commands.get(commandSlice[0]); cmd == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,33 +150,42 @@ 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)
|
evt := eventFromContext(ctx)
|
||||||
|
|
||||||
settings, settingsErr := b.getSettings(evt.RoomID)
|
cfg, serr := b.getSettings(evt.RoomID)
|
||||||
if settingsErr != nil {
|
if serr != nil {
|
||||||
b.Error(ctx, evt.RoomID, settingsErr.Error())
|
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("`**")
|
||||||
|
value := cfg.Get(cmd.key)
|
||||||
if command.isOption && settingsErr == nil {
|
if cmd.sanitizer != nil {
|
||||||
value := settings.Get(command.key)
|
switch value != "" {
|
||||||
|
case false:
|
||||||
if value == "" {
|
msg.WriteString("(currently not set)")
|
||||||
msg.WriteString(" (currently not set)")
|
case true:
|
||||||
} else {
|
msg.WriteString("(currently `")
|
||||||
msg.WriteString(fmt.Sprintf(" (currently `%v`)", b.formatOptionValue(command.key, value)))
|
msg.WriteString(value)
|
||||||
|
if cmd.key == optionMailbox {
|
||||||
|
msg.WriteString("@")
|
||||||
|
msg.WriteString(b.domain)
|
||||||
|
}
|
||||||
|
msg.WriteString("`)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.WriteString(" - ")
|
msg.WriteString(" - ")
|
||||||
|
|
||||||
msg.WriteString(command.description)
|
msg.WriteString(cmd.description)
|
||||||
msg.WriteString("\n")
|
msg.WriteString("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,12 +222,12 @@ 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) {
|
||||||
@@ -247,24 +244,24 @@ func (b *Bot) getOption(ctx context.Context, name string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Notice(ctx, evt.RoomID, fmt.Sprintf(
|
if name == optionMailbox {
|
||||||
"`%s` of this room is `%s`",
|
value = value + "@" + b.domain
|
||||||
name,
|
}
|
||||||
b.formatOptionValue(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) {
|
||||||
sanitizer, ok := sanitizers[name]
|
cmd := commands.get(name)
|
||||||
if ok {
|
if cmd != nil {
|
||||||
value = sanitizer(value)
|
value = cmd.sanitizer(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
evt := eventFromContext(ctx)
|
evt := eventFromContext(ctx)
|
||||||
if name == optionMailbox {
|
if name == optionMailbox {
|
||||||
existingID, ok := b.GetMapping(value)
|
existingID, ok := b.GetMapping(value)
|
||||||
if ok && existingID != "" && existingID != evt.RoomID {
|
if ok && existingID != "" && existingID != evt.RoomID {
|
||||||
b.Notice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s` already taken, kupo", b.formatOptionValue(name, value)))
|
b.Notice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken, kupo", value, b.domain))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -283,7 +280,7 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
|
|||||||
cfg.Set(name, value)
|
cfg.Set(name, value)
|
||||||
if name == optionMailbox {
|
if name == optionMailbox {
|
||||||
cfg.Set(optionOwner, evt.Sender.String())
|
cfg.Set(optionOwner, evt.Sender.String())
|
||||||
b.rooms.Store(b.formatOptionValue(name, value), evt.RoomID)
|
b.rooms.Store(value, evt.RoomID)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = b.setSettings(evt.RoomID, cfg)
|
err = b.setSettings(evt.RoomID, cfg)
|
||||||
@@ -292,9 +289,9 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Notice(ctx, evt.RoomID, fmt.Sprintf(
|
if name == optionMailbox {
|
||||||
"`%s` of this room set to `%s`",
|
value = value + "@" + b.domain
|
||||||
name,
|
}
|
||||||
b.formatOptionValue(name, value),
|
|
||||||
))
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -36,13 +35,7 @@ func (s settings) Allowed(noowner bool, userID id.UserID) bool {
|
|||||||
|
|
||||||
// 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 {
|
||||||
@@ -119,15 +112,3 @@ func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
|
|||||||
func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error {
|
func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error {
|
||||||
return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg)
|
return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) formatOptionValue(name string, value string) string {
|
|
||||||
if value == "" {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
if name == optionMailbox {
|
|
||||||
value = fmt.Sprintf("%s@%s", value, b.domain)
|
|
||||||
}
|
|
||||||
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user