presence subscription both ways
[svn42.git] / r3-netstatus / r3xmppbot / r3xmppbot.go
index 1be12f8..6017c3a 100644 (file)
@@ -247,8 +247,8 @@ func (botdata *XmppBot) isAuthenticated(jid string) bool {
     return in_map && time.Now().Unix() - authtime < botdata.auth_timeout_
 }
 
-const help_text_ string = "\n*auth*<password>* ...enables you to use more commands\n*time* ...gives bot time"
-const help_text_auth string = "You are authorized to use the following commands\n*on* ...you will be notified of r3 status changes\n*off* ...you will no longer recieve notifications\n*on_while_offline* ...you will be notified of r3 status changes even if you are offline\n*status* ...query current status\n*time* ...gives bot time\n*bye* ...logout"
+const help_text_ string = "\n*auth*<password>* ...Enables you to use more commands.\n*time* ...Returns bot time."
+const help_text_auth string = "You are authorized to use the following commands:\n*on* ...You will be notified of r3 status changes.\n*off* ...You will no longer recieve notifications.\n*on_while_offline* ...You will be notified of r3 status changes even if you are offline.\n*status* ...Query current status.\n*time* ...Returns bot time.\n*bye* ...Logout."
 
 //~ var re_msg_auth_    *regexp.Regexp     = regexp.MustCompile("auth\s+(\S+)")
 
@@ -292,7 +292,7 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout
             case botdata.auth_cmd_, botdata.auth_cmd2_:
                 botdata.jid_lastauthtime_[inmsg.GetHeader().From] = time.Now().Unix()
                 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_auth, nil)
-            case "status", "*status*":
+            case "status", "*status*", "off", "*off*", "on", "*on*", "on_while_offline", "*on_while_offline*":
                 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Sorry, you need to be authorized to do that." , nil)
             case "time", "*time*":
                 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, time.Now().String() , nil)
@@ -313,11 +313,21 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm
                 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
@@ -358,12 +368,22 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure
         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
     }
 
+    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)