2020-12-21 00:32:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
func formatMessage(tplString string, m *irc.Message, r *Rule, fields map[string]interface{}) (string, error) {
|
2021-05-25 14:26:03 +00:00
|
|
|
compiledFields := map[string]interface{}{}
|
|
|
|
|
|
|
|
if config != nil {
|
|
|
|
configLock.RLock()
|
|
|
|
for k, v := range config.Variables {
|
|
|
|
compiledFields[k] = v
|
|
|
|
}
|
|
|
|
compiledFields["permitTimeout"] = int64(config.PermitTimeout / time.Second)
|
|
|
|
configLock.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range fields {
|
|
|
|
compiledFields[k] = v
|
2020-12-25 18:31:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 13:27:24 +00:00
|
|
|
if m != nil {
|
2021-05-25 14:26:03 +00:00
|
|
|
compiledFields["msg"] = m
|
|
|
|
compiledFields["username"] = m.User
|
2021-05-06 13:27:24 +00:00
|
|
|
|
2021-06-28 11:31:37 +00:00
|
|
|
if len(m.Params) > 0 {
|
2021-05-25 14:26:03 +00:00
|
|
|
compiledFields["channel"] = m.Params[0]
|
2021-05-06 13:27:24 +00:00
|
|
|
}
|
2020-12-25 18:39:44 +00:00
|
|
|
}
|
|
|
|
|
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-05-25 14:26:03 +00:00
|
|
|
err = tpl.Execute(buf, compiledFields)
|
2020-12-25 18:31:07 +00:00
|
|
|
|
|
|
|
return buf.String(), errors.Wrap(err, "execute template")
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|