2021-04-21 20:43:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"text/template"
|
2021-08-19 13:33:56 +00:00
|
|
|
"time"
|
2021-04-21 20:43:33 +00:00
|
|
|
|
|
|
|
korvike "github.com/Luzifer/korvike/functions"
|
2021-08-19 13:33:56 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2021-04-21 20:43:33 +00:00
|
|
|
"github.com/go-irc/irc"
|
2021-08-19 14:40:34 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-04-21 20:43:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var tplFuncs = newTemplateFuncProvider()
|
|
|
|
|
2021-08-19 14:40:34 +00:00
|
|
|
type templateFuncProvider struct {
|
|
|
|
funcs map[string]plugins.TemplateFuncGetter
|
|
|
|
lock *sync.RWMutex
|
|
|
|
}
|
2021-04-21 20:43:33 +00:00
|
|
|
|
|
|
|
func newTemplateFuncProvider() *templateFuncProvider {
|
|
|
|
out := &templateFuncProvider{
|
2021-08-19 14:40:34 +00:00
|
|
|
funcs: map[string]plugins.TemplateFuncGetter{},
|
2021-04-21 20:43:33 +00:00
|
|
|
lock: new(sync.RWMutex),
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (t *templateFuncProvider) GetFuncMap(m *irc.Message, r *plugins.Rule, fields map[string]interface{}) template.FuncMap {
|
2021-04-21 20:43:33 +00:00
|
|
|
t.lock.RLock()
|
|
|
|
defer t.lock.RUnlock()
|
|
|
|
|
|
|
|
out := make(template.FuncMap)
|
|
|
|
|
|
|
|
for n, fg := range t.funcs {
|
|
|
|
out[n] = fg(m, r, fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:40:34 +00:00
|
|
|
func (t *templateFuncProvider) Register(name string, fg plugins.TemplateFuncGetter) {
|
2021-04-21 20:43:33 +00:00
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
2021-08-19 14:40:34 +00:00
|
|
|
if _, ok := t.funcs[name]; ok {
|
|
|
|
log.Fatalf("Duplicate registration of %q template function", name)
|
|
|
|
}
|
2021-04-21 20:43:33 +00:00
|
|
|
|
2021-08-19 14:40:34 +00:00
|
|
|
t.funcs[name] = fg
|
2021-04-21 20:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Register Korvike functions
|
|
|
|
for n, f := range korvike.GetFunctionMap() {
|
2021-08-19 14:40:34 +00:00
|
|
|
tplFuncs.Register(n, plugins.GenericTemplateFunctionGetter(f))
|
2021-04-21 20:43:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 14:40:34 +00:00
|
|
|
tplFuncs.Register("toLower", plugins.GenericTemplateFunctionGetter(strings.ToLower))
|
|
|
|
tplFuncs.Register("toUpper", plugins.GenericTemplateFunctionGetter(strings.ToUpper))
|
|
|
|
tplFuncs.Register("followDate", plugins.GenericTemplateFunctionGetter(func(from, to string) (time.Time, error) { return twitchClient.GetFollowDate(from, to) }))
|
|
|
|
tplFuncs.Register("concat", plugins.GenericTemplateFunctionGetter(func(delim string, parts ...string) string { return strings.Join(parts, delim) }))
|
|
|
|
tplFuncs.Register("variable", plugins.GenericTemplateFunctionGetter(func(name string, defVal ...string) string {
|
2021-06-29 12:08:26 +00:00
|
|
|
value := store.GetVariable(name)
|
|
|
|
if value == "" && len(defVal) > 0 {
|
|
|
|
return defVal[0]
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}))
|
2021-04-21 20:43:33 +00:00
|
|
|
}
|