2024-01-01 16:52:18 +00:00
|
|
|
// Package filesay contains an actor to paste a remote URL as chat
|
|
|
|
// commands i.e. for bulk banning users
|
2022-03-04 22:34:59 +00:00
|
|
|
package filesay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2024-01-01 16:52:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2022-03-04 22:34:59 +00:00
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2022-03-04 22:34:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
actorName = "filesay"
|
|
|
|
|
|
|
|
httpTimeout = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
2022-10-25 16:47:30 +00:00
|
|
|
var (
|
|
|
|
formatMessage plugins.MsgFormatter
|
|
|
|
send plugins.SendMessageFunc
|
|
|
|
)
|
2022-03-04 22:34:59 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
2022-03-04 22:34:59 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
formatMessage = args.FormatMessage
|
2022-10-25 16:47:30 +00:00
|
|
|
send = args.SendMessage
|
2022-03-04 22:34:59 +00:00
|
|
|
|
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Takes the content of an URL and pastes it to the current channel",
|
|
|
|
Name: "FileSay",
|
|
|
|
Type: actorName,
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Source of the content to post",
|
|
|
|
Key: "source",
|
|
|
|
Name: "source",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type actor struct{}
|
|
|
|
|
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) {
|
2022-03-04 22:34:59 +00:00
|
|
|
ptrStringEmpty := func(v string) *string { return &v }("")
|
|
|
|
|
|
|
|
source, err := formatMessage(attrs.MustString("source", ptrStringEmpty), m, r, eventData)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing source template")
|
|
|
|
}
|
|
|
|
|
|
|
|
if source == "" {
|
|
|
|
return false, errors.New("source template evaluated to empty string")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := url.Parse(source); err != nil {
|
|
|
|
return false, errors.Wrap(err, "parsing URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), httpTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, source, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "creating HTTP request")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing HTTP request")
|
|
|
|
}
|
2024-01-01 16:52:18 +00:00
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing response body (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2022-03-04 22:34:59 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return false, errors.Errorf("http status %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
|
|
for scanner.Scan() {
|
2022-10-25 16:47:30 +00:00
|
|
|
if err = send(&irc.Message{
|
2022-03-04 22:34:59 +00:00
|
|
|
Command: "PRIVMSG",
|
|
|
|
Params: []string{
|
|
|
|
plugins.DeriveChannel(m, eventData),
|
|
|
|
scanner.Text(),
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return false, errors.Wrap(err, "sending message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) IsAsync() bool { return true }
|
|
|
|
func (actor) Name() string { return actorName }
|
2022-03-04 22:34:59 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *plugins.FieldCollection) error {
|
2022-10-31 16:26:53 +00:00
|
|
|
sourceTpl, err := attrs.String("source")
|
|
|
|
if err != nil || sourceTpl == "" {
|
2022-03-04 22:34:59 +00:00
|
|
|
return errors.New("source is expected to be non-empty string")
|
|
|
|
}
|
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
if err = tplValidator(sourceTpl); err != nil {
|
|
|
|
return errors.Wrap(err, "validating source template")
|
|
|
|
}
|
|
|
|
|
2022-03-04 22:34:59 +00:00
|
|
|
return nil
|
|
|
|
}
|