xmpp status reflects room status
authorBernhard Tittelbach <xro@realraum.at>
Thu, 12 Sep 2013 15:15:33 +0000 (15:15 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Thu, 12 Sep 2013 15:15:33 +0000 (15:15 +0000)
r3-netstatus/main.go
r3-netstatus/r3xmppbot/r3xmppbot.go

index ec65f06..c6229ab 100644 (file)
@@ -22,6 +22,7 @@ var (
     xmpp_login_ struct {jid string; pass string}
     xmpp_bot_authstring_ string
     xmpp_state_save_dir_ string
+    button_press_timeout_ int64 = 3600
 )
 
 //-------
@@ -62,14 +63,27 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface
     }()
 
     var present, locked, shut bool = false, true, true
+    var last_buttonpress int64 = 0
     var who string
     button_msg := "The button has been pressed ! Propably someone is bored and in need of company ! ;-)"
-
+    button_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowFreeForChat, "The button has been pressed :-)"}
+    
+    xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe, "Nobody is here"}
+    
     for eventinterface := range(events) {
         switch event := eventinterface.(type) {
             case PresenceUpdate:
                 present = event.Present
                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true}
+                if present {
+                    if time.Now().Unix() - last_buttonpress < button_press_timeout_ {
+                        xmpp_presence_events_chan_ <- button_status
+                    } else {
+                        xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowOnline,"Somebody is present"}
+                    }
+                } else {
+                    xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe,"Nobody is here"}
+                }                
             case DoorCommandEvent:
                 if len(event.Who) > 0 && len(event.Using) > 0 {
                     who = fmt.Sprintf("%s (%s)",event.Who, event.Using)
@@ -83,6 +97,8 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface
                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true}
             case ButtonPressUpdate:
                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo}
+                xmpp_presence_events_chan_ <- button_status
+                last_buttonpress = event.Ts
         }
        }
 }
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)