From 16ce4314b96c8ddeb4728acfb88311e37cf66278 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Wed, 24 Aug 2022 10:16:28 +0300 Subject: [PATCH 1/3] Sanitize settings on Get() and add convenience getters Sanitizing options on Get() ensures that when someone asks for a given option which may not be defined (`nosubject`, `nosender`), we'll return a valid value (`'true'` or `'false'`) and not `''` (empty string, undefined). This way, users do not need to wonder if "nosender is not set" is handled like "true" or "false" or in some 3rd way. They also don't need to think about "how to unset this setting, now that I've set it to something". Options will appear to have a default sanitized value no matter if they've explicitly been set or not. The `NoSender()` and `NoSubject()` getters are just there for convenience, so that we won't need to do casting in other places. --- bot/bot.go | 4 ++-- bot/data.go | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 402a391..d09fdf1 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -102,12 +102,12 @@ func (b *Bot) Send(ctx context.Context, from, to, subject, plaintext, html strin } var text strings.Builder - if !utils.Bool(settings.Get("nosender")) { + if !settings.NoSender() { text.WriteString("From: ") text.WriteString(from) text.WriteString("\n\n") } - if !utils.Bool(settings.Get("nosubject")) { + if !settings.NoSubject() { text.WriteString("# ") text.WriteString(subject) text.WriteString("\n\n") diff --git a/bot/data.go b/bot/data.go index 3b83a08..995a090 100644 --- a/bot/data.go +++ b/bot/data.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/getsentry/sentry-go" + "gitlab.com/etke.cc/postmoogle/utils" "maunium.net/go/mautrix/id" ) @@ -39,7 +40,21 @@ func (s settings) Allowed(noowner bool, userID id.UserID) bool { // Get option func (s settings) Get(key string) string { - return s[strings.ToLower(strings.TrimSpace(key))] + rawValue := s[strings.ToLower(strings.TrimSpace(key))] + + sanitizer, exists := sanitizers[key] + if exists { + return sanitizer(rawValue) + } + return rawValue +} + +func (s settings) NoSender() bool { + return utils.Bool(s.Get("nosender")) +} + +func (s settings) NoSubject() bool { + return utils.Bool(s.Get("nosubject")) } // Set option From 61031648814d8ab02a375a6c941fbcb171b0572f Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Wed, 24 Aug 2022 10:31:42 +0300 Subject: [PATCH 2/3] Fix linting error --- bot/data.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/data.go b/bot/data.go index 995a090..5551e7a 100644 --- a/bot/data.go +++ b/bot/data.go @@ -6,8 +6,9 @@ import ( "strings" "github.com/getsentry/sentry-go" - "gitlab.com/etke.cc/postmoogle/utils" "maunium.net/go/mautrix/id" + + "gitlab.com/etke.cc/postmoogle/utils" ) const settingskey = "cc.etke.postmoogle.settings" From e86a9ed9520c430c4516c4331fe756799bc55930 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Wed, 24 Aug 2022 10:33:19 +0300 Subject: [PATCH 3/3] Rename variable --- bot/data.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/data.go b/bot/data.go index 5551e7a..239c8ed 100644 --- a/bot/data.go +++ b/bot/data.go @@ -41,13 +41,13 @@ func (s settings) Allowed(noowner bool, userID id.UserID) bool { // Get option func (s settings) Get(key string) string { - rawValue := s[strings.ToLower(strings.TrimSpace(key))] + value := s[strings.ToLower(strings.TrimSpace(key))] sanitizer, exists := sanitizers[key] if exists { - return sanitizer(rawValue) + return sanitizer(value) } - return rawValue + return value } func (s settings) NoSender() bool {