Merge branch 'sanitize-on-get-option' into 'main'

Sanitize settings on Get() and add convenience getters

See merge request etke.cc/postmoogle!8
This commit is contained in:
Aine
2022-08-24 07:36:51 +00:00
2 changed files with 19 additions and 3 deletions

View File

@@ -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")

View File

@@ -7,6 +7,8 @@ import (
"github.com/getsentry/sentry-go"
"maunium.net/go/mautrix/id"
"gitlab.com/etke.cc/postmoogle/utils"
)
const settingskey = "cc.etke.postmoogle.settings"
@@ -39,7 +41,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))]
value := s[strings.ToLower(strings.TrimSpace(key))]
sanitizer, exists := sanitizers[key]
if exists {
return sanitizer(value)
}
return value
}
func (s settings) NoSender() bool {
return utils.Bool(s.Get("nosender"))
}
func (s settings) NoSubject() bool {
return utils.Bool(s.Get("nosubject"))
}
// Set option