2020-12-21 00:32:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2020-12-21 00:32:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-09-22 13:36:45 +00:00
|
|
|
availableActions = map[string]plugins.ActorCreationFunc{}
|
2020-12-21 00:32:39 +00:00
|
|
|
availableActionsLock = new(sync.RWMutex)
|
|
|
|
)
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
// Compile-time assertion
|
|
|
|
var _ plugins.ActorRegistrationFunc = registerAction
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
func getActorByName(name string) (plugins.Actor, error) {
|
|
|
|
availableActionsLock.RLock()
|
|
|
|
defer availableActionsLock.RUnlock()
|
|
|
|
|
|
|
|
acf, ok := availableActions[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("undefined actor %q called", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return acf(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerAction(name string, acf plugins.ActorCreationFunc) {
|
2020-12-21 00:32:39 +00:00
|
|
|
availableActionsLock.Lock()
|
|
|
|
defer availableActionsLock.Unlock()
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
if _, ok := availableActions[name]; ok {
|
|
|
|
log.WithField("name", name).Fatal("Duplicate registration of actor")
|
|
|
|
}
|
|
|
|
|
|
|
|
availableActions[name] = acf
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
func triggerAction(c *irc.Client, m *irc.Message, rule *plugins.Rule, ra *plugins.RuleAction, eventData *plugins.FieldCollection) (preventCooldown bool, err error) {
|
2020-12-21 00:32:39 +00:00
|
|
|
availableActionsLock.RLock()
|
|
|
|
defer availableActionsLock.RUnlock()
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
a, err := getActorByName(ra.Type)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "getting actor")
|
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
logger := log.WithField("actor", a.Name())
|
2021-06-11 11:52:42 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
if a.IsAsync() {
|
|
|
|
go func() {
|
|
|
|
if _, err := a.Execute(c, m, rule, eventData, ra.Attributes); err != nil {
|
|
|
|
logger.WithError(err).Error("Error in async actor")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return preventCooldown, nil
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
apc, err := a.Execute(c, m, rule, eventData, ra.Attributes)
|
|
|
|
return apc, errors.Wrap(err, "execute action")
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
func handleMessage(c *irc.Client, m *irc.Message, event *string, eventData *plugins.FieldCollection) {
|
2021-09-02 21:26:39 +00:00
|
|
|
for _, r := range config.GetMatchingRules(m, event, eventData) {
|
2021-08-11 22:12:10 +00:00
|
|
|
var preventCooldown bool
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
for _, a := range r.Actions {
|
2021-09-22 13:36:45 +00:00
|
|
|
apc, err := triggerAction(c, m, r, a, eventData)
|
2021-08-11 22:12:10 +00:00
|
|
|
if err != nil {
|
2020-12-21 00:32:39 +00:00
|
|
|
log.WithError(err).Error("Unable to trigger action")
|
2021-09-11 18:04:47 +00:00
|
|
|
break // Break execution when one action fails
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
2021-08-11 22:12:10 +00:00
|
|
|
preventCooldown = preventCooldown || apc
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock command
|
2021-08-11 22:12:10 +00:00
|
|
|
if !preventCooldown {
|
2021-09-02 21:26:39 +00:00
|
|
|
r.SetCooldown(timerStore, m, eventData)
|
2021-08-11 22:12:10 +00:00
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
2022-02-08 18:58:19 +00:00
|
|
|
|
|
|
|
// Send events to registered handlers
|
|
|
|
if event != nil {
|
|
|
|
notifyEventHandlers(*event, eventData)
|
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|