2018-11-13 19:51:19 +00:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/bridge"
|
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
2020-03-01 19:59:19 +00:00
|
|
|
"github.com/slack-go/slack"
|
2018-11-13 19:51:19 +00:00
|
|
|
)
|
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
type BLegacy struct {
|
2018-11-13 19:51:19 +00:00
|
|
|
*Bslack
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLegacy(cfg *bridge.Config) bridge.Bridger {
|
2019-06-13 22:42:55 +00:00
|
|
|
b := &BLegacy{Bslack: newBridge(cfg)}
|
|
|
|
b.legacy = true
|
|
|
|
return b
|
2018-11-13 19:51:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
func (b *BLegacy) Connect() error {
|
2018-11-13 19:51:19 +00:00
|
|
|
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)")
|
2019-03-20 21:52:23 +00:00
|
|
|
b.sc = slack.New(b.GetString(tokenConfig), slack.OptionDebug(b.GetBool("debug")))
|
|
|
|
b.channels = newChannelManager(b.Log, b.sc)
|
|
|
|
b.users = newUserManager(b.Log, b.sc)
|
2018-11-13 19:51:19 +00:00
|
|
|
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)")
|
2019-03-20 21:52:23 +00:00
|
|
|
b.sc = slack.New(b.GetString(tokenConfig), slack.OptionDebug(b.GetBool("debug")))
|
|
|
|
b.channels = newChannelManager(b.Log, b.sc)
|
|
|
|
b.users = newUserManager(b.Log, b.sc)
|
2018-11-13 19:51:19 +00:00
|
|
|
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
|
|
|
|
}
|