func SelfHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) { Msg.Body = strings.TrimSpace(Msg.Body) if !strings.HasPrefix(Msg.Body, "!") { Conn.Send(room, "groupchat", Msg.Body) return } command, err := GetCommand(Msg.Body, Msg.From, "./plugins/") if err != nil { Conn.Send(Msg.From, "chat", err.Error()) return } out, err := command.CombinedOutput() if err != nil { log.Println(err) Conn.Send(Msg.From, "chat", err.Error()) return } Conn.Send(Msg.From, "chat", strings.TrimRight(string(out), " \t\n")) }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) var ( Conn *xmpp.Conn err error ) START: for { stop = make(chan struct{}) if err != nil { admin = admin[:0] if Conn != nil { log.Println("Conn check:", Conn.Close()) } time.Sleep(5 * time.Second) } Conn, err = xmpp.Dial(connect, id, server, password, resource, nil) if err != nil { log.Println("Conn", err) continue } if err := Conn.SignalPresence("dnd", "is there some food in this world?", 12); err != nil { log.Println("Signal", err) continue } if err := Conn.SendPresence(room+"/"+name, ""); err != nil { log.Println("Presence", err) continue } go func(Conn *xmpp.Conn, stop chan struct{}) { for { select { case <-time.After(60 * time.Second): 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 { select { case <-stop: default: log.Println("KeepAlive err:", err) close(stop) } return } case <-stop: return } } }(Conn, stop) go func(Conn *xmpp.Conn, stop chan struct{}) { for { next, err := Conn.Next() if err != nil { select { case <-stop: default: log.Println("KeepAlive err:", err) close(stop) } return } cs <- next } }(Conn, stop) for { select { case next = <-cs: case <-stop: Conn.Close() Conn = nil continue START case <-time.After(65 * time.Second): log.Println(Conn.Close(), "\n\t", "closed after 65 seconds of inactivity") close(stop) Conn = nil continue START } switch t := next.Value.(type) { case *xmpp.ClientPresence: PresenceHandler(Conn, t) case *xmpp.ClientMessage: if len(t.Delay.Stamp) == 0 && len(t.Subject) == 0 { log.Println(t) if GetNick(t.From) != name { if t.Type == "groupchat" { go MessageHandler(Conn, t) } else if xmpp.RemoveResourceFromJid(strings.ToLower(t.From)) == me { go SelfHandler(Conn, t) } } } } } log.Println(Conn.Close(), "\n\t", "wtf am I doing here?") } }
func MessageHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) { switch { case strings.HasPrefix(Msg.Body, "!megakick "): s := strings.Split(Msg.Body, "!megakick ") if in(admin, Msg.From) { Conn.ModUse(room, s[1], "none", "") } else { Conn.Send(room, "groupchat", fmt.Sprintf("%s: GTFO", GetNick(Msg.From))) } case strings.HasPrefix(Msg.Body, "!"): //any external command 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() stderr, _ := cmd.StderrPipe() if err := cmd.Start(); err != nil { 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 } out, _ := ioutil.ReadAll(stdout) outerr, _ := ioutil.ReadAll(stderr) cmd.Wait() if len(outerr) != 0 && in(admin, Msg.From) { Conn.Send(Msg.From, "chat", strings.TrimRight(string(outerr), " \t\n")) } Conn.Send(room, "groupchat", strings.TrimRight(string(out), " \t\n")) case call.MatchString(Msg.Body): //chat command, err := GetCommand(call.ReplaceAllString(Msg.Body, "!answer"), Msg.From, "./chat/") if err != nil { log.Println(err) return } 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", strings.TrimRight(string(out), " \t\n")) } }