2020-12-21 00:32:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
korvike "github.com/Luzifer/korvike/functions"
|
|
|
|
|
|
|
|
"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) {
|
|
|
|
// Create anonymous functions in current context in order to access function variables
|
2021-03-21 13:43:26 +00:00
|
|
|
messageFunctions := make(template.FuncMap)
|
|
|
|
for n, f := range korvike.GetFunctionMap() {
|
|
|
|
messageFunctions[n] = f
|
|
|
|
}
|
2020-12-24 14:06:59 +00:00
|
|
|
|
2021-01-20 22:45:09 +00:00
|
|
|
// Generic functions
|
|
|
|
messageFunctions["toLower"] = strings.ToLower
|
|
|
|
messageFunctions["toUpper"] = strings.ToUpper
|
2021-01-20 23:35:42 +00:00
|
|
|
messageFunctions["followDate"] = twitch.GetFollowDate
|
2021-01-20 22:45:09 +00:00
|
|
|
|
|
|
|
// Message specific functions
|
2020-12-25 18:39:44 +00:00
|
|
|
messageFunctions["arg"] = func(arg int) (string, error) {
|
2020-12-24 12:19:58 +00:00
|
|
|
msgParts := strings.Split(m.Trailing(), " ")
|
2020-12-25 18:31:07 +00:00
|
|
|
if len(msgParts) <= arg {
|
2020-12-24 12:19:58 +00:00
|
|
|
return "", errors.New("argument not found")
|
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2020-12-25 18:31:07 +00:00
|
|
|
return msgParts[arg], nil
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 01:01:13 +00:00
|
|
|
messageFunctions["channelCounter"] = func(name string) (string, error) {
|
|
|
|
channel, ok := fields["channel"].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("channel not available")
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join([]string{channel, name}, ":"), nil
|
|
|
|
}
|
|
|
|
|
2020-12-25 18:39:44 +00:00
|
|
|
messageFunctions["counterValue"] = func(name string, _ ...string) int64 {
|
2020-12-24 12:19:58 +00:00
|
|
|
return store.GetCounterValue(name)
|
|
|
|
}
|
|
|
|
|
2021-01-20 23:35:42 +00:00
|
|
|
messageFunctions["fixUsername"] = func(username string) string { return strings.TrimLeft(username, "@#") }
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2020-12-25 18:31:07 +00:00
|
|
|
messageFunctions["group"] = func(idx int) (string, error) {
|
|
|
|
fields := r.matchMessage.FindStringSubmatch(m.Trailing())
|
|
|
|
if len(fields) <= idx {
|
|
|
|
return "", errors.New("group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return fields[idx], nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 12:30:42 +00:00
|
|
|
messageFunctions["recentGame"] = func(username string, v ...string) (string, error) {
|
2021-03-21 13:02:44 +00:00
|
|
|
game, _, err := twitch.GetRecentStreamInfo(strings.TrimLeft(username, "#"))
|
2020-12-24 12:30:42 +00:00
|
|
|
if err != nil && len(v) > 0 {
|
|
|
|
return v[0], nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 12:19:58 +00:00
|
|
|
return game, err
|
|
|
|
}
|
2020-12-25 18:31:07 +00:00
|
|
|
|
2020-12-25 18:39:44 +00:00
|
|
|
messageFunctions["tag"] = func(tag string) string {
|
|
|
|
s, _ := m.GetTag(tag)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-12-25 18:31:07 +00:00
|
|
|
// Parse and execute template
|
|
|
|
tpl, err := template.
|
|
|
|
New(tplString).
|
|
|
|
Funcs(messageFunctions).
|
|
|
|
Parse(tplString)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "parse template")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fields == nil {
|
|
|
|
fields = map[string]interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
fields["msg"] = m
|
|
|
|
fields["permitTimeout"] = int64(*&config.PermitTimeout / time.Second)
|
|
|
|
fields["username"] = m.User
|
|
|
|
|
2020-12-26 00:36:18 +00:00
|
|
|
if m.Command == "PRIVMSG" && len(m.Params) > 0 {
|
|
|
|
fields["channel"] = m.Params[0]
|
|
|
|
}
|
|
|
|
|
2020-12-25 18:31:07 +00:00
|
|
|
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
|
|
|
}
|