forked from ghaoo/rboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.go
185 lines (149 loc) · 3.65 KB
/
scripts.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package rboot
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const defaultScriptDir = "scripts"
var scripts = make(map[string]script)
type script struct {
Name string `yaml:"name"`
Ruleset map[string]string `yaml:"ruleset"`
Usage map[string]string `yaml:"usage"`
Description string `yaml:"description"`
Version string `yaml:"version"`
Command []command `yaml:"command"`
}
type command struct {
Dir string `yaml:"dir"`
Cmd []string `yaml:"cmd"`
}
// 设置脚本
func setupScript(bot *Robot, in *Message) (msg []*Message) {
rule := in.Header.Get("rule")
extargs := in.Header["args"]
scp := scripts[rule]
userid := in.Header["Senderstaffid"][0]
code,err := bot.Brain.Get("session",userid)
if err != nil {
return NewMessages("Query Session DB Failed")
}
if string(code) == "" {
return NewMessages("You session expired, use !auth and !auth <authcode> re-establish session")
}
for _, sc := range scp.Command {
for _, c := range sc.Cmd {
args := strings.Split(c, " ")
//pass args from user input to scripts
out, err := runCommand(sc.Dir, args[0], extargs[1:]...)
if err != nil {
return NewMessages(err.Error())
}
msg = append(msg, NewMessage(out, in.From))
}
}
return msg
}
// 注册脚本
func registerScript() error {
scpDir := os.Getenv("SCRIPT_DIR")
if scpDir == "" {
scpDir = defaultScriptDir
}
scps, err := allScripts(scpDir)
if err != nil {
return err
}
if len(scps) <= 0 {
return fmt.Errorf("no script found")
}
for _, scp := range scps {
if len(scp.Command) <= 0 {
continue
}
// 注册插件到脚本
RegisterPlugin(scp.Name, Plugin{
Action: setupScript,
Ruleset: scp.Ruleset,
Usage: scp.Usage,
Description: scp.Description,
})
scripts[scp.Name] = scp
}
return nil
}
func allScripts(dir string) ([]script, error) {
scpFiles, err := filepath.Glob(filepath.Join(dir, "*.yml"))
if err != nil {
return nil, err
}
var scps = make([]script, 0)
for _, file := range scpFiles {
data, err := loadScript(file)
if err != nil {
continue
}
var scp = script{}
err = yaml.Unmarshal(data, &scp)
if err != nil {
continue
}
scps = append(scps, scp)
}
return scps, nil
}
func loadScript(file string) ([]byte, error) {
_, err := os.Stat(file)
if os.IsNotExist(err) {
return nil, err
}
fi, err := os.Open(file)
if err != nil {
return nil, err
}
defer fi.Close()
return ioutil.ReadAll(fi)
}
func runCommand(dir, command string, args ...string) (string, error) {
cmd := exec.Command(command, args...)
if dir != "" {
cmd.Dir = dir
}
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("run script error: %v - %q", err, string(output))
}
return string(output), nil
}
func init() {
err := registerScript()
if err != nil {
logrus.WithFields(logrus.Fields{
"mod": `rboot`,
}).Errorf("register script err: %v", err)
}
RegisterPlugin("refresh_scripts", Plugin{
Action: func(bot *Robot, incoming *Message) []*Message {
err := registerScript()
if err != nil {
logrus.WithFields(logrus.Fields{
"mod": `rboot`,
}).Error(err)
return NewMessages(err.Error(), incoming.From)
}
return NewMessages("更新成功!", incoming.From)
},
Ruleset: map[string]string{
"refresh_scripts": `^!refresh_scripts`,
},
Usage: map[string]string{
"!refresh_scripts": "重新加载插件YML配置文件",
},
Description: "当插件配置有变化时更新插件YML配置文件",
})
}