This commit is contained in:
Alexey Derlaft 2015-08-13 17:45:40 +03:00
parent 6aedddfdf4
commit 7c9c5ff2eb
1 changed files with 69 additions and 62 deletions

131
main.go
View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/cxindex/xmpp" "github.com/derlaft/xmpp"
"io/ioutil" "io/ioutil"
"log" "log"
"os/exec" "os/exec"
@ -13,9 +13,16 @@ import (
"time" "time"
) )
const room = "ttyh@conference.jabber.ru" const (
const name = обe" room = "room@conference.example.com"
const me = "hypnotoad@xmpp.ru" name = "botname"
server = "example.com"
me = name + "@" + server
id = name
password = "password"
resource = "resource"
connect = "xmpp.example.com:5222"
)
var ( var (
ping time.Time ping time.Time
@ -30,77 +37,77 @@ func main() {
Conn *xmpp.Conn Conn *xmpp.Conn
err error err error
) )
start: for {
if Conn != nil { if Conn != nil {
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
log.Println("Conn check:", Conn.Close()) log.Println("Conn check:", Conn.Close())
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
} }
Conn, err = xmpp.Dial("xmpp.ru:5222", "hypnotoad", "xmpp.ru", "pass", "AllHailHypnotoad", nil) Conn, err = xmpp.Dial(connect, id, server, password, resource, &xmpp.Config{SkipTLS: true})
if err != nil { if err != nil {
log.Println("Conn", err) log.Println("Conn", err)
goto start continue
} }
if err := Conn.SignalPresence("dnd", "is there some food in this world?", 12); err != nil { if err := Conn.SignalPresence("dnd", "is there some food in this world?", 12); err != nil {
log.Println("Signal", err) log.Println("Signal", err)
goto start 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)
goto start continue
} }
go func(Conn *xmpp.Conn) { go func(Conn *xmpp.Conn) {
for { for {
select { select {
case <-time.After(60 * time.Second): case <-time.After(60 * time.Second):
Conn.SendIQ("jabber.ru", "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("jabber.ru", "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) log.Println("KeepAlive err:", err)
return
}
ping = time.Now()
}
}
}(Conn)
go func(Conn *xmpp.Conn) {
for {
next, err := Conn.Next()
if err != nil {
log.Println("Next err:", err)
return return
} }
ping = time.Now() cs <- next
} }
} }(Conn)
}(Conn)
go func(Conn *xmpp.Conn) {
for { for {
next, err := Conn.Next() select {
if err != nil { case next = <-cs:
log.Println("Next err:", err) case <-time.After(65 * time.Second):
return log.Println(Conn.Close(), "\n\t", "closed after 65 seconds of inactivity")
break
} }
cs <- next switch t := next.Value.(type) {
} case *xmpp.ClientPresence:
}(Conn) PresenceHandler(Conn, t)
case *xmpp.ClientMessage:
for { if len(t.Delay.Stamp) == 0 && len(t.Subject) == 0 {
select { log.Println(t)
case next = <-cs: if GetNick(t.From) != name {
case <-time.After(65 * time.Second): if t.Type == "groupchat" {
log.Println(Conn.Close(), "\n\t", "closed after 65 seconds of inactivity") go MessageHandler(Conn, t)
goto start } else if xmpp.RemoveResourceFromJid(strings.ToLower(t.From)) == me {
} go SelfHandler(Conn, t)
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?")
} }
log.Println(Conn.Close(), "\n\t", "wtf am I doing here?")
goto start
} }
func SelfHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) { func SelfHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) {