2024-01-01 16:52:18 +00:00
|
|
|
// Package raw contains an actor to send raw IRC messages
|
2021-08-19 13:33:56 +00:00
|
|
|
package raw
|
2021-05-26 13:36:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-05-26 13:36:03 +00:00
|
|
|
)
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
const actorName = "raw"
|
|
|
|
|
2022-10-25 16:47:30 +00:00
|
|
|
var (
|
|
|
|
formatMessage plugins.MsgFormatter
|
|
|
|
send plugins.SendMessageFunc
|
2022-10-31 16:26:53 +00:00
|
|
|
|
|
|
|
ptrStringEmpty = func(s string) *string { return &s }("")
|
2022-10-25 16:47:30 +00:00
|
|
|
)
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
2021-08-19 13:33:56 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
formatMessage = args.FormatMessage
|
2022-10-25 16:47:30 +00:00
|
|
|
send = args.SendMessage
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Send raw IRC message",
|
|
|
|
Name: "Send RAW Message",
|
|
|
|
Type: "raw",
|
2021-06-11 11:52:42 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Raw message to send (must be a valid IRC protocol message)",
|
|
|
|
Key: "message",
|
|
|
|
Name: "Message",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
type actor struct{}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
rawMsg, err := formatMessage(attrs.MustString("message", nil), m, r, eventData)
|
2021-06-11 11:52:42 +00:00
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing raw message")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := irc.ParseMessage(rawMsg)
|
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "parsing raw message")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2022-10-25 16:47:30 +00:00
|
|
|
send(msg),
|
2021-06-11 11:52:42 +00:00
|
|
|
"sending raw message",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) IsAsync() bool { return false }
|
|
|
|
func (actor) Name() string { return actorName }
|
2021-09-22 13:36:45 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *plugins.FieldCollection) (err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
if v, err := attrs.String("message"); err != nil || v == "" {
|
|
|
|
return errors.New("message must be non-empty string")
|
|
|
|
}
|
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
if err = tplValidator(attrs.MustString("message", ptrStringEmpty)); err != nil {
|
|
|
|
return errors.Wrap(err, "validating message template")
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
return nil
|
|
|
|
}
|