2021-01-10 21:15:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/pkg/twitch"
|
2021-11-25 22:48:16 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2021-01-10 21:15:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-09-22 13:36:45 +00:00
|
|
|
registerAction("script", func() plugins.Actor { return &ActorScript{} })
|
|
|
|
|
|
|
|
registerActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Execute external script / command",
|
|
|
|
Name: "Execute Script / Command",
|
|
|
|
Type: "script",
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Command to execute",
|
|
|
|
Key: "command",
|
|
|
|
Name: "Command",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeStringSlice,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "false",
|
|
|
|
Description: "Do not activate cooldown for route when command exits non-zero",
|
|
|
|
Key: "skip_cooldown_on_error",
|
|
|
|
Name: "Skip Cooldown on Error",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeBool,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-01-10 21:15:57 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
type ActorScript struct{}
|
2021-03-13 22:39:35 +00:00
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
func (a ActorScript) Execute(c *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
|
|
|
command, err := attrs.StringSlice("command")
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "getting command")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-03-13 22:39:35 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
for i := range command {
|
|
|
|
tmp, err := formatMessage(command[i], 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, "execute command argument template")
|
2021-01-10 21:15:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
command[i] = tmp
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
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-09-02 21:26:39 +00:00
|
|
|
scriptInput := map[string]interface{}{
|
2021-08-19 13:33:56 +00:00
|
|
|
"badges": twitch.ParseBadgeLevels(m),
|
2021-09-02 21:26:39 +00:00
|
|
|
"channel": plugins.DeriveChannel(m, eventData),
|
|
|
|
"username": plugins.DeriveUser(m, eventData),
|
|
|
|
}
|
|
|
|
|
|
|
|
if m != nil {
|
|
|
|
scriptInput["message"] = m.Trailing()
|
|
|
|
scriptInput["tags"] = m.Tags
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(stdin).Encode(scriptInput); 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-09-22 13:36:45 +00:00
|
|
|
return attrs.MustBool("skip_cooldown_on_error", ptrBoolFalse), 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-09-22 13:36:45 +00:00
|
|
|
apc, err := triggerAction(c, m, r, action, eventData)
|
2021-08-11 22:12:10 +00:00
|
|
|
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" }
|
2021-09-22 13:36:45 +00:00
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
func (a ActorScript) Validate(attrs *plugins.FieldCollection) (err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
if cmd, err := attrs.StringSlice("command"); err != nil || len(cmd) == 0 {
|
|
|
|
return errors.New("command must be slice of strings with length > 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|