2012-10-11 15:00:43 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2012-11-01 11:59:29 +00:00
|
|
|
|
"github.com/cxindex/xmpp"
|
2012-10-11 15:00:43 +00:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"regexp"
|
|
|
|
|
"runtime"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const room = "ttyh@conference.jabber.ru"
|
2012-10-18 10:41:33 +00:00
|
|
|
|
const name = "Жобe"
|
2012-10-11 15:00:43 +00:00
|
|
|
|
const me = "hypnotoad@xmpp.ru"
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
ping time.Time
|
|
|
|
|
admin []string
|
2012-10-18 10:41:33 +00:00
|
|
|
|
cs = make(chan xmpp.Stanza)
|
|
|
|
|
next xmpp.Stanza
|
2012-10-11 15:00:43 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2012-10-12 09:40:02 +00:00
|
|
|
|
start:
|
|
|
|
|
Conn, err := xmpp.Dial("xmpp.ru:5222", "hypnotoad", "xmpp.ru", "pass", "AllHailHypnotoad", nil)
|
2012-10-11 15:00:43 +00:00
|
|
|
|
if err != nil {
|
2012-11-14 08:20:35 +00:00
|
|
|
|
log.Println("Conn", err)
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
goto start
|
2012-10-11 15:00:43 +00:00
|
|
|
|
}
|
2012-11-14 08:20:35 +00:00
|
|
|
|
if err := Conn.SignalPresence("dnd", "is there some food in this world?", 12); err != nil {
|
|
|
|
|
log.Println("Signal", err)
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
goto start
|
2012-10-11 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
if err := Conn.SendPresence(room+"/"+name, ""); err != nil {
|
2012-11-14 08:20:35 +00:00
|
|
|
|
log.Println("Presence", err)
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
goto start
|
2012-10-11 15:00:43 +00:00
|
|
|
|
}
|
2013-01-16 12:35:43 +00:00
|
|
|
|
|
2012-10-18 10:41:33 +00:00
|
|
|
|
go func(Conn *xmpp.Conn) {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-time.After(60 * time.Second):
|
|
|
|
|
Conn.SendIQ("jabber.ru", "set", "<keepalive xmlns='urn:xmpp:keepalive:0'> <interval>60</interval> </keepalive>")
|
|
|
|
|
if _, _, err = Conn.SendIQ("jabber.ru", "get", "<ping xmlns='urn:xmpp:ping'/>"); err != nil {
|
|
|
|
|
log.Println("KeepAlive err:", err)
|
|
|
|
|
return
|
2012-10-11 15:00:43 +00:00
|
|
|
|
}
|
2012-10-18 10:41:33 +00:00
|
|
|
|
ping = time.Now()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}(Conn)
|
|
|
|
|
|
|
|
|
|
go func(Conn *xmpp.Conn) {
|
|
|
|
|
for {
|
|
|
|
|
next, err := Conn.Next()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("Next err:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
cs <- next
|
|
|
|
|
}
|
|
|
|
|
}(Conn)
|
2012-10-11 15:00:43 +00:00
|
|
|
|
|
|
|
|
|
for {
|
2012-10-18 10:41:33 +00:00
|
|
|
|
select {
|
|
|
|
|
case next = <-cs:
|
|
|
|
|
case <-time.After(65 * time.Second):
|
2012-11-01 11:59:29 +00:00
|
|
|
|
log.Println(Conn.Close(), "\n\t", "closed after 65 seconds of inactivity")
|
2012-10-12 09:40:02 +00:00
|
|
|
|
goto start
|
2012-10-11 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
switch t := next.Value.(type) {
|
|
|
|
|
case *xmpp.ClientPresence:
|
|
|
|
|
PresenceHandler(Conn, t)
|
|
|
|
|
case *xmpp.ClientMessage:
|
2013-05-16 07:31:23 +00:00
|
|
|
|
if len(t.Delay.Stamp) == 0 && len(t.Subject) == 0 {
|
2013-01-16 12:35:43 +00:00
|
|
|
|
log.Println(t)
|
2013-05-16 07:31:23 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|
2012-10-11 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-11-01 11:59:29 +00:00
|
|
|
|
log.Println(Conn.Close(), "\n\t", "wtf am I doing here?")
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
goto start
|
2012-10-11 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SelfHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) {
|
|
|
|
|
Msg.Body = strings.TrimSpace(Msg.Body)
|
|
|
|
|
Conn.Send(room, "groupchat", Msg.Body)
|
2013-01-29 19:54:05 +00:00
|
|
|
|
if Msg.From == me+"/gsend" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2012-10-11 15:00:43 +00:00
|
|
|
|
Strip(&Msg.Body, &Msg.From)
|
|
|
|
|
if err := exec.Command("bash", "-c", GetCommand("!"+Msg.Body, Msg.From, "./func/")).Run(); err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2013-01-14 14:26:18 +00:00
|
|
|
|
case f("^\\!megakick", &Msg.Body):
|
2013-01-16 12:35:43 +00:00
|
|
|
|
Strip(&Msg.Body, &Msg.From)
|
2013-01-14 14:26:18 +00:00
|
|
|
|
s := (strings.Split(Msg.Body, "!megakick "))
|
|
|
|
|
if in(admin, Msg.From) {
|
2013-01-16 12:35:43 +00:00
|
|
|
|
Conn.ModUse(room, s[1], "none", "")
|
|
|
|
|
} else {
|
|
|
|
|
Conn.Send(room, "groupchat", fmt.Sprintf("%s: GTFO", GetNick(Msg.From)))
|
2013-01-14 14:26:18 +00:00
|
|
|
|
}
|
2012-10-11 15:00:43 +00:00
|
|
|
|
case f("^\\!", &Msg.Body): //any external command
|
|
|
|
|
Strip(&Msg.Body, &Msg.From)
|
|
|
|
|
cmd := exec.Command("bash", "-c", GetCommand(Msg.Body, Msg.From, "./plugins/"))
|
|
|
|
|
stdout, _ := cmd.StdoutPipe()
|
|
|
|
|
stderr, _ := cmd.StderrPipe()
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
out, _ := ioutil.ReadAll(stdout)
|
|
|
|
|
outerr, _ := ioutil.ReadAll(stderr)
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
|
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) {
|
|
|
|
|
Conn.Send(Msg.From, "chat", string(outerr))
|
|
|
|
|
}
|
|
|
|
|
Conn.Send(room, "groupchat", strings.TrimRight(string(out), " \n"))
|
|
|
|
|
case f("^"+name, &Msg.Body): //chat
|
|
|
|
|
Strip(&Msg.Body, &Msg.From)
|
2012-11-12 14:52:58 +00:00
|
|
|
|
r, _ := regexp.Compile("^\\./chat/" + name + "[:,]")
|
2012-10-11 15:00:43 +00:00
|
|
|
|
command := r.ReplaceAllString(GetCommand("!"+Msg.Body, Msg.From, "./chat/"), "./chat/answer")
|
|
|
|
|
out, err := exec.Command("bash", "-c", command).CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
Conn.Send(room, "groupchat", strings.TrimRight(string(out), " \n"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PresenceHandler(Conn *xmpp.Conn, Prs *xmpp.ClientPresence) {
|
|
|
|
|
switch Prs.Item.Affiliation {
|
|
|
|
|
case "owner":
|
|
|
|
|
fallthrough
|
|
|
|
|
case "admin":
|
|
|
|
|
if Prs.Item.Role != "none" {
|
|
|
|
|
if !in(admin, Prs.From) {
|
|
|
|
|
admin = append(admin, Prs.From)
|
|
|
|
|
}
|
2013-06-06 10:40:25 +00:00
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
if in(admin, Prs.From) {
|
2012-10-11 15:00:43 +00:00
|
|
|
|
admin = del(admin, Prs.From)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetCommand(body, from, dir string) string {
|
|
|
|
|
split := strings.SplitAfterN(body, " ", 2)
|
|
|
|
|
r, _ := regexp.Compile("^\\!")
|
|
|
|
|
command := r.ReplaceAllString(split[0], dir) + " '" + GetNick(from) + "' '" + strconv.FormatBool(in(admin, from)) + "'"
|
|
|
|
|
if len(split) == 2 {
|
|
|
|
|
command += " '" + split[1] + "'"
|
|
|
|
|
}
|
|
|
|
|
return command
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Strip(s, s2 *string) {
|
2013-12-24 09:22:15 +00:00
|
|
|
|
strip, _ := regexp.Compile("(`|\\$|\\.\\.)")
|
|
|
|
|
q, _ := regexp.Compile("(\"|')")
|
|
|
|
|
*s = q.ReplaceAllString(strip.ReplaceAllString(*s, ""), "“")
|
|
|
|
|
*s2 = q.ReplaceAllString(strip.ReplaceAllString(*s2, ""), "“")
|
2012-10-11 15:00:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetNick(s string) string {
|
|
|
|
|
slash := strings.Index(s, "/")
|
|
|
|
|
if slash != -1 {
|
|
|
|
|
return s[slash+1:]
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func in(slice []string, value string) bool {
|
|
|
|
|
for _, v := range slice {
|
|
|
|
|
if v == value {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func pos(slice []string, value string) int {
|
|
|
|
|
for p, v := range slice {
|
|
|
|
|
if v == value {
|
|
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func del(slice []string, value string) []string {
|
|
|
|
|
if i := pos(slice, value); i >= 0 {
|
|
|
|
|
return append(slice[:i], slice[i+1:]...)
|
|
|
|
|
}
|
|
|
|
|
return slice
|
|
|
|
|
}
|