2020-12-21 01:32:39 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-12-14 18:35:07 +01:00
|
|
|
"regexp"
|
2022-07-16 21:45:14 +02:00
|
|
|
"strings"
|
2020-12-21 01:32:39 +01:00
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 19:51:38 +02:00
|
|
|
"gopkg.in/irc.v4"
|
2021-11-25 23:48:16 +01:00
|
|
|
|
2024-04-03 21:00:28 +02:00
|
|
|
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
2022-11-02 22:38:14 +01:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2020-12-21 01:32:39 +01:00
|
|
|
)
|
|
|
|
|
2021-12-14 18:35:07 +01:00
|
|
|
var (
|
|
|
|
// Compile-time assertion
|
|
|
|
_ plugins.MsgFormatter = formatMessage
|
|
|
|
|
|
|
|
stripNewline = regexp.MustCompile(`(?m)\s*\n\s*`)
|
2022-06-17 22:13:47 +02:00
|
|
|
|
2024-04-03 21:00:28 +02:00
|
|
|
formatMessageFieldSetters = []func(compiledFields *fieldcollection.FieldCollection, m *irc.Message, fields *fieldcollection.FieldCollection){
|
2022-06-17 22:13:47 +02:00
|
|
|
formatMessageFieldChannel,
|
|
|
|
formatMessageFieldMessage,
|
|
|
|
formatMessageFieldUserID,
|
|
|
|
formatMessageFieldUsername,
|
|
|
|
}
|
2021-12-14 18:35:07 +01:00
|
|
|
)
|
2021-08-19 15:33:56 +02:00
|
|
|
|
2024-04-03 21:00:28 +02:00
|
|
|
func formatMessage(tplString string, m *irc.Message, r *plugins.Rule, fields *fieldcollection.FieldCollection) (string, error) {
|
|
|
|
compiledFields := fieldcollection.NewFieldCollection()
|
2021-05-25 16:26:03 +02:00
|
|
|
|
|
|
|
if config != nil {
|
|
|
|
configLock.RLock()
|
2021-11-11 14:59:08 +01:00
|
|
|
compiledFields.SetFromData(config.Variables)
|
|
|
|
compiledFields.Set("permitTimeout", int64(config.PermitTimeout/time.Second))
|
2021-05-25 16:26:03 +02:00
|
|
|
configLock.RUnlock()
|
|
|
|
}
|
|
|
|
|
2021-11-11 14:59:08 +01:00
|
|
|
compiledFields.SetFromData(fields.Data())
|
2020-12-25 19:31:07 +01:00
|
|
|
|
2022-06-17 22:13:47 +02:00
|
|
|
for _, fn := range formatMessageFieldSetters {
|
|
|
|
fn(compiledFields, m, fields)
|
2020-12-25 19:39:44 +01:00
|
|
|
}
|
|
|
|
|
2021-12-14 18:35:07 +01:00
|
|
|
// Template in frontend supports newlines, messages do not
|
|
|
|
tplString = stripNewline.ReplaceAllString(tplString, " ")
|
|
|
|
|
2020-12-25 19:31:07 +01:00
|
|
|
// Parse and execute template
|
|
|
|
tpl, err := template.
|
|
|
|
New(tplString).
|
2021-05-25 16:26:03 +02:00
|
|
|
Funcs(tplFuncs.GetFuncMap(m, r, compiledFields)).
|
2020-12-25 19:31:07 +01:00
|
|
|
Parse(tplString)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "parse template")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2021-11-11 14:59:08 +01:00
|
|
|
err = tpl.Execute(buf, compiledFields.Data())
|
2020-12-25 19:31:07 +01:00
|
|
|
|
2022-07-16 21:45:14 +02:00
|
|
|
return strings.TrimSpace(buf.String()), errors.Wrap(err, "execute template")
|
2020-12-21 01:32:39 +01:00
|
|
|
}
|
2022-06-17 22:13:47 +02:00
|
|
|
|
2024-04-03 21:00:28 +02:00
|
|
|
func formatMessageFieldChannel(compiledFields *fieldcollection.FieldCollection, m *irc.Message, fields *fieldcollection.FieldCollection) {
|
2022-06-17 22:13:47 +02:00
|
|
|
compiledFields.Set(eventFieldChannel, plugins.DeriveChannel(m, fields))
|
|
|
|
}
|
|
|
|
|
2024-04-03 21:00:28 +02:00
|
|
|
func formatMessageFieldMessage(compiledFields *fieldcollection.FieldCollection, m *irc.Message, _ *fieldcollection.FieldCollection) {
|
2022-06-17 22:13:47 +02:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
compiledFields.Set("msg", m)
|
|
|
|
}
|
|
|
|
|
2024-04-03 21:00:28 +02:00
|
|
|
func formatMessageFieldUserID(compiledFields *fieldcollection.FieldCollection, m *irc.Message, _ *fieldcollection.FieldCollection) {
|
2022-06-17 22:13:47 +02:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if uid := m.Tags["user-id"]; uid != "" {
|
|
|
|
compiledFields.Set(eventFieldUserID, uid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 21:00:28 +02:00
|
|
|
func formatMessageFieldUsername(compiledFields *fieldcollection.FieldCollection, m *irc.Message, fields *fieldcollection.FieldCollection) {
|
2022-06-17 22:13:47 +02:00
|
|
|
compiledFields.Set("username", plugins.DeriveUser(m, fields))
|
|
|
|
}
|
2022-10-31 17:26:53 +01:00
|
|
|
|
|
|
|
func validateTemplate(tplString string) error {
|
|
|
|
// Template in frontend supports newlines, messages do not
|
|
|
|
tplString = stripNewline.ReplaceAllString(tplString, " ")
|
|
|
|
|
|
|
|
_, err := template.
|
|
|
|
New(tplString).
|
2024-04-03 21:00:28 +02:00
|
|
|
Funcs(tplFuncs.GetFuncMap(nil, nil, fieldcollection.NewFieldCollection())).
|
2022-10-31 17:26:53 +01:00
|
|
|
Parse(tplString)
|
|
|
|
return errors.Wrap(err, "parsing template")
|
|
|
|
}
|