Simplify function definitions

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-12-25 19:31:07 +01:00
parent f2f4d43412
commit cad3f982cd
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
6 changed files with 50 additions and 42 deletions

View file

@ -8,7 +8,7 @@ import (
) )
func init() { func init() {
registerAction(func(c *irc.Client, m *irc.Message, r *ruleAction) error { registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
if r.Ban == nil { if r.Ban == nil {
return nil return nil
} }

View file

@ -6,7 +6,7 @@ import (
) )
func init() { func init() {
registerAction(func(c *irc.Client, m *irc.Message, r *ruleAction) error { registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
if r.Counter == nil { if r.Counter == nil {
return nil return nil
} }

View file

@ -6,12 +6,12 @@ import (
) )
func init() { func init() {
registerAction(func(c *irc.Client, m *irc.Message, r *ruleAction) error { registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
if r.Respond == nil { if r.Respond == nil {
return nil return nil
} }
msg, err := formatMessage(*r.Respond, m, nil) msg, err := formatMessage(*r.Respond, m, ruleDef, nil)
if err != nil { if err != nil {
return errors.Wrap(err, "preparing response") return errors.Wrap(err, "preparing response")
} }

View file

@ -9,7 +9,7 @@ import (
) )
func init() { func init() {
registerAction(func(c *irc.Client, m *irc.Message, r *ruleAction) error { registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
if r.Timeout == nil { if r.Timeout == nil {
return nil return nil
} }

View file

@ -13,7 +13,7 @@ var (
availableActionsLock = new(sync.RWMutex) availableActionsLock = new(sync.RWMutex)
) )
type actionFunc func(*irc.Client, *irc.Message, *ruleAction) error type actionFunc func(*irc.Client, *irc.Message, *rule, *ruleAction) error
func registerAction(af actionFunc) { func registerAction(af actionFunc) {
availableActionsLock.Lock() availableActionsLock.Lock()
@ -22,12 +22,12 @@ func registerAction(af actionFunc) {
availableActions = append(availableActions, af) availableActions = append(availableActions, af)
} }
func triggerActions(c *irc.Client, m *irc.Message, ra *ruleAction) error { func triggerActions(c *irc.Client, m *irc.Message, rule *rule, ra *ruleAction) error {
availableActionsLock.RLock() availableActionsLock.RLock()
defer availableActionsLock.RUnlock() defer availableActionsLock.RUnlock()
for _, af := range availableActions { for _, af := range availableActions {
if err := af(c, m, ra); err != nil { if err := af(c, m, rule, ra); err != nil {
return errors.Wrap(err, "execute action") return errors.Wrap(err, "execute action")
} }
} }
@ -38,7 +38,7 @@ func triggerActions(c *irc.Client, m *irc.Message, ra *ruleAction) error {
func handleMessage(c *irc.Client, m *irc.Message, event *string) { func handleMessage(c *irc.Client, m *irc.Message, event *string) {
for _, r := range config.GetMatchingRules(m, event) { for _, r := range config.GetMatchingRules(m, event) {
for _, a := range r.Actions { for _, a := range r.Actions {
if err := triggerActions(c, m, a); err != nil { if err := triggerActions(c, m, r, a); err != nil {
log.WithError(err).Error("Unable to trigger action") log.WithError(err).Error("Unable to trigger action")
} }
} }

View file

@ -12,9 +12,48 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
var messageFunctions = korvike.GetFunctionMap() 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
messageFunctions := korvike.GetFunctionMap()
messageFunctions["fixUsername"] = func(username string) string { return strings.TrimLeft(username, "@") }
func formatMessage(tplString string, m *irc.Message, fields map[string]interface{}) (string, error) { messageFunctions["getArg"] = func(arg int) (string, error) {
msgParts := strings.Split(m.Trailing(), " ")
if len(msgParts) <= arg {
return "", errors.New("argument not found")
}
return msgParts[arg], nil
}
messageFunctions["getCounterValue"] = func(name string, _ ...string) int64 {
return store.GetCounterValue(name)
}
messageFunctions["getTag"] = func(tag string) string {
s, _ := m.GetTag(tag)
return s
}
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
}
messageFunctions["recentGame"] = func(username string, v ...string) (string, error) {
game, _, err := twitch.getRecentStreamInfo(username)
if err != nil && len(v) > 0 {
return v[0], nil
}
return game, err
}
// Parse and execute template
tpl, err := template. tpl, err := template.
New(tplString). New(tplString).
Funcs(messageFunctions). Funcs(messageFunctions).
@ -36,34 +75,3 @@ func formatMessage(tplString string, m *irc.Message, fields map[string]interface
return buf.String(), errors.Wrap(err, "execute template") return buf.String(), errors.Wrap(err, "execute template")
} }
func init() {
messageFunctions["fixUsername"] = func(username string) string { return strings.TrimLeft(username, "@") }
messageFunctions["getArg"] = func(m *irc.Message, params ...int) (string, error) {
msgParts := strings.Split(m.Trailing(), " ")
if len(msgParts) < params[0]+1 {
return "", errors.New("argument not found")
}
return msgParts[params[0]], nil
}
messageFunctions["getCounterValue"] = func(name string, _ ...string) int64 {
return store.GetCounterValue(name)
}
messageFunctions["getTag"] = func(m *irc.Message, params ...string) string {
s, _ := m.GetTag(params[0])
return s
}
messageFunctions["recentGame"] = func(username string, v ...string) (string, error) {
game, _, err := twitch.getRecentStreamInfo(username)
if err != nil && len(v) > 0 {
return v[0], nil
}
return game, err
}
}