2017-06-21 23:02:05 +00:00
|
|
|
package bsteam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-11-15 18:24:22 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2018-05-06 14:32:24 +00:00
|
|
|
|
2018-02-26 23:33:21 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2017-06-21 23:02:05 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-05-06 14:32:24 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2017-06-21 23:02:05 +00: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 22:52:14 +00:00
|
|
|
*bridge.Config
|
2017-06-21 23:02:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
|
|
|
b := &Bsteam{Config: cfg}
|
2017-06-21 23:02:05 +00:00
|
|
|
b.userMap = make(map[steamid.SteamId]string)
|
|
|
|
b.connected = make(chan struct{})
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bsteam) Connect() error {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting")
|
2017-06-21 23:02:05 +00:00
|
|
|
b.c = steam.NewClient()
|
|
|
|
go b.handleEvents()
|
|
|
|
go b.c.Connect()
|
|
|
|
select {
|
|
|
|
case <-b.connected:
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connection succeeded")
|
2017-06-21 23:02:05 +00: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 12:51:41 +00:00
|
|
|
func (b *Bsteam) JoinChannel(channel config.ChannelInfo) error {
|
|
|
|
id, err := steamid.NewId(channel.Name)
|
2017-06-21 23:02:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.c.Social.JoinChat(id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 20:59:37 +00:00
|
|
|
func (b *Bsteam) Send(msg config.Message) (string, error) {
|
2017-09-11 20:45:15 +00:00
|
|
|
// ignore delete messages
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2017-09-11 20:45:15 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2017-06-21 23:02:05 +00:00
|
|
|
id, err := steamid.NewId(msg.Channel)
|
|
|
|
if err != nil {
|
2017-08-27 20:59:37 +00:00
|
|
|
return "", err
|
2017-06-21 23:02:05 +00:00
|
|
|
}
|
2018-05-06 14:32:24 +00: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)
|
|
|
|
}
|
2018-12-07 22:48:24 +00:00
|
|
|
for i := range msg.Extra["file"] {
|
|
|
|
if err := b.handleFileInfo(&msg, msg.Extra["file"][i]); err != nil {
|
|
|
|
b.Log.Error(err)
|
2018-05-06 14:32:24 +00:00
|
|
|
}
|
2018-12-07 22:48:24 +00:00
|
|
|
b.c.Social.SendMessage(id, steamlang.EChatEntryType_ChatMsg, msg.Username+msg.Text)
|
2018-05-06 14:32:24 +00:00
|
|
|
}
|
2018-12-07 22:48:24 +00:00
|
|
|
return "", nil
|
2018-05-06 14:32:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 23:02:05 +00:00
|
|
|
b.c.Social.SendMessage(id, steamlang.EChatEntryType_ChatMsg, msg.Username+msg.Text)
|
2017-08-27 20:59:37 +00:00
|
|
|
return "", nil
|
2017-06-21 23:02:05 +00: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"
|
|
|
|
}
|