First working version for multi Steam and XMPP users
This commit is contained in:
parent
11b43f35bf
commit
683574a814
|
|
@ -29,7 +29,6 @@ var (
|
||||||
db = new(sql.DB)
|
db = new(sql.DB)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
d, err := sql.Open("sqlite3", databaseFile)
|
d, err := sql.Open("sqlite3", databaseFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -47,7 +46,6 @@ func Close() {
|
||||||
db.Close()
|
db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (newLine *DatabaseLine) AddLine() bool {
|
func (newLine *DatabaseLine) AddLine() bool {
|
||||||
log.Printf("%sAdd new line %v", LogInfo, newLine)
|
log.Printf("%sAdd new line %v", LogInfo, newLine)
|
||||||
stmt, err := db.Prepare(insertDatabaseStmt)
|
stmt, err := db.Prepare(insertDatabaseStmt)
|
||||||
|
|
@ -93,7 +91,7 @@ func RemoveLine(jid string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLine(jid string) (*DatabaseLine) {
|
func GetLine(jid string) *DatabaseLine {
|
||||||
log.Printf("%sGet line %s", LogInfo, jid)
|
log.Printf("%sGet line %s", LogInfo, jid)
|
||||||
ret := new(DatabaseLine)
|
ret := new(DatabaseLine)
|
||||||
|
|
||||||
|
|
@ -111,3 +109,21 @@ func GetLine(jid string) (*DatabaseLine) {
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAllLines() []DatabaseLine {
|
||||||
|
log.Printf("%sGet all lines", LogInfo)
|
||||||
|
var ret []DatabaseLine
|
||||||
|
|
||||||
|
rows, err := db.Query("select jid, steamLogin, steamPwd from users")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%sError on select query", LogError, err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
user := new(DatabaseLine)
|
||||||
|
rows.Scan(&user.Jid, &user.SteamLogin, &user.SteamPwd)
|
||||||
|
ret = append(ret, *user)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package gateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Philipp15b/go-steam"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SentryDirectory = "sentries/"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
VersionToSend = ""
|
||||||
|
)
|
||||||
|
|
||||||
|
type GatewayInfo struct {
|
||||||
|
// Steam
|
||||||
|
SteamLogin string
|
||||||
|
SteamPassword string
|
||||||
|
SteamAuthCode string
|
||||||
|
SteamLoginInfo *steam.LogOnDetails
|
||||||
|
SteamClient *steam.Client
|
||||||
|
SentryFile string
|
||||||
|
FriendSteamId map[string]struct{}
|
||||||
|
SteamConnecting bool
|
||||||
|
|
||||||
|
// XMPP
|
||||||
|
XMPP_JID_Client string
|
||||||
|
XMPP_Out chan interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) Run() {
|
||||||
|
go g.SteamRun()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) SetSteamAuthCode(authCode string) {
|
||||||
|
g.SteamAuthCode = authCode
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,215 @@
|
||||||
|
package gateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Philipp15b/go-steam"
|
||||||
|
"github.com/Philipp15b/go-steam/internal/steamlang"
|
||||||
|
"github.com/Philipp15b/go-steam/steamid"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
// "time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
serverAddrs = "servers.addr"
|
||||||
|
|
||||||
|
State_Offline = steamlang.EPersonaState_Offline
|
||||||
|
State_Online = steamlang.EPersonaState_Online
|
||||||
|
State_Busy = steamlang.EPersonaState_Busy
|
||||||
|
State_Away = steamlang.EPersonaState_Away
|
||||||
|
State_Snooze = steamlang.EPersonaState_Snooze
|
||||||
|
State_LookingToTrade = steamlang.EPersonaState_LookingToTrade
|
||||||
|
State_LookingToPlay = steamlang.EPersonaState_LookingToPlay
|
||||||
|
State_Max = steamlang.EPersonaState_Max
|
||||||
|
|
||||||
|
LogSteamInfo = "\t[STEAM INFO]\t"
|
||||||
|
LogSteamError = "\t[STEAM ERROR]\t"
|
||||||
|
LogSteamDebug = "\t[STEAM DEBUG]\t"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (g *GatewayInfo) SteamRun() {
|
||||||
|
log.Printf("%sRunning", LogSteamInfo)
|
||||||
|
g.setLoginInfos()
|
||||||
|
g.SteamClient = steam.NewClient()
|
||||||
|
g.SteamConnecting = false
|
||||||
|
// g.SteamClient.ConnectionTimeout = 10 * time.Second
|
||||||
|
|
||||||
|
g.mainSteam()
|
||||||
|
|
||||||
|
log.Printf("%sReach main method's end", LogSteamInfo)
|
||||||
|
go g.SteamRun()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) mainSteam() {
|
||||||
|
for event := range g.SteamClient.Events() {
|
||||||
|
switch e := event.(type) {
|
||||||
|
case *steam.ConnectedEvent:
|
||||||
|
// Connected on server
|
||||||
|
g.SteamConnecting = false
|
||||||
|
log.Printf("%sConnected on Steam serveur", LogSteamDebug)
|
||||||
|
g.SteamClient.Auth.LogOn(g.SteamLoginInfo)
|
||||||
|
|
||||||
|
case *steam.MachineAuthUpdateEvent:
|
||||||
|
// Received sentry file
|
||||||
|
ioutil.WriteFile(g.SentryFile, e.Hash, 0666)
|
||||||
|
|
||||||
|
case *steam.LoggedOnEvent:
|
||||||
|
// Logged on
|
||||||
|
g.SendSteamPresence(steamlang.EPersonaState_Online)
|
||||||
|
g.SendXmppMessage(XmppJidComponent, "", "Connected on Steam network")
|
||||||
|
|
||||||
|
case steam.FatalErrorEvent:
|
||||||
|
log.Printf("%sFatalError: ", LogSteamError, e)
|
||||||
|
g.SendXmppMessage(XmppJidComponent, "", "Steam Fatal Error : "+e.Error())
|
||||||
|
g.DisconnectAllSteamFriend()
|
||||||
|
return
|
||||||
|
|
||||||
|
case error:
|
||||||
|
log.Printf("%s", LogSteamError, e)
|
||||||
|
g.SendXmppMessage(XmppJidComponent, "", "Steam Error : "+e.Error())
|
||||||
|
|
||||||
|
case *steam.ClientCMListEvent:
|
||||||
|
// Save servers addresses
|
||||||
|
b, err := json.Marshal(*e)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%sFailed to json.Marshal() servers list", LogSteamError)
|
||||||
|
} else {
|
||||||
|
ioutil.WriteFile(serverAddrs, b, 0666)
|
||||||
|
}
|
||||||
|
|
||||||
|
case *steam.PersonaStateEvent:
|
||||||
|
// Presenc received
|
||||||
|
steamId := e.FriendId.ToString()
|
||||||
|
name := e.Name
|
||||||
|
gameName := e.GameName
|
||||||
|
|
||||||
|
var status string
|
||||||
|
var tpye string
|
||||||
|
switch e.State {
|
||||||
|
case State_Offline:
|
||||||
|
status = Status_offline
|
||||||
|
tpye = Type_unavailable
|
||||||
|
case State_Online:
|
||||||
|
status = Status_online
|
||||||
|
tpye = Type_available
|
||||||
|
case State_Busy:
|
||||||
|
status = Status_do_not_disturb
|
||||||
|
tpye = Type_available
|
||||||
|
case State_Away:
|
||||||
|
status = Status_away
|
||||||
|
tpye = Type_available
|
||||||
|
case State_Snooze:
|
||||||
|
status = Status_extended_away
|
||||||
|
tpye = Type_available
|
||||||
|
}
|
||||||
|
if _, ok := g.FriendSteamId[steamId]; !ok {
|
||||||
|
// Send subscribsion
|
||||||
|
g.SendXmppPresence(status, Type_subscribe, steamId+"@"+XmppJidComponent, gameName, name)
|
||||||
|
g.FriendSteamId[steamId] = struct{}{}
|
||||||
|
}
|
||||||
|
g.SendXmppPresence(status, tpye, steamId+"@"+XmppJidComponent, gameName, name)
|
||||||
|
|
||||||
|
case *steam.ChatMsgEvent:
|
||||||
|
// Message received
|
||||||
|
g.SendXmppMessage(e.ChatterId.ToString()+"@"+XmppJidComponent, "", e.Message)
|
||||||
|
|
||||||
|
default:
|
||||||
|
log.Printf("%s", LogSteamDebug, e)
|
||||||
|
// TODO send message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) setLoginInfos() {
|
||||||
|
var sentryHash steam.SentryHash
|
||||||
|
sentryHash, err := ioutil.ReadFile(g.SentryFile)
|
||||||
|
|
||||||
|
g.SteamLoginInfo = new(steam.LogOnDetails)
|
||||||
|
g.SteamLoginInfo.Username = g.SteamLogin
|
||||||
|
g.SteamLoginInfo.Password = g.SteamPassword
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
g.SteamLoginInfo.SentryFileHash = sentryHash
|
||||||
|
log.Printf("%sAuthentification by SentryFileHash", LogSteamDebug)
|
||||||
|
} else if g.SteamAuthCode != "" {
|
||||||
|
g.SteamLoginInfo.AuthCode = g.SteamAuthCode
|
||||||
|
log.Printf("%sAuthentification by AuthCode (%s, %s, %s)", LogSteamDebug, g.SteamLoginInfo.Username, g.SteamLoginInfo.Password, g.SteamAuthCode)
|
||||||
|
} else {
|
||||||
|
log.Printf("%sFirst authentification (%s, %s)", LogSteamDebug, g.SteamLoginInfo.Username, g.SteamLoginInfo.Password)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) IsSteamConnected() bool {
|
||||||
|
return g.SteamClient.Connected()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) SteamConnect() {
|
||||||
|
if g.IsSteamConnected() {
|
||||||
|
log.Printf("%sTry to connect, but already connected", LogSteamDebug)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if g.SteamConnecting {
|
||||||
|
log.Printf("%sTry to connect, but currently connecting…", LogSteamDebug)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
g.SteamConnecting = true
|
||||||
|
b, err := ioutil.ReadFile(serverAddrs)
|
||||||
|
if err == nil {
|
||||||
|
var toList steam.ClientCMListEvent
|
||||||
|
err := json.Unmarshal(b, &toList)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%sFailed to json.Unmarshal() servers list", LogSteamError)
|
||||||
|
} else {
|
||||||
|
log.Printf("%sConnecting...", LogSteamInfo, toList.Addresses[0])
|
||||||
|
g.SteamClient.ConnectTo(toList.Addresses[0])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Printf("%sFailed to read servers list file", LogSteamError)
|
||||||
|
log.Printf("%sConnecting...", LogSteamInfo)
|
||||||
|
g.SteamClient.Connect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) SteamDisconnect() {
|
||||||
|
if !g.IsSteamConnected() {
|
||||||
|
log.Printf("%sTry to disconnect, but already disconnected", LogSteamDebug)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("%sSteam disconnect", LogSteamInfo)
|
||||||
|
|
||||||
|
g.XMPP_Disconnect()
|
||||||
|
g.DisconnectAllSteamFriend()
|
||||||
|
go g.SteamClient.Disconnect()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) DisconnectAllSteamFriend() {
|
||||||
|
for sid, _ := range g.FriendSteamId {
|
||||||
|
g.SendXmppPresence(Status_offline, Type_unavailable, sid+"@"+XmppJidComponent, "", "")
|
||||||
|
delete(g.FriendSteamId, sid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) SendSteamMessage(steamId, message string) {
|
||||||
|
if !g.IsSteamConnected() {
|
||||||
|
log.Printf("%sTry to send message, but disconnected", LogSteamDebug)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
steamIdUint64, err := strconv.ParseUint(steamId, 10, 64)
|
||||||
|
if err == nil {
|
||||||
|
g.SteamClient.Social.SendMessage(steamid.SteamId(steamIdUint64), steamlang.EChatEntryType_ChatMsg, message)
|
||||||
|
} else {
|
||||||
|
log.Printf("%sFailed to get SteamId from %s", LogSteamError, steamId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) SendSteamPresence(status steamlang.EPersonaState) {
|
||||||
|
if !g.IsSteamConnected() {
|
||||||
|
log.Printf("%sTry to send presence, but disconnected", LogSteamDebug)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
g.SteamClient.Social.SetPersonaState(status)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,154 @@
|
||||||
|
package gateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.kingpenguin.tk/chteufleur/go-xmpp.git"
|
||||||
|
"github.com/Philipp15b/go-steam/internal/steamlang"
|
||||||
|
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Status_online = ""
|
||||||
|
Status_offline = ""
|
||||||
|
Status_away = "away"
|
||||||
|
Status_chat = "chat"
|
||||||
|
Status_do_not_disturb = "dnd"
|
||||||
|
Status_extended_away = "xa"
|
||||||
|
|
||||||
|
Type_available = ""
|
||||||
|
Type_unavailable = "unavailable"
|
||||||
|
Type_subscribe = "subscribe"
|
||||||
|
Type_subscribed = "subscribed"
|
||||||
|
Type_unsubscribe = "unsubscribe"
|
||||||
|
Type_unsubscribed = "unsubscribed"
|
||||||
|
Type_probe = "probe"
|
||||||
|
Type_error = "error"
|
||||||
|
|
||||||
|
ActionConnexion = "action_xmpp_connexion"
|
||||||
|
ActionDeconnexion = "action_xmpp_deconnexion"
|
||||||
|
ActionMainMethodEnded = "action_xmpp_main_method_ended"
|
||||||
|
|
||||||
|
LogXmppInfo = "\t[XMPP INFO]\t"
|
||||||
|
LogXmppError = "\t[XMPP ERROR]\t"
|
||||||
|
LogXmppDebug = "\t[XMPP DEBUG]\t"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
XmppJidComponent = ""
|
||||||
|
)
|
||||||
|
|
||||||
|
func (g *GatewayInfo) ReceivedXMPP_Presence(presence *xmpp.Presence) {
|
||||||
|
if presence.Type == Type_probe || presence.Type == Type_error {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
transfertPresence := false
|
||||||
|
|
||||||
|
if presence.Type == Type_subscribe {
|
||||||
|
// Send presence to tell that the JID has been added to roster
|
||||||
|
g.SendXmppPresence("", Type_subscribed, presence.To, g.XMPP_JID_Client, "")
|
||||||
|
|
||||||
|
} else if presence.Type == Type_subscribed {
|
||||||
|
} else if presence.Type == Type_unsubscribe {
|
||||||
|
} else if presence.Type == Type_unsubscribed {
|
||||||
|
} else if presence.To == XmppJidComponent {
|
||||||
|
// Destination is gateway itself
|
||||||
|
if presence.Type == Type_unavailable {
|
||||||
|
// Disconnect
|
||||||
|
// TODO multi client connected management
|
||||||
|
g.XMPP_Disconnect()
|
||||||
|
go g.SteamDisconnect()
|
||||||
|
} else if presence.Type == Type_available {
|
||||||
|
// TODO multi client connected management
|
||||||
|
go g.SteamConnect()
|
||||||
|
transfertPresence = true
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Destination is Steam user
|
||||||
|
if presence.Type == Type_unavailable {
|
||||||
|
// Disconnect
|
||||||
|
// TODO multi client connected management
|
||||||
|
g.XMPP_Disconnect()
|
||||||
|
go g.SteamDisconnect()
|
||||||
|
} else if presence.Type == Type_available {
|
||||||
|
// TODO multi client connected management
|
||||||
|
go g.SteamConnect()
|
||||||
|
transfertPresence = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if transfertPresence {
|
||||||
|
// Transfert presence to Steam network
|
||||||
|
var steamStatus steamlang.EPersonaState
|
||||||
|
|
||||||
|
switch presence.Show {
|
||||||
|
case Status_online:
|
||||||
|
steamStatus = State_Online
|
||||||
|
|
||||||
|
case Status_away:
|
||||||
|
steamStatus = State_Away
|
||||||
|
|
||||||
|
case Status_chat:
|
||||||
|
steamStatus = State_Online
|
||||||
|
|
||||||
|
case Status_extended_away:
|
||||||
|
steamStatus = State_Snooze
|
||||||
|
|
||||||
|
case Status_do_not_disturb:
|
||||||
|
steamStatus = State_Busy
|
||||||
|
}
|
||||||
|
|
||||||
|
if g.IsSteamConnected() {
|
||||||
|
g.SendSteamPresence(steamStatus)
|
||||||
|
g.SendXmppPresence(presence.Show, presence.Type, "", VersionToSend, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) ReceivedXMPP_Message(message *xmpp.Message) {
|
||||||
|
steamID := strings.SplitN(message.To, "@", 2)[0]
|
||||||
|
// TODO add subject if exist to the message
|
||||||
|
g.SendSteamMessage(steamID, message.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) XMPP_Disconnect() {
|
||||||
|
g.SendXmppPresence(Status_offline, Type_unavailable, "", "", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) SendXmppPresence(status, tpye, from, message, nick string) {
|
||||||
|
p := xmpp.Presence{To: g.XMPP_JID_Client}
|
||||||
|
|
||||||
|
if status != "" {
|
||||||
|
p.Show = status
|
||||||
|
}
|
||||||
|
if tpye != "" {
|
||||||
|
p.Type = tpye
|
||||||
|
}
|
||||||
|
if message != "" {
|
||||||
|
p.Status = message
|
||||||
|
}
|
||||||
|
if nick != "" {
|
||||||
|
p.Nick = nick
|
||||||
|
}
|
||||||
|
if from == "" {
|
||||||
|
p.From = XmppJidComponent
|
||||||
|
} else {
|
||||||
|
p.From = from
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%sSend presence %v", LogXmppInfo, p)
|
||||||
|
g.XMPP_Out <- p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GatewayInfo) SendXmppMessage(from, subject, message string) {
|
||||||
|
m := xmpp.Message{To: g.XMPP_JID_Client, From: from, Body: message, Type: "chat"}
|
||||||
|
|
||||||
|
if subject != "" {
|
||||||
|
m.Subject = subject
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%sSend message %v", LogXmppInfo, m)
|
||||||
|
g.XMPP_Out <- m
|
||||||
|
}
|
||||||
181
main.go
181
main.go
|
|
@ -1,10 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/steam"
|
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/database"
|
||||||
|
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/gateway"
|
||||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/xmpp"
|
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/xmpp"
|
||||||
|
|
||||||
"github.com/Philipp15b/go-steam/internal/steamlang"
|
|
||||||
"github.com/jimlawless/cfg"
|
"github.com/jimlawless/cfg"
|
||||||
|
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -15,16 +15,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "go-xmpp4steam v0.2.1"
|
Version = "go-xmpp4steam v0.3.0"
|
||||||
configurationFilePath = "xmpp4steam.cfg"
|
configurationFilePath = "xmpp4steam.cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
mapConfig = make(map[string]string)
|
mapConfig = make(map[string]string)
|
||||||
SetSteamId = make(map[string]struct{})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
gateway.VersionToSend = Version
|
||||||
err := cfg.Load(configurationFilePath, mapConfig)
|
err := cfg.Load(configurationFilePath, mapConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to load configuration file.", err)
|
log.Fatal("Failed to load configuration file.", err)
|
||||||
|
|
@ -34,27 +34,15 @@ func init() {
|
||||||
xmpp.Addr = mapConfig["xmpp_server_address"] + ":" + mapConfig["xmpp_server_port"]
|
xmpp.Addr = mapConfig["xmpp_server_address"] + ":" + mapConfig["xmpp_server_port"]
|
||||||
xmpp.JidStr = mapConfig["xmpp_hostname"]
|
xmpp.JidStr = mapConfig["xmpp_hostname"]
|
||||||
xmpp.Secret = mapConfig["xmpp_secret"]
|
xmpp.Secret = mapConfig["xmpp_secret"]
|
||||||
xmpp.PreferedJID = mapConfig["xmpp_authorized_jid"]
|
|
||||||
xmpp.Debug = mapConfig["xmpp_debug"] == "true"
|
xmpp.Debug = mapConfig["xmpp_debug"] == "true"
|
||||||
|
gateway.XmppJidComponent = xmpp.JidStr
|
||||||
// Steam config
|
|
||||||
steam.Username = mapConfig["steam_login"]
|
|
||||||
steam.Password = mapConfig["steam_password"]
|
|
||||||
steam.AuthCode = ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go gatewayXmppSteamAction()
|
allDbUsers := database.GetAllLines()
|
||||||
go gatewaySteamXmppAction()
|
for _, dbUser := range allDbUsers {
|
||||||
|
xmpp.AddNewUser(dbUser.Jid, dbUser.SteamLogin, dbUser.SteamPwd)
|
||||||
go gatewayXmppSteamPresence()
|
}
|
||||||
go gatewayXmppSteamMessage()
|
|
||||||
go gatewayXmppSteamAuthCode()
|
|
||||||
|
|
||||||
go gatewaySteamXmppMessage()
|
|
||||||
go gatewaySteamXmppPresence()
|
|
||||||
|
|
||||||
go steam.Run()
|
|
||||||
go xmpp.Run()
|
go xmpp.Run()
|
||||||
|
|
||||||
sigchan := make(chan os.Signal, 1)
|
sigchan := make(chan os.Signal, 1)
|
||||||
|
|
@ -63,159 +51,8 @@ func main() {
|
||||||
signal.Notify(sigchan, os.Kill)
|
signal.Notify(sigchan, os.Kill)
|
||||||
<-sigchan
|
<-sigchan
|
||||||
|
|
||||||
steam.Disconnect()
|
|
||||||
xmpp.Disconnect()
|
xmpp.Disconnect()
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
log.Println("Exit main()")
|
log.Println("Exit main()")
|
||||||
}
|
}
|
||||||
|
|
||||||
// XMPP -> Steam gateways
|
|
||||||
func gatewayXmppSteamAction() {
|
|
||||||
for {
|
|
||||||
action := <-xmpp.ChanAction
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case xmpp.ActionConnexion:
|
|
||||||
if !steam.IsConnected() {
|
|
||||||
steam.Connect()
|
|
||||||
}
|
|
||||||
|
|
||||||
case xmpp.ActionDeconnexion:
|
|
||||||
if steam.IsConnected() {
|
|
||||||
steam.Disconnect()
|
|
||||||
}
|
|
||||||
|
|
||||||
case xmpp.ActionMainMethodEnded:
|
|
||||||
go xmpp.Run()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func gatewayXmppSteamPresence() {
|
|
||||||
for {
|
|
||||||
status := <-xmpp.ChanPresence
|
|
||||||
tpye := <-xmpp.ChanPresence
|
|
||||||
|
|
||||||
var steamStatus steamlang.EPersonaState
|
|
||||||
|
|
||||||
switch status {
|
|
||||||
case xmpp.Status_online:
|
|
||||||
steamStatus = steam.State_Online
|
|
||||||
|
|
||||||
case xmpp.Status_away:
|
|
||||||
steamStatus = steam.State_Away
|
|
||||||
|
|
||||||
case xmpp.Status_chat:
|
|
||||||
|
|
||||||
case xmpp.Status_extended_away:
|
|
||||||
steamStatus = steam.State_Snooze
|
|
||||||
|
|
||||||
case xmpp.Status_do_not_disturb:
|
|
||||||
steamStatus = steam.State_Busy
|
|
||||||
}
|
|
||||||
|
|
||||||
steam.SendPresence(steamStatus)
|
|
||||||
xmpp.SendPresence(status, tpye, Version)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func gatewayXmppSteamMessage() {
|
|
||||||
for {
|
|
||||||
steamId := <-xmpp.ChanMessage
|
|
||||||
message := <-xmpp.ChanMessage
|
|
||||||
|
|
||||||
steam.SendMessage(steamId, message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func gatewayXmppSteamAuthCode() {
|
|
||||||
for {
|
|
||||||
authCode := <-xmpp.ChanAuthCode
|
|
||||||
steam.AuthCode = authCode
|
|
||||||
steam.Disconnect()
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
go steam.Run()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// /XMPP -> Steam gateways
|
|
||||||
|
|
||||||
// Steam -> XMPP gateways
|
|
||||||
func gatewaySteamXmppAction() {
|
|
||||||
for {
|
|
||||||
action := <-steam.ChanAction
|
|
||||||
switch action {
|
|
||||||
case steam.ActionConnected:
|
|
||||||
xmpp.SendPresence(xmpp.CurrentStatus, xmpp.Type_available, Version)
|
|
||||||
|
|
||||||
case steam.ActionDisconnected:
|
|
||||||
xmpp.Disconnect()
|
|
||||||
disconnectAllSteamUser()
|
|
||||||
|
|
||||||
case steam.ActionFatalError:
|
|
||||||
disconnectAllSteamUser()
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
go steam.Run()
|
|
||||||
|
|
||||||
case steam.ActionMainMethodEnded:
|
|
||||||
go steam.Run()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func gatewaySteamXmppMessage() {
|
|
||||||
for {
|
|
||||||
steamId := <-steam.ChanMessage
|
|
||||||
message := <-steam.ChanMessage
|
|
||||||
xmpp.SendMessage(steamId+"@"+xmpp.JidStr, message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func gatewaySteamXmppPresence() {
|
|
||||||
for {
|
|
||||||
steamId := <-steam.ChanPresence
|
|
||||||
name := <-steam.ChanPresence
|
|
||||||
stat := <-steam.ChanPresenceSteam
|
|
||||||
gameName := <-steam.ChanPresence
|
|
||||||
|
|
||||||
var status string
|
|
||||||
var tpye string
|
|
||||||
switch stat {
|
|
||||||
case steam.State_Offline:
|
|
||||||
status = xmpp.Status_offline
|
|
||||||
tpye = xmpp.Type_unavailable
|
|
||||||
|
|
||||||
case steam.State_Online:
|
|
||||||
status = xmpp.Status_online
|
|
||||||
tpye = xmpp.Type_available
|
|
||||||
|
|
||||||
case steam.State_Busy:
|
|
||||||
status = xmpp.Status_do_not_disturb
|
|
||||||
tpye = xmpp.Type_available
|
|
||||||
|
|
||||||
case steam.State_Away:
|
|
||||||
status = xmpp.Status_away
|
|
||||||
tpye = xmpp.Type_available
|
|
||||||
|
|
||||||
case steam.State_Snooze:
|
|
||||||
status = xmpp.Status_extended_away
|
|
||||||
tpye = xmpp.Type_available
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := SetSteamId[steamId]; !ok {
|
|
||||||
xmpp.SendPresenceFrom(status, xmpp.Type_subscribe, steamId+"@"+xmpp.JidStr, gameName, name)
|
|
||||||
SetSteamId[steamId] = struct{}{}
|
|
||||||
}
|
|
||||||
xmpp.SendPresenceFrom(status, tpye, steamId+"@"+xmpp.JidStr, gameName, name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func disconnectAllSteamUser() {
|
|
||||||
for sid, _ := range SetSteamId {
|
|
||||||
xmpp.SendPresenceFrom(xmpp.Status_offline, xmpp.Type_unavailable, sid+"@"+xmpp.JidStr, "", "")
|
|
||||||
delete(SetSteamId, sid)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// /Steam -> XMPP gateways
|
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
{"Addresses":[{"IP":"162.254.197.41","Port":27020},{"IP":"146.66.152.10","Port":27017},{"IP":"146.66.152.11","Port":27020},{"IP":"146.66.152.11","Port":27019},{"IP":"146.66.152.10","Port":27018},{"IP":"162.254.197.40","Port":27018},{"IP":"146.66.152.10","Port":27019},{"IP":"146.66.152.11","Port":27018},{"IP":"146.66.152.11","Port":27017},{"IP":"146.66.152.10","Port":27020},{"IP":"162.254.197.41","Port":27019},{"IP":"162.254.197.41","Port":27017},{"IP":"162.254.197.40","Port":27019},{"IP":"162.254.197.42","Port":27021},{"IP":"162.254.197.42","Port":27017},{"IP":"162.254.197.42","Port":27019},{"IP":"162.254.197.41","Port":27021},{"IP":"162.254.197.42","Port":27018},{"IP":"162.254.197.40","Port":27021},{"IP":"162.254.197.40","Port":27017},{"IP":"162.254.197.41","Port":27018},{"IP":"162.254.197.42","Port":27020},{"IP":"162.254.197.40","Port":27020},{"IP":"162.254.196.43","Port":27017},{"IP":"162.254.196.43","Port":27020},{"IP":"162.254.196.42","Port":27020},{"IP":"162.254.196.40","Port":27021},{"IP":"162.254.196.42","Port":27021},{"IP":"162.254.196.40","Port":27020},{"IP":"162.254.196.40","Port":27019},{"IP":"162.254.196.43","Port":27018},{"IP":"162.254.196.41","Port":27020},{"IP":"162.254.196.42","Port":27017},{"IP":"185.25.182.10","Port":27020},{"IP":"162.254.196.41","Port":27017},{"IP":"162.254.196.42","Port":27019},{"IP":"162.254.196.40","Port":27018},{"IP":"162.254.196.43","Port":27019},{"IP":"162.254.196.41","Port":27019},{"IP":"162.254.196.42","Port":27018},{"IP":"162.254.196.41","Port":27021},{"IP":"162.254.196.40","Port":27017},{"IP":"146.66.155.8","Port":27017},{"IP":"185.25.182.10","Port":27019},{"IP":"155.133.242.8","Port":27019},{"IP":"185.25.182.10","Port":27018},{"IP":"146.66.155.8","Port":27018},{"IP":"185.25.180.15","Port":27019},{"IP":"155.133.242.8","Port":27020},{"IP":"185.25.180.15","Port":27018},{"IP":"155.133.242.9","Port":27020},{"IP":"162.254.196.41","Port":27018},{"IP":"146.66.155.8","Port":27019},{"IP":"155.133.242.8","Port":27018},{"IP":"146.66.155.8","Port":27020},{"IP":"185.25.182.10","Port":27017},{"IP":"155.133.242.8","Port":27017},{"IP":"185.25.180.15","Port":27017},{"IP":"185.25.180.15","Port":27020},{"IP":"155.133.242.9","Port":27017},{"IP":"185.25.180.14","Port":27020},{"IP":"155.133.242.9","Port":27018},{"IP":"162.254.196.43","Port":27021},{"IP":"155.133.242.9","Port":27019},{"IP":"185.25.180.14","Port":27017},{"IP":"185.25.180.14","Port":27018},{"IP":"185.25.180.14","Port":27019},{"IP":"208.78.164.11","Port":27017},{"IP":"208.78.164.11","Port":27019},{"IP":"208.78.164.9","Port":27019},{"IP":"208.78.164.12","Port":27019},{"IP":"208.78.164.9","Port":27017},{"IP":"208.78.164.12","Port":27018},{"IP":"208.78.164.13","Port":27019},{"IP":"208.78.164.13","Port":27018},{"IP":"208.78.164.10","Port":27017},{"IP":"208.78.164.14","Port":27018},{"IP":"208.78.164.14","Port":27019},{"IP":"208.78.164.14","Port":27017},{"IP":"208.78.164.12","Port":27017}]}
|
{"Addresses":[{"IP":"146.66.152.11","Port":27019},{"IP":"146.66.152.11","Port":27017},{"IP":"146.66.152.10","Port":27019},{"IP":"162.254.197.42","Port":27017},{"IP":"146.66.152.10","Port":27020},{"IP":"162.254.197.42","Port":27019},{"IP":"146.66.152.10","Port":27018},{"IP":"146.66.152.10","Port":27017},{"IP":"162.254.197.41","Port":27021},{"IP":"162.254.197.41","Port":27017},{"IP":"162.254.197.40","Port":27019},{"IP":"162.254.197.40","Port":27020},{"IP":"162.254.197.40","Port":27021},{"IP":"146.66.152.11","Port":27020},{"IP":"162.254.197.40","Port":27018},{"IP":"162.254.197.41","Port":27020},{"IP":"162.254.197.40","Port":27017},{"IP":"162.254.197.41","Port":27018},{"IP":"162.254.197.42","Port":27020},{"IP":"146.66.152.11","Port":27018},{"IP":"162.254.197.42","Port":27021},{"IP":"162.254.197.41","Port":27019},{"IP":"162.254.197.42","Port":27018},{"IP":"162.254.196.43","Port":27017},{"IP":"162.254.196.40","Port":27018},{"IP":"162.254.196.43","Port":27018},{"IP":"162.254.196.42","Port":27020},{"IP":"162.254.196.41","Port":27020},{"IP":"162.254.196.43","Port":27019},{"IP":"162.254.196.41","Port":27021},{"IP":"162.254.196.42","Port":27018},{"IP":"162.254.196.40","Port":27020},{"IP":"162.254.196.41","Port":27017},{"IP":"162.254.196.41","Port":27018},{"IP":"162.254.196.40","Port":27017},{"IP":"162.254.196.41","Port":27019},{"IP":"162.254.196.42","Port":27021},{"IP":"162.254.196.40","Port":27021},{"IP":"162.254.196.43","Port":27021},{"IP":"162.254.196.42","Port":27017},{"IP":"162.254.196.40","Port":27019},{"IP":"162.254.196.42","Port":27019},{"IP":"162.254.196.43","Port":27020},{"IP":"146.66.155.8","Port":27017},{"IP":"146.66.155.8","Port":27019},{"IP":"185.25.182.10","Port":27020},{"IP":"146.66.155.8","Port":27020},{"IP":"185.25.182.10","Port":27017},{"IP":"185.25.180.15","Port":27019},{"IP":"185.25.180.15","Port":27018},{"IP":"155.133.242.8","Port":27019},{"IP":"155.133.242.8","Port":27020},{"IP":"185.25.180.15","Port":27020},{"IP":"155.133.242.9","Port":27019},{"IP":"155.133.242.8","Port":27018},{"IP":"146.66.155.8","Port":27018},{"IP":"185.25.182.10","Port":27019},{"IP":"185.25.180.14","Port":27018},{"IP":"155.133.242.9","Port":27020},{"IP":"185.25.180.14","Port":27020},{"IP":"185.25.180.15","Port":27017},{"IP":"185.25.180.14","Port":27017},{"IP":"185.25.180.14","Port":27019},{"IP":"155.133.242.8","Port":27017},{"IP":"155.133.242.9","Port":27017},{"IP":"185.25.182.10","Port":27018},{"IP":"155.133.242.9","Port":27018},{"IP":"208.78.164.10","Port":27019},{"IP":"208.78.164.14","Port":27017},{"IP":"208.78.164.11","Port":27018},{"IP":"208.78.164.10","Port":27018},{"IP":"208.78.164.11","Port":27019},{"IP":"208.78.164.12","Port":27019},{"IP":"208.78.164.13","Port":27019},{"IP":"208.78.164.10","Port":27017},{"IP":"208.78.164.9","Port":27017},{"IP":"208.78.164.14","Port":27019},{"IP":"208.78.164.11","Port":27017},{"IP":"208.78.164.9","Port":27018},{"IP":"208.78.164.9","Port":27019}]}
|
||||||
168
steam/steam.go
168
steam/steam.go
|
|
@ -1,168 +0,0 @@
|
||||||
package steam
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/Philipp15b/go-steam"
|
|
||||||
"github.com/Philipp15b/go-steam/internal/steamlang"
|
|
||||||
"github.com/Philipp15b/go-steam/steamid"
|
|
||||||
|
|
||||||
"encoding/json"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
sentryFile = "sentry"
|
|
||||||
serverAddrs = "servers.addr"
|
|
||||||
|
|
||||||
State_Offline = steamlang.EPersonaState_Offline
|
|
||||||
State_Online = steamlang.EPersonaState_Online
|
|
||||||
State_Busy = steamlang.EPersonaState_Busy
|
|
||||||
State_Away = steamlang.EPersonaState_Away
|
|
||||||
State_Snooze = steamlang.EPersonaState_Snooze
|
|
||||||
State_LookingToTrade = steamlang.EPersonaState_LookingToTrade
|
|
||||||
State_LookingToPlay = steamlang.EPersonaState_LookingToPlay
|
|
||||||
State_Max = steamlang.EPersonaState_Max
|
|
||||||
|
|
||||||
ActionConnected = "steam_connected"
|
|
||||||
ActionDisconnected = "steam_disconnected"
|
|
||||||
ActionFatalError = "steam_fatal_error"
|
|
||||||
ActionMainMethodEnded = "action_steam_main_method_ended"
|
|
||||||
|
|
||||||
LogInfo = "\t[STEAM INFO]\t"
|
|
||||||
LogError = "\t[STEAM ERROR]\t"
|
|
||||||
LogDebug = "\t[STEAM DEBUG]\t"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
Username = ""
|
|
||||||
Password = ""
|
|
||||||
AuthCode = ""
|
|
||||||
|
|
||||||
myLoginInfo = new(steam.LogOnDetails)
|
|
||||||
client = steam.NewClient()
|
|
||||||
|
|
||||||
ChanPresence = make(chan string)
|
|
||||||
ChanPresenceSteam = make(chan steamlang.EPersonaState)
|
|
||||||
ChanMessage = make(chan string)
|
|
||||||
ChanAction = make(chan string)
|
|
||||||
)
|
|
||||||
|
|
||||||
func Run() {
|
|
||||||
log.Printf("%sRunning", LogInfo)
|
|
||||||
setLoginInfos()
|
|
||||||
client = steam.NewClient()
|
|
||||||
client.ConnectionTimeout = 10 * time.Second
|
|
||||||
|
|
||||||
mainSteam()
|
|
||||||
ChanAction <- ActionMainMethodEnded
|
|
||||||
}
|
|
||||||
|
|
||||||
func mainSteam() {
|
|
||||||
for event := range client.Events() {
|
|
||||||
switch e := event.(type) {
|
|
||||||
case *steam.ConnectedEvent:
|
|
||||||
client.Auth.LogOn(myLoginInfo)
|
|
||||||
|
|
||||||
case *steam.MachineAuthUpdateEvent:
|
|
||||||
ioutil.WriteFile(sentryFile, e.Hash, 0666)
|
|
||||||
|
|
||||||
case *steam.LoggedOnEvent:
|
|
||||||
SendPresence(steamlang.EPersonaState_Online)
|
|
||||||
ChanAction <- ActionConnected
|
|
||||||
|
|
||||||
case steam.FatalErrorEvent:
|
|
||||||
log.Printf("%sFatalError: ", LogError, e)
|
|
||||||
ChanAction <- ActionFatalError
|
|
||||||
return
|
|
||||||
|
|
||||||
case error:
|
|
||||||
log.Printf("%s", LogError, e)
|
|
||||||
|
|
||||||
case *steam.ClientCMListEvent:
|
|
||||||
// Save servers addresses
|
|
||||||
b, err := json.Marshal(*e)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("%sFailed to json.Marshal() servers list", LogError)
|
|
||||||
} else {
|
|
||||||
ioutil.WriteFile(serverAddrs, b, 0666)
|
|
||||||
}
|
|
||||||
|
|
||||||
case *steam.PersonaStateEvent:
|
|
||||||
ChanPresence <- e.FriendId.ToString()
|
|
||||||
ChanPresence <- e.Name
|
|
||||||
ChanPresenceSteam <- e.State
|
|
||||||
ChanPresence <- e.GameName
|
|
||||||
|
|
||||||
case *steam.ChatMsgEvent:
|
|
||||||
ChanMessage <- e.ChatterId.ToString()
|
|
||||||
ChanMessage <- e.Message
|
|
||||||
|
|
||||||
default:
|
|
||||||
log.Printf("%s", LogDebug, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func setLoginInfos() {
|
|
||||||
var sentryHash steam.SentryHash
|
|
||||||
sentryHash, err := ioutil.ReadFile(sentryFile)
|
|
||||||
|
|
||||||
myLoginInfo.Username = Username
|
|
||||||
myLoginInfo.Password = Password
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
myLoginInfo.SentryFileHash = sentryHash
|
|
||||||
log.Printf("%sAuthentification by SentryFileHash", LogDebug)
|
|
||||||
} else if AuthCode != "" {
|
|
||||||
myLoginInfo.AuthCode = AuthCode
|
|
||||||
log.Printf("%sAuthentification by AuthCode (%s)", LogDebug, AuthCode)
|
|
||||||
} else {
|
|
||||||
log.Printf("%sFirst authentification", LogDebug)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsConnected() bool {
|
|
||||||
return client.Connected()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Connect() {
|
|
||||||
if IsConnected() {
|
|
||||||
log.Printf("%sTry to connect, but already connected", LogDebug)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := ioutil.ReadFile(serverAddrs)
|
|
||||||
if err == nil {
|
|
||||||
var toList steam.ClientCMListEvent
|
|
||||||
err := json.Unmarshal(b, &toList)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("%sFailed to json.Unmarshal() servers list", LogError)
|
|
||||||
} else {
|
|
||||||
log.Printf("%sConnecting...", LogInfo)
|
|
||||||
client.ConnectTo(toList.Addresses[0])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.Printf("%sFailed to read servers list file", LogError)
|
|
||||||
client.Connect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Disconnect() {
|
|
||||||
log.Printf("%sSteam disconnect", LogInfo)
|
|
||||||
go client.Disconnect()
|
|
||||||
}
|
|
||||||
|
|
||||||
func SendMessage(steamId, message string) {
|
|
||||||
steamIdUint64, err := strconv.ParseUint(steamId, 10, 64)
|
|
||||||
if err == nil {
|
|
||||||
client.Social.SendMessage(steamid.SteamId(steamIdUint64), steamlang.EChatEntryType_ChatMsg, message)
|
|
||||||
} else {
|
|
||||||
log.Printf("%sFailed to get SteamId from %s", LogError, steamId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func SendPresence(status steamlang.EPersonaState) {
|
|
||||||
client.Social.SetPersonaState(status)
|
|
||||||
}
|
|
||||||
|
|
@ -2,12 +2,15 @@ package xmpp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.kingpenguin.tk/chteufleur/go-xmpp.git"
|
"git.kingpenguin.tk/chteufleur/go-xmpp.git"
|
||||||
|
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/database"
|
||||||
|
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CommandAuthcode = "steamAuthCodeCommand"
|
CommandAuthcode = "steamAuthCodeCommand"
|
||||||
|
CommandGetIdentifiants = "steamGetIdentifiants"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -20,7 +23,9 @@ func execDiscoCommand(iq *xmpp.Iq) {
|
||||||
discoItem := &xmpp.DiscoItems{Node: xmpp.NodeAdHocCommand}
|
discoItem := &xmpp.DiscoItems{Node: xmpp.NodeAdHocCommand}
|
||||||
|
|
||||||
// Add available commands
|
// Add available commands
|
||||||
discoI := &xmpp.DiscoItem{JID: jid.Domain, Node: CommandAuthcode, Name: "Add Auth Code"}
|
discoI := &xmpp.DiscoItem{JID: jid.Domain, Node: CommandAuthcode, Name: "Add Steam Auth Code"}
|
||||||
|
discoItem.Item = append(discoItem.Item, *discoI)
|
||||||
|
discoI = &xmpp.DiscoItem{JID: jid.Domain, Node: CommandGetIdentifiants, Name: "Steam registration"}
|
||||||
discoItem.Item = append(discoItem.Item, *discoI)
|
discoItem.Item = append(discoItem.Item, *discoI)
|
||||||
|
|
||||||
reply.PayloadEncode(discoItem)
|
reply.PayloadEncode(discoItem)
|
||||||
|
|
@ -45,6 +50,16 @@ func execCommandAdHoc(iq *xmpp.Iq) {
|
||||||
cmdXForm.Fields = append(cmdXForm.Fields, *field)
|
cmdXForm.Fields = append(cmdXForm.Fields, *field)
|
||||||
cmd.XForm = *cmdXForm
|
cmd.XForm = *cmdXForm
|
||||||
|
|
||||||
|
} else if adHoc.Node == CommandGetIdentifiants {
|
||||||
|
// Command Auth Code
|
||||||
|
cmdXForm := &xmpp.AdHocXForm{Type: xmpp.TypeAdHocForm, Title: "Steam Account Info", Instructions: "Please provide your Steam login and password."}
|
||||||
|
|
||||||
|
field := &xmpp.AdHocField{Var: "login", Label: "Steam Login", Type: xmpp.TypeAdHocFieldTextSingle}
|
||||||
|
cmdXForm.Fields = append(cmdXForm.Fields, *field)
|
||||||
|
field = &xmpp.AdHocField{Var: "password", Label: "Steam Password", Type: xmpp.TypeAdHocFieldTextSingle}
|
||||||
|
cmdXForm.Fields = append(cmdXForm.Fields, *field)
|
||||||
|
|
||||||
|
cmd.XForm = *cmdXForm
|
||||||
}
|
}
|
||||||
reply.PayloadEncode(cmd)
|
reply.PayloadEncode(cmd)
|
||||||
comp.Out <- reply
|
comp.Out <- reply
|
||||||
|
|
@ -70,11 +85,54 @@ func execCommandAdHoc(iq *xmpp.Iq) {
|
||||||
}
|
}
|
||||||
if authCode != "" {
|
if authCode != "" {
|
||||||
// Succeded
|
// Succeded
|
||||||
ChanAuthCode <- authCode
|
jidBare := strings.SplitN(iq.From, "/", 2)[0]
|
||||||
note.Value = "Commande effectuée avec succes !"
|
g := MapGatewayInfo[jidBare]
|
||||||
|
if g != nil {
|
||||||
|
g.SetSteamAuthCode(authCode)
|
||||||
|
note.Value = "Command succeded !"
|
||||||
|
} else {
|
||||||
|
note.Value = "Your are not registred. Please, register before sending Steam auth code."
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Failed
|
// Failed
|
||||||
note.Value = "Une erreur c'est produite à l'exécution de la commande…"
|
note.Value = "Error append while executing command"
|
||||||
|
}
|
||||||
|
cmd.Note = *note
|
||||||
|
|
||||||
|
} else if adHoc.Node == CommandGetIdentifiants {
|
||||||
|
cmdXForm := &xmpp.AdHocXForm{Type: xmpp.TypeAdHocResult, Title: "Steam Account Info"}
|
||||||
|
cmd.XForm = *cmdXForm
|
||||||
|
note := &xmpp.AdHocNote{Type: xmpp.TypeAdHocNoteInfo}
|
||||||
|
|
||||||
|
// Command Auth Code
|
||||||
|
steamLogin := ""
|
||||||
|
steamPwd := ""
|
||||||
|
fields := adHoc.XForm.Fields
|
||||||
|
for _, field := range fields {
|
||||||
|
if field.Var == "login" {
|
||||||
|
steamLogin = field.Value
|
||||||
|
} else if field.Var == "password" {
|
||||||
|
steamPwd = field.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if steamLogin != "" && steamPwd != "" {
|
||||||
|
// Succeded
|
||||||
|
jidBare := strings.SplitN(iq.From, "/", 2)[0]
|
||||||
|
dbUser := new(database.DatabaseLine)
|
||||||
|
dbUser.Jid = jidBare
|
||||||
|
dbUser.SteamLogin = steamLogin
|
||||||
|
dbUser.SteamPwd = steamPwd
|
||||||
|
|
||||||
|
// TODO update
|
||||||
|
if dbUser.AddLine() {
|
||||||
|
AddNewUser(dbUser.Jid, dbUser.SteamLogin, dbUser.SteamPwd)
|
||||||
|
note.Value = "Command succeded !"
|
||||||
|
} else {
|
||||||
|
note.Value = "Error append while executing command"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Failed
|
||||||
|
note.Value = "Failed because Steam login or Steam password is empty."
|
||||||
}
|
}
|
||||||
cmd.Note = *note
|
cmd.Note = *note
|
||||||
}
|
}
|
||||||
|
|
|
||||||
133
xmpp/xmpp.go
133
xmpp/xmpp.go
|
|
@ -2,35 +2,20 @@ package xmpp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.kingpenguin.tk/chteufleur/go-xmpp.git"
|
"git.kingpenguin.tk/chteufleur/go-xmpp.git"
|
||||||
|
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/gateway"
|
||||||
|
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Status_online = ""
|
|
||||||
Status_offline = ""
|
|
||||||
Status_away = "away"
|
|
||||||
Status_chat = "chat"
|
|
||||||
Status_do_not_disturb = "dnd"
|
|
||||||
Status_extended_away = "xa"
|
|
||||||
|
|
||||||
Type_available = ""
|
|
||||||
Type_unavailable = "unavailable"
|
|
||||||
Type_subscribe = "subscribe"
|
|
||||||
Type_subscribed = "subscribed"
|
|
||||||
Type_unsubscribe = "unsubscribe"
|
|
||||||
Type_unsubscribed = "unsubscribed"
|
|
||||||
Type_probe = "probe"
|
|
||||||
Type_error = "error"
|
|
||||||
|
|
||||||
ActionConnexion = "action_xmpp_connexion"
|
ActionConnexion = "action_xmpp_connexion"
|
||||||
ActionDeconnexion = "action_xmpp_deconnexion"
|
ActionDeconnexion = "action_xmpp_deconnexion"
|
||||||
ActionMainMethodEnded = "action_xmpp_main_method_ended"
|
ActionMainMethodEnded = "action_xmpp_main_method_ended"
|
||||||
|
|
||||||
LogInfo = "\t[XMPP INFO]\t"
|
LogInfo = "\t[XMPP COMPONENT INFO]\t"
|
||||||
LogError = "\t[XMPP ERROR]\t"
|
LogError = "\t[XMPP COMPONENT ERROR]\t"
|
||||||
LogDebug = "\t[XMPP DEBUG]\t"
|
LogDebug = "\t[XMPP COMPONENT DEBUG]\t"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -38,21 +23,15 @@ var (
|
||||||
JidStr = ""
|
JidStr = ""
|
||||||
Secret = ""
|
Secret = ""
|
||||||
|
|
||||||
PreferedJID = ""
|
|
||||||
|
|
||||||
jid xmpp.JID
|
jid xmpp.JID
|
||||||
stream = new(xmpp.Stream)
|
stream = new(xmpp.Stream)
|
||||||
comp = new(xmpp.XMPP)
|
comp = new(xmpp.XMPP)
|
||||||
|
|
||||||
ChanPresence = make(chan string)
|
|
||||||
ChanMessage = make(chan string)
|
|
||||||
ChanAction = make(chan string)
|
ChanAction = make(chan string)
|
||||||
|
|
||||||
CurrentStatus = Status_offline
|
|
||||||
|
|
||||||
setJIDconnected = make(map[string]bool)
|
|
||||||
|
|
||||||
Debug = true
|
Debug = true
|
||||||
|
|
||||||
|
MapGatewayInfo = make(map[string]*gateway.GatewayInfo)
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
|
|
@ -67,35 +46,34 @@ func Run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func mainXMPP() {
|
func mainXMPP() {
|
||||||
|
// Define xmpp out for all users
|
||||||
|
for _, u := range MapGatewayInfo {
|
||||||
|
u.XMPP_Out = comp.Out
|
||||||
|
}
|
||||||
|
|
||||||
for x := range comp.In {
|
for x := range comp.In {
|
||||||
switch v := x.(type) {
|
switch v := x.(type) {
|
||||||
case *xmpp.Presence:
|
case *xmpp.Presence:
|
||||||
if strings.SplitN(v.From, "/", 2)[0] == PreferedJID && v.Type != Type_probe {
|
jidBare := strings.SplitN(v.From, "/", 2)[0]
|
||||||
if v.Type == Type_unavailable && v.To == JidStr {
|
g := MapGatewayInfo[jidBare]
|
||||||
delete(setJIDconnected, v.From)
|
if g != nil {
|
||||||
log.Printf("%sPresence reçut unavailable", LogDebug)
|
log.Printf("%sPresence transfered to %s", LogDebug, jidBare)
|
||||||
if len(setJIDconnected) <= 0 {
|
g.ReceivedXMPP_Presence(v)
|
||||||
// Disconnect only when all JID are disconnected
|
} else {
|
||||||
Disconnect()
|
if v.Type != gateway.Type_error && v.Type != gateway.Type_probe {
|
||||||
ChanAction <- ActionDeconnexion
|
SendPresence(gateway.Status_offline, gateway.Type_unavailable, jid.Domain, v.From, "Your are not registred", "")
|
||||||
}
|
}
|
||||||
} else if v.Type == Type_subscribe {
|
|
||||||
SendPresenceFrom("", Type_subscribed, JidStr, "", "")
|
|
||||||
} else if v.Type != Type_subscribed { // Type subscribed is send by JID without ressourse
|
|
||||||
log.Printf("%sAdd connected user. JID : %s", LogInfo, v.From)
|
|
||||||
setJIDconnected[v.From] = true
|
|
||||||
CurrentStatus = v.Show
|
|
||||||
ChanAction <- ActionConnexion
|
|
||||||
}
|
|
||||||
|
|
||||||
ChanPresence <- v.Show
|
|
||||||
ChanPresence <- v.Type
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case *xmpp.Message:
|
case *xmpp.Message:
|
||||||
steamID := strings.SplitN(v.To, "@", 2)[0]
|
jidBare := strings.SplitN(v.From, "/", 2)[0]
|
||||||
ChanMessage <- steamID
|
g := MapGatewayInfo[jidBare]
|
||||||
ChanMessage <- v.Body
|
if g != nil {
|
||||||
|
log.Printf("%sMessage transfered to %s", LogDebug, jidBare)
|
||||||
|
g.ReceivedXMPP_Message(v)
|
||||||
|
} else {
|
||||||
|
SendMessage(v.From, "", "Your are not registred. If you want to register, please, send an Ad-Hoc command.")
|
||||||
|
}
|
||||||
|
|
||||||
case *xmpp.Iq:
|
case *xmpp.Iq:
|
||||||
switch v.PayloadName().Space {
|
switch v.PayloadName().Space {
|
||||||
|
|
@ -112,7 +90,7 @@ func mainXMPP() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send deconnexion
|
// Send deconnexion
|
||||||
SendPresence(Status_offline, Type_unavailable, "")
|
SendPresence(gateway.Status_offline, gateway.Type_unavailable, "", "", "", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func must(v interface{}, err error) interface{} {
|
func must(v interface{}, err error) interface{} {
|
||||||
|
|
@ -124,26 +102,11 @@ func must(v interface{}, err error) interface{} {
|
||||||
|
|
||||||
func Disconnect() {
|
func Disconnect() {
|
||||||
log.Printf("%sXMPP disconnect", LogInfo)
|
log.Printf("%sXMPP disconnect", LogInfo)
|
||||||
SendPresence(Status_offline, Type_unavailable, "")
|
SendPresence(gateway.Status_offline, gateway.Type_unavailable, "", "", "", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendPresence(status, tpye, message string) {
|
func SendPresence(status, tpye, from, to, message, nick string) {
|
||||||
comp.Out <- xmpp.Presence{To: PreferedJID, From: jid.Domain, Show: status, Type: tpye, Status: message}
|
p := xmpp.Presence{}
|
||||||
}
|
|
||||||
|
|
||||||
func SendPresenceFrom(status, tpye, from, message, nick string) {
|
|
||||||
/*
|
|
||||||
if message == "" {
|
|
||||||
comp.Out <- xmpp.Presence{To: PreferedJID, From: from, Show: status, Type: tpye, Nick: nick}
|
|
||||||
} else if nick == "" {
|
|
||||||
comp.Out <- xmpp.Presence{To: PreferedJID, From: from, Show: status, Type: tpye, Status: message}
|
|
||||||
} else if nick == "" {
|
|
||||||
comp.Out <- xmpp.Presence{To: PreferedJID, From: from, Show: status, Type: tpye}
|
|
||||||
} else {
|
|
||||||
comp.Out <- xmpp.Presence{To: PreferedJID, From: from, Show: status, Type: tpye, Status: message, Nick: nick}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
p := xmpp.Presence{To: PreferedJID, From: from}
|
|
||||||
|
|
||||||
if status != "" {
|
if status != "" {
|
||||||
p.Show = status
|
p.Show = status
|
||||||
|
|
@ -157,10 +120,40 @@ func SendPresenceFrom(status, tpye, from, message, nick string) {
|
||||||
if nick != "" {
|
if nick != "" {
|
||||||
p.Nick = nick
|
p.Nick = nick
|
||||||
}
|
}
|
||||||
|
if from == "" {
|
||||||
|
p.From = jid.Domain
|
||||||
|
} else {
|
||||||
|
p.From = from
|
||||||
|
}
|
||||||
|
if to != "" {
|
||||||
|
p.To = to
|
||||||
|
}
|
||||||
|
|
||||||
comp.Out <- p
|
comp.Out <- p
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendMessage(from, message string) {
|
func SendMessage(to, subject, message string) {
|
||||||
comp.Out <- xmpp.Message{To: PreferedJID, From: from, Body: message}
|
m := xmpp.Message{From: jid.Domain, To: to, Body: message, Type: "chat"}
|
||||||
|
|
||||||
|
if subject != "" {
|
||||||
|
m.Subject = subject
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%sSenp message %v", LogInfo, m)
|
||||||
|
comp.Out <- m
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddNewUser(jid, steamLogin, steamPwd string) {
|
||||||
|
log.Printf("%sAdd user %s to the map", LogInfo, jid)
|
||||||
|
|
||||||
|
// TODO Move Gateway creation into right package
|
||||||
|
g := new(gateway.GatewayInfo)
|
||||||
|
g.SteamLogin = steamLogin
|
||||||
|
g.SteamPassword = steamPwd
|
||||||
|
g.XMPP_JID_Client = jid
|
||||||
|
g.SentryFile = gateway.SentryDirectory + jid
|
||||||
|
g.FriendSteamId = make(map[string]struct{})
|
||||||
|
|
||||||
|
MapGatewayInfo[jid] = g
|
||||||
|
go g.Run()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,4 @@ xmpp_server_address=192.168.1.2
|
||||||
xmpp_server_port=5347
|
xmpp_server_port=5347
|
||||||
xmpp_hostname=xmppsteam.kingpenguin.tk
|
xmpp_hostname=xmppsteam.kingpenguin.tk
|
||||||
xmpp_secret=xmpp4steam_password
|
xmpp_secret=xmpp4steam_password
|
||||||
xmpp_authorized_jid=chteufleur@kingpenguin.tk
|
|
||||||
xmpp_debug=true
|
xmpp_debug=true
|
||||||
|
|
||||||
# Steam informations
|
|
||||||
steam_login=toto
|
|
||||||
steam_password=toto_password123$
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue