| 1 | package bslack |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "github.com/matterbridge-org/matterbridge/bridge" |
| 7 | "github.com/matterbridge-org/matterbridge/matterhook" |
| 8 | "github.com/slack-go/slack" |
| 9 | ) |
| 10 | |
| 11 | type BLegacy struct { |
| 12 | *Bslack |
| 13 | } |
| 14 | |
| 15 | func NewLegacy(cfg *bridge.Config) bridge.Bridger { |
| 16 | b := &BLegacy{Bslack: newBridge(cfg)} |
| 17 | b.legacy = true |
| 18 | return b |
| 19 | } |
| 20 | |
| 21 | func (b *BLegacy) Connect() error { |
| 22 | b.RLock() |
| 23 | defer b.RUnlock() |
| 24 | if b.GetString(incomingWebhookConfig) != "" { |
| 25 | switch { |
| 26 | case b.GetString(outgoingWebhookConfig) != "": |
| 27 | b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)") |
| 28 | b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{ |
| 29 | InsecureSkipVerify: b.GetBool(skipTLSConfig), |
| 30 | BindAddress: b.GetString(incomingWebhookConfig), |
| 31 | }) |
| 32 | case b.GetString(tokenConfig) != "": |
| 33 | b.Log.Info("Connecting using token (sending)") |
| 34 | b.sc = slack.New(b.GetString(tokenConfig)) |
| 35 | b.rtm = b.sc.NewRTM() |
| 36 | go b.rtm.ManageConnection() |
| 37 | b.Log.Info("Connecting using webhookbindaddress (receiving)") |
| 38 | b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{ |
| 39 | InsecureSkipVerify: b.GetBool(skipTLSConfig), |
| 40 | BindAddress: b.GetString(incomingWebhookConfig), |
| 41 | }) |
| 42 | default: |
| 43 | b.Log.Info("Connecting using webhookbindaddress (receiving)") |
| 44 | b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{ |
| 45 | InsecureSkipVerify: b.GetBool(skipTLSConfig), |
| 46 | BindAddress: b.GetString(incomingWebhookConfig), |
| 47 | }) |
| 48 | } |
| 49 | go b.handleSlack() |
| 50 | return nil |
| 51 | } |
| 52 | if b.GetString(outgoingWebhookConfig) != "" { |
| 53 | b.Log.Info("Connecting using webhookurl (sending)") |
| 54 | b.mh = matterhook.New(b.GetString(outgoingWebhookConfig), matterhook.Config{ |
| 55 | InsecureSkipVerify: b.GetBool(skipTLSConfig), |
| 56 | DisableServer: true, |
| 57 | }) |
| 58 | if b.GetString(tokenConfig) != "" { |
| 59 | b.Log.Info("Connecting using token (receiving)") |
| 60 | b.sc = slack.New(b.GetString(tokenConfig), slack.OptionDebug(b.GetBool("debug"))) |
| 61 | b.channels = newChannelManager(b.Log, b.sc) |
| 62 | b.users = newUserManager(b.Log, b.sc) |
| 63 | b.rtm = b.sc.NewRTM() |
| 64 | go b.rtm.ManageConnection() |
| 65 | go b.handleSlack() |
| 66 | } |
| 67 | } else if b.GetString(tokenConfig) != "" { |
| 68 | b.Log.Info("Connecting using token (sending and receiving)") |
| 69 | b.sc = slack.New(b.GetString(tokenConfig), slack.OptionDebug(b.GetBool("debug"))) |
| 70 | b.channels = newChannelManager(b.Log, b.sc) |
| 71 | b.users = newUserManager(b.Log, b.sc) |
| 72 | b.rtm = b.sc.NewRTM() |
| 73 | go b.rtm.ManageConnection() |
| 74 | go b.handleSlack() |
| 75 | } |
| 76 | if b.GetString(incomingWebhookConfig) == "" && b.GetString(outgoingWebhookConfig) == "" && b.GetString(tokenConfig) == "" { |
| 77 | return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured") |
| 78 | } |
| 79 | return nil |
| 80 | } |
| 81 | |