rename options, rename settings types

This commit is contained in:
Aine
2022-08-30 14:45:07 +03:00
parent 627e090afd
commit 9f66d1fee6
7 changed files with 66 additions and 66 deletions

View File

@@ -23,8 +23,8 @@ type Bot struct {
allowedAdmins []*regexp.Regexp allowedAdmins []*regexp.Regexp
commands commandList commands commandList
rooms sync.Map rooms sync.Map
botcfg cache.Cache[botsettings] botcfg cache.Cache[botSettings]
cfg cache.Cache[roomsettings] cfg cache.Cache[roomSettings]
log *logger.Logger log *logger.Logger
lp *linkpearl.Linkpearl lp *linkpearl.Linkpearl
mu map[id.RoomID]*sync.Mutex mu map[id.RoomID]*sync.Mutex
@@ -44,8 +44,8 @@ func New(
prefix: prefix, prefix: prefix,
domain: domain, domain: domain,
rooms: sync.Map{}, rooms: sync.Map{},
botcfg: cache.NewLRU[botsettings](1), botcfg: cache.NewLRU[botSettings](1),
cfg: cache.NewLRU[roomsettings](1000), cfg: cache.NewLRU[roomSettings](1000),
log: log, log: log,
lp: lp, lp: lp,
mu: map[id.RoomID]*sync.Mutex{}, mu: map[id.RoomID]*sync.Mutex{},

View File

@@ -54,59 +54,59 @@ func (b *Bot) buildCommandList() commandList {
{allowed: b.allowOwner}, // delimiter {allowed: b.allowOwner}, // delimiter
// options commands // options commands
{ {
key: optionMailbox, key: roomOptionMailbox,
description: "Get or set mailbox of the room", description: "Get or set mailbox of the room",
sanitizer: utils.Mailbox, sanitizer: utils.Mailbox,
allowed: b.allowOwner, allowed: b.allowOwner,
}, },
{ {
key: optionOwner, key: roomOptionOwner,
description: "Get or set owner of the room", description: "Get or set owner of the room",
sanitizer: func(s string) string { return s }, sanitizer: func(s string) string { return s },
allowed: b.allowOwner, allowed: b.allowOwner,
}, },
{allowed: b.allowOwner}, // delimiter {allowed: b.allowOwner}, // delimiter
{ {
key: optionNoSender, key: roomOptionNoSender,
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, roomOptionNoSender,
), ),
sanitizer: utils.SanitizeBoolString, sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner, allowed: b.allowOwner,
}, },
{ {
key: optionNoSubject, key: roomOptionNoSubject,
description: fmt.Sprintf( description: fmt.Sprintf(
"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, roomOptionNoSubject,
), ),
sanitizer: utils.SanitizeBoolString, sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner, allowed: b.allowOwner,
}, },
{ {
key: optionNoHTML, key: roomOptionNoHTML,
description: fmt.Sprintf( description: fmt.Sprintf(
"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, roomOptionNoHTML,
), ),
sanitizer: utils.SanitizeBoolString, sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner, allowed: b.allowOwner,
}, },
{ {
key: optionNoThreads, key: roomOptionNoThreads,
description: fmt.Sprintf( description: fmt.Sprintf(
"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, roomOptionNoThreads,
), ),
sanitizer: utils.SanitizeBoolString, sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner, allowed: b.allowOwner,
}, },
{ {
key: optionNoFiles, key: roomOptionNoFiles,
description: fmt.Sprintf( description: fmt.Sprintf(
"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, roomOptionNoFiles,
), ),
sanitizer: utils.SanitizeBoolString, sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner, allowed: b.allowOwner,
@@ -180,7 +180,7 @@ func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
msg.WriteString("To get started, assign an email address to this room by sending a `") msg.WriteString("To get started, assign an email address to this room by sending a `")
msg.WriteString(b.prefix) msg.WriteString(b.prefix)
msg.WriteString(" ") msg.WriteString(" ")
msg.WriteString(optionMailbox) msg.WriteString(roomOptionMailbox)
msg.WriteString("` command.\n") msg.WriteString("` command.\n")
msg.WriteString("You will then be able to send emails to `SOME_INBOX@") msg.WriteString("You will then be able to send emails to `SOME_INBOX@")
@@ -221,7 +221,7 @@ func (b *Bot) sendHelp(ctx context.Context) {
case true: case true:
msg.WriteString("(currently `") msg.WriteString("(currently `")
msg.WriteString(value) msg.WriteString(value)
if cmd.key == optionMailbox { if cmd.key == roomOptionMailbox {
msg.WriteString("@") msg.WriteString("@")
msg.WriteString(b.domain) msg.WriteString(b.domain)
} }

View File

@@ -13,7 +13,7 @@ import (
func (b *Bot) sendMailboxes(ctx context.Context) { func (b *Bot) sendMailboxes(ctx context.Context) {
evt := eventFromContext(ctx) evt := eventFromContext(ctx)
mailboxes := map[string]roomsettings{} mailboxes := map[string]roomSettings{}
slice := []string{} slice := []string{}
b.rooms.Range(func(key any, value any) bool { b.rooms.Range(func(key any, value any) bool {
if key == nil { if key == nil {
@@ -79,7 +79,7 @@ func (b *Bot) runDelete(ctx context.Context, commandSlice []string) {
roomID := v.(id.RoomID) roomID := v.(id.RoomID)
b.rooms.Delete(mailbox) b.rooms.Delete(mailbox)
err := b.setRoomSettings(roomID, roomsettings{}) err := b.setRoomSettings(roomID, roomSettings{})
if err != nil { if err != nil {
b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) b.Error(ctx, evt.RoomID, "cannot update settings: %v", err)
return return

View File

@@ -13,7 +13,7 @@ func (b *Bot) runStop(ctx context.Context) {
return return
} }
mailbox := cfg.Get(optionMailbox) mailbox := cfg.Get(roomOptionMailbox)
if mailbox == "" { if mailbox == "" {
b.SendNotice(ctx, evt.RoomID, "that room is not configured yet") b.SendNotice(ctx, evt.RoomID, "that room is not configured yet")
return return
@@ -21,7 +21,7 @@ func (b *Bot) runStop(ctx context.Context) {
b.rooms.Delete(mailbox) b.rooms.Delete(mailbox)
err = b.setRoomSettings(evt.RoomID, roomsettings{}) err = b.setRoomSettings(evt.RoomID, roomSettings{})
if err != nil { if err != nil {
b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) b.Error(ctx, evt.RoomID, "cannot update settings: %v", err)
return return
@@ -52,7 +52,7 @@ func (b *Bot) getOption(ctx context.Context, name string) {
return return
} }
if name == optionMailbox { if name == roomOptionMailbox {
value = value + "@" + b.domain value = value + "@" + b.domain
} }
@@ -66,7 +66,7 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
} }
evt := eventFromContext(ctx) evt := eventFromContext(ctx)
if name == optionMailbox { if name == roomOptionMailbox {
existingID, ok := b.GetMapping(value) existingID, ok := b.GetMapping(value)
if ok && existingID != "" && existingID != evt.RoomID { if ok && existingID != "" && existingID != evt.RoomID {
b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken, kupo", value, b.domain)) b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken, kupo", value, b.domain))
@@ -83,8 +83,8 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
old := cfg.Get(name) old := cfg.Get(name)
cfg.Set(name, value) cfg.Set(name, value)
if name == optionMailbox { if name == roomOptionMailbox {
cfg.Set(optionOwner, evt.Sender.String()) cfg.Set(roomOptionOwner, evt.Sender.String())
if old != "" { if old != "" {
b.rooms.Delete(old) b.rooms.Delete(old)
} }
@@ -98,7 +98,7 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
return return
} }
if name == optionMailbox { if name == roomOptionMailbox {
value = value + "@" + b.domain value = value + "@" + b.domain
} }

View File

@@ -21,7 +21,7 @@ const (
eventInReplyToKey = "cc.etke.postmoogle.inReplyTo" eventInReplyToKey = "cc.etke.postmoogle.inReplyTo"
) )
func email2content(email *utils.Email, cfg roomsettings, threadID id.EventID) *event.Content { func email2content(email *utils.Email, cfg roomSettings, threadID id.EventID) *event.Content {
var text strings.Builder var text strings.Builder
if !cfg.NoSender() { if !cfg.NoSender() {
text.WriteString("From: ") text.WriteString("From: ")

View File

@@ -14,20 +14,20 @@ const (
botOptionUsers = "users" botOptionUsers = "users"
) )
type botsettings map[string]string type botSettings map[string]string
// Get option // Get option
func (s botsettings) Get(key string) string { func (s botSettings) Get(key string) string {
return s[strings.ToLower(strings.TrimSpace(key))] return s[strings.ToLower(strings.TrimSpace(key))]
} }
// Set option // Set option
func (s botsettings) Set(key, value string) { func (s botSettings) Set(key, value string) {
s[strings.ToLower(strings.TrimSpace(key))] = value s[strings.ToLower(strings.TrimSpace(key))] = value
} }
// Users option // Users option
func (s botsettings) Users() []string { func (s botSettings) Users() []string {
return strings.Split(s.Get(botOptionUsers), " ") return strings.Split(s.Get(botOptionUsers), " ")
} }
@@ -47,13 +47,13 @@ func (b *Bot) migrateBotSettings(users []string) error {
return nil return nil
} }
func (b *Bot) getBotSettings() botsettings { func (b *Bot) getBotSettings() botSettings {
cfg := b.botcfg.Get(acBotSettingsKey) cfg := b.botcfg.Get(acBotSettingsKey)
if cfg != nil { if cfg != nil {
return cfg return cfg
} }
config := botsettings{} config := botSettings{}
err := b.lp.GetClient().GetAccountData(acBotSettingsKey, &config) err := b.lp.GetClient().GetAccountData(acBotSettingsKey, &config)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "M_NOT_FOUND") { if strings.Contains(err.Error(), "M_NOT_FOUND") {
@@ -67,7 +67,7 @@ func (b *Bot) getBotSettings() botsettings {
return config return config
} }
func (b *Bot) setBotSettings(cfg botsettings) error { func (b *Bot) setBotSettings(cfg botSettings) error {
b.botcfg.Set(acBotSettingsKey, cfg) b.botcfg.Set(acBotSettingsKey, cfg)
return utils.UnwrapError(b.lp.GetClient().SetAccountData(acBotSettingsKey, cfg)) return utils.UnwrapError(b.lp.GetClient().SetAccountData(acBotSettingsKey, cfg))
} }

View File

@@ -14,16 +14,16 @@ const acRoomSettingsKey = "cc.etke.postmoogle.settings"
// option keys // option keys
const ( const (
optionOwner = "owner" roomOptionOwner = "owner"
optionMailbox = "mailbox" roomOptionMailbox = "mailbox"
optionNoSender = "nosender" roomOptionNoSender = "nosender"
optionNoSubject = "nosubject" roomOptionNoSubject = "nosubject"
optionNoHTML = "nohtml" roomOptionNoHTML = "nohtml"
optionNoThreads = "nothreads" roomOptionNoThreads = "nothreads"
optionNoFiles = "nofiles" roomOptionNoFiles = "nofiles"
) )
type roomsettings map[string]string type roomSettings map[string]string
// settingsOld of a room // settingsOld of a room
type settingsOld struct { type settingsOld struct {
@@ -33,41 +33,41 @@ type settingsOld struct {
} }
// Get option // Get option
func (s roomsettings) Get(key string) string { func (s roomSettings) Get(key string) string {
return s[strings.ToLower(strings.TrimSpace(key))] return s[strings.ToLower(strings.TrimSpace(key))]
} }
// Set option // Set option
func (s roomsettings) Set(key, value string) { func (s roomSettings) Set(key, value string) {
s[strings.ToLower(strings.TrimSpace(key))] = value s[strings.ToLower(strings.TrimSpace(key))] = value
} }
func (s roomsettings) Mailbox() string { func (s roomSettings) Mailbox() string {
return s.Get(optionMailbox) return s.Get(roomOptionMailbox)
} }
func (s roomsettings) Owner() string { func (s roomSettings) Owner() string {
return s.Get(optionOwner) return s.Get(roomOptionOwner)
} }
func (s roomsettings) NoSender() bool { func (s roomSettings) NoSender() bool {
return utils.Bool(s.Get(optionNoSender)) return utils.Bool(s.Get(roomOptionNoSender))
} }
func (s roomsettings) NoSubject() bool { func (s roomSettings) NoSubject() bool {
return utils.Bool(s.Get(optionNoSubject)) return utils.Bool(s.Get(roomOptionNoSubject))
} }
func (s roomsettings) NoHTML() bool { func (s roomSettings) NoHTML() bool {
return utils.Bool(s.Get(optionNoHTML)) return utils.Bool(s.Get(roomOptionNoHTML))
} }
func (s roomsettings) NoThreads() bool { func (s roomSettings) NoThreads() bool {
return utils.Bool(s.Get(optionNoThreads)) return utils.Bool(s.Get(roomOptionNoThreads))
} }
func (s roomsettings) NoFiles() bool { func (s roomSettings) NoFiles() bool {
return utils.Bool(s.Get(optionNoFiles)) return utils.Bool(s.Get(roomOptionNoFiles))
} }
// TODO: remove after migration // TODO: remove after migration
@@ -82,10 +82,10 @@ func (b *Bot) migrateSettings(roomID id.RoomID) {
if config.Mailbox == "" { if config.Mailbox == "" {
return return
} }
cfg := roomsettings{} cfg := roomSettings{}
cfg.Set(optionMailbox, config.Mailbox) cfg.Set(roomOptionMailbox, config.Mailbox)
cfg.Set(optionOwner, config.Owner.String()) cfg.Set(roomOptionOwner, config.Owner.String())
cfg.Set(optionNoSender, strconv.FormatBool(config.NoSender)) cfg.Set(roomOptionNoSender, strconv.FormatBool(config.NoSender))
err = b.setRoomSettings(roomID, cfg) err = b.setRoomSettings(roomID, cfg)
if err != nil { if err != nil {
@@ -93,13 +93,13 @@ func (b *Bot) migrateSettings(roomID id.RoomID) {
} }
} }
func (b *Bot) getRoomSettings(roomID id.RoomID) (roomsettings, error) { func (b *Bot) getRoomSettings(roomID id.RoomID) (roomSettings, error) {
cfg := b.cfg.Get(roomID.String()) cfg := b.cfg.Get(roomID.String())
if cfg != nil { if cfg != nil {
return cfg, nil return cfg, nil
} }
config := roomsettings{} config := roomSettings{}
err := b.lp.GetClient().GetRoomAccountData(roomID, acRoomSettingsKey, &config) err := b.lp.GetClient().GetRoomAccountData(roomID, acRoomSettingsKey, &config)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "M_NOT_FOUND") { if strings.Contains(err.Error(), "M_NOT_FOUND") {
@@ -115,7 +115,7 @@ func (b *Bot) getRoomSettings(roomID id.RoomID) (roomsettings, error) {
return config, utils.UnwrapError(err) return config, utils.UnwrapError(err)
} }
func (b *Bot) setRoomSettings(roomID id.RoomID, cfg roomsettings) error { func (b *Bot) setRoomSettings(roomID id.RoomID, cfg roomSettings) error {
b.cfg.Set(roomID.String(), cfg) b.cfg.Set(roomID.String(), cfg)
return utils.UnwrapError(b.lp.GetClient().SetRoomAccountData(roomID, acRoomSettingsKey, cfg)) return utils.UnwrapError(b.lp.GetClient().SetRoomAccountData(roomID, acRoomSettingsKey, cfg))
} }