2021-04-21 20:43:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-13 12:30:45 +00:00
|
|
|
"fmt"
|
2021-04-21 20:43:33 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"text/template"
|
2021-08-19 13:33:56 +00:00
|
|
|
"time"
|
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-11-25 22:48:16 +00:00
|
|
|
|
|
|
|
korvike "github.com/Luzifer/korvike/functions"
|
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
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-11-11 13:59:08 +00:00
|
|
|
func (t *templateFuncProvider) GetFuncMap(m *irc.Message, r *plugins.Rule, fields *plugins.FieldCollection) 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-12-06 16:25:19 +00:00
|
|
|
func (t *templateFuncProvider) GetFuncNames() []string {
|
|
|
|
t.lock.RLock()
|
|
|
|
defer t.lock.RUnlock()
|
|
|
|
|
|
|
|
var out []string
|
|
|
|
|
|
|
|
for n := range t.funcs {
|
|
|
|
out = append(out, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-08-19 15:27:47 +00:00
|
|
|
log.Fatalf("Duplicate registration of %q template function", name) //nolint:gocritic // Yeah, the unlock will not run but the process will end
|
2021-08-19 14:40:34 +00:00
|
|
|
}
|
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-10-13 12:30:45 +00:00
|
|
|
tplFuncs.Register("concat", plugins.GenericTemplateFunctionGetter(func(delim string, parts ...string) string { return strings.Join(parts, delim) }))
|
|
|
|
|
|
|
|
tplFuncs.Register("formatDuration", plugins.GenericTemplateFunctionGetter(func(dur time.Duration, units ...string) string {
|
|
|
|
dLeft := dur
|
|
|
|
|
|
|
|
if len(units) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var parts []string
|
|
|
|
for idx, div := range []time.Duration{time.Hour, time.Minute, time.Second} {
|
|
|
|
part := dLeft / div
|
|
|
|
dLeft -= part * div
|
|
|
|
|
|
|
|
if len(units) <= idx || units[idx] == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parts = append(parts, fmt.Sprintf("%d %s", part, units[idx]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(parts, ", ")
|
|
|
|
}))
|
|
|
|
|
2021-08-19 14:40:34 +00:00
|
|
|
tplFuncs.Register("toLower", plugins.GenericTemplateFunctionGetter(strings.ToLower))
|
|
|
|
tplFuncs.Register("toUpper", plugins.GenericTemplateFunctionGetter(strings.ToUpper))
|
2021-10-13 12:30:45 +00:00
|
|
|
|
2021-08-19 14:40:34 +00:00
|
|
|
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
|
|
|
}
|