Use string constants for options, not magic strings

This also adds `Mailbox()` and `Owner()` getters for completeness.

Wording has been changed a bit to avoid saying "that room". It sounds
better if it's "this room" or just "the room".
This commit is contained in:
Slavi Pantaleev
2022-08-24 10:46:42 +03:00
parent 79ed440fdc
commit 26edcdadbc
2 changed files with 42 additions and 20 deletions

View File

@@ -2,6 +2,7 @@ package bot
import ( import (
"context" "context"
"fmt"
"strings" "strings"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
@@ -18,20 +19,26 @@ var (
commands = map[string]string{ commands = map[string]string{
// special commands // special commands
"help": "Get help", "help": "Get help",
"stop": "Disable bridge for that room and clear all configuration", "stop": "Disable bridge for the room and clear all configuration",
// options commands // options commands
"mailbox": "Get or set mailbox of that room", optionMailbox: "Get or set mailbox of the room",
"owner": "Get or set owner of that room", optionOwner: "Get or set owner of the room",
"nosender": "Get or set `nosender` of that room (`true` - hide email sender; `false` - show email sender)", optionNoSender: fmt.Sprintf(
"nosubject": "Get or set `nosubject` of that room (`true` - hide email subject; `false` - show email subject)", "Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)",
optionNoSender,
),
optionNoSubject: fmt.Sprintf(
"Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)",
optionNoSubject,
),
} }
// sanitizers is map of option name => sanitizer function // sanitizers is map of option name => sanitizer function
sanitizers = map[string]sanitizerFunc{ sanitizers = map[string]sanitizerFunc{
"mailbox": utils.Mailbox, optionMailbox: utils.Mailbox,
"nosender": utils.SanitizeBoolString, optionNoSender: utils.SanitizeBoolString,
"nosubject": utils.SanitizeBoolString, optionNoSubject: utils.SanitizeBoolString,
} }
) )
@@ -106,7 +113,7 @@ func (b *Bot) runStop(ctx context.Context, evt *event.Event) {
return return
} }
mailbox := cfg.Get("mailbox") mailbox := cfg.Get(optionMailbox)
if mailbox == "" { if mailbox == "" {
b.Notice(span.Context(), evt.RoomID, "that room is not configured yet") b.Notice(span.Context(), evt.RoomID, "that room is not configured yet")
return return
@@ -150,7 +157,7 @@ func (b *Bot) getOption(ctx context.Context, evt *event.Event, name string) {
return return
} }
if name == "mailbox" { if name == optionMailbox {
msg = msg + "@" + b.domain msg = msg + "@" + b.domain
} }
@@ -167,7 +174,7 @@ func (b *Bot) setOption(ctx context.Context, evt *event.Event, name, value strin
value = sanitizer(value) value = sanitizer(value)
} }
if name == "mailbox" { if name == optionMailbox {
existingID, ok := b.GetMapping(ctx, value) existingID, ok := b.GetMapping(ctx, value)
if ok && existingID != "" && existingID != evt.RoomID { if ok && existingID != "" && existingID != evt.RoomID {
b.Notice(span.Context(), evt.RoomID, "Mailbox %s@%s already taken", value, b.domain) b.Notice(span.Context(), evt.RoomID, "Mailbox %s@%s already taken", value, b.domain)
@@ -187,9 +194,9 @@ func (b *Bot) setOption(ctx context.Context, evt *event.Event, name, value strin
} }
cfg.Set(name, value) cfg.Set(name, value)
if name == "mailbox" { if name == optionMailbox {
msg = msg + "@" + b.domain msg = msg + "@" + b.domain
cfg.Set("owner", evt.Sender.String()) cfg.Set(optionOwner, evt.Sender.String())
b.roomsmu.Lock() b.roomsmu.Lock()
b.rooms[value] = evt.RoomID b.rooms[value] = evt.RoomID
b.roomsmu.Unlock() b.roomsmu.Unlock()

View File

@@ -13,6 +13,13 @@ import (
const settingskey = "cc.etke.postmoogle.settings" const settingskey = "cc.etke.postmoogle.settings"
const (
optionOwner = "owner"
optionMailbox = "mailbox"
optionNoSender = "nosender"
optionNoSubject = "nosubject"
)
var migrations = []string{} var migrations = []string{}
// settings of a room // settings of a room
@@ -31,7 +38,7 @@ func (s settings) Allowed(noowner bool, userID id.UserID) bool {
return true return true
} }
owner := s.Get("owner") owner := s.Owner()
if owner == "" { if owner == "" {
return true return true
} }
@@ -50,12 +57,20 @@ func (s settings) Get(key string) string {
return value return value
} }
func (s settings) Mailbox() string {
return s.Get(optionMailbox)
}
func (s settings) Owner() string {
return s.Get(optionOwner)
}
func (s settings) NoSender() bool { func (s settings) NoSender() bool {
return utils.Bool(s.Get("nosender")) return utils.Bool(s.Get(optionNoSender))
} }
func (s settings) NoSubject() bool { func (s settings) NoSubject() bool {
return utils.Bool(s.Get("nosubject")) return utils.Bool(s.Get(optionNoSubject))
} }
// Set option // Set option
@@ -110,7 +125,7 @@ func (b *Bot) syncRooms(ctx context.Context) error {
b.log.Warn("cannot get %s settings: %v", roomID, err) b.log.Warn("cannot get %s settings: %v", roomID, err)
continue continue
} }
mailbox := cfg.Get("mailbox") mailbox := cfg.Mailbox()
if mailbox != "" { if mailbox != "" {
b.rooms[mailbox] = roomID b.rooms[mailbox] = roomID
} }
@@ -132,9 +147,9 @@ func (b *Bot) migrateSettings(ctx context.Context, roomID id.RoomID) {
return return
} }
cfg := settings{} cfg := settings{}
cfg.Set("mailbox", config.Mailbox) cfg.Set(optionMailbox, config.Mailbox)
cfg.Set("owner", config.Owner.String()) cfg.Set(optionOwner, config.Owner.String())
cfg.Set("nosender", strconv.FormatBool(config.NoSender)) cfg.Set(optionNoSender, strconv.FormatBool(config.NoSender))
err = b.setSettings(ctx, roomID, cfg) err = b.setSettings(ctx, roomID, cfg)
if err != nil { if err != nil {