From cad3f982cd1fdfdbf940c4034473415bee477a35 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 25 Dec 2020 19:31:07 +0100 Subject: [PATCH] Simplify function definitions Signed-off-by: Knut Ahlers --- action_ban.go | 2 +- action_counter.go | 2 +- action_respond.go | 4 +-- action_timeout.go | 2 +- actions.go | 8 ++--- msgformatter.go | 74 ++++++++++++++++++++++++++--------------------- 6 files changed, 50 insertions(+), 42 deletions(-) diff --git a/action_ban.go b/action_ban.go index a795ad1..a5a3343 100644 --- a/action_ban.go +++ b/action_ban.go @@ -8,7 +8,7 @@ import ( ) 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 { return nil } diff --git a/action_counter.go b/action_counter.go index bdd2b54..fc4e2ef 100644 --- a/action_counter.go +++ b/action_counter.go @@ -6,7 +6,7 @@ import ( ) 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 { return nil } diff --git a/action_respond.go b/action_respond.go index 4b39cab..7263ee5 100644 --- a/action_respond.go +++ b/action_respond.go @@ -6,12 +6,12 @@ import ( ) 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 { return nil } - msg, err := formatMessage(*r.Respond, m, nil) + msg, err := formatMessage(*r.Respond, m, ruleDef, nil) if err != nil { return errors.Wrap(err, "preparing response") } diff --git a/action_timeout.go b/action_timeout.go index 3f3b5e6..834473b 100644 --- a/action_timeout.go +++ b/action_timeout.go @@ -9,7 +9,7 @@ import ( ) 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 { return nil } diff --git a/actions.go b/actions.go index ecb2dda..f08596b 100644 --- a/actions.go +++ b/actions.go @@ -13,7 +13,7 @@ var ( 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) { availableActionsLock.Lock() @@ -22,12 +22,12 @@ func registerAction(af actionFunc) { 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() defer availableActionsLock.RUnlock() 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") } } @@ -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) { for _, r := range config.GetMatchingRules(m, event) { 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") } } diff --git a/msgformatter.go b/msgformatter.go index e70273b..9d10cb4 100644 --- a/msgformatter.go +++ b/msgformatter.go @@ -12,9 +12,48 @@ import ( "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. New(tplString). 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") } - -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 - } -}