1
0
Fork 0
This commit is contained in:
Chteufleur 2015-10-19 21:42:01 +02:00
parent 0a610cf8c8
commit bc9eee01f3
3 changed files with 232 additions and 245 deletions

243
main.go
View File

@ -1,194 +1,189 @@
package main package main
import ( import (
"go-xmpp4steam/steam" "go-xmpp4steam/steam"
"go-xmpp4steam/xmpp" "go-xmpp4steam/xmpp"
"github.com/jimlawless/cfg" "github.com/Philipp15b/go-steam/internal/steamlang"
"github.com/Philipp15b/go-steam/internal/steamlang" "github.com/jimlawless/cfg"
"bufio" "bufio"
"log" "log"
"os" "os"
"strings" "strings"
"time" "time"
) )
const ( const (
configurationFilePath = "xmpp4steam.cfg" configurationFilePath = "xmpp4steam.cfg"
) )
var ( var (
mapConfig = make(map[string]string) mapConfig = make(map[string]string)
SetSteamId = make(map[string]struct{}) SetSteamId = make(map[string]struct{})
) )
func init() { func init() {
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)
} }
// XMPP config // XMPP config
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.PreferedJID = mapConfig["xmpp_authorized_jid"]
// Steam config // Steam config
steam.Username = mapConfig["steam_login"] steam.Username = mapConfig["steam_login"]
steam.Password = mapConfig["steam_password"] steam.Password = mapConfig["steam_password"]
steam.AuthCode = mapConfig["steam_auth_code"] steam.AuthCode = mapConfig["steam_auth_code"]
xmpp.Version = "0.1.0" xmpp.Version = "0.1.0"
} }
func main() { func main() {
go gatewayXmppSteamAction() go gatewayXmppSteamAction()
go gatewaySteamXmppAction() go gatewaySteamXmppAction()
go gatewayXmppSteamPresence() go gatewayXmppSteamPresence()
go gatewayXmppSteamMessage() go gatewayXmppSteamMessage()
go gatewaySteamXmppMessage() go gatewaySteamXmppMessage()
go gatewaySteamXmppPresence() go gatewaySteamXmppPresence()
go steam.Run() go steam.Run()
xmpp.Run() xmpp.Run()
// inputStop() // inputStop()
steam.Disconnect() steam.Disconnect()
xmpp.Disconnect() xmpp.Disconnect()
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }
// XMPP -> Steam gateways // XMPP -> Steam gateways
func gatewayXmppSteamAction() { func gatewayXmppSteamAction() {
for { for {
action := <- xmpp.ChanAction action := <-xmpp.ChanAction
switch action { switch action {
case xmpp.ActionConnexion: case xmpp.ActionConnexion:
if !steam.IsConnected() { if !steam.IsConnected() {
steam.Connect() steam.Connect()
} }
case xmpp.ActionDeconnexion: case xmpp.ActionDeconnexion:
if steam.IsConnected() { if steam.IsConnected() {
steam.Disconnect() steam.Disconnect()
} }
} }
} }
} }
func gatewayXmppSteamPresence() { func gatewayXmppSteamPresence() {
for { for {
status := <- xmpp.ChanPresence status := <-xmpp.ChanPresence
var steamStatus steamlang.EPersonaState var steamStatus steamlang.EPersonaState
switch status { switch status {
case xmpp.Status_online: case xmpp.Status_online:
steamStatus = steam.State_Online steamStatus = steam.State_Online
case xmpp.Status_away: case xmpp.Status_away:
steamStatus = steam.State_Away steamStatus = steam.State_Away
case xmpp.Status_chat: case xmpp.Status_chat:
case xmpp.Status_extended_away: case xmpp.Status_extended_away:
steamStatus = steam.State_Snooze steamStatus = steam.State_Snooze
case xmpp.Status_do_not_disturb: case xmpp.Status_do_not_disturb:
steamStatus = steam.State_Busy steamStatus = steam.State_Busy
} }
steam.SendPresence(steamStatus) steam.SendPresence(steamStatus)
} }
} }
func gatewayXmppSteamMessage() { func gatewayXmppSteamMessage() {
for { for {
steamId := <- xmpp.ChanMessage steamId := <-xmpp.ChanMessage
message := <- xmpp.ChanMessage message := <-xmpp.ChanMessage
steam.SendMessage(steamId, message) steam.SendMessage(steamId, message)
} }
} }
// /XMPP -> Steam gateways // /XMPP -> Steam gateways
// Steam -> XMPP gateways // Steam -> XMPP gateways
func gatewaySteamXmppAction() { func gatewaySteamXmppAction() {
for { for {
action := <- steam.ChanAction action := <-steam.ChanAction
switch action { switch action {
case steam.ActionConnected: case steam.ActionConnected:
xmpp.SendPresence(xmpp.CurrentStatus, xmpp.Type_available) xmpp.SendPresence(xmpp.CurrentStatus, xmpp.Type_available)
case steam.ActionDisconnected: case steam.ActionDisconnected:
xmpp.Disconnect() xmpp.Disconnect()
for sid, _ := range SetSteamId { for sid, _ := range SetSteamId {
xmpp.SendPresenceFrom(xmpp.Status_offline, xmpp.Type_unavailable, sid+"@"+xmpp.JidStr) xmpp.SendPresenceFrom(xmpp.Status_offline, xmpp.Type_unavailable, sid+"@"+xmpp.JidStr)
delete(SetSteamId, sid) delete(SetSteamId, sid)
} }
} }
} }
} }
func gatewaySteamXmppMessage() { func gatewaySteamXmppMessage() {
for { for {
steamId := <- steam.ChanMessage steamId := <-steam.ChanMessage
message := <- steam.ChanMessage message := <-steam.ChanMessage
xmpp.SendMessage(steamId+"@"+xmpp.JidStr, message) xmpp.SendMessage(steamId+"@"+xmpp.JidStr, message)
} }
} }
func gatewaySteamXmppPresence() { func gatewaySteamXmppPresence() {
for { for {
// name := steam.ChanPresence // name := steam.ChanPresence
steamId := <- steam.ChanPresence steamId := <-steam.ChanPresence
stat := <- steam.ChanPresenceSteam stat := <-steam.ChanPresenceSteam
SetSteamId[steamId] = struct{}{} SetSteamId[steamId] = struct{}{}
var status string var status string
var tpye string var tpye string
switch stat { switch stat {
case steam.State_Offline: case steam.State_Offline:
status = xmpp.Status_offline status = xmpp.Status_offline
tpye = xmpp.Type_unavailable tpye = xmpp.Type_unavailable
case steam.State_Online: case steam.State_Online:
status = xmpp.Status_online status = xmpp.Status_online
tpye = xmpp.Type_available tpye = xmpp.Type_available
case steam.State_Busy: case steam.State_Busy:
status = xmpp.Status_do_not_disturb status = xmpp.Status_do_not_disturb
tpye = xmpp.Type_available tpye = xmpp.Type_available
case steam.State_Away: case steam.State_Away:
status = xmpp.Status_away status = xmpp.Status_away
tpye = xmpp.Type_available tpye = xmpp.Type_available
case steam.State_Snooze: case steam.State_Snooze:
status = xmpp.Status_extended_away status = xmpp.Status_extended_away
tpye = xmpp.Type_available tpye = xmpp.Type_available
} }
xmpp.SendPresenceFrom(status, tpye, steamId+"@"+xmpp.JidStr) xmpp.SendPresenceFrom(status, tpye, steamId+"@"+xmpp.JidStr)
} }
} }
// /Steam -> XMPP gateways // /Steam -> XMPP gateways
func inputStop() { func inputStop() {
for { for {
in := bufio.NewReader(os.Stdin) in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n') line, err := in.ReadString('\n')
if err != nil { if err != nil {
@ -196,8 +191,8 @@ func inputStop() {
} }
line = strings.TrimRight(line, "\n") line = strings.TrimRight(line, "\n")
if line == "stop" { if line == "stop" {
return return
} }
} }
} }

View File

@ -7,126 +7,123 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"log"
"strconv" "strconv"
"time" "time"
"log"
) )
const ( const (
sentryFile = "sentry" sentryFile = "sentry"
serverAddrs = "servers.addr" serverAddrs = "servers.addr"
State_Offline = steamlang.EPersonaState_Offline State_Offline = steamlang.EPersonaState_Offline
State_Online = steamlang.EPersonaState_Online State_Online = steamlang.EPersonaState_Online
State_Busy = steamlang.EPersonaState_Busy State_Busy = steamlang.EPersonaState_Busy
State_Away = steamlang.EPersonaState_Away State_Away = steamlang.EPersonaState_Away
State_Snooze = steamlang.EPersonaState_Snooze State_Snooze = steamlang.EPersonaState_Snooze
State_LookingToTrade = steamlang.EPersonaState_LookingToTrade State_LookingToTrade = steamlang.EPersonaState_LookingToTrade
State_LookingToPlay = steamlang.EPersonaState_LookingToPlay State_LookingToPlay = steamlang.EPersonaState_LookingToPlay
State_Max = steamlang.EPersonaState_Max State_Max = steamlang.EPersonaState_Max
ActionConnected = "steam_connected" ActionConnected = "steam_connected"
ActionDisconnected = "steam_disconnected" ActionDisconnected = "steam_disconnected"
LogInfo = "\t[STEAM INFO]\t" LogInfo = "\t[STEAM INFO]\t"
LogError = "\t[STEAM ERROR]\t" LogError = "\t[STEAM ERROR]\t"
LogDebug = "\t[STEAM DEBUG]\t" LogDebug = "\t[STEAM DEBUG]\t"
) )
var ( var (
Username = "" Username = ""
Password = "" Password = ""
AuthCode = "" AuthCode = ""
myLoginInfo = new(steam.LogOnDetails) myLoginInfo = new(steam.LogOnDetails)
client = steam.NewClient() client = steam.NewClient()
ChanPresence = make(chan string) ChanPresence = make(chan string)
ChanPresenceSteam = make(chan steamlang.EPersonaState) ChanPresenceSteam = make(chan steamlang.EPersonaState)
ChanMessage = make(chan string) ChanMessage = make(chan string)
ChanAction = make(chan string) ChanAction = make(chan string)
) )
func Run() { func Run() {
log.Printf("%sRunning", LogInfo) log.Printf("%sRunning", LogInfo)
setLoginInfos() setLoginInfos()
client = steam.NewClient() client = steam.NewClient()
client.ConnectionTimeout = 10 * time.Second client.ConnectionTimeout = 10 * time.Second
mainSteam() mainSteam()
} }
func mainSteam() { func mainSteam() {
for event := range client.Events() { for event := range client.Events() {
switch e := event.(type) { switch e := event.(type) {
case *steam.ConnectedEvent: case *steam.ConnectedEvent:
client.Auth.LogOn(myLoginInfo) client.Auth.LogOn(myLoginInfo)
case *steam.MachineAuthUpdateEvent: case *steam.MachineAuthUpdateEvent:
ioutil.WriteFile(sentryFile, e.Hash, 0666) ioutil.WriteFile(sentryFile, e.Hash, 0666)
case *steam.LoggedOnEvent: case *steam.LoggedOnEvent:
SendPresence(steamlang.EPersonaState_Online) SendPresence(steamlang.EPersonaState_Online)
ChanAction <- ActionConnected ChanAction <- ActionConnected
case steam.FatalErrorEvent: case steam.FatalErrorEvent:
log.Printf("%sFatalError: ", LogError, e) log.Printf("%sFatalError: ", LogError, e)
ChanAction <- ActionDisconnected ChanAction <- ActionDisconnected
// Re run Steam // Re run Steam
go func() { go func() {
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
Run() Run()
}() }()
return return
case error: case error:
log.Printf("%s", LogError, e) log.Printf("%s", LogError, e)
case *steam.ClientCMListEvent: case *steam.ClientCMListEvent:
// Save servers addresses // Save servers addresses
b, err := json.Marshal(*e) b, err := json.Marshal(*e)
if err != nil { if err != nil {
log.Printf("%sFailed to json.Marshal() servers list", LogError) log.Printf("%sFailed to json.Marshal() servers list", LogError)
} }
ioutil.WriteFile(serverAddrs, b, 0666) ioutil.WriteFile(serverAddrs, b, 0666)
case *steam.PersonaStateEvent: case *steam.PersonaStateEvent:
// ChanPresence <- e.Name // ChanPresence <- e.Name
ChanPresence <- e.FriendId.ToString() ChanPresence <- e.FriendId.ToString()
ChanPresenceSteam <- e.State ChanPresenceSteam <- e.State
case *steam.ChatMsgEvent: case *steam.ChatMsgEvent:
ChanMessage <- e.ChatterId.ToString() ChanMessage <- e.ChatterId.ToString()
ChanMessage <- e.Message ChanMessage <- e.Message
default: default:
log.Printf("%s", LogDebug, e) log.Printf("%s", LogDebug, e)
} }
} }
} }
func setLoginInfos() { func setLoginInfos() {
var sentryHash steam.SentryHash var sentryHash steam.SentryHash
sentryHash, err := ioutil.ReadFile(sentryFile) sentryHash, err := ioutil.ReadFile(sentryFile)
myLoginInfo.Username = Username myLoginInfo.Username = Username
myLoginInfo.Password = Password myLoginInfo.Password = Password
// TODO think again // TODO think again
if err == nil { if err == nil {
myLoginInfo.SentryFileHash = sentryHash myLoginInfo.SentryFileHash = sentryHash
log.Printf("%sAuthentification by SentryFileHash", LogDebug) log.Printf("%sAuthentification by SentryFileHash", LogDebug)
} else if AuthCode != "" { } else if AuthCode != "" {
myLoginInfo.AuthCode = AuthCode myLoginInfo.AuthCode = AuthCode
log.Printf("%sAuthentification by AuthCode", LogDebug) log.Printf("%sAuthentification by AuthCode", LogDebug)
} else { } else {
log.Printf("%sFirst authentification", LogDebug) log.Printf("%sFirst authentification", LogDebug)
} }
} }
func IsConnected() bool { func IsConnected() bool {
return client.Connected() return client.Connected()
} }
@ -155,7 +152,6 @@ func Disconnect() {
go client.Disconnect() go client.Disconnect()
} }
func SendMessage(steamId, message string) { func SendMessage(steamId, message string) {
steamIdUint64, err := strconv.ParseUint(steamId, 10, 64) steamIdUint64, err := strconv.ParseUint(steamId, 10, 64)
if err == nil { if err == nil {
@ -169,8 +165,6 @@ func SendPresence(status steamlang.EPersonaState) {
client.Social.SetPersonaState(status) client.Social.SetPersonaState(status)
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// First authentification error // First authentification error
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -1,95 +1,93 @@
package xmpp package xmpp
import ( import (
// "github.com/emgee/go-xmpp" // "github.com/emgee/go-xmpp"
"go-xmpp" "go-xmpp"
"log" "log"
"strings" "strings"
) )
const ( const (
Status_online = "" Status_online = ""
Status_offline = "" Status_offline = ""
Status_away = "away" Status_away = "away"
Status_chat = "chat" Status_chat = "chat"
Status_do_not_disturb = "dnd" Status_do_not_disturb = "dnd"
Status_extended_away = "xa" Status_extended_away = "xa"
Type_available = "" Type_available = ""
Type_unavailable = "unavailable" Type_unavailable = "unavailable"
ActionConnexion = "action_xmpp_connexion" ActionConnexion = "action_xmpp_connexion"
ActionDeconnexion = "action_xmpp_deconnexion" ActionDeconnexion = "action_xmpp_deconnexion"
LogInfo = "\t[XMPP INFO]\t" LogInfo = "\t[XMPP INFO]\t"
LogError = "\t[XMPP ERROR]\t" LogError = "\t[XMPP ERROR]\t"
LogDebug = "\t[XMPP DEBUG]\t" LogDebug = "\t[XMPP DEBUG]\t"
) )
var ( var (
Addr = "127.0.0.1:5347" Addr = "127.0.0.1:5347"
JidStr = "" JidStr = ""
Secret = "" Secret = ""
PreferedJID = "" 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) ChanPresence = make(chan string)
ChanMessage = make(chan string) ChanMessage = make(chan string)
ChanAction = make(chan string) ChanAction = make(chan string)
CurrentStatus = Status_offline CurrentStatus = Status_offline
Version = "" Version = ""
) )
func Run() { func Run() {
log.Printf("%sRunning", LogInfo) log.Printf("%sRunning", LogInfo)
// Create stream and configure it as a component connection. // Create stream and configure it as a component connection.
jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID) jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID)
stream = must(xmpp.NewStream(Addr, &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream) stream = must(xmpp.NewStream(Addr, &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream)
comp = must(xmpp.NewComponentXMPP(stream, jid, Secret)).(*xmpp.XMPP) comp = must(xmpp.NewComponentXMPP(stream, jid, Secret)).(*xmpp.XMPP)
SendPresence(Status_online, Type_available) SendPresence(Status_online, Type_available)
mainXMPP() mainXMPP()
} }
func mainXMPP() { func mainXMPP() {
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.To == JidStr { if strings.SplitN(v.From, "/", 2)[0] == PreferedJID && v.To == JidStr {
if v.Type == Type_unavailable { if v.Type == Type_unavailable {
Disconnect() Disconnect()
ChanAction <- ActionDeconnexion ChanAction <- ActionDeconnexion
} else { } else {
// SendPresence(v.Show, v.Type) // SendPresence(v.Show, v.Type)
CurrentStatus = v.Show CurrentStatus = v.Show
ChanAction <- ActionConnexion ChanAction <- ActionConnexion
} }
ChanPresence <- v.Show ChanPresence <- v.Show
} }
case *xmpp.Message: case *xmpp.Message:
steamID := strings.SplitN(v.To, "@", 2)[0] steamID := strings.SplitN(v.To, "@", 2)[0]
ChanMessage <- steamID ChanMessage <- steamID
ChanMessage <- v.Body ChanMessage <- v.Body
default: default:
log.Printf("%srecv: %v", LogDebug, x) log.Printf("%srecv: %v", LogDebug, x)
} }
} }
// Send deconnexion // Send deconnexion
SendPresence(Status_offline, Type_unavailable) SendPresence(Status_offline, Type_unavailable)
} }
func must(v interface{}, err error) interface{} { func must(v interface{}, err error) interface{} {
@ -100,17 +98,17 @@ func must(v interface{}, err error) interface{} {
} }
func Disconnect() { func Disconnect() {
SendPresence(Status_offline, Type_unavailable) SendPresence(Status_offline, Type_unavailable)
} }
func SendPresence(status, tpye string) { func SendPresence(status, tpye string) {
comp.Out <- xmpp.Presence{To: PreferedJID, From: jid.Domain, Show: status, Type: tpye, Status: "go-xmpp4steam v"+Version} comp.Out <- xmpp.Presence{To: PreferedJID, From: jid.Domain, Show: status, Type: tpye, Status: "go-xmpp4steam v" + Version}
} }
func SendPresenceFrom(status, tpye, from string) { func SendPresenceFrom(status, tpye, from string) {
comp.Out <- xmpp.Presence{To: PreferedJID, From: from, Show: status, Type: tpye} comp.Out <- xmpp.Presence{To: PreferedJID, From: from, Show: status, Type: tpye}
} }
func SendMessage(from, message string) { func SendMessage(from, message string) {
comp.Out <- xmpp.Message{To: PreferedJID, From: from, Body: message} comp.Out <- xmpp.Message{To: PreferedJID, From: from, Body: message}
} }