From 0a610cf8c8eadaf92275d1a0b35c45ee0a2fd579 Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Mon, 19 Oct 2015 21:28:23 +0200 Subject: [PATCH 1/2] Remove stdinput to stop --- main.go | 6 ++++-- xmpp/xmpp.go | 4 +++- xmpp4steam.cfg | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 77a09c3..70bbdfa 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,8 @@ func init() { steam.Username = mapConfig["steam_login"] steam.Password = mapConfig["steam_password"] steam.AuthCode = mapConfig["steam_auth_code"] + + xmpp.Version = "0.1.0" } func main() { @@ -52,9 +54,9 @@ func main() { go gatewaySteamXmppPresence() go steam.Run() - go xmpp.Run() + xmpp.Run() - inputStop() + // inputStop() steam.Disconnect() xmpp.Disconnect() diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 33ae1c5..e320cc6 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -44,6 +44,8 @@ var ( ChanAction = make(chan string) CurrentStatus = Status_offline + + Version = "" ) @@ -102,7 +104,7 @@ func Disconnect() { } func SendPresence(status, tpye string) { - comp.Out <- xmpp.Presence{To: PreferedJID, From: jid.Domain, Show: status, Type: tpye} + comp.Out <- xmpp.Presence{To: PreferedJID, From: jid.Domain, Show: status, Type: tpye, Status: "go-xmpp4steam v"+Version} } func SendPresenceFrom(status, tpye, from string) { diff --git a/xmpp4steam.cfg b/xmpp4steam.cfg index 666da00..3734f75 100644 --- a/xmpp4steam.cfg +++ b/xmpp4steam.cfg @@ -1,8 +1,8 @@ # XMPP informations xmpp_server_address=192.168.1.2 xmpp_server_port=5347 -xmpp_hostname=steam.kingpenguin.tk -xmpp_secret=steam_gateway_password +xmpp_hostname=xmppsteam.kingpenguin.tk +xmpp_secret=xmpp4steam_password xmpp_authorized_jid=chteufleur@kingpenguin.tk # Steam informations From bc9eee01f3b4a116566c8fcef062b511bdcd9cde Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Mon, 19 Oct 2015 21:42:01 +0200 Subject: [PATCH 2/2] go fmt --- main.go | 243 ++++++++++++++++++++++++------------------------- steam/steam.go | 120 ++++++++++++------------ xmpp/xmpp.go | 114 ++++++++++++----------- 3 files changed, 232 insertions(+), 245 deletions(-) diff --git a/main.go b/main.go index 70bbdfa..812c53f 100644 --- a/main.go +++ b/main.go @@ -1,194 +1,189 @@ package main import ( - "go-xmpp4steam/steam" - "go-xmpp4steam/xmpp" + "go-xmpp4steam/steam" + "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" - "log" - "os" - "strings" - "time" + "bufio" + "log" + "os" + "strings" + "time" ) const ( - configurationFilePath = "xmpp4steam.cfg" + configurationFilePath = "xmpp4steam.cfg" ) var ( - mapConfig = make(map[string]string) - SetSteamId = make(map[string]struct{}) + mapConfig = make(map[string]string) + SetSteamId = make(map[string]struct{}) ) func init() { err := cfg.Load(configurationFilePath, mapConfig) - if err != nil { + if err != nil { log.Fatal("Failed to load configuration file.", err) } - // XMPP config - xmpp.Addr = mapConfig["xmpp_server_address"]+":"+mapConfig["xmpp_server_port"] - xmpp.JidStr = mapConfig["xmpp_hostname"] - xmpp.Secret = mapConfig["xmpp_secret"] - xmpp.PreferedJID = mapConfig["xmpp_authorized_jid"] + // XMPP config + xmpp.Addr = mapConfig["xmpp_server_address"] + ":" + mapConfig["xmpp_server_port"] + xmpp.JidStr = mapConfig["xmpp_hostname"] + xmpp.Secret = mapConfig["xmpp_secret"] + xmpp.PreferedJID = mapConfig["xmpp_authorized_jid"] - // Steam config - steam.Username = mapConfig["steam_login"] - steam.Password = mapConfig["steam_password"] - steam.AuthCode = mapConfig["steam_auth_code"] + // Steam config + steam.Username = mapConfig["steam_login"] + steam.Password = mapConfig["steam_password"] + steam.AuthCode = mapConfig["steam_auth_code"] - xmpp.Version = "0.1.0" + xmpp.Version = "0.1.0" } func main() { - go gatewayXmppSteamAction() - go gatewaySteamXmppAction() + go gatewayXmppSteamAction() + go gatewaySteamXmppAction() - go gatewayXmppSteamPresence() - go gatewayXmppSteamMessage() + go gatewayXmppSteamPresence() + go gatewayXmppSteamMessage() - go gatewaySteamXmppMessage() - go gatewaySteamXmppPresence() + go gatewaySteamXmppMessage() + go gatewaySteamXmppPresence() - go steam.Run() - xmpp.Run() + go steam.Run() + xmpp.Run() - // inputStop() + // inputStop() - steam.Disconnect() - xmpp.Disconnect() - time.Sleep(1 * time.Second) + steam.Disconnect() + xmpp.Disconnect() + time.Sleep(1 * time.Second) } - // XMPP -> Steam gateways func gatewayXmppSteamAction() { - for { - action := <- xmpp.ChanAction + for { + action := <-xmpp.ChanAction - switch action { - case xmpp.ActionConnexion: - if !steam.IsConnected() { - steam.Connect() - } + switch action { + case xmpp.ActionConnexion: + if !steam.IsConnected() { + steam.Connect() + } - case xmpp.ActionDeconnexion: - if steam.IsConnected() { - steam.Disconnect() - } - } - } + case xmpp.ActionDeconnexion: + if steam.IsConnected() { + steam.Disconnect() + } + } + } } func gatewayXmppSteamPresence() { - for { - status := <- xmpp.ChanPresence - var steamStatus steamlang.EPersonaState + for { + status := <-xmpp.ChanPresence + var steamStatus steamlang.EPersonaState - switch status { - case xmpp.Status_online: - steamStatus = steam.State_Online + switch status { + case xmpp.Status_online: + steamStatus = steam.State_Online - case xmpp.Status_away: - steamStatus = steam.State_Away + case xmpp.Status_away: + steamStatus = steam.State_Away - case xmpp.Status_chat: + case xmpp.Status_chat: - case xmpp.Status_extended_away: - steamStatus = steam.State_Snooze + case xmpp.Status_extended_away: + steamStatus = steam.State_Snooze - case xmpp.Status_do_not_disturb: - steamStatus = steam.State_Busy - } + case xmpp.Status_do_not_disturb: + steamStatus = steam.State_Busy + } - steam.SendPresence(steamStatus) - } + steam.SendPresence(steamStatus) + } } func gatewayXmppSteamMessage() { - for { - steamId := <- xmpp.ChanMessage - message := <- xmpp.ChanMessage + for { + steamId := <-xmpp.ChanMessage + message := <-xmpp.ChanMessage - steam.SendMessage(steamId, message) - } + steam.SendMessage(steamId, message) + } } + // /XMPP -> Steam gateways - - // Steam -> XMPP gateways func gatewaySteamXmppAction() { - for { - action := <- steam.ChanAction - switch action { - case steam.ActionConnected: - xmpp.SendPresence(xmpp.CurrentStatus, xmpp.Type_available) + for { + action := <-steam.ChanAction + switch action { + case steam.ActionConnected: + xmpp.SendPresence(xmpp.CurrentStatus, xmpp.Type_available) - case steam.ActionDisconnected: - xmpp.Disconnect() - for sid, _ := range SetSteamId { - xmpp.SendPresenceFrom(xmpp.Status_offline, xmpp.Type_unavailable, sid+"@"+xmpp.JidStr) - delete(SetSteamId, sid) - } - } - } + case steam.ActionDisconnected: + xmpp.Disconnect() + for sid, _ := range SetSteamId { + xmpp.SendPresenceFrom(xmpp.Status_offline, xmpp.Type_unavailable, sid+"@"+xmpp.JidStr) + delete(SetSteamId, sid) + } + } + } } func gatewaySteamXmppMessage() { - for { - steamId := <- steam.ChanMessage - message := <- steam.ChanMessage - xmpp.SendMessage(steamId+"@"+xmpp.JidStr, message) - } + for { + steamId := <-steam.ChanMessage + message := <-steam.ChanMessage + xmpp.SendMessage(steamId+"@"+xmpp.JidStr, message) + } } func gatewaySteamXmppPresence() { - for { - // name := steam.ChanPresence - steamId := <- steam.ChanPresence - stat := <- steam.ChanPresenceSteam + for { + // name := steam.ChanPresence + steamId := <-steam.ChanPresence + stat := <-steam.ChanPresenceSteam - SetSteamId[steamId] = struct{}{} + SetSteamId[steamId] = struct{}{} - var status string - var tpye string - switch stat { - case steam.State_Offline: - status = xmpp.Status_offline - tpye = xmpp.Type_unavailable + 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_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_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_Away: + status = xmpp.Status_away + tpye = xmpp.Type_available - case steam.State_Snooze: - status = xmpp.Status_extended_away - tpye = xmpp.Type_available - } + case steam.State_Snooze: + status = xmpp.Status_extended_away + tpye = xmpp.Type_available + } - xmpp.SendPresenceFrom(status, tpye, steamId+"@"+xmpp.JidStr) - } + xmpp.SendPresenceFrom(status, tpye, steamId+"@"+xmpp.JidStr) + } } + // /Steam -> XMPP gateways - - - - func inputStop() { - for { + for { in := bufio.NewReader(os.Stdin) line, err := in.ReadString('\n') if err != nil { @@ -196,8 +191,8 @@ func inputStop() { } line = strings.TrimRight(line, "\n") - if line == "stop" { - return - } + if line == "stop" { + return + } } } diff --git a/steam/steam.go b/steam/steam.go index f4bf119..d388fce 100644 --- a/steam/steam.go +++ b/steam/steam.go @@ -7,126 +7,123 @@ import ( "encoding/json" "io/ioutil" + "log" "strconv" "time" - "log" ) const ( - sentryFile = "sentry" + 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_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 + State_LookingToPlay = steamlang.EPersonaState_LookingToPlay + State_Max = steamlang.EPersonaState_Max - ActionConnected = "steam_connected" + ActionConnected = "steam_connected" ActionDisconnected = "steam_disconnected" - LogInfo = "\t[STEAM INFO]\t" + LogInfo = "\t[STEAM INFO]\t" LogError = "\t[STEAM ERROR]\t" LogDebug = "\t[STEAM DEBUG]\t" ) var ( - Username = "" - Password = "" - AuthCode = "" + Username = "" + Password = "" + AuthCode = "" - myLoginInfo = new(steam.LogOnDetails) - client = steam.NewClient() + myLoginInfo = new(steam.LogOnDetails) + client = steam.NewClient() - ChanPresence = make(chan string) + ChanPresence = make(chan string) ChanPresenceSteam = make(chan steamlang.EPersonaState) - ChanMessage = make(chan string) - ChanAction = make(chan string) + ChanMessage = make(chan string) + ChanAction = make(chan string) ) - func Run() { log.Printf("%sRunning", LogInfo) setLoginInfos() client = steam.NewClient() client.ConnectionTimeout = 10 * time.Second - mainSteam() + mainSteam() } func mainSteam() { for event := range client.Events() { switch e := event.(type) { - case *steam.ConnectedEvent: - client.Auth.LogOn(myLoginInfo) + case *steam.ConnectedEvent: + client.Auth.LogOn(myLoginInfo) - case *steam.MachineAuthUpdateEvent: - ioutil.WriteFile(sentryFile, e.Hash, 0666) + case *steam.MachineAuthUpdateEvent: + ioutil.WriteFile(sentryFile, e.Hash, 0666) - case *steam.LoggedOnEvent: - SendPresence(steamlang.EPersonaState_Online) - ChanAction <- ActionConnected + case *steam.LoggedOnEvent: + SendPresence(steamlang.EPersonaState_Online) + ChanAction <- ActionConnected - case steam.FatalErrorEvent: - log.Printf("%sFatalError: ", LogError, e) - ChanAction <- ActionDisconnected - // Re run Steam - go func() { - time.Sleep(2 * time.Second) - Run() - }() - return + case steam.FatalErrorEvent: + log.Printf("%sFatalError: ", LogError, e) + ChanAction <- ActionDisconnected + // Re run Steam + go func() { + time.Sleep(2 * time.Second) + Run() + }() + return - case error: - log.Printf("%s", LogError, e) + 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) - } - ioutil.WriteFile(serverAddrs, b, 0666) + case *steam.ClientCMListEvent: + // Save servers addresses + b, err := json.Marshal(*e) + if err != nil { + log.Printf("%sFailed to json.Marshal() servers list", LogError) + } + ioutil.WriteFile(serverAddrs, b, 0666) - case *steam.PersonaStateEvent: - // ChanPresence <- e.Name - ChanPresence <- e.FriendId.ToString() - ChanPresenceSteam <- e.State + case *steam.PersonaStateEvent: + // ChanPresence <- e.Name + ChanPresence <- e.FriendId.ToString() + ChanPresenceSteam <- e.State - case *steam.ChatMsgEvent: - ChanMessage <- e.ChatterId.ToString() - ChanMessage <- e.Message + case *steam.ChatMsgEvent: + ChanMessage <- e.ChatterId.ToString() + ChanMessage <- e.Message - default: - log.Printf("%s", LogDebug, e) + default: + log.Printf("%s", LogDebug, e) } } } - func setLoginInfos() { var sentryHash steam.SentryHash sentryHash, err := ioutil.ReadFile(sentryFile) - myLoginInfo.Username = Username - myLoginInfo.Password = Password + myLoginInfo.Username = Username + myLoginInfo.Password = Password // TODO think again if err == nil { myLoginInfo.SentryFileHash = sentryHash log.Printf("%sAuthentification by SentryFileHash", LogDebug) } else if AuthCode != "" { - myLoginInfo.AuthCode = AuthCode + myLoginInfo.AuthCode = AuthCode log.Printf("%sAuthentification by AuthCode", LogDebug) } else { log.Printf("%sFirst authentification", LogDebug) } } - func IsConnected() bool { return client.Connected() } @@ -155,7 +152,6 @@ func Disconnect() { go client.Disconnect() } - func SendMessage(steamId, message string) { steamIdUint64, err := strconv.ParseUint(steamId, 10, 64) if err == nil { @@ -169,8 +165,6 @@ func SendPresence(status steamlang.EPersonaState) { client.Social.SetPersonaState(status) } - - //------------------------------------------------------------------------------ // First authentification error //------------------------------------------------------------------------------ diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index e320cc6..3a0fe74 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -1,95 +1,93 @@ package xmpp - import ( - // "github.com/emgee/go-xmpp" - "go-xmpp" + // "github.com/emgee/go-xmpp" + "go-xmpp" - "log" - "strings" + "log" + "strings" ) const ( - Status_online = "" - Status_offline = "" - Status_away = "away" - Status_chat = "chat" - Status_do_not_disturb = "dnd" - Status_extended_away = "xa" + 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_available = "" + Type_unavailable = "unavailable" - ActionConnexion = "action_xmpp_connexion" - ActionDeconnexion = "action_xmpp_deconnexion" + ActionConnexion = "action_xmpp_connexion" + ActionDeconnexion = "action_xmpp_deconnexion" - LogInfo = "\t[XMPP INFO]\t" - LogError = "\t[XMPP ERROR]\t" - LogDebug = "\t[XMPP DEBUG]\t" + LogInfo = "\t[XMPP INFO]\t" + LogError = "\t[XMPP ERROR]\t" + LogDebug = "\t[XMPP DEBUG]\t" ) var ( Addr = "127.0.0.1:5347" - JidStr = "" + JidStr = "" Secret = "" - PreferedJID = "" + PreferedJID = "" - jid xmpp.JID - stream = new(xmpp.Stream) - comp = new(xmpp.XMPP) + jid xmpp.JID + stream = new(xmpp.Stream) + comp = new(xmpp.XMPP) - ChanPresence = make(chan string) - ChanMessage = make(chan string) - ChanAction = make(chan string) + ChanPresence = make(chan string) + ChanMessage = make(chan string) + ChanAction = make(chan string) - CurrentStatus = Status_offline + CurrentStatus = Status_offline - Version = "" + Version = "" ) - func Run() { - log.Printf("%sRunning", LogInfo) + log.Printf("%sRunning", LogInfo) // Create stream and configure it as a component connection. jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID) stream = must(xmpp.NewStream(Addr, &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream) comp = must(xmpp.NewComponentXMPP(stream, jid, Secret)).(*xmpp.XMPP) - SendPresence(Status_online, Type_available) + SendPresence(Status_online, Type_available) - mainXMPP() + mainXMPP() } func mainXMPP() { for x := range comp.In { - switch v := x.(type) { - case *xmpp.Presence: - if strings.SplitN(v.From, "/", 2)[0] == PreferedJID && v.To == JidStr { - if v.Type == Type_unavailable { - Disconnect() - ChanAction <- ActionDeconnexion - } else { - // SendPresence(v.Show, v.Type) - CurrentStatus = v.Show - ChanAction <- ActionConnexion - } + switch v := x.(type) { + case *xmpp.Presence: + if strings.SplitN(v.From, "/", 2)[0] == PreferedJID && v.To == JidStr { + if v.Type == Type_unavailable { + Disconnect() + ChanAction <- ActionDeconnexion + } else { + // SendPresence(v.Show, v.Type) + CurrentStatus = v.Show + ChanAction <- ActionConnexion + } - ChanPresence <- v.Show - } + ChanPresence <- v.Show + } - case *xmpp.Message: - steamID := strings.SplitN(v.To, "@", 2)[0] - ChanMessage <- steamID - ChanMessage <- v.Body + case *xmpp.Message: + steamID := strings.SplitN(v.To, "@", 2)[0] + ChanMessage <- steamID + ChanMessage <- v.Body - default: - log.Printf("%srecv: %v", LogDebug, x) - } + default: + log.Printf("%srecv: %v", LogDebug, x) + } } - // Send deconnexion - SendPresence(Status_offline, Type_unavailable) + // Send deconnexion + SendPresence(Status_offline, Type_unavailable) } func must(v interface{}, err error) interface{} { @@ -100,17 +98,17 @@ func must(v interface{}, err error) interface{} { } func Disconnect() { - SendPresence(Status_offline, Type_unavailable) + SendPresence(Status_offline, Type_unavailable) } 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) { - 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) { - comp.Out <- xmpp.Message{To: PreferedJID, From: from, Body: message} + comp.Out <- xmpp.Message{To: PreferedJID, From: from, Body: message} }