-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugins.go
148 lines (129 loc) · 2.68 KB
/
plugins.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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"sync"
"syscall"
)
type Plugin struct {
cmd *exec.Cmd
stdin io.Writer
stdout io.Reader
stderr io.Reader
bot *Bot
}
func (p *Plugin) String() string {
return fmt.Sprintf("%s", p.cmd.Path)
}
var wg sync.WaitGroup
func (p *Plugin) start() {
defer wg.Done()
err := p.cmd.Run()
if err != nil {
p.bot.toStderr <- fmt.Sprintf("%s: %s\n", p, err)
p.unload()
}
}
func (p *Plugin) kill() error {
if p.cmd == nil {
log.Fatalf("%v: plugin.cmd is nil", p)
}
if p.cmd.Process == nil {
log.Fatalf("%v: plugin.cmd.Process is nil", p)
}
if err := p.cmd.Process.Signal(syscall.Signal(0)); err == nil {
if err := p.cmd.Process.Signal(syscall.Signal(15)); err != nil {
if err := p.cmd.Process.Kill(); err != nil {
log.Fatal("failed to kill: ", err)
}
log.Println("Killed (9): ", p)
} else {
log.Println("Killed (15): ", p)
}
p.cmd.Wait()
return nil
} else {
log.Printf("command '%v' process.Signal on pid %d returned: %v\n", p.cmd, p.cmd.Process.Pid, err)
return err
}
}
func (p *Plugin) unload() {
for i, plugin := range p.bot.plugins {
if p == plugin {
p.bot.plugins[i] = nil
}
}
}
func (p *Plugin) publish(in io.Reader, out chan string) {
reader := bufio.NewReader(in)
defer wg.Done()
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
return
} else if err != nil {
logErr.Printf("[%s] %s", p.cmd.Path, err)
return
}
out <- line
}
}
func (bot *Bot) newPlugin(path string) *Plugin {
cmd := exec.Command(path)
var p *Plugin = &Plugin{}
var err error
p.stdin, err = cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
p.stdout, err = cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
p.stderr, err = cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
wg.Add(2)
go p.publish(p.stdout, bot.toConn)
go p.publish(p.stderr, bot.toStderr)
p.cmd = cmd
p.bot = bot
wg.Add(1)
go p.start()
return p
}
func (bot *Bot) LoadAllPlugins() {
bot.pluginsMutex.Lock()
bot.plugins = []*Plugin{}
pluginDir, _ := os.Open("plugins")
files, _ := pluginDir.Readdir(-1)
for _, f := range files {
if f.Mode().IsRegular() && f.Mode().Perm()&0111 != 0 {
bot.plugins = append(bot.plugins, bot.newPlugin(path.Join("plugins", f.Name())))
}
}
bot.pluginsMutex.Unlock()
log.Printf("All plugins loaded, wg: %v\n", wg)
}
func (bot *Bot) UnloadAllPlugins() {
bot.pluginsMutex.Lock()
for _, plugin := range bot.plugins {
if plugin == nil {
continue
}
plugin.kill()
plugin.unload()
}
bot.pluginsMutex.Unlock()
log.Printf("All plugins unloaded, wg: %v\n", wg)
}
func (bot *Bot) reloadAllPlugins() {
bot.UnloadAllPlugins()
bot.LoadAllPlugins()
}