Add registration for raw-message-handlers

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-09-02 15:01:10 +02:00
parent f0b6556083
commit e4d4c45e7a
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
3 changed files with 46 additions and 7 deletions

View file

@ -57,12 +57,13 @@ func registerRoute(route plugins.HTTPRouteRegistrationArgs) error {
func getRegistrationArguments() plugins.RegistrationArguments { func getRegistrationArguments() plugins.RegistrationArguments {
return plugins.RegistrationArguments{ return plugins.RegistrationArguments{
FormatMessage: formatMessage, FormatMessage: formatMessage,
GetLogger: func(moduleName string) *log.Entry { return log.WithField("module", moduleName) }, GetLogger: func(moduleName string) *log.Entry { return log.WithField("module", moduleName) },
RegisterActor: registerAction, RegisterActor: registerAction,
RegisterAPIRoute: registerRoute, RegisterAPIRoute: registerRoute,
RegisterCron: cronService.AddFunc, RegisterCron: cronService.AddFunc,
RegisterTemplateFunction: tplFuncs.Register, RegisterRawMessageHandler: registerRawMessageHandler,
SendMessage: sendMessage, RegisterTemplateFunction: tplFuncs.Register,
SendMessage: sendMessage,
} }
} }

33
irc.go
View file

@ -4,13 +4,42 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"strings" "strings"
"sync"
"github.com/Luzifer/twitch-bot/plugins"
"github.com/Luzifer/twitch-bot/twitch" "github.com/Luzifer/twitch-bot/twitch"
"github.com/go-irc/irc" "github.com/go-irc/irc"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var (
rawMessageHandlers []plugins.RawMessageHandlerFunc
rawMessageHandlersLock sync.Mutex
)
func notifyRawMessageHandlers(m *irc.Message) error {
rawMessageHandlersLock.Lock()
defer rawMessageHandlersLock.Unlock()
for _, fn := range rawMessageHandlers {
if err := fn(m); err != nil {
return errors.Wrap(err, "executing raw message handlers")
}
}
return nil
}
func registerRawMessageHandler(fn plugins.RawMessageHandlerFunc) error {
rawMessageHandlersLock.Lock()
defer rawMessageHandlersLock.Unlock()
rawMessageHandlers = append(rawMessageHandlers, fn)
return nil
}
type ircHandler struct { type ircHandler struct {
conn *tls.Conn conn *tls.Conn
c *irc.Client c *irc.Client
@ -127,6 +156,10 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
}).Trace("Unhandled message") }).Trace("Unhandled message")
// Unhandled message type, not yet needed // Unhandled message type, not yet needed
} }
if err := notifyRawMessageHandlers(m); err != nil {
log.WithError(err).Error("Unable to notify raw message handlers")
}
} }
func (i ircHandler) Run() error { return errors.Wrap(i.c.Run(), "running IRC client") } func (i ircHandler) Run() error { return errors.Wrap(i.c.Run(), "running IRC client") }

View file

@ -29,6 +29,9 @@ type (
MsgFormatter func(tplString string, m *irc.Message, r *Rule, fields map[string]interface{}) (string, error) MsgFormatter func(tplString string, m *irc.Message, r *Rule, fields map[string]interface{}) (string, error)
RawMessageHandlerFunc func(m *irc.Message) error
RawMessageHandlerRegisterFunc func(RawMessageHandlerFunc) error
// RegisterFunc is the type of function your plugin must expose with the name Register // RegisterFunc is the type of function your plugin must expose with the name Register
RegisterFunc func(RegistrationArguments) error RegisterFunc func(RegistrationArguments) error
@ -43,6 +46,8 @@ type (
RegisterAPIRoute HTTPRouteRegistrationFunc RegisterAPIRoute HTTPRouteRegistrationFunc
// RegisterCron is a method to register cron functions in the global cron instance // RegisterCron is a method to register cron functions in the global cron instance
RegisterCron CronRegistrationFunc RegisterCron CronRegistrationFunc
// RegisterRawMessageHandler is a method to register an handler to receive ALL messages received
RegisterRawMessageHandler RawMessageHandlerRegisterFunc
// RegisterTemplateFunction can be used to register a new template functions // RegisterTemplateFunction can be used to register a new template functions
RegisterTemplateFunction TemplateFuncRegister RegisterTemplateFunction TemplateFuncRegister
// SendMessage can be used to send a message not triggered by an event // SendMessage can be used to send a message not triggered by an event