diff options
author | Wim <wim@42.be> | 2018-12-07 23:48:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-07 23:48:24 +0100 |
commit | f9fb33e6964d9230961300245c516a3abd39a539 (patch) | |
tree | 245a64b2cc43ba951b8785f01915e84a3306b7c3 /bridge/steam | |
parent | f72d5de2d757e193ea51da1fd1c75114d94c2c3f (diff) | |
download | matterbridge-msglm-f9fb33e6964d9230961300245c516a3abd39a539.tar.gz matterbridge-msglm-f9fb33e6964d9230961300245c516a3abd39a539.tar.bz2 matterbridge-msglm-f9fb33e6964d9230961300245c516a3abd39a539.zip |
Refactor steam bridge (#630)
* split up in different files
* decrease complexity
Diffstat (limited to 'bridge/steam')
-rw-r--r-- | bridge/steam/handlers.go | 126 | ||||
-rw-r--r-- | bridge/steam/steam.go | 95 |
2 files changed, 131 insertions, 90 deletions
diff --git a/bridge/steam/handlers.go b/bridge/steam/handlers.go new file mode 100644 index 00000000..a591281e --- /dev/null +++ b/bridge/steam/handlers.go @@ -0,0 +1,126 @@ +package bsteam + +import ( + "fmt" + "strconv" + + "github.com/42wim/matterbridge/bridge/config" + "github.com/Philipp15b/go-steam" + "github.com/Philipp15b/go-steam/protocol/steamlang" +) + +func (b *Bsteam) handleChatMsg(e *steam.ChatMsgEvent) { + b.Log.Debugf("Receiving ChatMsgEvent: %#v", e) + b.Log.Debugf("<= Sending message from %s on %s to gateway", b.getNick(e.ChatterId), b.Account) + var channel int64 + if e.ChatRoomId == 0 { + channel = int64(e.ChatterId) + } else { + // for some reason we have to remove 0x18000000000000 + // TODO + // https://github.com/42wim/matterbridge/pull/630#discussion_r238102751 + // channel = int64(e.ChatRoomId) & 0xfffffffffffff + channel = int64(e.ChatRoomId) - 0x18000000000000 + } + msg := config.Message{ + Username: b.getNick(e.ChatterId), + Text: e.Message, + Channel: strconv.FormatInt(channel, 10), + Account: b.Account, + UserID: strconv.FormatInt(int64(e.ChatterId), 10), + } + b.Remote <- msg +} + +func (b *Bsteam) handleEvents() { + myLoginInfo := &steam.LogOnDetails{ + Username: b.GetString("Login"), + Password: b.GetString("Password"), + AuthCode: b.GetString("AuthCode"), + } + // TODO Attempt to read existing auth hash to avoid steam guard. + // Maybe works + //myLoginInfo.SentryFileHash, _ = ioutil.ReadFile("sentry") + for event := range b.c.Events() { + switch e := event.(type) { + case *steam.ChatMsgEvent: + b.handleChatMsg(e) + case *steam.PersonaStateEvent: + b.Log.Debugf("PersonaStateEvent: %#v\n", e) + b.Lock() + b.userMap[e.FriendId] = e.Name + b.Unlock() + case *steam.ConnectedEvent: + b.c.Auth.LogOn(myLoginInfo) + case *steam.MachineAuthUpdateEvent: + // TODO sentry files for 2 auth + /* + b.Log.Info("authupdate", e) + b.Log.Info("hash", e.Hash) + ioutil.WriteFile("sentry", e.Hash, 0666) + */ + case *steam.LogOnFailedEvent: + b.Log.Info("Logon failed", e) + err := b.handleLogOnFailed(e, myLoginInfo) + if err != nil { + b.Log.Error(err) + return + } + case *steam.LoggedOnEvent: + b.Log.Debugf("LoggedOnEvent: %#v", e) + b.connected <- struct{}{} + b.Log.Debugf("setting online") + b.c.Social.SetPersonaState(steamlang.EPersonaState_Online) + case *steam.DisconnectedEvent: + b.Log.Info("Disconnected") + b.Log.Info("Attempting to reconnect...") + b.c.Connect() + case steam.FatalErrorEvent: + b.Log.Errorf("steam FatalErrorEvent: %#v", e) + default: + b.Log.Debugf("unknown event %#v", e) + } + } +} + +func (b *Bsteam) handleLogOnFailed(e *steam.LogOnFailedEvent, myLoginInfo *steam.LogOnDetails) error { + switch e.Result { + case steamlang.EResult_AccountLogonDeniedNeedTwoFactorCode: + b.Log.Info("Steam guard isn't letting me in! Enter 2FA code:") + var code string + fmt.Scanf("%s", &code) + // TODO https://github.com/42wim/matterbridge/pull/630#discussion_r238103978 + myLoginInfo.TwoFactorCode = code + case steamlang.EResult_AccountLogonDenied: + b.Log.Info("Steam guard isn't letting me in! Enter auth code:") + var code string + fmt.Scanf("%s", &code) + // TODO https://github.com/42wim/matterbridge/pull/630#discussion_r238103978 + myLoginInfo.AuthCode = code + case steamlang.EResult_InvalidLoginAuthCode: + return fmt.Errorf("Steam guard: invalid login auth code: %#v ", e.Result) + default: + return fmt.Errorf("LogOnFailedEvent: %#v ", e.Result) + // TODO: Handle EResult_InvalidLoginAuthCode + } + return nil +} + +// handleFileInfo handles config.FileInfo and adds correct file comment or URL to msg.Text. +// Returns error if cast fails. +func (b *Bsteam) handleFileInfo(msg *config.Message, f interface{}) error { + if _, ok := f.(config.FileInfo); !ok { + return fmt.Errorf("handleFileInfo cast failed %#v", f) + } + fi := f.(config.FileInfo) + if fi.Comment != "" { + msg.Text += fi.Comment + ": " + } + if fi.URL != "" { + msg.Text = fi.URL + if fi.Comment != "" { + msg.Text = fi.Comment + ": " + fi.URL + } + } + return nil +} diff --git a/bridge/steam/steam.go b/bridge/steam/steam.go index 1a4dab27..5a577a28 100644 --- a/bridge/steam/steam.go +++ b/bridge/steam/steam.go @@ -2,7 +2,6 @@ package bsteam import ( "fmt" - "strconv" "sync" "time" @@ -73,22 +72,13 @@ func (b *Bsteam) Send(msg config.Message) (string, error) { for _, rmsg := range helper.HandleExtra(&msg, b.General) { b.c.Social.SendMessage(id, steamlang.EChatEntryType_ChatMsg, rmsg.Username+rmsg.Text) } - if len(msg.Extra["file"]) > 0 { - for _, f := range msg.Extra["file"] { - fi := f.(config.FileInfo) - if fi.Comment != "" { - msg.Text += fi.Comment + ": " - } - if fi.URL != "" { - msg.Text = fi.URL - if fi.Comment != "" { - msg.Text = fi.Comment + ": " + fi.URL - } - } - b.c.Social.SendMessage(id, steamlang.EChatEntryType_ChatMsg, msg.Username+msg.Text) + for i := range msg.Extra["file"] { + if err := b.handleFileInfo(&msg, msg.Extra["file"][i]); err != nil { + b.Log.Error(err) } - return "", nil + b.c.Social.SendMessage(id, steamlang.EChatEntryType_ChatMsg, msg.Username+msg.Text) } + return "", nil } b.c.Social.SendMessage(id, steamlang.EChatEntryType_ChatMsg, msg.Username+msg.Text) @@ -103,78 +93,3 @@ func (b *Bsteam) getNick(id steamid.SteamId) string { } return "unknown" } - -func (b *Bsteam) handleEvents() { - myLoginInfo := new(steam.LogOnDetails) - myLoginInfo.Username = b.GetString("Login") - myLoginInfo.Password = b.GetString("Password") - myLoginInfo.AuthCode = b.GetString("AuthCode") - // Attempt to read existing auth hash to avoid steam guard. - // Maybe works - //myLoginInfo.SentryFileHash, _ = ioutil.ReadFile("sentry") - for event := range b.c.Events() { - //b.Log.Info(event) - switch e := event.(type) { - case *steam.ChatMsgEvent: - b.Log.Debugf("Receiving ChatMsgEvent: %#v", e) - b.Log.Debugf("<= Sending message from %s on %s to gateway", b.getNick(e.ChatterId), b.Account) - var channel int64 - if e.ChatRoomId == 0 { - channel = int64(e.ChatterId) - } else { - // for some reason we have to remove 0x18000000000000 - channel = int64(e.ChatRoomId) - 0x18000000000000 - } - msg := config.Message{Username: b.getNick(e.ChatterId), Text: e.Message, Channel: strconv.FormatInt(channel, 10), Account: b.Account, UserID: strconv.FormatInt(int64(e.ChatterId), 10)} - b.Remote <- msg - case *steam.PersonaStateEvent: - b.Log.Debugf("PersonaStateEvent: %#v\n", e) - b.Lock() - b.userMap[e.FriendId] = e.Name - b.Unlock() - case *steam.ConnectedEvent: - b.c.Auth.LogOn(myLoginInfo) - case *steam.MachineAuthUpdateEvent: - /* - b.Log.Info("authupdate", e) - b.Log.Info("hash", e.Hash) - ioutil.WriteFile("sentry", e.Hash, 0666) - */ - case *steam.LogOnFailedEvent: - b.Log.Info("Logon failed", e) - switch e.Result { - case steamlang.EResult_AccountLogonDeniedNeedTwoFactorCode: - { - b.Log.Info("Steam guard isn't letting me in! Enter 2FA code:") - var code string - fmt.Scanf("%s", &code) - myLoginInfo.TwoFactorCode = code - } - case steamlang.EResult_AccountLogonDenied: - { - b.Log.Info("Steam guard isn't letting me in! Enter auth code:") - var code string - fmt.Scanf("%s", &code) - myLoginInfo.AuthCode = code - } - default: - b.Log.Errorf("LogOnFailedEvent: %#v ", e.Result) - // TODO: Handle EResult_InvalidLoginAuthCode - return - } - case *steam.LoggedOnEvent: - b.Log.Debugf("LoggedOnEvent: %#v", e) - b.connected <- struct{}{} - b.Log.Debugf("setting online") - b.c.Social.SetPersonaState(steamlang.EPersonaState_Online) - case *steam.DisconnectedEvent: - b.Log.Info("Disconnected") - b.Log.Info("Attempting to reconnect...") - b.c.Connect() - case steam.FatalErrorEvent: - b.Log.Error(e) - default: - b.Log.Debugf("unknown event %#v", e) - } - } -} |