From cbba478c4eca3c3bf534492adb8ef889e2289dd7 Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Tue, 3 Sep 2013 13:40:18 +0000 Subject: [PATCH] refactor and clean up interface --- r3-netstatus/main.go | 20 ++++++-- r3-netstatus/r3xmppbot/r3xmppbot.go | 89 ++++++++++++----------------------- 2 files changed, 46 insertions(+), 63 deletions(-) diff --git a/r3-netstatus/main.go b/r3-netstatus/main.go index 3e6290a..81411f0 100644 --- a/r3-netstatus/main.go +++ b/r3-netstatus/main.go @@ -37,25 +37,39 @@ func init() { //------- +func IfThenElseStr(c bool, strue, sfalse string) string { + if c {return strue} else {return sfalse} +} + +func composeMessage(present, locked, shut bool, who string, ts int64) string { + return fmt.Sprintf("%s (Door is %s and %s and was last used by %s at %s)", + IfThenElseStr(present, "Somebody is present!" , "Everybody left."), + IfThenElseStr(locked, "locked","unlocked"), + IfThenElseStr(shut, "shut","ajar"), + who, + time.Unix(ts,0).String()) +} + func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) { events := ps.Sub("presence","door","buttons") var present, locked, shut bool = false, true, true var who string = "Unknown" + button_msg := "The button has been pressed ! Propably someone is bored and in need of company ! ;-)" for eventinterface := range(events) { switch event := eventinterface.(type) { case PresenceUpdate: present = event.Present - xmpp_presence_events_chan_ <- r3xmppbot.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Ts: event.Ts} + xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true} case DoorCommandEvent: who = event.Who xmpp_presence_events_chan_ <- fmt.Sprintln("DoorCommand:",event.Command, "using", event.Using, "by", event.Who, time.Unix(event.Ts,0)) case DoorStatusUpdate: locked = event.Locked shut = event.Shut - xmpp_presence_events_chan_ <- r3xmppbot.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Ts: event.Ts} + 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.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Button: true, Ts: event.Ts} + xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo} } } } diff --git a/r3-netstatus/r3xmppbot/r3xmppbot.go b/r3-netstatus/r3xmppbot/r3xmppbot.go index 7a19c77..da99930 100644 --- a/r3-netstatus/r3xmppbot/r3xmppbot.go +++ b/r3-netstatus/r3xmppbot/r3xmppbot.go @@ -2,7 +2,6 @@ package r3xmppbot import ( xmpp "code.google.com/p/goexmpp" - "fmt" "log" "crypto/tls" "os" @@ -22,9 +21,6 @@ import ( //~ log.Printf(fmt, v...) //~ } -func IfThenElseStr(c bool, strue, sfalse string) string { - if c {return strue} else {return sfalse} -} func (botdata *XmppBot) makeXMPPMessage(to string, message interface{}, subject interface{}) *xmpp.Message { xmppmsgheader := xmpp.Header{To: to, @@ -76,9 +72,9 @@ type R3JIDDesire int const ( R3NoChange R3JIDDesire = -1 - R3NoInfo R3JIDDesire = iota // ignore first value by assigning to blank identifier - R3NoOfflineInfo - R3AllInfo + R3NeverInfo R3JIDDesire = iota // ignore first value by assigning to blank identifier + R3OnlineOnlyInfo + R3AlwaysInfo R3DebugInfo ) @@ -94,13 +90,10 @@ type JabberEvent struct { StatusNow bool } -type XMPPPresenceEvent struct { - Present bool - Who string - DoorLock bool - DoorShut bool - Button bool - Ts int64 +type XMPPMsgEvent struct { + Msg string + DistributeLevel R3JIDDesire + RememberAsStatus bool } type RealraumXmppNotifierConfig map[string]JidData @@ -160,32 +153,10 @@ func init() { //~ xmpp.Warn = logger } -func composeMessage(pec *XMPPPresenceEvent, both bool) *string { - var msg string - msg = "" - if pec.Button { - msg = "The button has been pressed ! Propably someone is bored and in need of company ! ;-)" - if both { msg += "\n"; } else { - msg += " --- " + time.Unix(pec.Ts,0).String() - return &msg; - } - } - msg += fmt.Sprintf("%s (Door is %s and %s and was last used by %s at %s)", - IfThenElseStr(pec.Present, "Somebody is present!" , "Everybody left."), - IfThenElseStr(pec.DoorLock, "locked","unlocked"), - IfThenElseStr(pec.DoorShut, "shut","ajar"), - pec.Who, - time.Unix(pec.Ts,0).String()) - return &msg -} - func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presence_events <- chan interface{}, jabber_events <- chan JabberEvent) { - var debug_msg bool - var last_presence_event *XMPPPresenceEvent - var msg *string + var last_status_msg *string for { - debug_msg = false select { case pe := <-presence_events: switch pec := pe.(type) { @@ -193,25 +164,23 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc xmppout <- pec continue case string: - msg = &pec - debug_msg = true - case XMPPPresenceEvent: - debug_msg = last_presence_event != nil && last_presence_event.Present == pec.Present - last_presence_event = &pec - msg = composeMessage(last_presence_event, false) - default: - break - } - - if msg == nil { continue } - - for to, jiddata := range botdata.realraum_jids_ { - if debug_msg && jiddata.Wants < R3DebugInfo { - continue + for to, jiddata := range botdata.realraum_jids_ { + if jiddata.Wants >= R3DebugInfo { + xmppout <- botdata.makeXMPPMessage(to, pec, nil) + } } - if (jiddata.Wants == R3NoOfflineInfo && jiddata.Online) || jiddata.Wants > R3NoOfflineInfo { - xmppout <- botdata.makeXMPPMessage(to, msg, nil) + + case XMPPMsgEvent: + if pec.RememberAsStatus { + last_status_msg = &pec.Msg } + for to, jiddata := range botdata.realraum_jids_ { + if jiddata.Wants >= pec.DistributeLevel && ((jiddata.Wants == R3OnlineOnlyInfo && jiddata.Online) || jiddata.Wants > R3OnlineOnlyInfo) { + xmppout <- botdata.makeXMPPMessage(to, pec.Msg, nil) + } + } + default: + break } case je := <-jabber_events: @@ -219,8 +188,8 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc jid_data, jid_in_map := botdata.realraum_jids_[simple_jid] if jid_in_map { jid_data.Online = je.Online - if je.StatusNow && last_presence_event != nil { - xmppout <- botdata.makeXMPPMessage(je.JID, composeMessage(last_presence_event, true), nil) + if je.StatusNow && last_status_msg != nil { + xmppout <- botdata.makeXMPPMessage(je.JID, last_status_msg, nil) } if je.Wants > R3NoChange { jid_data.Wants = je.Wants @@ -262,13 +231,13 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout if botdata.isAuthenticated(inmsg.GetHeader().From) { switch bodytext { case "on", "*on*": - jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NoOfflineInfo, false} + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3OnlineOnlyInfo, false} xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive r3 information while online" , "Your New Status") case "off", "*off*": - jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NoInfo, false} + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NeverInfo, false} xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Do not receive r3 information" , "Your New Status") case "on_while_offline", "*on_while_offline*": - jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3AllInfo, false} + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3AlwaysInfo, false} xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive r3 information even while offline" , "Your New Status") case "debug": jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3DebugInfo, false} @@ -320,7 +289,7 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm 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} + jabber_events <- JabberEvent{stanza.GetHeader().From, false, R3NeverInfo, false} botdata.jid_lastauthtime_[stanza.GetHeader().From] = 0 //logout xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "unsubscribe") case "unavailable": -- 1.7.10.4