Add new Slack connection and forked legacy Slack bridge (#582)
This commit is contained in:
parent
f7a5077d5d
commit
e19ba5a06a
|
@ -163,6 +163,7 @@ type ConfigValues struct {
|
||||||
Mattermost map[string]Protocol
|
Mattermost map[string]Protocol
|
||||||
Matrix map[string]Protocol
|
Matrix map[string]Protocol
|
||||||
Slack map[string]Protocol
|
Slack map[string]Protocol
|
||||||
|
SlackLegacy map[string]Protocol
|
||||||
Steam map[string]Protocol
|
Steam map[string]Protocol
|
||||||
Gitter map[string]Protocol
|
Gitter map[string]Protocol
|
||||||
Xmpp map[string]Protocol
|
Xmpp map[string]Protocol
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
package bslack
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/42wim/matterbridge/bridge"
|
||||||
|
"github.com/42wim/matterbridge/matterhook"
|
||||||
|
"github.com/nlopes/slack"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BslackLegacy struct {
|
||||||
|
*Bslack
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLegacy(cfg *bridge.Config) bridge.Bridger {
|
||||||
|
return &BslackLegacy{Bslack: newBridge(cfg)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BslackLegacy) Connect() error {
|
||||||
|
b.RLock()
|
||||||
|
defer b.RUnlock()
|
||||||
|
if b.GetString(incomingWebhookConfig) != "" {
|
||||||
|
switch {
|
||||||
|
case b.GetString(outgoingWebhookConfig) != "":
|
||||||
|
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
||||||
|
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
||||||
|
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
||||||
|
BindAddress: b.GetString(incomingWebhookConfig),
|
||||||
|
})
|
||||||
|
case b.GetString(tokenConfig) != "":
|
||||||
|
b.Log.Info("Connecting using token (sending)")
|
||||||
|
b.sc = slack.New(b.GetString(tokenConfig))
|
||||||
|
b.rtm = b.sc.NewRTM()
|
||||||
|
go b.rtm.ManageConnection()
|
||||||
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
||||||
|
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
||||||
|
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
||||||
|
BindAddress: b.GetString(incomingWebhookConfig),
|
||||||
|
})
|
||||||
|
default:
|
||||||
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
||||||
|
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
||||||
|
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
||||||
|
BindAddress: b.GetString(incomingWebhookConfig),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
go b.handleSlack()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if b.GetString(outgoingWebhookConfig) != "" {
|
||||||
|
b.Log.Info("Connecting using webhookurl (sending)")
|
||||||
|
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
||||||
|
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
||||||
|
DisableServer: true,
|
||||||
|
})
|
||||||
|
if b.GetString(tokenConfig) != "" {
|
||||||
|
b.Log.Info("Connecting using token (receiving)")
|
||||||
|
b.sc = slack.New(b.GetString(tokenConfig))
|
||||||
|
b.rtm = b.sc.NewRTM()
|
||||||
|
go b.rtm.ManageConnection()
|
||||||
|
go b.handleSlack()
|
||||||
|
}
|
||||||
|
} else if b.GetString(tokenConfig) != "" {
|
||||||
|
b.Log.Info("Connecting using token (sending and receiving)")
|
||||||
|
b.sc = slack.New(b.GetString(tokenConfig))
|
||||||
|
b.rtm = b.sc.NewRTM()
|
||||||
|
go b.rtm.ManageConnection()
|
||||||
|
go b.handleSlack()
|
||||||
|
}
|
||||||
|
if b.GetString(incomingWebhookConfig) == "" && b.GetString(outgoingWebhookConfig) == "" && b.GetString(tokenConfig) == "" {
|
||||||
|
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -73,6 +73,20 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(cfg *bridge.Config) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
|
// Print a deprecation warning for legacy non-bot tokens (#527).
|
||||||
|
token := cfg.GetString(tokenConfig)
|
||||||
|
if token != "" && !strings.HasPrefix(token, "xoxb") {
|
||||||
|
cfg.Log.Error("Non-bot token detected. It is STRONGLY recommended to use a proper bot-token instead.")
|
||||||
|
cfg.Log.Error("Legacy tokens may be deprecated by Slack at short notice. See the Matterbridge GitHub wiki for a migration guide.")
|
||||||
|
cfg.Log.Error("To continue using a legacy token please move your configuration to a \"slack-legacy\" bridge instead.")
|
||||||
|
cfg.Log.Error("Delaying start of bridge by 30 seconds. Future Matterbridge release will fail here unless you use a \"slack-legacy\" bridge.")
|
||||||
|
time.Sleep(30 * time.Second)
|
||||||
|
return NewLegacy(cfg)
|
||||||
|
}
|
||||||
|
return newBridge(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBridge(cfg *bridge.Config) *Bslack {
|
||||||
newCache, err := lru.New(5000)
|
newCache, err := lru.New(5000)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cfg.Log.Fatalf("Could not create LRU cache for Slack bridge: %v", err)
|
cfg.Log.Fatalf("Could not create LRU cache for Slack bridge: %v", err)
|
||||||
|
@ -97,56 +111,38 @@ func (b *Bslack) Command(cmd string) string {
|
||||||
func (b *Bslack) Connect() error {
|
func (b *Bslack) Connect() error {
|
||||||
b.RLock()
|
b.RLock()
|
||||||
defer b.RUnlock()
|
defer b.RUnlock()
|
||||||
if b.GetString(incomingWebhookConfig) != "" {
|
|
||||||
switch {
|
if b.GetString(incomingWebhookConfig) == "" && b.GetString(outgoingWebhookConfig) == "" && b.GetString(tokenConfig) == "" {
|
||||||
case b.GetString(outgoingWebhookConfig) != "":
|
return errors.New("no connection method found: WebhookBindAddress, WebhookURL or Token need to be configured")
|
||||||
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
|
||||||
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
|
||||||
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
|
||||||
BindAddress: b.GetString(incomingWebhookConfig),
|
|
||||||
})
|
|
||||||
case b.GetString(tokenConfig) != "":
|
|
||||||
b.Log.Info("Connecting using token (sending)")
|
|
||||||
b.sc = slack.New(b.GetString(tokenConfig))
|
|
||||||
b.rtm = b.sc.NewRTM()
|
|
||||||
go b.rtm.ManageConnection()
|
|
||||||
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
|
||||||
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
|
||||||
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
|
||||||
BindAddress: b.GetString(incomingWebhookConfig),
|
|
||||||
})
|
|
||||||
default:
|
|
||||||
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
|
||||||
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
|
||||||
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
|
||||||
BindAddress: b.GetString(incomingWebhookConfig),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
go b.handleSlack()
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
if b.GetString(outgoingWebhookConfig) != "" {
|
|
||||||
b.Log.Info("Connecting using webhookurl (sending)")
|
// If we have a token we use the Slack websocket-based RTM for both sending and receiving.
|
||||||
b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{
|
if token := b.GetString(tokenConfig); token != "" {
|
||||||
InsecureSkipVerify: b.GetBool(skipTLSConfig),
|
b.Log.Info("Connecting using token")
|
||||||
DisableServer: true,
|
b.sc = slack.New(token)
|
||||||
})
|
|
||||||
if b.GetString(tokenConfig) != "" {
|
|
||||||
b.Log.Info("Connecting using token (receiving)")
|
|
||||||
b.sc = slack.New(b.GetString(tokenConfig))
|
|
||||||
b.rtm = b.sc.NewRTM()
|
|
||||||
go b.rtm.ManageConnection()
|
|
||||||
go b.handleSlack()
|
|
||||||
}
|
|
||||||
} else if b.GetString(tokenConfig) != "" {
|
|
||||||
b.Log.Info("Connecting using token (sending and receiving)")
|
|
||||||
b.sc = slack.New(b.GetString(tokenConfig))
|
|
||||||
b.rtm = b.sc.NewRTM()
|
b.rtm = b.sc.NewRTM()
|
||||||
go b.rtm.ManageConnection()
|
go b.rtm.ManageConnection()
|
||||||
go b.handleSlack()
|
go b.handleSlack()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
if b.GetString(incomingWebhookConfig) == "" && b.GetString(outgoingWebhookConfig) == "" && b.GetString(tokenConfig) == "" {
|
|
||||||
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured")
|
// In absence of a token we fall back to incoming and outgoing Webhooks.
|
||||||
|
b.mh = matterhook.New(
|
||||||
|
"",
|
||||||
|
matterhook.Config{
|
||||||
|
InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
|
DisableServer: true,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if b.GetString(outgoingWebhookConfig) != "" {
|
||||||
|
b.Log.Info("Using specified webhook for outgoing messages.")
|
||||||
|
b.mh.Url = b.GetString(outgoingWebhookConfig)
|
||||||
|
}
|
||||||
|
if b.GetString(incomingWebhookConfig) != "" {
|
||||||
|
b.Log.Info("Setting up local webhook for incoming messages.")
|
||||||
|
b.mh.BindAddress = b.GetString(incomingWebhookConfig)
|
||||||
|
b.mh.DisableServer = false
|
||||||
|
go b.handleSlack()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,15 @@ package gateway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/sha1"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge"
|
"github.com/42wim/matterbridge/bridge"
|
||||||
"github.com/42wim/matterbridge/bridge/api"
|
"github.com/42wim/matterbridge/bridge/api"
|
||||||
|
@ -23,15 +28,8 @@ import (
|
||||||
bxmpp "github.com/42wim/matterbridge/bridge/xmpp"
|
bxmpp "github.com/42wim/matterbridge/bridge/xmpp"
|
||||||
bzulip "github.com/42wim/matterbridge/bridge/zulip"
|
bzulip "github.com/42wim/matterbridge/bridge/zulip"
|
||||||
"github.com/hashicorp/golang-lru"
|
"github.com/hashicorp/golang-lru"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
// "github.com/davecgh/go-spew/spew"
|
|
||||||
"crypto/sha1"
|
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/peterhellberg/emojilib"
|
"github.com/peterhellberg/emojilib"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Gateway struct {
|
type Gateway struct {
|
||||||
|
@ -55,19 +53,20 @@ type BrMsgID struct {
|
||||||
var flog *log.Entry
|
var flog *log.Entry
|
||||||
|
|
||||||
var bridgeMap = map[string]bridge.Factory{
|
var bridgeMap = map[string]bridge.Factory{
|
||||||
"api": api.New,
|
"api": api.New,
|
||||||
"discord": bdiscord.New,
|
"discord": bdiscord.New,
|
||||||
"gitter": bgitter.New,
|
"gitter": bgitter.New,
|
||||||
"irc": birc.New,
|
"irc": birc.New,
|
||||||
"mattermost": bmattermost.New,
|
"mattermost": bmattermost.New,
|
||||||
"matrix": bmatrix.New,
|
"matrix": bmatrix.New,
|
||||||
"rocketchat": brocketchat.New,
|
"rocketchat": brocketchat.New,
|
||||||
"slack": bslack.New,
|
"slack-legacy": bslack.NewLegacy,
|
||||||
"sshchat": bsshchat.New,
|
"slack": bslack.New,
|
||||||
"steam": bsteam.New,
|
"sshchat": bsshchat.New,
|
||||||
"telegram": btelegram.New,
|
"steam": bsteam.New,
|
||||||
"xmpp": bxmpp.New,
|
"telegram": btelegram.New,
|
||||||
"zulip": bzulip.New,
|
"xmpp": bxmpp.New,
|
||||||
|
"zulip": bzulip.New,
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
Loading…
Reference in New Issue