From 7e2db9f58b26c6db020008abccda8bdd338f212f Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Sun, 4 Sep 2016 14:47:48 +0200 Subject: [PATCH] Add support chatstates notification (issue #23) --- gateway/gateway.go | 2 ++ gateway/steam.go | 16 ++++++++++-- gateway/xmpp.go | 61 +++++++++++++++++++++++++++++++++++++++++++++- servers.addr | 2 +- xmpp/xmpp.go | 2 ++ 5 files changed, 79 insertions(+), 4 deletions(-) diff --git a/gateway/gateway.go b/gateway/gateway.go index 324f0c3..3adbe48 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -2,6 +2,7 @@ package gateway import ( "github.com/Philipp15b/go-steam" + "time" ) const ( @@ -24,6 +25,7 @@ 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 } diff --git a/gateway/steam.go b/gateway/steam.go index 3db6cc9..b1f4531 100644 --- a/gateway/steam.go +++ b/gateway/steam.go @@ -131,7 +131,11 @@ func (g *GatewayInfo) mainSteam() { case *steam.ChatMsgEvent: // Message received - g.SendXmppMessage(e.ChatterId.ToString()+"@"+XmppJidComponent, "", e.Message) + if e.EntryType == steamlang.EChatEntryType_Typing { + g.SendXmppMessageComposing(e.ChatterId.ToString() + "@" + XmppJidComponent) + } else { + g.SendXmppMessage(e.ChatterId.ToString()+"@"+XmppJidComponent, "", e.Message) + } case *steam.ChatInviteEvent: // Invitation to play @@ -219,6 +223,14 @@ func (g *GatewayInfo) DisconnectAllSteamFriend() { } func (g *GatewayInfo) SendSteamMessage(steamId, message string) { + g.sendSteamMessage(steamId, message, steamlang.EChatEntryType_ChatMsg) +} + +func (g *GatewayInfo) SendSteamMessageComposing(steamId string) { + g.sendSteamMessage(steamId, "", steamlang.EChatEntryType_Typing) +} + +func (g *GatewayInfo) sendSteamMessage(steamId, message string, chatEntryType steamlang.EChatEntryType) { if !g.IsSteamConnected() { log.Printf("%sTry to send message, but disconnected", LogSteamDebug) return @@ -226,7 +238,7 @@ func (g *GatewayInfo) SendSteamMessage(steamId, message string) { steamIdUint64, err := strconv.ParseUint(steamId, 10, 64) if err == nil { - g.SteamClient.Social.SendMessage(steamid.SteamId(steamIdUint64), steamlang.EChatEntryType_ChatMsg, message) + g.SteamClient.Social.SendMessage(steamid.SteamId(steamIdUint64), chatEntryType, message) } else { log.Printf("%sFailed to get SteamId from %s", LogSteamError, steamId) } diff --git a/gateway/xmpp.go b/gateway/xmpp.go index 1ccdfc3..c84176b 100644 --- a/gateway/xmpp.go +++ b/gateway/xmpp.go @@ -6,6 +6,7 @@ import ( "log" "strings" + "time" ) const ( @@ -124,7 +125,19 @@ func (g *GatewayInfo) ReceivedXMPP_Presence(presence *xmpp.Presence) { func (g *GatewayInfo) ReceivedXMPP_Message(message *xmpp.Message) { steamID := strings.SplitN(message.To, "@", 2)[0] - g.SendSteamMessage(steamID, message.Body) + if message.Composing != nil { + g.SendSteamMessageComposing(steamID) + } else if message.Paused != nil { + return + } else if message.Inactive != nil { + return + } else if message.Gone != nil { + return + } else { + if message.Body != "" { + g.SendSteamMessage(steamID, message.Body) + } + } } func (g *GatewayInfo) XMPP_Disconnect() { @@ -163,6 +176,37 @@ func (g *GatewayInfo) SendXmppPresence(status, tpye, to, from, message, nick str } func (g *GatewayInfo) SendXmppMessage(from, subject, message string) { + g.sendXmppMessage(from, subject, message, &xmpp.Active{}) + g.stopComposingTimer(from) +} + +func (g *GatewayInfo) SendXmppMessageComposing(from string) { + g.sendXmppMessage(from, "", "", &xmpp.Composing{}) + 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 +} + +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 + } + delete(g.XMPP_Composing_Timers, from) + } +} + +func (g *GatewayInfo) sendXmppMessage(from, subject, message string, chatState interface{}) { if from != XmppJidComponent || from == XmppJidComponent && g.DebugMessage { m := xmpp.Message{To: g.XMPP_JID_Client, From: from, Body: message, Type: "chat"} @@ -170,6 +214,21 @@ func (g *GatewayInfo) SendXmppMessage(from, subject, message string) { m.Subject = subject } + switch v := chatState.(type) { + case *xmpp.Active: + m.Active = v + case *xmpp.Composing: + m.Composing = v + case *xmpp.Paused: + m.Paused = v + case *xmpp.Inactive: + m.Inactive = v + case *xmpp.Gone: + m.Gone = v + default: + m.Active = &xmpp.Active{} + } + log.Printf("%sSend message %v", LogXmppInfo, m) g.XMPP_Out <- m } diff --git a/servers.addr b/servers.addr index 3a7a503..79d9f8e 100644 --- a/servers.addr +++ b/servers.addr @@ -1 +1 @@ -{"Addresses":[{"IP":"146.66.152.11","Port":27019},{"IP":"162.254.197.40","Port":27021},{"IP":"146.66.152.11","Port":27020},{"IP":"146.66.152.10","Port":27018},{"IP":"146.66.152.11","Port":27017},{"IP":"162.254.197.41","Port":27021},{"IP":"146.66.152.11","Port":27018},{"IP":"146.66.152.10","Port":27020},{"IP":"146.66.152.10","Port":27019},{"IP":"146.66.152.10","Port":27017},{"IP":"162.254.197.41","Port":27020},{"IP":"162.254.197.42","Port":27021},{"IP":"162.254.197.41","Port":27017},{"IP":"162.254.197.40","Port":27017},{"IP":"162.254.197.40","Port":27019},{"IP":"162.254.197.40","Port":27020},{"IP":"162.254.197.41","Port":27019},{"IP":"162.254.197.40","Port":27018},{"IP":"162.254.197.41","Port":27018},{"IP":"162.254.197.42","Port":27017},{"IP":"162.254.197.42","Port":27019},{"IP":"162.254.197.42","Port":27018},{"IP":"162.254.197.42","Port":27020},{"IP":"162.254.196.43","Port":27021},{"IP":"162.254.196.41","Port":27021},{"IP":"162.254.196.43","Port":27018},{"IP":"162.254.196.43","Port":27020},{"IP":"162.254.196.40","Port":27017},{"IP":"162.254.196.41","Port":27017},{"IP":"162.254.196.43","Port":27019},{"IP":"162.254.196.40","Port":27018},{"IP":"162.254.196.41","Port":27019},{"IP":"162.254.196.42","Port":27021},{"IP":"162.254.196.42","Port":27020},{"IP":"162.254.196.42","Port":27018},{"IP":"162.254.196.42","Port":27017},{"IP":"162.254.196.43","Port":27017},{"IP":"162.254.196.40","Port":27020},{"IP":"162.254.196.41","Port":27020},{"IP":"162.254.196.40","Port":27021},{"IP":"162.254.196.41","Port":27018},{"IP":"162.254.196.40","Port":27019},{"IP":"162.254.196.42","Port":27019},{"IP":"185.25.180.14","Port":27020},{"IP":"155.133.242.9","Port":27018},{"IP":"155.133.242.9","Port":27017},{"IP":"185.25.180.14","Port":27019},{"IP":"155.133.242.9","Port":27020},{"IP":"146.66.155.8","Port":27018},{"IP":"146.66.155.8","Port":27017},{"IP":"146.66.155.8","Port":27020},{"IP":"185.25.182.10","Port":27018},{"IP":"185.25.180.14","Port":27018},{"IP":"155.133.242.8","Port":27017},{"IP":"185.25.180.15","Port":27019},{"IP":"155.133.242.9","Port":27019},{"IP":"146.66.155.8","Port":27019},{"IP":"185.25.182.10","Port":27017},{"IP":"185.25.182.10","Port":27019},{"IP":"185.25.180.15","Port":27020},{"IP":"185.25.180.15","Port":27018},{"IP":"155.133.242.8","Port":27019},{"IP":"155.133.242.8","Port":27018},{"IP":"155.133.242.8","Port":27020},{"IP":"185.25.180.15","Port":27017},{"IP":"185.25.180.14","Port":27017},{"IP":"185.25.182.10","Port":27020},{"IP":"208.78.164.14","Port":27018},{"IP":"208.78.164.12","Port":27017},{"IP":"208.78.164.10","Port":27017},{"IP":"208.78.164.14","Port":27017},{"IP":"208.78.164.12","Port":27018},{"IP":"208.78.164.13","Port":27019},{"IP":"208.78.164.14","Port":27019},{"IP":"208.78.164.13","Port":27017},{"IP":"208.78.164.10","Port":27019},{"IP":"208.78.164.12","Port":27019},{"IP":"208.78.164.10","Port":27018},{"IP":"208.78.164.13","Port":27018},{"IP":"162.254.195.45","Port":27021}]} \ No newline at end of file +{"Addresses":[{"IP":"162.254.197.42","Port":27021},{"IP":"146.66.152.10","Port":27020},{"IP":"162.254.197.42","Port":27018},{"IP":"162.254.197.41","Port":27019},{"IP":"146.66.152.10","Port":27017},{"IP":"146.66.152.11","Port":27017},{"IP":"146.66.152.11","Port":27019},{"IP":"146.66.152.11","Port":27018},{"IP":"146.66.152.11","Port":27020},{"IP":"162.254.197.40","Port":27018},{"IP":"162.254.197.41","Port":27018},{"IP":"162.254.197.42","Port":27019},{"IP":"162.254.197.42","Port":27020},{"IP":"162.254.197.40","Port":27017},{"IP":"162.254.197.40","Port":27020},{"IP":"146.66.152.10","Port":27019},{"IP":"162.254.197.42","Port":27017},{"IP":"162.254.197.41","Port":27017},{"IP":"162.254.197.40","Port":27019},{"IP":"162.254.197.41","Port":27021},{"IP":"146.66.152.10","Port":27018},{"IP":"162.254.197.41","Port":27020},{"IP":"162.254.196.43","Port":27020},{"IP":"162.254.196.43","Port":27017},{"IP":"162.254.196.42","Port":27019},{"IP":"162.254.196.42","Port":27017},{"IP":"162.254.196.43","Port":27019},{"IP":"162.254.196.43","Port":27018},{"IP":"162.254.196.42","Port":27018},{"IP":"162.254.196.41","Port":27021},{"IP":"162.254.196.40","Port":27020},{"IP":"162.254.196.40","Port":27019},{"IP":"162.254.196.41","Port":27017},{"IP":"162.254.196.40","Port":27018},{"IP":"162.254.196.43","Port":27021},{"IP":"162.254.196.41","Port":27018},{"IP":"162.254.196.41","Port":27020},{"IP":"162.254.197.40","Port":27021},{"IP":"162.254.196.41","Port":27019},{"IP":"162.254.196.40","Port":27021},{"IP":"162.254.196.42","Port":27021},{"IP":"162.254.196.40","Port":27017},{"IP":"162.254.196.42","Port":27020},{"IP":"146.66.155.8","Port":27019},{"IP":"185.25.182.10","Port":27018},{"IP":"185.25.182.10","Port":27020},{"IP":"185.25.182.10","Port":27017},{"IP":"146.66.155.8","Port":27017},{"IP":"185.25.180.14","Port":27018},{"IP":"185.25.180.14","Port":27017},{"IP":"185.25.182.10","Port":27019},{"IP":"146.66.155.8","Port":27018},{"IP":"185.25.180.15","Port":27018},{"IP":"155.133.242.9","Port":27017},{"IP":"185.25.180.15","Port":27017},{"IP":"185.25.180.15","Port":27019},{"IP":"155.133.242.8","Port":27019},{"IP":"155.133.242.9","Port":27019},{"IP":"185.25.180.14","Port":27020},{"IP":"155.133.242.9","Port":27020},{"IP":"155.133.242.8","Port":27020},{"IP":"185.25.180.14","Port":27019},{"IP":"146.66.155.8","Port":27020},{"IP":"185.25.180.15","Port":27020},{"IP":"155.133.242.8","Port":27017},{"IP":"155.133.242.9","Port":27018},{"IP":"155.133.242.8","Port":27018},{"IP":"208.78.164.13","Port":27017},{"IP":"208.78.164.12","Port":27018},{"IP":"208.78.164.10","Port":27019},{"IP":"208.78.164.10","Port":27018},{"IP":"208.78.164.13","Port":27019},{"IP":"208.78.164.12","Port":27019},{"IP":"208.78.164.14","Port":27017},{"IP":"208.78.164.13","Port":27018},{"IP":"208.78.164.14","Port":27019},{"IP":"208.78.164.12","Port":27017},{"IP":"208.78.164.14","Port":27018},{"IP":"208.78.164.10","Port":27017},{"IP":"162.254.195.44","Port":27018}]} \ No newline at end of file diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index e991d7f..d6e396d 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -7,6 +7,7 @@ import ( "log" "strings" + "time" ) const ( @@ -202,6 +203,7 @@ func AddNewUser(jid, 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 MapGatewayInfo[jid] = g