presence subscription both ways
authorBernhard Tittelbach <xro@realraum.at>
Wed, 28 Aug 2013 02:39:23 +0000 (02:39 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Wed, 28 Aug 2013 02:39:23 +0000 (02:39 +0000)
r3-netstatus/r3xmppbot/r3xmppbot.go

index 1a2d9f0..6017c3a 100644 (file)
@@ -313,11 +313,21 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm
                 if stanza.GetHeader() == nil {
                     continue
                 }
                 if stanza.GetHeader() == nil {
                     continue
                 }
-                if stanza.GetHeader().Type == "subscribe" {
-                    xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribed")
+                switch stanza.GetHeader().Type {
+                    case "subscribe":
+                        xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribed")
+                        jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false}                        
+                        xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribe")
+                    case "unsubscribe", "unsubscribed":
+                        jabber_events <- JabberEvent{stanza.GetHeader().From, false, R3NoInfo, false}
+                        botdata.jid_lastauthtime_[stanza.GetHeader().From] = 0 //logout
+                        xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "unsubscribe")
+                    case "unavailable":
+                        jabber_events <- JabberEvent{stanza.GetHeader().From, false, R3NoChange, false}
+                        botdata.jid_lastauthtime_[stanza.GetHeader().From] = 0 //logout
+                    default:
+                        jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false}
                 }
                 }
-                jabber_events <- JabberEvent{stanza.GetHeader().From, stanza.GetHeader().Type != "unavailable", R3NoChange, false}
-                if stanza.GetHeader().Type == "unavailable" { botdata.jid_lastauthtime_[stanza.GetHeader().From] = 0}  //logout if offline
             case *xmpp.Iq:
                 if stanza.GetHeader() == nil {
                     continue
             case *xmpp.Iq:
                 if stanza.GetHeader() == nil {
                     continue
@@ -358,12 +368,22 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure
         return nil, nil, err
     }
 
         return nil, nil, err
     }
 
-    err = botdata.xmppclient_.StartSession(false, &xmpp.Presence{})
+    err = botdata.xmppclient_.StartSession(true, &xmpp.Presence{})
     if err != nil {
         log.Println("'Error StartSession:", err)
         return nil, nil, err
     }
 
     if err != nil {
         log.Println("'Error StartSession:", err)
         return nil, nil, err
     }
 
+    roster := xmpp.Roster(botdata.xmppclient_)
+    for _, entry := range roster {
+        if entry.Subscription == "from" {
+            botdata.xmppclient_.Out <- botdata.makeXMPPPresence(entry.Jid, "subscribe")
+        }
+        if entry.Subscription == "none" {
+            delete(botdata.realraum_jids_, entry.Jid)
+        }
+    }
+
     presence_events := make(chan interface{},1)
     jabber_events := make(chan JabberEvent,1)
 
     presence_events := make(chan interface{},1)
     jabber_events := make(chan JabberEvent,1)