xmpp status reflects room status
[svn42.git] / r3-netstatus / r3xmppbot / r3xmppbot.go
index f795afc..48a9f5c 100644 (file)
@@ -56,7 +56,7 @@ func (botdata *XmppBot) makeXMPPMessage(to string, message interface{}, subject
     return &xmpp.Message{Header: xmppmsgheader , Subject: msgsubject, Body: msgbody, Thread: &xmpp.Generic{}}
 }
 
-func (botdata *XmppBot) makeXMPPPresence(to, ptype string) *xmpp.Presence {
+func (botdata *XmppBot) makeXMPPPresence(to, ptype, show, status string) *xmpp.Presence {
     xmppmsgheader := xmpp.Header{To: to,
                                                             From: botdata.my_jid_,
                                                             Id: <-xmpp.Id,
@@ -65,7 +65,18 @@ func (botdata *XmppBot) makeXMPPPresence(to, ptype string) *xmpp.Presence {
                                                             Innerxml: "",
                                                             Error: nil,
                                                             Nested: make([]interface{},0)}
-    return &xmpp.Presence{Header: xmppmsgheader}
+    var gen_show, gen_status *xmpp.Generic
+    if len(show) == 0 {
+        gen_show = nil
+    } else {
+        gen_show = &xmpp.Generic{Chardata: show}
+    }
+    if len(status) == 0 {
+        gen_status = nil
+    } else {
+        gen_status = &xmpp.Generic{Chardata: status}
+    }                                                            
+    return &xmpp.Presence{Header: xmppmsgheader, Show: gen_show, Status: gen_status}
 }
 
 type R3JIDDesire int
@@ -79,6 +90,14 @@ const (
     R3DebugInfo
 )
 
+const (
+    ShowOnline string = ""
+    ShowAway string = "away"
+    ShowNotAvailabe string = "xa"
+    ShowDoNotDisturb string = "dnd"
+    ShowFreeForChat string = "chat"
+)
+
 type JidData struct {
        Online  bool
     Wants   R3JIDDesire
@@ -97,6 +116,11 @@ type XMPPMsgEvent struct {
     RememberAsStatus bool
 }
 
+type XMPPStatusEvent struct {
+    Show string
+    Status string
+}
+
 type RealraumXmppNotifierConfig map[string]JidData
 
 type XmppBot struct {
@@ -176,7 +200,10 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc
                             xmppout <-  botdata.makeXMPPMessage(to, pec, nil)
                         }
                     }
-                    
+                
+                case XMPPStatusEvent:
+                    xmppout <- botdata.makeXMPPPresence("", "", pec.Show, pec.Status)
+                
                 case XMPPMsgEvent:
                     if pec.RememberAsStatus {
                         last_status_msg = &pec.Msg
@@ -303,13 +330,13 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm
                 }
                 switch stanza.GetHeader().Type {
                     case "subscribe":
-                        xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribed")
+                        xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribed", "", "")
                         jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false}                        
-                        xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribe")
+                        xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribe", "", "")
                     case "unsubscribe", "unsubscribed":
                         jabber_events <- JabberEvent{stanza.GetHeader().From, false, R3NeverInfo, false}
                         botdata.jid_lastauthtime_[stanza.GetHeader().From] = 0 //logout
-                        xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "unsubscribe")
+                        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
@@ -365,7 +392,7 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure
     roster := xmpp.Roster(botdata.xmppclient_)
     for _, entry := range roster {
         if entry.Subscription == "from" {
-            botdata.xmppclient_.Out <- botdata.makeXMPPPresence(entry.Jid, "subscribe")
+            botdata.xmppclient_.Out <- botdata.makeXMPPPresence(entry.Jid, "subscribe", "","")
         }
         if entry.Subscription == "none" {
             delete(botdata.realraum_jids_, entry.Jid)