-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_script.go
151 lines (124 loc) · 4.27 KB
/
action_script.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"github.com/pkg/errors"
"gopkg.in/irc.v4"
"github.com/Luzifer/go_helpers/v2/fieldcollection"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
func init() {
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,
},
},
})
}
// ActorScript contains an actor to execute arbitrary commands and scripts
type ActorScript struct{}
// Execute implements actor interface
func (ActorScript) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
command, err := attrs.StringSlice("command")
if err != nil {
return false, errors.Wrap(err, "getting command")
}
for i := range command {
tmp, err := formatMessage(command[i], m, r, eventData)
if err != nil {
return false, errors.Wrap(err, "execute command argument template")
}
command[i] = tmp
}
ctx, cancel := context.WithTimeout(context.Background(), cfg.CommandTimeout)
defer cancel()
var (
stdin = new(bytes.Buffer)
stdout = new(bytes.Buffer)
)
scriptInput := map[string]interface{}{
"badges": twitch.ParseBadgeLevels(m),
"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 {
return false, errors.Wrap(err, "encoding script input")
}
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
if err := cmd.Run(); err != nil {
return attrs.MustBool("skip_cooldown_on_error", ptrBoolFalse), errors.Wrapf(err, "running command in rule %s", r.UUID)
}
if stdout.Len() == 0 {
// Script was successful but did not yield actions
return false, nil
}
var (
actions []*plugins.RuleAction
decoder = json.NewDecoder(stdout)
)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&actions); err != nil {
return false, errors.Wrap(err, "decoding actions output")
}
for _, action := range actions {
apc, err := triggerAction(c, m, r, action, eventData)
if err != nil {
return preventCooldown, errors.Wrap(err, "execute returned action")
}
preventCooldown = preventCooldown || apc
}
return preventCooldown, nil
}
// IsAsync implements actor interface
func (ActorScript) IsAsync() bool { return false }
// Name implements actor interface
func (ActorScript) Name() string { return "script" }
// Validate implements actor interface
func (ActorScript) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
if err = attrs.ValidateSchema(
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "command", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeStringSlice}),
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "skip_cooldown_on_error", Type: fieldcollection.SchemaFieldTypeBool}),
fieldcollection.MustHaveNoUnknowFields,
); err != nil {
return fmt.Errorf("validating attributes: %w", err)
}
for i, el := range attrs.MustStringSlice("command", nil) {
if err = tplValidator(el); err != nil {
return errors.Wrapf(err, "validating cmd template (element %d)", i)
}
}
return nil
}