2021-08-19 13:33:56 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
2022-10-25 16:47:30 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-08-24 22:44:49 +00:00
|
|
|
"github.com/robfig/cron/v3"
|
2024-01-01 16:52:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2023-11-25 19:23:34 +00:00
|
|
|
"gorm.io/gorm"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/database"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
2021-08-19 13:33:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2024-01-01 16:52:18 +00:00
|
|
|
// Actor defines an interface to implement in the plugin for actors
|
2021-08-19 13:33:56 +00:00
|
|
|
Actor interface {
|
|
|
|
// Execute will be called after the config was read into the Actor
|
2021-11-11 13:59:08 +00:00
|
|
|
Execute(c *irc.Client, m *irc.Message, r *Rule, evtData *FieldCollection, attrs *FieldCollection) (preventCooldown bool, err error)
|
2021-08-19 13:33:56 +00:00
|
|
|
// IsAsync may return true if the Execute function is to be executed
|
|
|
|
// in a Go routine as of long runtime. Normally it should return false
|
|
|
|
// except in very specific cases
|
|
|
|
IsAsync() bool
|
|
|
|
// Name must return an unique name for the actor in order to identify
|
|
|
|
// it in the logs for debugging purposes
|
|
|
|
Name() string
|
2021-09-22 13:36:45 +00:00
|
|
|
// Validate will be called to validate the loaded configuration. It should
|
|
|
|
// return an error if required keys are missing from the AttributeStore
|
|
|
|
// or if keys contain broken configs
|
2022-10-31 16:26:53 +00:00
|
|
|
Validate(TemplateValidatorFunc, *FieldCollection) error
|
2021-08-19 13:33:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// ActorCreationFunc is a function to return a new instance of the
|
|
|
|
// plugins actor
|
2021-08-19 13:33:56 +00:00
|
|
|
ActorCreationFunc func() Actor
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// ActorRegistrationFunc is passed from the bot to the plugins
|
|
|
|
// RegisterFunc to register a new actor in the bot
|
2021-09-22 13:36:45 +00:00
|
|
|
ActorRegistrationFunc func(name string, acf ActorCreationFunc)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// ActorDocumentationRegistrationFunc is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to register a new actor documentation
|
2021-09-22 13:36:45 +00:00
|
|
|
ActorDocumentationRegistrationFunc func(ActionDocumentation)
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// ChannelPermissionCheckFunc is available to check whether the bot
|
|
|
|
// has stored scopes / permissions for the given channel. All given
|
|
|
|
// scopes need to be available to return true.
|
|
|
|
ChannelPermissionCheckFunc func(channel string, scopes ...string) (bool, error)
|
|
|
|
// ChannelAnyPermissionCheckFunc is available to check whether the bot
|
|
|
|
// has stored scopes / permissions for the given channel. Any of the
|
|
|
|
// given scopes need to be available to return true.
|
2022-12-05 17:57:41 +00:00
|
|
|
ChannelAnyPermissionCheckFunc func(channel string, scopes ...string) (bool, error)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// CronRegistrationFunc is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to register a new cron function in the
|
|
|
|
// internal cron scheduler
|
2021-08-24 22:44:49 +00:00
|
|
|
CronRegistrationFunc func(spec string, cmd func()) (cron.EntryID, error)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// DatabaseCopyFunc defines the function type the plugin must
|
|
|
|
// implement and register to enable the bot to replicate its database
|
|
|
|
// stored content into a new database
|
2023-11-25 19:23:34 +00:00
|
|
|
DatabaseCopyFunc func(src, target *gorm.DB) error
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// EventHandlerFunc defines the type of function required to listen
|
|
|
|
// for events
|
|
|
|
EventHandlerFunc func(evt string, eventData *FieldCollection) error
|
|
|
|
// EventHandlerRegisterFunc is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to register a new event handler function
|
|
|
|
// which is then fed with all events occurring in the bot
|
2022-02-08 18:58:19 +00:00
|
|
|
EventHandlerRegisterFunc func(EventHandlerFunc) error
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// LoggerCreationFunc is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to retrieve a pre-configured logrus.Entry
|
|
|
|
// scoped for the given module name
|
|
|
|
LoggerCreationFunc func(moduleName string) *logrus.Entry
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// ModuleConfigGetterFunc is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to fetch module generic or channel specific
|
|
|
|
// configuration from the module configuration
|
2023-09-02 11:49:15 +00:00
|
|
|
ModuleConfigGetterFunc func(module, channel string) *FieldCollection
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// MsgFormatter is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to format messages using all registered and
|
|
|
|
// available template functions
|
2021-11-11 13:59:08 +00:00
|
|
|
MsgFormatter func(tplString string, m *irc.Message, r *Rule, fields *FieldCollection) (string, error)
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// MsgModificationFunc can be used to modify messages between the
|
|
|
|
// plugins generating them and the bot sending them to the Twitch
|
|
|
|
// servers
|
|
|
|
MsgModificationFunc func(*irc.Message) error
|
|
|
|
// MsgModificationRegistrationFunc is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to register a new MsgModificationFunc for
|
|
|
|
// the given prefix
|
2022-10-25 16:47:30 +00:00
|
|
|
MsgModificationRegistrationFunc func(linePrefix string, modFn MsgModificationFunc)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// RawMessageHandlerFunc is the type of function to implement in
|
|
|
|
// your plugin in order to process raw-messages from the IRC
|
|
|
|
// connection
|
|
|
|
RawMessageHandlerFunc func(m *irc.Message) error
|
|
|
|
// RawMessageHandlerRegisterFunc is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to register a new RawMessageHandlerFunc
|
2021-09-02 13:01:10 +00:00
|
|
|
RawMessageHandlerRegisterFunc func(RawMessageHandlerFunc) error
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
// RegisterFunc is the type of function your plugin must expose with the name Register
|
|
|
|
RegisterFunc func(RegistrationArguments) error
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// RegistrationArguments is the object your RegisterFunc will receive
|
|
|
|
// and can use to interact with the bot instance
|
2021-08-19 13:33:56 +00:00
|
|
|
RegistrationArguments struct {
|
2022-03-30 22:23:19 +00:00
|
|
|
// CreateEvent allows to create an event handed out to all modules to handle
|
|
|
|
CreateEvent EventHandlerFunc
|
2021-08-19 13:33:56 +00:00
|
|
|
// FormatMessage is a method to convert templates into strings using internally known variables / configs
|
|
|
|
FormatMessage MsgFormatter
|
2023-07-14 14:15:58 +00:00
|
|
|
// FrontendNotify is a way to send a notification to the frontend
|
|
|
|
FrontendNotify func(string)
|
2024-03-15 18:51:00 +00:00
|
|
|
// GetBaseURL returns the configured BaseURL for the bot
|
|
|
|
GetBaseURL func() string
|
2022-09-10 11:39:07 +00:00
|
|
|
// GetDatabaseConnector returns an active database.Connector to access the backend storage database
|
|
|
|
GetDatabaseConnector func() database.Connector
|
2021-08-19 13:33:56 +00:00
|
|
|
// GetLogger returns a sirupsen log.Entry pre-configured with the module name
|
|
|
|
GetLogger LoggerCreationFunc
|
2023-09-02 11:49:15 +00:00
|
|
|
// GetModuleConfigForChannel returns the module configuration for the given channel if available
|
|
|
|
GetModuleConfigForChannel ModuleConfigGetterFunc
|
2021-09-10 15:57:04 +00:00
|
|
|
// GetTwitchClient retrieves a fully configured Twitch client with initialized cache
|
|
|
|
GetTwitchClient func() *twitch.Client
|
2022-04-23 15:24:49 +00:00
|
|
|
// GetTwitchClientForChannel retrieves a fully configured Twitch client with initialized cache for extended permission channels
|
|
|
|
GetTwitchClientForChannel func(string) (*twitch.Client, error)
|
2022-12-05 17:57:41 +00:00
|
|
|
// HasAnyPermissionForChannel checks whether ANY of the given permissions were granted for the given channel
|
|
|
|
HasAnyPermissionForChannel ChannelAnyPermissionCheckFunc
|
|
|
|
// HasPermissionForChannel checks whether ALL of the given permissions were granted for the given channel
|
|
|
|
HasPermissionForChannel ChannelPermissionCheckFunc
|
2021-08-19 13:33:56 +00:00
|
|
|
// RegisterActor is used to register a new IRC rule-actor implementing the Actor interface
|
|
|
|
RegisterActor ActorRegistrationFunc
|
2021-09-22 13:36:45 +00:00
|
|
|
// RegisterActorDocumentation is used to register an ActorDocumentation for the config editor
|
|
|
|
RegisterActorDocumentation ActorDocumentationRegistrationFunc
|
2021-08-28 15:27:24 +00:00
|
|
|
// RegisterAPIRoute registers a new HTTP handler function including documentation
|
|
|
|
RegisterAPIRoute HTTPRouteRegistrationFunc
|
2023-11-25 19:23:34 +00:00
|
|
|
// RegisterCopyDatabaseFunc registers a DatabaseCopyFunc for the
|
|
|
|
// database migration tool. Modules not registering such a func
|
|
|
|
// will not be copied over when migrating to another database.
|
|
|
|
RegisterCopyDatabaseFunc func(name string, fn DatabaseCopyFunc)
|
2021-08-24 22:44:49 +00:00
|
|
|
// RegisterCron is a method to register cron functions in the global cron instance
|
|
|
|
RegisterCron CronRegistrationFunc
|
2022-02-08 18:58:19 +00:00
|
|
|
// RegisterEventHandler is a method to register a handler function receiving ALL events
|
|
|
|
RegisterEventHandler EventHandlerRegisterFunc
|
2022-10-25 16:47:30 +00:00
|
|
|
// RegisterMessageModFunc is a method to register a handler to modify / react on messages
|
|
|
|
RegisterMessageModFunc MsgModificationRegistrationFunc
|
2021-09-02 13:01:10 +00:00
|
|
|
// RegisterRawMessageHandler is a method to register an handler to receive ALL messages received
|
|
|
|
RegisterRawMessageHandler RawMessageHandlerRegisterFunc
|
2021-08-19 14:40:34 +00:00
|
|
|
// RegisterTemplateFunction can be used to register a new template functions
|
|
|
|
RegisterTemplateFunction TemplateFuncRegister
|
2021-08-19 13:33:56 +00:00
|
|
|
// SendMessage can be used to send a message not triggered by an event
|
|
|
|
SendMessage SendMessageFunc
|
2022-02-08 18:58:19 +00:00
|
|
|
// ValidateToken offers a way to validate a token and determine whether it has permissions on a given module
|
|
|
|
ValidateToken ValidateTokenFunc
|
2021-08-19 13:33:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// SendMessageFunc is available through the RegistrationArguments
|
|
|
|
// and MUST be used to send messages to the Twitch servers
|
2021-08-19 13:33:56 +00:00
|
|
|
SendMessageFunc func(*irc.Message) error
|
2021-08-19 14:40:34 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// TemplateFuncGetter is the type of function to implement in the
|
|
|
|
// plugin to create a new template function on request of the bot
|
|
|
|
TemplateFuncGetter func(*irc.Message, *Rule, *FieldCollection) any
|
|
|
|
// TemplateFuncRegister is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to register a new TemplateFuncGetter
|
2023-08-25 21:37:37 +00:00
|
|
|
TemplateFuncRegister func(name string, fg TemplateFuncGetter, doc ...TemplateFuncDocumentation)
|
2022-02-08 18:58:19 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// TemplateValidatorFunc is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to validate templates considering all
|
|
|
|
// registered template functions
|
2022-10-31 16:26:53 +00:00
|
|
|
TemplateValidatorFunc func(raw string) error
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// ValidateTokenFunc is passed from the bot to the
|
|
|
|
// plugins RegisterFunc to validate tokens and their access
|
|
|
|
// permissions
|
2022-02-08 18:58:19 +00:00
|
|
|
ValidateTokenFunc func(token string, modules ...string) error
|
2021-08-19 13:33:56 +00:00
|
|
|
)
|
2021-08-19 14:40:34 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// ErrSkipSendingMessage should be returned by a MsgModificationFunc
|
|
|
|
// to prevent the message to be sent to the Twitch servers
|
2022-10-25 16:47:30 +00:00
|
|
|
var ErrSkipSendingMessage = errors.New("skip sending message")
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// GenericTemplateFunctionGetter wraps a generic template function not
|
|
|
|
// requiring access to the irc.Message, Rule or FieldCollection to
|
|
|
|
// satisfy the TemplateFuncGetter interface
|
|
|
|
func GenericTemplateFunctionGetter(f any) TemplateFuncGetter {
|
|
|
|
return func(*irc.Message, *Rule, *FieldCollection) any { return f }
|
2021-08-19 14:40:34 +00:00
|
|
|
}
|