2021-01-10 21:15:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
|
|
|
"github.com/Luzifer/twitch-bot/twitch"
|
2021-01-10 21:15:57 +00:00
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-08-19 13:33:56 +00:00
|
|
|
registerAction(func() plugins.Actor { return &ActorScript{} })
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-01-10 21:15:57 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
type ActorScript struct {
|
|
|
|
Command []string `json:"command" yaml:"command"`
|
|
|
|
}
|
2021-03-13 22:39:35 +00:00
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (a ActorScript) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule) (preventCooldown bool, err error) {
|
2021-06-11 11:52:42 +00:00
|
|
|
if len(a.Command) == 0 {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-03-13 22:39:35 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
var command []string
|
|
|
|
for _, arg := range a.Command {
|
|
|
|
tmp, err := formatMessage(arg, m, r, nil)
|
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "execute command argument template")
|
2021-01-10 21:15:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
command = append(command, tmp)
|
|
|
|
}
|
2021-01-10 21:15:57 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cfg.CommandTimeout)
|
|
|
|
defer cancel()
|
2021-01-10 21:15:57 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
var (
|
|
|
|
stdin = new(bytes.Buffer)
|
|
|
|
stdout = new(bytes.Buffer)
|
|
|
|
)
|
2021-01-10 21:15:57 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
if err := json.NewEncoder(stdin).Encode(map[string]interface{}{
|
2021-08-19 13:33:56 +00:00
|
|
|
"badges": twitch.ParseBadgeLevels(m),
|
2021-06-11 11:52:42 +00:00
|
|
|
"channel": m.Params[0],
|
|
|
|
"message": m.Trailing(),
|
|
|
|
"tags": m.Tags,
|
|
|
|
"username": m.User,
|
|
|
|
}); err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "encoding script input")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-01-10 21:15:57 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
cmd := exec.CommandContext(ctx, command[0], command[1:]...) // #nosec G204 // This is expected to call a command with parameters
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdin = stdin
|
|
|
|
cmd.Stdout = stdout
|
2021-01-10 21:15:57 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "running command")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-01-10 21:15:57 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
if stdout.Len() == 0 {
|
|
|
|
// Script was successful but did not yield actions
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2021-08-19 13:33:56 +00:00
|
|
|
actions []*plugins.RuleAction
|
2021-06-11 11:52:42 +00:00
|
|
|
decoder = json.NewDecoder(stdout)
|
|
|
|
)
|
|
|
|
|
|
|
|
decoder.DisallowUnknownFields()
|
|
|
|
if err := decoder.Decode(&actions); err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "decoding actions output")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, action := range actions {
|
2021-08-11 22:12:10 +00:00
|
|
|
apc, err := triggerActions(c, m, r, action)
|
|
|
|
if err != nil {
|
|
|
|
return preventCooldown, errors.Wrap(err, "execute returned action")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-08-11 22:12:10 +00:00
|
|
|
preventCooldown = preventCooldown || apc
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return preventCooldown, nil
|
2021-01-10 21:15:57 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
|
|
|
func (a ActorScript) IsAsync() bool { return false }
|
|
|
|
func (a ActorScript) Name() string { return "script" }
|