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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
availableActions []actionFunc
|
|
|
|
availableActionsLock = new(sync.RWMutex)
|
|
|
|
)
|
|
|
|
|
2020-12-25 18:31:07 +00:00
|
|
|
type actionFunc func(*irc.Client, *irc.Message, *rule, *ruleAction) error
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
|
|
func registerAction(af actionFunc) {
|
|
|
|
availableActionsLock.Lock()
|
|
|
|
defer availableActionsLock.Unlock()
|
|
|
|
|
|
|
|
availableActions = append(availableActions, af)
|
|
|
|
}
|
|
|
|
|
2020-12-25 18:31:07 +00:00
|
|
|
func triggerActions(c *irc.Client, m *irc.Message, rule *rule, ra *ruleAction) error {
|
2020-12-21 00:32:39 +00:00
|
|
|
availableActionsLock.RLock()
|
|
|
|
defer availableActionsLock.RUnlock()
|
|
|
|
|
|
|
|
for _, af := range availableActions {
|
2020-12-25 18:31:07 +00:00
|
|
|
if err := af(c, m, rule, ra); err != nil {
|
2020-12-21 00:32:39 +00:00
|
|
|
return errors.Wrap(err, "execute action")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMessage(c *irc.Client, m *irc.Message, event *string) {
|
|
|
|
for _, r := range config.GetMatchingRules(m, event) {
|
|
|
|
for _, a := range r.Actions {
|
2020-12-25 18:31:07 +00:00
|
|
|
if err := triggerActions(c, m, r, a); err != nil {
|
2020-12-21 00:32:39 +00:00
|
|
|
log.WithError(err).Error("Unable to trigger action")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock command
|
2021-01-24 00:46:40 +00:00
|
|
|
timerStore.AddCooldown(r.MatcherID())
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
}
|