diff --git a/gateway/gateway.go b/gateway/gateway.go index e4a7d27..bb90a8b 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -2,8 +2,6 @@ package gateway import ( "github.com/Philipp15b/go-steam" - - "time" ) const ( @@ -33,10 +31,10 @@ type GatewayInfo struct { XMPP_JID_Client string XMPP_Out chan interface{} XMPP_Connected_Client map[string]bool - XMPP_Composing_Timers map[string]*time.Timer DebugMessage bool XMPP_IQ_RemoteRoster_Request map[string]string AllowEditRoster bool + ChatstateNotificationData chan string } type StatusSteamFriend struct { @@ -48,6 +46,7 @@ type StatusSteamFriend struct { func (g *GatewayInfo) Run() { go g.SteamRun() + go g.chatstatesNotification() } func (g *GatewayInfo) SetSteamAuthCode(authCode string) { diff --git a/gateway/xmpp.go b/gateway/xmpp.go index e93d40d..28d87cb 100644 --- a/gateway/xmpp.go +++ b/gateway/xmpp.go @@ -249,49 +249,80 @@ func (g *GatewayInfo) removeAllUserFromRoster() { func (g *GatewayInfo) SendXmppMessage(from, subject, message string) { g.sendXmppMessage(from, subject, message, &xmpp.Active{}) - return - - g.stopComposingTimer(from) - - // Make inactive after 2 min if nothing happen - t := time.AfterFunc(120*time.Second, func() { - g.sendXmppMessage(from, "", "", &xmpp.Inactive{}) - }) - g.XMPP_Composing_Timers[from] = t + g.ChatstateNotificationData <- from + g.ChatstateNotificationData <- "inactive" } func (g *GatewayInfo) SendXmppMessageLeaveConversation(from string) { g.sendXmppMessage(from, "", "", &xmpp.Gone{}) - return - - g.stopComposingTimer(from) + g.ChatstateNotificationData <- from + g.ChatstateNotificationData <- "stop" } func (g *GatewayInfo) SendXmppMessageComposing(from string) { g.sendXmppMessage(from, "", "", &xmpp.Composing{}) - return - - g.stopComposingTimer(from) - - timer := time.AfterFunc(20*time.Second, func() { - g.sendXmppMessage(from, "", "", &xmpp.Paused{}) - - t := time.AfterFunc(100*time.Second, func() { - g.sendXmppMessage(from, "", "", &xmpp.Inactive{}) - }) - g.XMPP_Composing_Timers[from] = t - }) - g.XMPP_Composing_Timers[from] = timer + g.ChatstateNotificationData <- from + g.ChatstateNotificationData <- "paused" + g.ChatstateNotificationData <- from + g.ChatstateNotificationData <- "inactive" } -func (g *GatewayInfo) stopComposingTimer(from string) { - if t, ok := g.XMPP_Composing_Timers[from]; ok { - // Delete previous timer if exist - if !t.Stop() { - // Prevent firing after stop - <-t.C +func (g *GatewayInfo) chatstatesNotification() { + inactiveTimers := make(map[string]*time.Timer) + pausedTimers := make(map[string]*time.Timer) + g.ChatstateNotificationData = make(chan string) + + for { + jid := <-g.ChatstateNotificationData + chatstate := <-g.ChatstateNotificationData + + timerInactive, okInactive := inactiveTimers[jid] + timerPaused, okPaused := pausedTimers[jid] + + switch chatstate { + case "stop": + if okInactive { + if !timerInactive.Stop() { + <-timerInactive.C + } + delete(inactiveTimers, jid) + } + if okPaused { + if !timerPaused.Stop() { + <-timerPaused.C + } + delete(pausedTimers, jid) + } + + case "paused": + if okInactive { + if !timerPaused.Stop() { + <-timerPaused.C + } + timerPaused.Reset(20 * time.Second) + } else { + timerPaused = time.AfterFunc(20*time.Second, func() { + g.sendXmppMessage(jid, "", "", &xmpp.Paused{}) + delete(pausedTimers, jid) + }) + pausedTimers[jid] = timerPaused + } + + case "inactive": + if okInactive { + if !timerInactive.Stop() { + <-timerInactive.C + } + timerInactive.Reset(120 * time.Second) + } else { + timerInactive = time.AfterFunc(120*time.Second, func() { + g.sendXmppMessage(jid, "", "", &xmpp.Inactive{}) + delete(inactiveTimers, jid) + }) + inactiveTimers[jid] = timerInactive + } + } - delete(g.XMPP_Composing_Timers, from) } } diff --git a/logger/logger.go b/logger/logger.go index 2cae8bf..7c49a2e 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -1,7 +1,7 @@ package logger import ( - "io" + "io" "log" ) @@ -12,7 +12,7 @@ var ( ) func Init(infoHandle io.Writer, warningHandle io.Writer, errorHandle io.Writer) { - Info = log.New(infoHandle, "INFO : ", log.Ldate|log.Ltime|log.Lshortfile) + Info = log.New(infoHandle, "INFO : ", log.Ldate|log.Ltime|log.Lshortfile) Debug = log.New(warningHandle, "DEBUG : ", log.Ldate|log.Ltime|log.Lshortfile) - Error = log.New(errorHandle, "ERROR : ", log.Ldate|log.Ltime|log.Lshortfile) + Error = log.New(errorHandle, "ERROR : ", log.Ldate|log.Ltime|log.Lshortfile) } diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 6c3d064..d27ea2f 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -280,7 +280,6 @@ func AddNewUser(jidUser, steamLogin, steamPwd string, debugMessage bool) { g.XMPP_Out = comp.Out g.XMPP_Connected_Client = make(map[string]bool) - g.XMPP_Composing_Timers = make(map[string]*time.Timer) g.DebugMessage = debugMessage g.XMPP_IQ_RemoteRoster_Request = make(map[string]string) g.AllowEditRoster = false