go-xmpp4steam/gateway/gateway.go

140 lines
3.1 KiB
Go

package gateway
import (
"sync"
"github.com/Philipp15b/go-steam"
)
const (
resource = "go-xmpp4steam"
)
var (
SentryDirectory = "sentries/"
XmppGroupUser = "Steam"
RemoteRosterRequestPermission = "remote-roster-request-permission"
RemoteRosterRequestRoster = "remote-roster-request-roster"
)
type GatewayInfo struct {
// Steam
SteamLogin string
SteamPassword string
SteamLoginInfo *steam.LogOnDetails
SteamClient *steam.Client
SentryFile string
friendSteamId *FriendSteam
SteamConnecting bool
Deleting bool
// XMPP
XMPP_JID_Client string
XMPP_Out chan interface{}
xmpp_Connected_Client *XmppConnectedClient
DebugMessage bool
XMPP_IQ_RemoteRoster_Request map[string]string
AllowEditRoster bool
ChatstateNotificationData chan string
}
type FriendSteam struct {
steamId map[string]*StatusSteamFriend
sync.RWMutex
}
type XmppConnectedClient struct {
client map[string]bool
sync.RWMutex
}
type StatusSteamFriend struct {
XMPP_Status string
XMPP_Type string
SteamGameName string
SteamName string
}
func (g *GatewayInfo) Run() {
go g.SteamRun()
go g.chatstatesNotification()
}
func (g *GatewayInfo) SetSteamAuthCode(authCode string) {
g.SteamLoginInfo.AuthCode = authCode
}
func (g *GatewayInfo) Disconnect() {
go g.XMPP_Disconnect()
go g.SteamDisconnect()
g.SteamConnecting = false
}
func (g *GatewayInfo) Delete() {
g.Deleting = true
if g.AllowEditRoster {
g.removeAllUserFromRoster()
}
g.Disconnect()
}
func (s *GatewayInfo) CreateSteamIds() {
s.friendSteamId = &FriendSteam{steamId: make(map[string]*StatusSteamFriend)}
}
func (s *GatewayInfo) GetFriendSteamId(steamId string) *StatusSteamFriend {
s.friendSteamId.RLock()
defer s.friendSteamId.RUnlock()
return s.friendSteamId.steamId[steamId]
}
func (s *GatewayInfo) GetAllFriendSteamId() []string {
s.friendSteamId.RLock()
defer s.friendSteamId.RUnlock()
allSteamIds := make([]string, len(s.friendSteamId.steamId))
i := 0
for steamId := range s.friendSteamId.steamId {
allSteamIds[i] = steamId
i++
}
return allSteamIds
}
func (s *GatewayInfo) SetFriendSteamId(steamId string, status *StatusSteamFriend) {
s.friendSteamId.Lock()
s.friendSteamId.steamId[steamId] = status
s.friendSteamId.Unlock()
}
func (s *GatewayInfo) RemoveFriendSteamId(steamId string) {
s.friendSteamId.Lock()
delete(s.friendSteamId.steamId, steamId)
s.friendSteamId.Unlock()
}
func (s *GatewayInfo) CreateXmppConnectedClient() {
s.xmpp_Connected_Client = &XmppConnectedClient{client: make(map[string]bool)}
}
func (s *GatewayInfo) SetXmppConnectedClient(jid string) {
s.xmpp_Connected_Client.Lock()
s.xmpp_Connected_Client.client[jid] = true
s.xmpp_Connected_Client.Unlock()
}
func (s *GatewayInfo) RemoveXmppConnectedClient(jid string) {
s.xmpp_Connected_Client.Lock()
delete(s.xmpp_Connected_Client.client, jid)
s.xmpp_Connected_Client.Unlock()
}
func (s *GatewayInfo) GetLenXmppConnectedClient() int {
s.xmpp_Connected_Client.RLock()
defer s.xmpp_Connected_Client.RUnlock()
return len(s.xmpp_Connected_Client.client)
}