Refactor connecting logic slack/mattermost. Fixes #216

This commit is contained in:
Wim 2017-07-15 16:49:47 +02:00
parent 6dee988b76
commit 9dcd51fb80
2 changed files with 100 additions and 35 deletions

View File

@ -1,6 +1,7 @@
package bmattermost package bmattermost
import ( import (
"errors"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/matterclient" "github.com/42wim/matterbridge/matterclient"
"github.com/42wim/matterbridge/matterhook" "github.com/42wim/matterbridge/matterhook"
@ -53,33 +54,49 @@ func (b *Bmattermost) Command(cmd string) string {
} }
func (b *Bmattermost) Connect() error { func (b *Bmattermost) Connect() error {
if b.Config.WebhookURL != "" && b.Config.WebhookBindAddress != "" { if b.Config.WebhookBindAddress != "" {
flog.Info("Connecting using webhookurl and webhookbindaddress") if b.Config.WebhookURL != "" {
b.mh = matterhook.New(b.Config.WebhookURL, flog.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, } else if b.Config.Login != "" {
BindAddress: b.Config.WebhookBindAddress}) flog.Info("Connecting using login/password (sending)")
} else if b.Config.WebhookURL != "" { err := b.apiLogin()
flog.Info("Connecting using webhookurl (for posting) and token") if err != nil {
return err
}
} else {
flog.Info("Connecting using webhookbindaddress (receiving)")
b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
BindAddress: b.Config.WebhookBindAddress})
}
go b.handleMatter()
return nil
}
if b.Config.WebhookURL != "" {
flog.Info("Connecting using webhookurl (sending)")
b.mh = matterhook.New(b.Config.WebhookURL, b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
DisableServer: true}) DisableServer: true})
} else { if b.Config.Login != "" {
flog.Info("Connecting using token") flog.Info("Connecting using login/password (receiving)")
b.mc = matterclient.New(b.Config.Login, b.Config.Password, err := b.apiLogin()
b.Config.Team, b.Config.Server) if err != nil {
b.mc.SkipTLSVerify = b.Config.SkipTLSVerify return err
b.mc.NoTLS = b.Config.NoTLS }
flog.Infof("Connecting %s (team: %s) on %s", b.Config.Login, b.Config.Team, b.Config.Server) go b.handleMatter()
err := b.mc.Login() }
return nil
} else if b.Config.Login != "" {
flog.Info("Connecting using login/password (sending and receiving)")
err := b.apiLogin()
if err != nil { if err != nil {
return err return err
} }
flog.Info("Connection succeeded") go b.handleMatter()
b.TeamId = b.mc.GetTeamId() }
go b.mc.WsReceiver() if b.Config.WebhookBindAddress == "" && b.Config.WebhookURL == "" && b.Config.Login == "" {
go b.mc.StatusLoop() return errors.New("No connection method found. See that you have WebhookBindAddress, WebhookURL or Login/Password/Server/Team configured.")
} }
go b.handleMatter()
return nil return nil
} }
@ -124,11 +141,11 @@ func (b *Bmattermost) Send(msg config.Message) error {
func (b *Bmattermost) handleMatter() { func (b *Bmattermost) handleMatter() {
mchan := make(chan *MMMessage) mchan := make(chan *MMMessage)
if b.Config.WebhookBindAddress != "" && b.Config.WebhookURL != "" { if b.Config.WebhookBindAddress != "" {
flog.Debugf("Choosing webhooks based receiving") flog.Debugf("Choosing webhooks based receiving")
go b.handleMatterHook(mchan) go b.handleMatterHook(mchan)
} else { } else {
flog.Debugf("Choosing login (api) based receiving") flog.Debugf("Choosing login/password based receiving")
go b.handleMatterClient(mchan) go b.handleMatterClient(mchan)
} }
for message := range mchan { for message := range mchan {
@ -185,3 +202,20 @@ func (b *Bmattermost) handleMatterHook(mchan chan *MMMessage) {
mchan <- m mchan <- m
} }
} }
func (b *Bmattermost) apiLogin() error {
b.mc = matterclient.New(b.Config.Login, b.Config.Password,
b.Config.Team, b.Config.Server)
b.mc.SkipTLSVerify = b.Config.SkipTLSVerify
b.mc.NoTLS = b.Config.NoTLS
flog.Infof("Connecting %s (team: %s) on %s", b.Config.Login, b.Config.Team, b.Config.Server)
err := b.mc.Login()
if err != nil {
return err
}
flog.Info("Connection succeeded")
b.TeamId = b.mc.GetTeamId()
go b.mc.WsReceiver()
go b.mc.StatusLoop()
return nil
}

View File

@ -1,6 +1,7 @@
package bslack package bslack
import ( import (
"errors"
"fmt" "fmt"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/matterhook" "github.com/42wim/matterbridge/matterhook"
@ -53,22 +54,52 @@ func (b *Bslack) Command(cmd string) string {
} }
func (b *Bslack) Connect() error { func (b *Bslack) Connect() error {
if b.Config.WebhookURL != "" && b.Config.WebhookBindAddress != "" { if b.Config.WebhookBindAddress != "" {
flog.Info("Connecting using webhookurl and webhookbindaddress") if b.Config.WebhookURL != "" {
flog.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
BindAddress: b.Config.WebhookBindAddress})
} else if b.Config.Token != "" {
flog.Info("Connecting using token (sending)")
b.sc = slack.New(b.Config.Token)
b.rtm = b.sc.NewRTM()
go b.rtm.ManageConnection()
flog.Info("Connecting using webhookbindaddress (receiving)")
b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
BindAddress: b.Config.WebhookBindAddress})
} else {
flog.Info("Connecting using webhookbindaddress (receiving)")
b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
BindAddress: b.Config.WebhookBindAddress})
}
go b.handleSlack()
return nil
}
if b.Config.WebhookURL != "" {
flog.Info("Connecting using webhookurl (sending)")
b.mh = matterhook.New(b.Config.WebhookURL, b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{BindAddress: b.Config.WebhookBindAddress}) matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
} else if b.Config.WebhookURL != "" { DisableServer: true})
flog.Info("Connecting using webhookurl (for posting) and token") if b.Config.Token != "" {
b.mh = matterhook.New(b.Config.WebhookURL, flog.Info("Connecting using token (receiving)")
matterhook.Config{DisableServer: true}) b.sc = slack.New(b.Config.Token)
} else { b.rtm = b.sc.NewRTM()
flog.Info("Connecting using token") go b.rtm.ManageConnection()
go b.handleSlack()
}
} else if b.Config.Token != "" {
flog.Info("Connecting using token (sending and receiving)")
b.sc = slack.New(b.Config.Token) b.sc = slack.New(b.Config.Token)
b.rtm = b.sc.NewRTM() b.rtm = b.sc.NewRTM()
go b.rtm.ManageConnection() go b.rtm.ManageConnection()
go b.handleSlack()
}
if b.Config.WebhookBindAddress == "" && b.Config.WebhookURL == "" && b.Config.Token == "" {
return errors.New("No connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured.")
} }
flog.Info("Connection succeeded")
go b.handleSlack()
return nil return nil
} }
@ -79,7 +110,7 @@ func (b *Bslack) Disconnect() error {
func (b *Bslack) JoinChannel(channel string) error { func (b *Bslack) JoinChannel(channel string) error {
// we can only join channels using the API // we can only join channels using the API
if b.Config.WebhookURL == "" || b.Config.WebhookBindAddress == "" { if b.Config.WebhookURL == "" && b.Config.WebhookBindAddress == "" {
if strings.HasPrefix(b.Config.Token, "xoxb") { if strings.HasPrefix(b.Config.Token, "xoxb") {
// TODO check if bot has already joined channel // TODO check if bot has already joined channel
return nil return nil
@ -176,7 +207,7 @@ func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {
func (b *Bslack) handleSlack() { func (b *Bslack) handleSlack() {
mchan := make(chan *MMMessage) mchan := make(chan *MMMessage)
if b.Config.WebhookBindAddress != "" && b.Config.WebhookURL != "" { if b.Config.WebhookBindAddress != "" {
flog.Debugf("Choosing webhooks based receiving") flog.Debugf("Choosing webhooks based receiving")
go b.handleMatterHook(mchan) go b.handleMatterHook(mchan)
} else { } else {