[core] Fix: Strip newlines from message templates

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-12-14 18:35:07 +01:00
parent 11e55fc5cb
commit 36f9133cd5
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

View file

@ -2,6 +2,7 @@ package main
import (
"bytes"
"regexp"
"text/template"
"time"
@ -11,8 +12,12 @@ import (
"github.com/Luzifer/twitch-bot/plugins"
)
// Compile-time assertion
var _ plugins.MsgFormatter = formatMessage
var (
// Compile-time assertion
_ plugins.MsgFormatter = formatMessage
stripNewline = regexp.MustCompile(`(?m)\s*\n\s*`)
)
func formatMessage(tplString string, m *irc.Message, r *plugins.Rule, fields *plugins.FieldCollection) (string, error) {
compiledFields := plugins.NewFieldCollection()
@ -32,6 +37,9 @@ func formatMessage(tplString string, m *irc.Message, r *plugins.Rule, fields *pl
compiledFields.Set("username", plugins.DeriveUser(m, fields))
compiledFields.Set("channel", plugins.DeriveChannel(m, fields))
// Template in frontend supports newlines, messages do not
tplString = stripNewline.ReplaceAllString(tplString, " ")
// Parse and execute template
tpl, err := template.
New(tplString).