diff --git a/bot/bot.go b/bot/bot.go index 8fbf124..195f273 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -23,8 +23,8 @@ type Bot struct { allowedAdmins []*regexp.Regexp commands commandList rooms sync.Map - botcfg cache.Cache[botsettings] - cfg cache.Cache[roomsettings] + botcfg cache.Cache[botSettings] + cfg cache.Cache[roomSettings] log *logger.Logger lp *linkpearl.Linkpearl mu map[id.RoomID]*sync.Mutex @@ -44,8 +44,8 @@ func New( prefix: prefix, domain: domain, rooms: sync.Map{}, - botcfg: cache.NewLRU[botsettings](1), - cfg: cache.NewLRU[roomsettings](1000), + botcfg: cache.NewLRU[botSettings](1), + cfg: cache.NewLRU[roomSettings](1000), log: log, lp: lp, mu: map[id.RoomID]*sync.Mutex{}, diff --git a/bot/command.go b/bot/command.go index 66b57fa..4db54ce 100644 --- a/bot/command.go +++ b/bot/command.go @@ -54,59 +54,59 @@ func (b *Bot) buildCommandList() commandList { {allowed: b.allowOwner}, // delimiter // options commands { - key: optionMailbox, + key: roomOptionMailbox, description: "Get or set mailbox of the room", sanitizer: utils.Mailbox, allowed: b.allowOwner, }, { - key: optionOwner, + key: roomOptionOwner, description: "Get or set owner of the room", sanitizer: func(s string) string { return s }, allowed: b.allowOwner, }, {allowed: b.allowOwner}, // delimiter { - key: optionNoSender, + key: roomOptionNoSender, description: fmt.Sprintf( "Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)", - optionNoSender, + roomOptionNoSender, ), sanitizer: utils.SanitizeBoolString, allowed: b.allowOwner, }, { - key: optionNoSubject, + key: roomOptionNoSubject, description: fmt.Sprintf( "Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)", - optionNoSubject, + roomOptionNoSubject, ), sanitizer: utils.SanitizeBoolString, allowed: b.allowOwner, }, { - key: optionNoHTML, + key: roomOptionNoHTML, description: fmt.Sprintf( "Get or set `%s` of the room (`true` - ignore HTML in email; `false` - parse HTML in emails)", - optionNoHTML, + roomOptionNoHTML, ), sanitizer: utils.SanitizeBoolString, allowed: b.allowOwner, }, { - key: optionNoThreads, + key: roomOptionNoThreads, description: fmt.Sprintf( "Get or set `%s` of the room (`true` - ignore email threads; `false` - convert email threads into matrix threads)", - optionNoThreads, + roomOptionNoThreads, ), sanitizer: utils.SanitizeBoolString, allowed: b.allowOwner, }, { - key: optionNoFiles, + key: roomOptionNoFiles, description: fmt.Sprintf( "Get or set `%s` of the room (`true` - ignore email attachments; `false` - upload email attachments)", - optionNoFiles, + roomOptionNoFiles, ), sanitizer: utils.SanitizeBoolString, 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(b.prefix) msg.WriteString(" ") - msg.WriteString(optionMailbox) + msg.WriteString(roomOptionMailbox) msg.WriteString("` command.\n") 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: msg.WriteString("(currently `") msg.WriteString(value) - if cmd.key == optionMailbox { + if cmd.key == roomOptionMailbox { msg.WriteString("@") msg.WriteString(b.domain) } diff --git a/bot/command_admin.go b/bot/command_admin.go index 38ad306..4b5bc0c 100644 --- a/bot/command_admin.go +++ b/bot/command_admin.go @@ -13,7 +13,7 @@ import ( func (b *Bot) sendMailboxes(ctx context.Context) { evt := eventFromContext(ctx) - mailboxes := map[string]roomsettings{} + mailboxes := map[string]roomSettings{} slice := []string{} b.rooms.Range(func(key any, value any) bool { if key == nil { @@ -79,7 +79,7 @@ func (b *Bot) runDelete(ctx context.Context, commandSlice []string) { roomID := v.(id.RoomID) b.rooms.Delete(mailbox) - err := b.setRoomSettings(roomID, roomsettings{}) + err := b.setRoomSettings(roomID, roomSettings{}) if err != nil { b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) return diff --git a/bot/command_owner.go b/bot/command_owner.go index 443dca3..becd5fe 100644 --- a/bot/command_owner.go +++ b/bot/command_owner.go @@ -13,7 +13,7 @@ func (b *Bot) runStop(ctx context.Context) { return } - mailbox := cfg.Get(optionMailbox) + mailbox := cfg.Get(roomOptionMailbox) if mailbox == "" { b.SendNotice(ctx, evt.RoomID, "that room is not configured yet") return @@ -21,7 +21,7 @@ func (b *Bot) runStop(ctx context.Context) { b.rooms.Delete(mailbox) - err = b.setRoomSettings(evt.RoomID, roomsettings{}) + err = b.setRoomSettings(evt.RoomID, roomSettings{}) if err != nil { b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) return @@ -52,7 +52,7 @@ func (b *Bot) getOption(ctx context.Context, name string) { return } - if name == optionMailbox { + if name == roomOptionMailbox { value = value + "@" + b.domain } @@ -66,7 +66,7 @@ func (b *Bot) setOption(ctx context.Context, name, value string) { } evt := eventFromContext(ctx) - if name == optionMailbox { + if name == roomOptionMailbox { existingID, ok := b.GetMapping(value) if ok && existingID != "" && existingID != evt.RoomID { 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) cfg.Set(name, value) - if name == optionMailbox { - cfg.Set(optionOwner, evt.Sender.String()) + if name == roomOptionMailbox { + cfg.Set(roomOptionOwner, evt.Sender.String()) if old != "" { b.rooms.Delete(old) } @@ -98,7 +98,7 @@ func (b *Bot) setOption(ctx context.Context, name, value string) { return } - if name == optionMailbox { + if name == roomOptionMailbox { value = value + "@" + b.domain } diff --git a/bot/email.go b/bot/email.go index a7c6349..2981136 100644 --- a/bot/email.go +++ b/bot/email.go @@ -21,7 +21,7 @@ const ( 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 if !cfg.NoSender() { text.WriteString("From: ") diff --git a/bot/settings_bot.go b/bot/settings_bot.go index dc44379..6173172 100644 --- a/bot/settings_bot.go +++ b/bot/settings_bot.go @@ -14,20 +14,20 @@ const ( botOptionUsers = "users" ) -type botsettings map[string]string +type botSettings map[string]string // Get option -func (s botsettings) Get(key string) string { +func (s botSettings) Get(key string) string { return s[strings.ToLower(strings.TrimSpace(key))] } // Set option -func (s botsettings) Set(key, value string) { +func (s botSettings) Set(key, value string) { s[strings.ToLower(strings.TrimSpace(key))] = value } // Users option -func (s botsettings) Users() []string { +func (s botSettings) Users() []string { return strings.Split(s.Get(botOptionUsers), " ") } @@ -47,13 +47,13 @@ func (b *Bot) migrateBotSettings(users []string) error { return nil } -func (b *Bot) getBotSettings() botsettings { +func (b *Bot) getBotSettings() botSettings { cfg := b.botcfg.Get(acBotSettingsKey) if cfg != nil { return cfg } - config := botsettings{} + config := botSettings{} err := b.lp.GetClient().GetAccountData(acBotSettingsKey, &config) if err != nil { if strings.Contains(err.Error(), "M_NOT_FOUND") { @@ -67,7 +67,7 @@ func (b *Bot) getBotSettings() botsettings { return config } -func (b *Bot) setBotSettings(cfg botsettings) error { +func (b *Bot) setBotSettings(cfg botSettings) error { b.botcfg.Set(acBotSettingsKey, cfg) return utils.UnwrapError(b.lp.GetClient().SetAccountData(acBotSettingsKey, cfg)) } diff --git a/bot/settings_room.go b/bot/settings_room.go index f65c021..ce0bfd6 100644 --- a/bot/settings_room.go +++ b/bot/settings_room.go @@ -14,16 +14,16 @@ const acRoomSettingsKey = "cc.etke.postmoogle.settings" // option keys const ( - optionOwner = "owner" - optionMailbox = "mailbox" - optionNoSender = "nosender" - optionNoSubject = "nosubject" - optionNoHTML = "nohtml" - optionNoThreads = "nothreads" - optionNoFiles = "nofiles" + roomOptionOwner = "owner" + roomOptionMailbox = "mailbox" + roomOptionNoSender = "nosender" + roomOptionNoSubject = "nosubject" + roomOptionNoHTML = "nohtml" + roomOptionNoThreads = "nothreads" + roomOptionNoFiles = "nofiles" ) -type roomsettings map[string]string +type roomSettings map[string]string // settingsOld of a room type settingsOld struct { @@ -33,41 +33,41 @@ type settingsOld struct { } // Get option -func (s roomsettings) Get(key string) string { +func (s roomSettings) Get(key string) string { return s[strings.ToLower(strings.TrimSpace(key))] } // Set option -func (s roomsettings) Set(key, value string) { +func (s roomSettings) Set(key, value string) { s[strings.ToLower(strings.TrimSpace(key))] = value } -func (s roomsettings) Mailbox() string { - return s.Get(optionMailbox) +func (s roomSettings) Mailbox() string { + return s.Get(roomOptionMailbox) } -func (s roomsettings) Owner() string { - return s.Get(optionOwner) +func (s roomSettings) Owner() string { + return s.Get(roomOptionOwner) } -func (s roomsettings) NoSender() bool { - return utils.Bool(s.Get(optionNoSender)) +func (s roomSettings) NoSender() bool { + return utils.Bool(s.Get(roomOptionNoSender)) } -func (s roomsettings) NoSubject() bool { - return utils.Bool(s.Get(optionNoSubject)) +func (s roomSettings) NoSubject() bool { + return utils.Bool(s.Get(roomOptionNoSubject)) } -func (s roomsettings) NoHTML() bool { - return utils.Bool(s.Get(optionNoHTML)) +func (s roomSettings) NoHTML() bool { + return utils.Bool(s.Get(roomOptionNoHTML)) } -func (s roomsettings) NoThreads() bool { - return utils.Bool(s.Get(optionNoThreads)) +func (s roomSettings) NoThreads() bool { + return utils.Bool(s.Get(roomOptionNoThreads)) } -func (s roomsettings) NoFiles() bool { - return utils.Bool(s.Get(optionNoFiles)) +func (s roomSettings) NoFiles() bool { + return utils.Bool(s.Get(roomOptionNoFiles)) } // TODO: remove after migration @@ -82,10 +82,10 @@ func (b *Bot) migrateSettings(roomID id.RoomID) { if config.Mailbox == "" { return } - cfg := roomsettings{} - cfg.Set(optionMailbox, config.Mailbox) - cfg.Set(optionOwner, config.Owner.String()) - cfg.Set(optionNoSender, strconv.FormatBool(config.NoSender)) + cfg := roomSettings{} + cfg.Set(roomOptionMailbox, config.Mailbox) + cfg.Set(roomOptionOwner, config.Owner.String()) + cfg.Set(roomOptionNoSender, strconv.FormatBool(config.NoSender)) err = b.setRoomSettings(roomID, cfg) 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()) if cfg != nil { return cfg, nil } - config := roomsettings{} + config := roomSettings{} err := b.lp.GetClient().GetRoomAccountData(roomID, acRoomSettingsKey, &config) if err != nil { 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) } -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) return utils.UnwrapError(b.lp.GetClient().SetRoomAccountData(roomID, acRoomSettingsKey, cfg)) }