--- /dev/null
+package r3xmppbot
+
+import (
+ xmpp "code.google.com/p/goexmpp"
+ "time"
+ "encoding/xml"
+)
+
+// XMPP Ping
+type XMPPPing struct {
+ XMLName xml.Name `xml:"urn:xmpp:ping ping"`
+}
+
+func HandleServerToClientPing(iq *xmpp.Iq, xmppout chan<- xmpp.Stanza) bool {
+ ///<iq from='juliet@capulet.lit/balcony' to='capulet.lit' id='s2c1' type='result'/>
+ if iq.Type != "get" { return false}
+ for _, ele := range iq.Nested {
+ if _, ok := ele.(*XMPPPing); ok {
+ xmppout <- &xmpp.Iq{Header: xmpp.Header{To: iq.From, From: iq.To, Id: iq.Id, Type: "result" }}
+ return true
+ }
+ }
+ return false
+}
+
+func (botdata *XmppBot) PingServer(timeout_ms time.Duration) (is_up bool) {
+///<iq from='juliet@capulet.lit/balcony' to='capulet.lit' id='c2s1' type='get'>
+/// <ping xmlns='urn:xmpp:ping'/>
+///</iq>
+ server_jid := new(xmpp.JID)
+ server_jid.Set(botdata.my_jid_)
+ iqping := &xmpp.Iq{Header: xmpp.Header{To: server_jid.Domain,
+ From: botdata.my_jid_,
+ Id: <-xmpp.Id,
+ Type: "get",
+ Nested: []interface{}{XMPPPing{}} } }
+ pong := make(chan bool, 1)
+ defer close(pong)
+ f := func(v xmpp.Stanza) bool {
+ defer recover() //recover from writing to possibly already closed chan
+ let_others_handle_stanza := false
+ iq, ok := v.(*xmpp.Iq)
+ if !ok {
+ Syslog_.Printf("response to iq ping wasn't iq: %s", v)
+ pong <- false
+ return true //let other handlers process reply
+ }
+ if iq.Type == "error" && iq.Error != nil && iq.Error.Type == "cancel"{
+ Debug_.Printf("response to iq ping was cancel, server does not support ping")
+ //server does not support ping, but at least we know server is still there
+ } else if iq.Type != "result" {
+ Syslog_.Printf("response to iq ping was not pong: %s", v)
+ let_others_handle_stanza = true //let other handlers process reply
+ }
+ pong <- true
+ return let_others_handle_stanza // return false so that Stanza v will not be appear in xmppclient_.Out()
+ }
+ botdata.xmppclient_.HandleStanza(iqping.Id, f)
+ botdata.xmppclient_.Out <- iqping
+ go func() {
+ defer func() {if x:= recover(); x == nil { Syslog_.Printf("response to iq ping timed out !!") }}() //recover from writing to possibly already closed chan. If we did not need to recover, then Handler did not receive reply
+ time.Sleep(timeout_ms * time.Millisecond)
+ pong <- false //xmpp ping timed out
+ }()
+ return <- pong
+}
"path"
)
-//~ type StdLogger struct {
-//~ }
-
-//~ func (s *StdLogger) Log(v ...interface{}) {
- //~ log.Println(v...)
-//~ }
-
-//~ func (s *StdLogger) Logf(fmt string, v ...interface{}) {
- //~ log.Printf(fmt, v...)
-//~ }
-
-
func (botdata *XmppBot) makeXMPPMessage(to string, message interface{}, subject interface{}) *xmpp.Message {
xmppmsgheader := xmpp.Header{To: to,
From: botdata.my_jid_,
my_login_password_ string
xmppclient_ *xmpp.Client
presence_events_ *chan interface{}
- ping_reply_ chan bool
}
}
}
-
-func init() {
- //~ logger := &StdLogger{}
- //~ xmpp.Debug = logger
- //~ xmpp.Info = logger
- //~ xmpp.Warn = logger
-}
-
func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presence_events <- chan interface{}, jabber_events <- chan JabberEvent) {
var last_status_msg *string
Syslog_.Printf("handleEventsforXMPP: run time panic: %v", x)
//FIXME: signal that xmpp bot has crashed
}
+ for _ = range(jabber_events) {} //cleanout jabber_events queue
}()
for {
select {
case pe, pe_still_open := <-presence_events:
- if ! pe_still_open { break }
+ if ! pe_still_open { return }
Debug_.Printf("handleEventsforXMPP<-presence_events: %T %+v", pe, pe)
switch pec := pe.(type) {
case xmpp.Stanza:
}
}
default:
- break
+ Debug_.Println("handleEventsforXMPP<-presence_events: unknown type received: quitting")
+ return
}
case je, je_still_open := <-jabber_events:
- if ! je_still_open { break }
+ if ! je_still_open { return }
Debug_.Printf("handleEventsforXMPP<-jabber_events: %T %+v", je, je)
simple_jid := removeJIDResource(je.JID)
jid_data, jid_in_map := botdata.realraum_jids_[simple_jid]
if inmsg.Body == nil || inmsg.GetHeader() == nil {
return
}
- if inmsg.GetHeader().Error != nil {
- Syslog_.Printf("XMPP Message Error: %s", inmsg.GetHeader().Error.Error())
+ if inmsg.Type == "error" || inmsg.Error != nil {
+ Syslog_.Printf("XMPP Message Error: %s", inmsg.Error.Error())
}
bodytext :=inmsg.Body.Chardata
if botdata.isAuthenticated(inmsg.GetHeader().From) {
defer func() {
if x := recover(); x != nil {
Syslog_.Printf("handleIncomingXMPPStanzas: run time panic: %v", x)
- close(jabber_events)
}
+ close(jabber_events)
}()
var incoming_stanza interface{}
botdata.handleIncomingMessageDialog(*stanza, xmppout, jabber_events)
case *xmpp.Presence:
if stanza.GetHeader() == nil { continue }
- if stanza.GetHeader().Error != nil {
- Syslog_.Printf("XMPP Presence Error: %s", stanza.GetHeader().Error.Error())
+ if stanza.Type == "error" || stanza.Error != nil {
+ Syslog_.Printf("XMPP Presence Error: %s", stanza.Error.Error())
}
switch stanza.GetHeader().Type {
case "subscribe":
}
case *xmpp.Iq:
if stanza.GetHeader() == nil { continue }
- if stanza.GetHeader().Error != nil {
- Syslog_.Printf("XMPP Iq Error: %s", stanza.GetHeader().Error.Error())
+ if stanza.Type == "error" || stanza.Error != nil {
+ Syslog_.Printf("XMPP Iq Error: %s", stanza.Error.Error())
}
+ if HandleServerToClientPing(stanza, xmppout) {continue} //if true then routine handled it and we can continue
+ Debug_.Printf("Unhandled Iq: %s", stanza)
}
}
}
+func init() {
+ //~ xmpp.Debug = &XMPPDebugLogger{}
+ xmpp.Info = &XMPPDebugLogger{}
+ xmpp.Warn = &XMPPLogger{}
+}
+
func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecuretls bool) (*XmppBot, chan interface{}, error) {
var err error
botdata := new(XmppBot)
botdata.my_jid_ = loginjid
botdata.my_login_password_ = loginpwd
botdata.auth_timeout_ = 3600*2
- botdata.ping_reply_ = make(chan bool)
botdata.config_file_ = path.Join(state_save_dir, "r3xmpp."+removeJIDResource(loginjid)+".json")
- //~ logger := &StdLogger{}
- //~ xmpp.Debug = logger
- //~ xmpp.Info = logger
- //~ xmpp.Warn = logger
-
xmpp.TlsConfig = tls.Config{InsecureSkipVerify: insecuretls}
botdata.realraum_jids_.loadFrom(botdata.config_file_)
}
func (botdata *XmppBot) StopBot() {
+ Syslog_.Println("Stopping XMPP Bot")
if botdata.xmppclient_ != nil {
close(botdata.xmppclient_.Out)
}
*botdata.presence_events_ <- false
close(*botdata.presence_events_)
}
+ botdata.config_file_ = ""
+ botdata.realraum_jids_ = nil
+ botdata.xmppclient_ = nil
}