Move from "bash -c" to direct execute, little refatoring
This commit is contained in:
parent
79a839b34a
commit
908a87953d
159
main.go
159
main.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/derlaft/xmpp"
|
"github.com/derlaft/xmpp"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -25,9 +26,9 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ping time.Time
|
|
||||||
admin []string
|
admin []string
|
||||||
cs = make(chan xmpp.Stanza)
|
cs = make(chan xmpp.Stanza)
|
||||||
|
stop chan struct{}
|
||||||
next xmpp.Stanza
|
next xmpp.Stanza
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,9 +41,12 @@ func main() {
|
||||||
|
|
||||||
START:
|
START:
|
||||||
for {
|
for {
|
||||||
if Conn != nil {
|
stop = make(chan struct{})
|
||||||
time.Sleep(5 * time.Second)
|
if(err != nil) {
|
||||||
log.Println("Conn check:", Conn.Close())
|
admin = admin[:0]
|
||||||
|
if Conn != nil {
|
||||||
|
log.Println("Conn check:", Conn.Close())
|
||||||
|
}
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,46 +59,68 @@ START:
|
||||||
log.Println("Signal", err)
|
log.Println("Signal", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := Conn.SendPresence(room+"/"+name, ""); err != nil {
|
if err := Conn.SendPresence(room + "/" + name, ""); err != nil {
|
||||||
log.Println("Presence", err)
|
log.Println("Presence", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
go func(Conn *xmpp.Conn) {
|
go func(Conn *xmpp.Conn, stop chan struct{}) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-time.After(60 * time.Second):
|
case <-time.After(60 * time.Second):
|
||||||
Conn.SendIQ(server, "set", "<keepalive xmlns='urn:xmpp:keepalive:0'> <interval>60</interval> </keepalive>")
|
Conn.SendIQ(server, "set", "<keepalive xmlns='urn:xmpp:keepalive:0'> <interval>60</interval> </keepalive>")
|
||||||
if _, _, err = Conn.SendIQ(server, "get", "<ping xmlns='urn:xmpp:ping'/>"); err != nil {
|
if _, _, err = Conn.SendIQ(server, "get", "<ping xmlns='urn:xmpp:ping'/>"); err != nil {
|
||||||
log.Println("KeepAlive err:", err)
|
select {
|
||||||
|
case <-stop:
|
||||||
|
default:
|
||||||
|
log.Println("KeepAlive err:", err)
|
||||||
|
close(stop)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ping = time.Now()
|
|
||||||
|
case <-stop:
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(Conn)
|
}(Conn, stop)
|
||||||
|
|
||||||
go func(Conn *xmpp.Conn) {
|
go func(Conn *xmpp.Conn, stop chan struct{}) {
|
||||||
for {
|
for {
|
||||||
next, err := Conn.Next()
|
next, err := Conn.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Next err:", err)
|
select {
|
||||||
|
case <-stop:
|
||||||
|
default:
|
||||||
|
log.Println("KeepAlive err:", err)
|
||||||
|
close(stop)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cs <- next
|
cs <- next
|
||||||
}
|
}
|
||||||
}(Conn)
|
}(Conn, stop)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case next = <-cs:
|
case next = <-cs:
|
||||||
|
|
||||||
|
case <-stop:
|
||||||
|
Conn.Close()
|
||||||
|
Conn = nil
|
||||||
|
continue START
|
||||||
|
|
||||||
case <-time.After(65 * time.Second):
|
case <-time.After(65 * time.Second):
|
||||||
log.Println(Conn.Close(), "\n\t", "closed after 65 seconds of inactivity")
|
log.Println(Conn.Close(), "\n\t", "closed after 65 seconds of inactivity")
|
||||||
|
close(stop)
|
||||||
|
Conn = nil
|
||||||
continue START
|
continue START
|
||||||
}
|
}
|
||||||
|
|
||||||
switch t := next.Value.(type) {
|
switch t := next.Value.(type) {
|
||||||
case *xmpp.ClientPresence:
|
case *xmpp.ClientPresence:
|
||||||
PresenceHandler(Conn, t)
|
PresenceHandler(Conn, t)
|
||||||
|
|
||||||
case *xmpp.ClientMessage:
|
case *xmpp.ClientMessage:
|
||||||
if len(t.Delay.Stamp) == 0 && len(t.Subject) == 0 {
|
if len(t.Delay.Stamp) == 0 && len(t.Subject) == 0 {
|
||||||
log.Println(t)
|
log.Println(t)
|
||||||
|
@ -114,67 +140,86 @@ START:
|
||||||
|
|
||||||
func SelfHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) {
|
func SelfHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) {
|
||||||
Msg.Body = strings.TrimSpace(Msg.Body)
|
Msg.Body = strings.TrimSpace(Msg.Body)
|
||||||
Conn.Send(room, "groupchat", Msg.Body)
|
if(!strings.HasPrefix(Msg.Body, "!")) {
|
||||||
if Msg.From == me+"/gsend" {
|
Conn.Send(room, "groupchat", Msg.Body)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Strip(&Msg.Body, &Msg.From)
|
command, err := GetCommand(Msg.Body, Msg.From, "./plugins/")
|
||||||
if err := exec.Command("bash", "-c", GetCommand("!"+Msg.Body, Msg.From, "./func/")).Run(); err != nil {
|
if(err != nil) {
|
||||||
|
Conn.Send(Msg.From, "chat", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
out, err := command.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
Conn.Send(Msg.From, "chat", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Conn.Send(Msg.From, "chat", string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var call = regexp.MustCompile("^" + name + "[:,]")
|
||||||
func MessageHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) {
|
func MessageHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) {
|
||||||
Msg.Body = strings.TrimSpace(Msg.Body)
|
|
||||||
f := func(s string, s2 *string) bool {
|
|
||||||
ok, _ := regexp.MatchString(s, *s2)
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
switch {
|
switch {
|
||||||
case f("^\\!megakick ", &Msg.Body):
|
case strings.HasPrefix(Msg.Body, "!megakick "):
|
||||||
Strip(&Msg.Body, &Msg.From)
|
s := strings.Split(Msg.Body, "!megakick ")
|
||||||
s := (strings.Split(Msg.Body, "!megakick "))
|
|
||||||
if in(admin, Msg.From) {
|
if in(admin, Msg.From) {
|
||||||
Conn.ModUse(room, s[1], "none", "")
|
Conn.ModUse(room, s[1], "none", "")
|
||||||
} else {
|
} else {
|
||||||
Conn.Send(room, "groupchat", fmt.Sprintf("%s: GTFO", GetNick(Msg.From)))
|
Conn.Send(room, "groupchat", fmt.Sprintf("%s: GTFO", GetNick(Msg.From)))
|
||||||
}
|
}
|
||||||
case f("^\\!", &Msg.Body): //any external command
|
|
||||||
Strip(&Msg.Body, &Msg.From)
|
case strings.HasPrefix(Msg.Body, "!"): //any external command
|
||||||
cmd := exec.Command("bash", "-c", GetCommand(Msg.Body, Msg.From, "./plugins/"))
|
cmd, err := GetCommand(Msg.Body, Msg.From, "./plugins/")
|
||||||
|
if(err != nil) {
|
||||||
|
Conn.Send(room, "groupchat", fmt.Sprintf("%s: WAT", GetNick(Msg.From)))
|
||||||
|
if(in(admin, Msg.From)) {
|
||||||
|
Conn.Send(Msg.From, "chat", err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
stdout, _ := cmd.StdoutPipe()
|
stdout, _ := cmd.StdoutPipe()
|
||||||
stderr, _ := cmd.StderrPipe()
|
stderr, _ := cmd.StderrPipe()
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
Conn.Send(room, "groupchat", fmt.Sprintf("%s: WAT", GetNick(Msg.From)))
|
||||||
|
if(in(admin, Msg.From)) {
|
||||||
|
Conn.Send(Msg.From, "chat", err.Error())
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
out, _ := ioutil.ReadAll(stdout)
|
out, _ := ioutil.ReadAll(stdout)
|
||||||
outerr, _ := ioutil.ReadAll(stderr)
|
outerr, _ := ioutil.ReadAll(stderr)
|
||||||
if err := cmd.Wait(); err != nil {
|
cmd.Wait()
|
||||||
if err.Error() == "exit status 127" {
|
|
||||||
Conn.Send(room, "groupchat", fmt.Sprintf("%s: WAT", GetNick(Msg.From)))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(outerr) != 0 && in(admin, Msg.From) {
|
if len(outerr) != 0 && in(admin, Msg.From) {
|
||||||
Conn.Send(Msg.From, "chat", string(outerr))
|
Conn.Send(Msg.From, "chat", string(outerr))
|
||||||
}
|
}
|
||||||
Conn.Send(room, "groupchat", strings.TrimRight(string(out), " \n"))
|
Conn.Send(room, "groupchat", string(out))
|
||||||
case f("^"+name, &Msg.Body): //chat
|
|
||||||
Strip(&Msg.Body, &Msg.From)
|
case call.MatchString(Msg.Body): //chat
|
||||||
r, _ := regexp.Compile("^\\./chat/" + name + "[:,]")
|
command, err := GetCommand(call.ReplaceAllString(Msg.Body, "!answer"), Msg.From, "./chat/")
|
||||||
command := r.ReplaceAllString(GetCommand("!"+Msg.Body, Msg.From, "./chat/"), "./chat/answer")
|
|
||||||
out, err := exec.Command("bash", "-c", command).CombinedOutput()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Conn.Send(room, "groupchat", strings.TrimRight(string(out), " \n"))
|
out, err := command.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
if(in(admin, Msg.From)) {
|
||||||
|
Conn.Send(Msg.From, "chat", err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Conn.Send(room, "groupchat", string(out))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PresenceHandler(Conn *xmpp.Conn, Prs *xmpp.ClientPresence) {
|
func PresenceHandler(Conn *xmpp.Conn, Prs *xmpp.ClientPresence) {
|
||||||
|
if(Prs.From == room + "/" + name && Prs.Item.Role == "none") {
|
||||||
|
log.Println("was kicked")
|
||||||
|
close(stop)
|
||||||
|
return
|
||||||
|
}
|
||||||
switch Prs.Item.Affiliation {
|
switch Prs.Item.Affiliation {
|
||||||
case "owner":
|
case "owner":
|
||||||
fallthrough
|
fallthrough
|
||||||
|
@ -191,21 +236,31 @@ func PresenceHandler(Conn *xmpp.Conn, Prs *xmpp.ClientPresence) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCommand(body, from, dir string) string {
|
//letter(ASCII or cyrillic), number, underscore only
|
||||||
|
var cmd_validator = regexp.MustCompile("^!(\\w|\\p{Cyrillic})*$")
|
||||||
|
func GetCommand(body, from, dir string) (*exec.Cmd, error) {
|
||||||
split := strings.SplitAfterN(body, " ", 2)
|
split := strings.SplitAfterN(body, " ", 2)
|
||||||
r, _ := regexp.Compile("^\\!")
|
cmd := strings.TrimSpace(split[0])
|
||||||
command := r.ReplaceAllString(split[0], dir) + " '" + GetNick(from) + "' '" + strconv.FormatBool(in(admin, from)) + "'"
|
|
||||||
if len(split) == 2 {
|
if(!cmd_validator.MatchString(cmd)) { return nil, fmt.Errorf("Bad command \"%s\"", cmd) }
|
||||||
command += " '" + split[1] + "'"
|
|
||||||
}
|
var (
|
||||||
return command
|
info os.FileInfo
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
path := dir + Strip(cmd[1:])
|
||||||
|
if info, err = os.Stat(path); err != nil { return nil, err }
|
||||||
|
if info.IsDir() || info.Mode() & 0111 == 0 { return nil, fmt.Errorf("\"%s\" isn't executable", path) }
|
||||||
|
|
||||||
|
args := []string{ Strip(GetNick(from)), strconv.FormatBool(in(admin, from)) }
|
||||||
|
if(len(split) > 1) { args = append(args, Strip(split[1])) }
|
||||||
|
return exec.Command(path, args...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Strip(s, s2 *string) {
|
var strip_regexp = regexp.MustCompile("(`|\\$|\\.\\.)")
|
||||||
strip, _ := regexp.Compile("(`|\\$|\\.\\.)")
|
var quote_regexp = regexp.MustCompile("(\"|')")
|
||||||
q, _ := regexp.Compile("(\"|')")
|
func Strip(s string) string {
|
||||||
*s = q.ReplaceAllString(strip.ReplaceAllString(*s, ""), "“")
|
return quote_regexp.ReplaceAllString(strip_regexp.ReplaceAllString(s, ""), "“")
|
||||||
*s2 = q.ReplaceAllString(strip.ReplaceAllString(*s2, ""), "“")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNick(s string) string {
|
func GetNick(s string) string {
|
||||||
|
|
Loading…
Reference in New Issue