Add support chatstates notification (issue #23)
This commit is contained in:
parent
ba48518357
commit
7e2db9f58b
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -131,7 +131,11 @@ func (g *GatewayInfo) mainSteam() {
|
|||
|
||||
case *steam.ChatMsgEvent:
|
||||
// Message received
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -124,8 +125,20 @@ func (g *GatewayInfo) ReceivedXMPP_Presence(presence *xmpp.Presence) {
|
|||
|
||||
func (g *GatewayInfo) ReceivedXMPP_Message(message *xmpp.Message) {
|
||||
steamID := strings.SplitN(message.To, "@", 2)[0]
|
||||
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() {
|
||||
g.SendXmppPresence(Status_offline, Type_unavailable, "", "", "", "")
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}]}
|
||||
{"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}]}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue