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