show errors
[svn42.git] / go / r3-netstatus / r3xmppbot / r3xmppbot.go
index 7863b70..34a1472 100644 (file)
@@ -136,6 +136,7 @@ type XmppBot struct {
     my_login_password_ string
     xmppclient_ *xmpp.Client
     presence_events_ *chan interface{}
+    ping_reply_ chan bool
 }
 
 
@@ -185,12 +186,15 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc
     defer func() {
         if x := recover(); x != nil {
             Syslog_.Printf("handleEventsforXMPP: run time panic: %v", x)
+            //FIXME: signal that xmpp bot has crashed
         }
     }()
 
        for {
                select {
-               case pe := <-presence_events:
+               case pe, pe_still_open := <-presence_events:
+            if ! pe_still_open { break }
+            Debug_.Printf("handleEventsforXMPP<-presence_events: %T %+v", pe, pe)
             switch pec := pe.(type) {
                 case xmpp.Stanza:
                     xmppout <- pec
@@ -218,11 +222,20 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc
                     break
                 }
 
-               case je := <-jabber_events:
+               case je, je_still_open := <-jabber_events:
+            if ! je_still_open { break }
+            Debug_.Printf("handleEventsforXMPP<-jabber_events: %T %+v", je, je)
             simple_jid := removeJIDResource(je.JID)
             jid_data, jid_in_map := botdata.realraum_jids_[simple_jid]
+
+            //send status if requested, even if user never changed any settings and thus is not in map
+            if last_status_msg != nil && je.StatusNow {
+                xmppout <-  botdata.makeXMPPMessage(je.JID, last_status_msg, nil)
+            }
+
             if jid_in_map {
-                if last_status_msg != nil && (je.StatusNow || (! jid_data.Online && je.Online && jid_data.Wants == R3OnlineOnlyWithRecapInfo) ) {
+                //if R3OnlineOnlyWithRecapInfo, we want a status update when coming online
+                if last_status_msg != nil && ! jid_data.Online && je.Online && jid_data.Wants == R3OnlineOnlyWithRecapInfo {
                     xmppout <-  botdata.makeXMPPMessage(je.JID, last_status_msg, nil)
                 }
                 jid_data.Online = je.Online
@@ -260,6 +273,9 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout
     if inmsg.Body == nil || inmsg.GetHeader() == nil {
         return
     }
+    if inmsg.GetHeader().Error != nil {
+        Syslog_.Printf("XMPP Message Error: %s", inmsg.GetHeader().Error.Error())
+    }
     bodytext :=inmsg.Body.Chardata
     if botdata.isAuthenticated(inmsg.GetHeader().From) {
         switch bodytext {
@@ -324,8 +340,9 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm
             case *xmpp.Message:
                 botdata.handleIncomingMessageDialog(*stanza, xmppout, jabber_events)
             case *xmpp.Presence:
-                if stanza.GetHeader() == nil {
-                    continue
+                if stanza.GetHeader() == nil { continue }
+                if stanza.GetHeader().Error != nil {
+                    Syslog_.Printf("XMPP Presence Error: %s", stanza.GetHeader().Error.Error())
                 }
                 switch stanza.GetHeader().Type {
                     case "subscribe":
@@ -343,8 +360,9 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm
                         jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false}
                 }
             case *xmpp.Iq:
-                if stanza.GetHeader() == nil {
-                    continue
+                if stanza.GetHeader() == nil { continue }
+                if stanza.GetHeader().Error != nil {
+                    Syslog_.Printf("XMPP Iq Error: %s", stanza.GetHeader().Error.Error())
                 }
         }
     }
@@ -361,6 +379,7 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure
     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")
 
@@ -388,6 +407,7 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure
 
     roster := xmpp.Roster(botdata.xmppclient_)
     for _, entry := range roster {
+        Debug_.Print(entry)
         if entry.Subscription == "from" {
             botdata.xmppclient_.Out <- botdata.makeXMPPPresence(entry.Jid, "subscribe", "","")
         }