2016-07-11 21:23:33 +02:00
|
|
|
package matterclient
|
|
|
|
|
|
|
|
import (
|
2016-08-15 18:49:17 +02:00
|
|
|
"encoding/json"
|
2017-04-09 23:15:11 +02:00
|
|
|
"fmt"
|
2016-07-11 21:23:33 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2017-07-25 23:57:27 +02:00
|
|
|
"github.com/hashicorp/golang-lru"
|
2016-07-11 21:23:33 +02:00
|
|
|
"github.com/jpillora/backoff"
|
2018-11-15 19:24:22 +01:00
|
|
|
prefixed "github.com/matterbridge/logrus-prefixed-formatter"
|
2018-11-18 18:55:05 +01:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
2018-11-15 19:24:22 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-07-11 21:23:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Credentials struct {
|
|
|
|
Login string
|
|
|
|
Team string
|
|
|
|
Pass string
|
2018-10-26 16:47:56 +02:00
|
|
|
Token string
|
|
|
|
CookieToken bool
|
2016-07-11 21:23:33 +02:00
|
|
|
Server string
|
|
|
|
NoTLS bool
|
|
|
|
SkipTLSVerify bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
2016-08-15 18:49:17 +02:00
|
|
|
Raw *model.WebSocketEvent
|
2016-07-11 21:23:33 +02:00
|
|
|
Post *model.Post
|
|
|
|
Team string
|
|
|
|
Channel string
|
|
|
|
Username string
|
|
|
|
Text string
|
2017-04-07 22:27:36 +02:00
|
|
|
Type string
|
2017-06-18 15:44:54 +02:00
|
|
|
UserID string
|
2016-07-11 21:23:33 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 20:43:43 +01:00
|
|
|
//nolint:golint
|
2016-07-11 21:23:33 +02:00
|
|
|
type Team struct {
|
|
|
|
Team *model.Team
|
|
|
|
Id string
|
2017-08-16 23:41:35 +02:00
|
|
|
Channels []*model.Channel
|
|
|
|
MoreChannels []*model.Channel
|
2016-07-11 21:23:33 +02:00
|
|
|
Users map[string]*model.User
|
|
|
|
}
|
|
|
|
|
|
|
|
type MMClient struct {
|
|
|
|
sync.RWMutex
|
|
|
|
*Credentials
|
2017-04-09 23:15:11 +02:00
|
|
|
Team *Team
|
|
|
|
OtherTeams []*Team
|
2017-08-16 23:41:35 +02:00
|
|
|
Client *model.Client4
|
2017-04-09 23:15:11 +02:00
|
|
|
User *model.User
|
|
|
|
Users map[string]*model.User
|
|
|
|
MessageChan chan *Message
|
|
|
|
log *log.Entry
|
|
|
|
WsClient *websocket.Conn
|
|
|
|
WsQuit bool
|
|
|
|
WsAway bool
|
|
|
|
WsConnected bool
|
|
|
|
WsSequence int64
|
|
|
|
WsPingChan chan *model.WebSocketResponse
|
|
|
|
ServerVersion string
|
2017-07-01 23:28:16 +02:00
|
|
|
OnWsConnect func()
|
2017-07-25 23:57:27 +02:00
|
|
|
lruCache *lru.Cache
|
2016-07-11 21:23:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(login, pass, team, server string) *MMClient {
|
|
|
|
cred := &Credentials{Login: login, Pass: pass, Team: team, Server: server}
|
|
|
|
mmclient := &MMClient{Credentials: cred, MessageChan: make(chan *Message, 100), Users: make(map[string]*model.User)}
|
2018-02-21 01:11:41 +01:00
|
|
|
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true})
|
|
|
|
mmclient.log = log.WithFields(log.Fields{"prefix": "matterclient"})
|
2017-07-25 23:57:27 +02:00
|
|
|
mmclient.lruCache, _ = lru.New(500)
|
2016-07-11 21:23:33 +02:00
|
|
|
return mmclient
|
|
|
|
}
|
|
|
|
|
2018-02-21 01:11:41 +01:00
|
|
|
func (m *MMClient) SetDebugLog() {
|
2018-02-22 22:57:34 +01:00
|
|
|
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false, ForceFormatting: true})
|
2018-02-21 01:11:41 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 21:23:33 +02:00
|
|
|
func (m *MMClient) SetLogLevel(level string) {
|
|
|
|
l, err := log.ParseLevel(level)
|
|
|
|
if err != nil {
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.SetLevel(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) Login() error {
|
2016-12-08 00:07:24 +01:00
|
|
|
// check if this is a first connect or a reconnection
|
|
|
|
firstConnection := true
|
2017-07-14 00:35:01 +02:00
|
|
|
if m.WsConnected {
|
2016-12-08 00:07:24 +01:00
|
|
|
firstConnection = false
|
|
|
|
}
|
2016-07-11 21:23:33 +02:00
|
|
|
m.WsConnected = false
|
|
|
|
if m.WsQuit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b := &backoff.Backoff{
|
|
|
|
Min: time.Second,
|
|
|
|
Max: 5 * time.Minute,
|
|
|
|
Jitter: true,
|
|
|
|
}
|
2018-10-26 16:47:56 +02:00
|
|
|
|
2018-11-29 23:53:43 +01:00
|
|
|
// do initialization setup
|
|
|
|
if err := m.initClient(firstConnection, b); err != nil {
|
|
|
|
return err
|
2017-04-09 23:15:11 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 23:53:43 +01:00
|
|
|
if err := m.doLogin(firstConnection, b); err != nil {
|
|
|
|
return err
|
2016-07-11 21:23:33 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 23:53:43 +01:00
|
|
|
if err := m.initUser(); err != nil {
|
2016-07-11 21:23:33 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Team == nil {
|
2018-03-15 20:50:32 +01:00
|
|
|
validTeamNames := make([]string, len(m.OtherTeams))
|
|
|
|
for i, t := range m.OtherTeams {
|
|
|
|
validTeamNames[i] = t.Team.Name
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Team '%s' not found in %v", m.Credentials.Team, validTeamNames)
|
2016-07-11 21:23:33 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 17:49:12 +02:00
|
|
|
m.wsConnect()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-11 21:23:33 +02:00
|
|
|
func (m *MMClient) Logout() error {
|
|
|
|
m.log.Debugf("logout as %s (team: %s) on %s", m.Credentials.Login, m.Credentials.Team, m.Credentials.Server)
|
|
|
|
m.WsQuit = true
|
|
|
|
m.WsClient.Close()
|
|
|
|
m.WsClient.UnderlyingConn().Close()
|
2017-07-01 22:41:28 +02:00
|
|
|
if strings.Contains(m.Credentials.Pass, model.SESSION_COOKIE_TOKEN) {
|
|
|
|
m.log.Debug("Not invalidating session in logout, credential is a token")
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-16 23:41:35 +02:00
|
|
|
_, resp := m.Client.Logout()
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
2016-07-11 21:23:33 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MMClient) WsReceiver() {
|
|
|
|
for {
|
2016-08-15 18:49:17 +02:00
|
|
|
var rawMsg json.RawMessage
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if m.WsQuit {
|
|
|
|
m.log.Debug("exiting WsReceiver")
|
|
|
|
return
|
2016-07-11 21:23:33 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:08:17 +02:00
|
|
|
if !m.WsConnected {
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-19 22:58:42 +02:00
|
|
|
if _, rawMsg, err = m.WsClient.ReadMessage(); err != nil {
|
|
|
|
m.log.Error("error:", err)
|
|
|
|
// reconnect
|
2017-07-01 17:49:12 +02:00
|
|
|
m.wsConnect()
|
2016-08-19 22:58:42 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 18:49:17 +02:00
|
|
|
var event model.WebSocketEvent
|
|
|
|
if err := json.Unmarshal(rawMsg, &event); err == nil && event.IsValid() {
|
2017-07-01 22:43:53 +02:00
|
|
|
m.log.Debugf("WsReceiver event: %#v", event)
|
2016-08-15 18:49:17 +02:00
|
|
|
msg := &Message{Raw: &event, Team: m.Credentials.Team}
|
|
|
|
m.parseMessage(msg)
|
2017-07-25 23:57:27 +02:00
|
|
|
// check if we didn't empty the message
|
|
|
|
if msg.Text != "" {
|
|
|
|
m.MessageChan <- msg
|
2017-09-08 00:16:17 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// if we have file attached but the message is empty, also send it
|
|
|
|
if msg.Post != nil {
|
2017-09-18 23:44:16 +02:00
|
|
|
if msg.Text != "" || len(msg.Post.FileIds) > 0 || msg.Post.Type == "slack_attachment" {
|
2017-09-08 00:16:17 +02:00
|
|
|
m.MessageChan <- msg
|
|
|
|
}
|
2017-07-25 23:57:27 +02:00
|
|
|
}
|
2016-08-15 18:49:17 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-07-11 21:23:33 +02:00
|
|
|
|
2016-08-15 18:49:17 +02:00
|
|
|
var response model.WebSocketResponse
|
|
|
|
if err := json.Unmarshal(rawMsg, &response); err == nil && response.IsValid() {
|
2017-07-01 22:43:53 +02:00
|
|
|
m.log.Debugf("WsReceiver response: %#v", response)
|
2016-08-15 18:49:17 +02:00
|
|
|
m.parseResponse(response)
|
|
|
|
continue
|
|
|
|
}
|
2016-07-11 21:23:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 22:53:09 +02:00
|
|
|
func (m *MMClient) StatusLoop() {
|
2017-07-01 23:28:16 +02:00
|
|
|
retries := 0
|
|
|
|
backoff := time.Second * 60
|
|
|
|
if m.OnWsConnect != nil {
|
|
|
|
m.OnWsConnect()
|
|
|
|
}
|
2018-11-07 20:36:50 +01:00
|
|
|
m.log.Debug("StatusLoop:", m.OnWsConnect != nil)
|
2016-08-14 22:53:09 +02:00
|
|
|
for {
|
|
|
|
if m.WsQuit {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m.WsConnected {
|
2018-12-08 17:04:10 +01:00
|
|
|
if err := m.checkAlive(); err != nil {
|
|
|
|
log.Errorf("Connection is not alive: %#v", err)
|
|
|
|
}
|
2016-08-15 18:49:17 +02:00
|
|
|
select {
|
|
|
|
case <-m.WsPingChan:
|
|
|
|
m.log.Debug("WS PONG received")
|
2017-07-01 23:28:16 +02:00
|
|
|
backoff = time.Second * 60
|
2016-08-15 18:49:17 +02:00
|
|
|
case <-time.After(time.Second * 5):
|
2017-07-01 23:28:16 +02:00
|
|
|
if retries > 3 {
|
2017-11-16 20:19:52 +01:00
|
|
|
m.log.Debug("StatusLoop() timeout")
|
2017-07-01 23:28:16 +02:00
|
|
|
m.Logout()
|
|
|
|
m.WsQuit = false
|
2017-11-16 20:19:52 +01:00
|
|
|
err := m.Login()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Login failed: %#v", err)
|
|
|
|
break
|
|
|
|
}
|
2017-07-01 23:28:16 +02:00
|
|
|
if m.OnWsConnect != nil {
|
|
|
|
m.OnWsConnect()
|
|
|
|
}
|
|
|
|
go m.WsReceiver()
|
|
|
|
} else {
|
|
|
|
retries++
|
|
|
|
backoff = time.Second * 5
|
|
|
|
}
|
2016-08-14 22:53:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-01 23:28:16 +02:00
|
|
|
time.Sleep(backoff)
|
2016-08-14 22:53:09 +02:00
|
|
|
}
|
|
|
|
}
|