2017-06-21 16:02:05 -07:00
|
|
|
package bsteam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-11-15 10:24:22 -08:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2018-05-06 07:32:24 -07:00
|
|
|
|
2018-02-26 15:33:21 -08:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2017-06-21 16:02:05 -07:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-05-06 07:32:24 -07:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2017-06-21 16:02:05 -07:00
|
|
|
"github.com/Philipp15b/go-steam"
|
|
|
|
"github.com/Philipp15b/go-steam/protocol/steamlang"
|
|
|
|
"github.com/Philipp15b/go-steam/steamid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bsteam struct {
|
|
|
|
c *steam.Client
|
|
|
|
connected chan struct{}
|
|
|
|
userMap map[steamid.SteamId]string
|
|
|
|
sync.RWMutex
|
2018-03-04 14:52:14 -08:00
|
|
|
*bridge.Config
|
2017-06-21 16:02:05 -07:00
|
|
|
}
|
|
|
|
|
2018-03-04 14:52:14 -08:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
|
|
|
b := &Bsteam{Config: cfg}
|
2017-06-21 16:02:05 -07:00
|
|
|
b.userMap = make(map[steamid.SteamId]string)
|
|
|
|
b.connected = make(chan struct{})
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bsteam) Connect() error {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connecting")
|
2017-06-21 16:02:05 -07:00
|
|
|
b.c = steam.NewClient()
|
|
|
|
go b.handleEvents()
|
|
|
|
go b.c.Connect()
|
|
|
|
select {
|
|
|
|
case <-b.connected:
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connection succeeded")
|
2017-06-21 16:02:05 -07:00
|
|
|
case <-time.After(time.Second * 30):
|
|
|
|
return fmt.Errorf("connection timed out")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bsteam) Disconnect() error {
|
|
|
|
b.c.Disconnect()
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-12 05:51:41 -07:00
|
|
|
func (b *Bsteam) JoinChannel(channel config.ChannelInfo) error {
|
|
|
|
id, err := steamid.NewId(channel.Name)
|
2017-06-21 16:02:05 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.c.Social.JoinChat(id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 13:59:37 -07:00
|
|
|
func (b *Bsteam) Send(msg config.Message) (string, error) {
|
2017-09-11 13:45:15 -07:00
|
|
|
// ignore delete messages
|
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
|
|
|
return "", nil
|
|
|
|
}
|
2017-06-21 16:02:05 -07:00
|
|
|
id, err := steamid.NewId(msg.Channel)
|
|
|
|
if err != nil {
|
2017-08-27 13:59:37 -07:00
|
|
|
return "", err
|
2017-06-21 16:02:05 -07:00
|
|
|
}
|
2018-05-06 07:32:24 -07:00
|
|
|
|
|
|
|
// Handle files
|
|
|
|
if msg.Extra != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 16:02:05 -07:00
|
|
|
b.c.Social.SendMessage(id, steamlang.EChatEntryType_ChatMsg, msg.Username+msg.Text)
|
2017-08-27 13:59:37 -07:00
|
|
|
return "", nil
|
2017-06-21 16:02:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bsteam) getNick(id steamid.SteamId) string {
|
|
|
|
b.RLock()
|
|
|
|
defer b.RUnlock()
|
|
|
|
if name, ok := b.userMap[id]; ok {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bsteam) handleEvents() {
|
|
|
|
myLoginInfo := new(steam.LogOnDetails)
|
2018-03-04 14:52:14 -08:00
|
|
|
myLoginInfo.Username = b.GetString("Login")
|
|
|
|
myLoginInfo.Password = b.GetString("Password")
|
|
|
|
myLoginInfo.AuthCode = b.GetString("AuthCode")
|
2017-06-21 16:02:05 -07:00
|
|
|
// Attempt to read existing auth hash to avoid steam guard.
|
|
|
|
// Maybe works
|
|
|
|
//myLoginInfo.SentryFileHash, _ = ioutil.ReadFile("sentry")
|
|
|
|
for event := range b.c.Events() {
|
2018-02-26 15:33:21 -08:00
|
|
|
//b.Log.Info(event)
|
2017-06-21 16:02:05 -07:00
|
|
|
switch e := event.(type) {
|
|
|
|
case *steam.ChatMsgEvent:
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debugf("Receiving ChatMsgEvent: %#v", e)
|
2018-02-28 13:23:29 -08:00
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", b.getNick(e.ChatterId), b.Account)
|
2017-11-07 15:36:20 -08:00
|
|
|
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
|
|
|
|
}
|
2017-07-03 13:10:26 -07:00
|
|
|
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)}
|
2017-06-21 16:02:05 -07:00
|
|
|
b.Remote <- msg
|
|
|
|
case *steam.PersonaStateEvent:
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debugf("PersonaStateEvent: %#v\n", e)
|
2017-06-21 16:02:05 -07:00
|
|
|
b.Lock()
|
|
|
|
b.userMap[e.FriendId] = e.Name
|
|
|
|
b.Unlock()
|
|
|
|
case *steam.ConnectedEvent:
|
|
|
|
b.c.Auth.LogOn(myLoginInfo)
|
|
|
|
case *steam.MachineAuthUpdateEvent:
|
|
|
|
/*
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("authupdate", e)
|
|
|
|
b.Log.Info("hash", e.Hash)
|
2017-06-21 16:02:05 -07:00
|
|
|
ioutil.WriteFile("sentry", e.Hash, 0666)
|
|
|
|
*/
|
|
|
|
case *steam.LogOnFailedEvent:
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Logon failed", e)
|
2017-06-21 16:02:05 -07:00
|
|
|
switch e.Result {
|
|
|
|
case steamlang.EResult_AccountLogonDeniedNeedTwoFactorCode:
|
|
|
|
{
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Steam guard isn't letting me in! Enter 2FA code:")
|
2017-06-21 16:02:05 -07:00
|
|
|
var code string
|
|
|
|
fmt.Scanf("%s", &code)
|
|
|
|
myLoginInfo.TwoFactorCode = code
|
|
|
|
}
|
|
|
|
case steamlang.EResult_AccountLogonDenied:
|
|
|
|
{
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Steam guard isn't letting me in! Enter auth code:")
|
2017-06-21 16:02:05 -07:00
|
|
|
var code string
|
|
|
|
fmt.Scanf("%s", &code)
|
|
|
|
myLoginInfo.AuthCode = code
|
|
|
|
}
|
|
|
|
default:
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Errorf("LogOnFailedEvent: %#v ", e.Result)
|
2017-06-21 16:02:05 -07:00
|
|
|
// TODO: Handle EResult_InvalidLoginAuthCode
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case *steam.LoggedOnEvent:
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debugf("LoggedOnEvent: %#v", e)
|
2017-06-21 16:02:05 -07:00
|
|
|
b.connected <- struct{}{}
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debugf("setting online")
|
2017-06-21 16:02:05 -07:00
|
|
|
b.c.Social.SetPersonaState(steamlang.EPersonaState_Online)
|
|
|
|
case *steam.DisconnectedEvent:
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Disconnected")
|
|
|
|
b.Log.Info("Attempting to reconnect...")
|
2017-06-21 16:02:05 -07:00
|
|
|
b.c.Connect()
|
|
|
|
case steam.FatalErrorEvent:
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Error(e)
|
2017-06-21 16:02:05 -07:00
|
|
|
default:
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debugf("unknown event %#v", e)
|
2017-06-21 16:02:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|