2020-12-21 00:32:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-12-25 18:31:07 +00:00
|
|
|
func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string]interface{}) (string, error) {
|
2021-04-21 20:43:33 +00:00
|
|
|
if fields == nil {
|
|
|
|
fields = map[string]interface{}{}
|
2020-12-25 18:31:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 20:43:33 +00:00
|
|
|
fields["msg"] = m
|
|
|
|
fields["permitTimeout"] = int64(config.PermitTimeout / time.Second)
|
|
|
|
fields["username"] = m.User
|
2020-12-25 18:31:07 +00:00
|
|
|
|
2021-04-21 20:43:33 +00:00
|
|
|
if m.Command == "PRIVMSG" && len(m.Params) > 0 {
|
|
|
|
fields["channel"] = m.Params[0]
|
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-04-21 20:43:33 +00:00
|
|
|
Funcs(tplFuncs.GetFuncMap(m, r, fields)).
|
2020-12-25 18:31:07 +00:00
|
|
|
Parse(tplString)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "parse template")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = tpl.Execute(buf, fields)
|
|
|
|
|
|
|
|
return buf.String(), errors.Wrap(err, "execute template")
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|