-
Notifications
You must be signed in to change notification settings - Fork 22
/
shell.go
68 lines (59 loc) · 1.56 KB
/
shell.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
package modules
import (
"bytes"
"os/exec"
"strings"
"github.com/anonyindian/logger"
"github.com/celestix/gotgproto/dispatcher"
"github.com/celestix/gotgproto/dispatcher/handlers"
"github.com/celestix/gotgproto/ext"
"github.com/gigauserbot/giga/bot/helpmaker"
"github.com/gotd/td/tg"
)
const ShellToUse = "bash"
func Shellout(command string) (error, string, string) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command(ShellToUse, "-c", command)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return err, stdout.String(), stderr.String()
}
func (m *module) LoadShell(dispatcher dispatcher.Dispatcher) {
var l = m.Logger.Create("SHELL")
defer l.ChangeLevel(logger.LevelInfo).Println("LOADED")
helpmaker.SetModuleHelp("shell", "help")
dispatcher.AddHandler(handlers.NewCommand("sh", sh))
}
func sh(ctx *ext.Context, u *ext.Update) error {
chat := u.EffectiveChat()
// msg := u.EffectiveMessage
cmd := strings.Fields(u.EffectiveMessage.Text)
if len(cmd) == 1 {
_, err := ctx.EditMessage(chat.GetID(), &tg.MessagesEditMessageRequest{
ID: u.EffectiveMessage.ID,
Message: "No command passed",
})
return err
}
err, out, errout := Shellout(strings.Join(cmd[1:], " "))
if err != nil {
logger.Println("error: %v\n", err)
}
m := ""
if out != "" {
m = "Standard output: \n" + out
}
if errout != "" {
if m != "" {
m += "\n"
}
m += "Standard Output Error: \n" + errout
}
_, err = ctx.EditMessage(chat.GetID(), &tg.MessagesEditMessageRequest{
Message: m,
ID: u.EffectiveMessage.ID,
})
return err
}