5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-26 00:51:36 +00:00

Deprecate URL,useAPI,BindAddress (slack,mattermost,rocketchat)

This commit is contained in:
Wim 2017-06-24 19:36:10 +02:00
parent 1b1a9ce250
commit 830361e48b
6 changed files with 122 additions and 77 deletions

View File

@ -40,7 +40,7 @@ type ChannelInfo struct {
type Protocol struct { type Protocol struct {
AuthCode string // steam AuthCode string // steam
BindAddress string // mattermost, slack BindAddress string // mattermost, slack // DEPRECATED
Buffer int // api Buffer int // api
EditSuffix string // mattermost, slack, discord, telegram, gitter EditSuffix string // mattermost, slack, discord, telegram, gitter
EditDisable bool // mattermost, slack, discord, telegram, gitter EditDisable bool // mattermost, slack, discord, telegram, gitter
@ -72,12 +72,14 @@ type Protocol struct {
SkipTLSVerify bool // IRC, mattermost SkipTLSVerify bool // IRC, mattermost
Team string // mattermost Team string // mattermost
Token string // gitter, slack, discord, api Token string // gitter, slack, discord, api
URL string // mattermost, slack, matrix URL string // mattermost, slack // DEPRECATED
UseAPI bool // mattermost, slack UseAPI bool // mattermost, slack
UseSASL bool // IRC UseSASL bool // IRC
UseTLS bool // IRC UseTLS bool // IRC
UseFirstName bool // telegram UseFirstName bool // telegram
WebhookURL string // discord WebhookBindAddress string // mattermost, slack
WebhookURL string // mattermost, slack
WebhookUse string // mattermost, slack, discord
} }
type ChannelOptions struct { type ChannelOptions struct {
@ -128,6 +130,28 @@ func NewConfig(cfgfile string) *Config {
if _, err := toml.DecodeFile(cfgfile, &cfg); err != nil { if _, err := toml.DecodeFile(cfgfile, &cfg); err != nil {
log.Fatal(err) log.Fatal(err)
} }
fail := false
for k, v := range cfg.Mattermost {
res := Deprecated(v, "mattermost."+k)
if res {
fail = res
}
}
for k, v := range cfg.Slack {
res := Deprecated(v, "slack."+k)
if res {
fail = res
}
}
for k, v := range cfg.Rocketchat {
res := Deprecated(v, "rocketchat."+k)
if res {
fail = res
}
}
if fail {
log.Fatalf("Fix your config. Please see changelog for more information")
}
return &cfg return &cfg
} }
@ -178,3 +202,17 @@ func GetIconURL(msg *Message, cfg *Protocol) string {
iconURL = strings.Replace(iconURL, "{PROTOCOL}", protocol, -1) iconURL = strings.Replace(iconURL, "{PROTOCOL}", protocol, -1)
return iconURL return iconURL
} }
func Deprecated(cfg Protocol, account string) bool {
if cfg.BindAddress != "" {
log.Printf("ERROR: %s BindAddress is deprecated, you need to change it to WebhookBindAddress.", account)
} else if cfg.URL != "" {
log.Printf("ERROR: %s URL is deprecated, you need to change it to WebhookURL.", account)
} else if cfg.UseAPI == true {
log.Printf("ERROR: %s UseAPI is deprecated, it's enabled by default, please remove it from your config file.", account)
} else {
return false
}
return true
//log.Fatalf("ERROR: Fix your config: %s", account)
}

View File

@ -49,6 +49,11 @@ func New(cfg config.Protocol, account string, c chan config.Message) *bdiscord {
func (b *bdiscord) Connect() error { func (b *bdiscord) Connect() error {
var err error var err error
flog.Info("Connecting") flog.Info("Connecting")
if b.Config.WebhookURL == "" {
flog.Info("Connecting using token")
} else {
flog.Info("Connecting using webhookurl (for posting) and token")
}
if !strings.HasPrefix(b.Config.Token, "Bot ") { if !strings.HasPrefix(b.Config.Token, "Bot ") {
b.Config.Token = "Bot " + b.Config.Token b.Config.Token = "Bot " + b.Config.Token
} }
@ -110,7 +115,7 @@ func (b *bdiscord) Send(msg config.Message) error {
return nil return nil
} }
if b.Config.WebhookURL == "" { if b.Config.WebhookURL == "" {
flog.Debugf("Broadcasting using API") flog.Debugf("Broadcasting using token (API)")
b.c.ChannelMessageSend(channelID, msg.Username+msg.Text) b.c.ChannelMessageSend(channelID, msg.Username+msg.Text)
} else { } else {
flog.Debugf("Broadcasting using Webhook") flog.Debugf("Broadcasting using Webhook")

View File

@ -55,12 +55,18 @@ func (b *Bmattermost) Command(cmd string) string {
} }
func (b *Bmattermost) Connect() error { func (b *Bmattermost) Connect() error {
if !b.Config.UseAPI { if b.Config.WebhookURL != "" && b.Config.WebhookBindAddress != "" {
flog.Info("Connecting webhooks") flog.Info("Connecting using webhookurl and webhookbindaddress")
b.mh = matterhook.New(b.Config.URL, b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
BindAddress: b.Config.BindAddress}) BindAddress: b.Config.WebhookBindAddress})
} else if b.Config.WebhookURL != "" {
flog.Info("Connecting using webhookurl (for posting) and token")
b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
DisableServer: true})
} else { } else {
flog.Info("Connecting using token")
b.mc = matterclient.New(b.Config.Login, b.Config.Password, b.mc = matterclient.New(b.Config.Login, b.Config.Password,
b.Config.Team, b.Config.Server) b.Config.Team, b.Config.Server)
b.mc.SkipTLSVerify = b.Config.SkipTLSVerify b.mc.SkipTLSVerify = b.Config.SkipTLSVerify
@ -85,7 +91,7 @@ func (b *Bmattermost) Disconnect() error {
func (b *Bmattermost) JoinChannel(channel string) error { func (b *Bmattermost) JoinChannel(channel string) error {
// we can only join channels using the API // we can only join channels using the API
if b.Config.UseAPI { if b.Config.WebhookURL == "" && b.Config.WebhookBindAddress == "" {
return b.mc.JoinChannel(b.mc.GetChannelId(channel, "")) return b.mc.JoinChannel(b.mc.GetChannelId(channel, ""))
} }
return nil return nil
@ -100,7 +106,7 @@ func (b *Bmattermost) Send(msg config.Message) error {
if b.Config.PrefixMessagesWithNick { if b.Config.PrefixMessagesWithNick {
message = nick + message message = nick + message
} }
if !b.Config.UseAPI { if b.Config.WebhookURL != "" {
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL} matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
matterMessage.IconURL = msg.Avatar matterMessage.IconURL = msg.Avatar
matterMessage.Channel = channel matterMessage.Channel = channel
@ -119,12 +125,13 @@ func (b *Bmattermost) Send(msg config.Message) error {
} }
func (b *Bmattermost) handleMatter() { func (b *Bmattermost) handleMatter() {
flog.Debugf("Choosing API based Mattermost connection: %t", b.Config.UseAPI)
mchan := make(chan *MMMessage) mchan := make(chan *MMMessage)
if b.Config.UseAPI { if b.Config.WebhookBindAddress != "" && b.Config.WebhookURL != "" {
go b.handleMatterClient(mchan) flog.Debugf("Choosing webhooks based receiving")
} else {
go b.handleMatterHook(mchan) go b.handleMatterHook(mchan)
} else {
flog.Debugf("Choosing login (api) based receiving")
go b.handleMatterClient(mchan)
} }
for message := range mchan { for message := range mchan {
flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.Account) flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.Account)

View File

@ -41,10 +41,10 @@ func (b *Brocketchat) Command(cmd string) string {
func (b *Brocketchat) Connect() error { func (b *Brocketchat) Connect() error {
flog.Info("Connecting webhooks") flog.Info("Connecting webhooks")
b.mh = matterhook.New(b.Config.URL, b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
DisableServer: true}) DisableServer: true})
b.rh = rockethook.New(b.Config.URL, rockethook.Config{BindAddress: b.Config.BindAddress}) b.rh = rockethook.New(b.Config.WebhookURL, rockethook.Config{BindAddress: b.Config.WebhookBindAddress})
go b.handleRocketHook() go b.handleRocketHook()
return nil return nil
} }

View File

@ -52,11 +52,16 @@ func (b *Bslack) Command(cmd string) string {
} }
func (b *Bslack) Connect() error { func (b *Bslack) Connect() error {
flog.Info("Connecting") if b.Config.WebhookURL != "" && b.Config.WebhookBindAddress != "" {
if !b.Config.UseAPI { flog.Info("Connecting using webhookurl and webhookbindaddress")
b.mh = matterhook.New(b.Config.URL, b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{BindAddress: b.Config.BindAddress}) matterhook.Config{BindAddress: b.Config.WebhookBindAddress})
} else if b.Config.WebhookURL != "" {
flog.Info("Connecting using webhookurl (for posting) and token")
b.mh = matterhook.New(b.Config.WebhookURL,
matterhook.Config{DisableServer: true})
} else { } else {
flog.Info("Connecting using token")
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()
@ -73,7 +78,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.UseAPI { 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
@ -96,7 +101,7 @@ func (b *Bslack) Send(msg config.Message) error {
if b.Config.PrefixMessagesWithNick { if b.Config.PrefixMessagesWithNick {
message = nick + " " + message message = nick + " " + message
} }
if !b.Config.UseAPI { if b.Config.WebhookURL != "" {
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL} matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
matterMessage.Channel = channel matterMessage.Channel = channel
matterMessage.UserName = nick matterMessage.UserName = nick
@ -169,18 +174,19 @@ func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {
} }
func (b *Bslack) handleSlack() { func (b *Bslack) handleSlack() {
flog.Debugf("Choosing API based slack connection: %t", b.Config.UseAPI)
mchan := make(chan *MMMessage) mchan := make(chan *MMMessage)
if b.Config.UseAPI { if b.Config.WebhookBindAddress != "" && b.Config.WebhookURL != "" {
go b.handleSlackClient(mchan) flog.Debugf("Choosing webhooks based receiving")
} else {
go b.handleMatterHook(mchan) go b.handleMatterHook(mchan)
} else {
flog.Debugf("Choosing token based receiving")
go b.handleSlackClient(mchan)
} }
time.Sleep(time.Second) time.Sleep(time.Second)
flog.Debug("Start listening for Slack messages") flog.Debug("Start listening for Slack messages")
for message := range mchan { for message := range mchan {
// do not send messages from ourself // do not send messages from ourself
if b.Config.UseAPI && message.Username == b.si.User.Name { if b.Config.WebhookURL == "" && b.Config.WebhookBindAddress == "" && message.Username == b.si.User.Name {
continue continue
} }
texts := strings.Split(message.Text, "\n") texts := strings.Split(message.Text, "\n")

View File

@ -199,43 +199,17 @@ ShowJoinPart=false
#REQUIRED #REQUIRED
[mattermost.work] [mattermost.work]
#### Settings for webhook matterbridge.
#### These settings will not be used when useAPI is enabled
#Url is your incoming webhook url as specified in mattermost.
#See account settings - integrations - incoming webhooks on mattermost.
#REQUIRED (unless useAPI=true)
URL="https://yourdomain/hooks/yourhookkey"
#Address to listen on for outgoing webhook requests from mattermost.
#See account settings - integrations - outgoing webhooks on mattermost.
#This setting will not be used when using -plus switch which doesn't use
#webhooks
#REQUIRED (unless useAPI=true)
BindAddress="0.0.0.0:9999"
#Icon that will be showed in mattermost.
#OPTIONAL
IconURL="http://youricon.png"
#### Settings for matterbridge -plus
#### Thse settings will only be used when using the -plus switch.
#### Settings for using matterbridge API
#OPTIONAL
useAPI=false
#The mattermost hostname. (do not prefix it with http or https) #The mattermost hostname. (do not prefix it with http or https)
#REQUIRED (when useAPI=true) #REQUIRED (when not using webhooks)
Server="yourmattermostserver.domain" Server="yourmattermostserver.domain"
#Your team on mattermost. #Your team on mattermost.
#REQUIRED (when useAPI=true) #REQUIRED (when not using webhooks)
Team="yourteam" Team="yourteam"
#login/pass of your bot. #login/pass of your bot.
#Use a dedicated user for this and not your own! #Use a dedicated user for this and not your own!
#REQUIRED (when useAPI=true) #REQUIRED (when not using webhooks)
Login="yourlogin" Login="yourlogin"
Password="yourpass" Password="yourpass"
@ -243,7 +217,30 @@ Password="yourpass"
#OPTIONAL (default false) #OPTIONAL (default false)
NoTLS=false NoTLS=false
#### Shared settings for matterbridge and -plus #### Settings for webhook matterbridge.
#NOT RECOMMENDED TO USE INCOMING/OUTGOING WEBHOOK. USE DEDICATED BOT USER WHEN POSSIBLE!
#You don't need to configure this, if you have configured the settings
#above.
#Url is your incoming webhook url as specified in mattermost.
#See account settings - integrations - incoming webhooks on mattermost.
#If specified, messages will be sent to mattermost using this URL
#OPTIONAL
WebhookURL="https://yourdomain/hooks/yourhookkey"
#Address to listen on for outgoing webhook requests from mattermost.
#See account settings - integrations - outgoing webhooks on mattermost.
#If specified, messages will be received from mattermost on this ip:port
#(this will only work if WebhookURL above is also configured)
#OPTIONAL
WebhookBindAddress="0.0.0.0:9999"
#Icon that will be showed in mattermost.
#This only works when WebhookURL is configured
#OPTIONAL
IconURL="http://youricon.png"
#### End settings for webhook matterbridge.
#Enable to not verify the certificate on your mattermost server. #Enable to not verify the certificate on your mattermost server.
#e.g. when using selfsigned certificates #e.g. when using selfsigned certificates
@ -345,15 +342,20 @@ ShowJoinPart=false
#In this example we use [slack.hobby] #In this example we use [slack.hobby]
#REQUIRED #REQUIRED
[slack.hobby] [slack.hobby]
#### Settings for webhook matterbridge. #Token to connect with the Slack API
#### These settings will not be used when useAPI is enabled #You'll have to use a test/api-token using a dedicated user and not a bot token.
#See https://github.com/42wim/matterbridge/issues/75 for more info.
#Use https://api.slack.com/custom-integrations/legacy-tokens
#REQUIRED (when not using webhooks)
Token="yourslacktoken"
#### Settings for webhook matterbridge.
#NOT RECOMMENDED TO USE INCOMING/OUTGOING WEBHOOK. USE SLACK API #NOT RECOMMENDED TO USE INCOMING/OUTGOING WEBHOOK. USE SLACK API
#AND DEDICATED BOT USER WHEN POSSIBLE! #AND DEDICATED BOT USER WHEN POSSIBLE!
#Url is your incoming webhook url as specified in slack #Url is your incoming webhook url as specified in slack
#See account settings - integrations - incoming webhooks on slack #See account settings - integrations - incoming webhooks on slack
#REQUIRED (unless useAPI=true) #OPTIONAL
URL="https://hooks.slack.com/services/yourhook" WebhookURL="https://hooks.slack.com/services/yourhook"
#NOT RECOMMENDED TO USE INCOMING/OUTGOING WEBHOOK. USE SLACK API #NOT RECOMMENDED TO USE INCOMING/OUTGOING WEBHOOK. USE SLACK API
#AND DEDICATED BOT USER WHEN POSSIBLE! #AND DEDICATED BOT USER WHEN POSSIBLE!
@ -361,21 +363,8 @@ URL="https://hooks.slack.com/services/yourhook"
#See account settings - integrations - outgoing webhooks on slack #See account settings - integrations - outgoing webhooks on slack
#This setting will not be used when useAPI is eanbled #This setting will not be used when useAPI is eanbled
#webhooks #webhooks
#REQUIRED (unless useAPI=true)
BindAddress="0.0.0.0:9999"
#### Settings for using slack API (RECOMMENDED)
#OPTIONAL #OPTIONAL
useAPI=false WebhookBindAddress="0.0.0.0:9999"
#Token to connect with the Slack API
#You'll have to use a test/api-token using a dedicated user and not a bot token.
#See https://github.com/42wim/matterbridge/issues/75 for more info.
#Use https://api.slack.com/custom-integrations/legacy-tokens
#REQUIRED (when useAPI=true)
Token="yourslacktoken"
#### Shared settings for webhooks and API
#Icon that will be showed in slack #Icon that will be showed in slack
#The string "{NICK}" (case sensitive) will be replaced by the actual nick / username. #The string "{NICK}" (case sensitive) will be replaced by the actual nick / username.
@ -559,12 +548,12 @@ ShowJoinPart=false
#Read #https://rocket.chat/docs/administrator-guides/integrations/#how-to-create-a-new-incoming-webhook #Read #https://rocket.chat/docs/administrator-guides/integrations/#how-to-create-a-new-incoming-webhook
#See administration - integrations - new integration - incoming webhook #See administration - integrations - new integration - incoming webhook
#REQUIRED #REQUIRED
URL="https://yourdomain/hooks/yourhookkey" WebhookURL="https://yourdomain/hooks/yourhookkey"
#Address to listen on for outgoing webhook requests from rocketchat. #Address to listen on for outgoing webhook requests from rocketchat.
#See administration - integrations - new integration - outgoing webhook #See administration - integrations - new integration - outgoing webhook
#REQUIRED #REQUIRED
BindAddress="0.0.0.0:9999" WebhookBindAddress="0.0.0.0:9999"
#Your nick/username as specified in your incoming webhook "Post as" setting #Your nick/username as specified in your incoming webhook "Post as" setting
#REQUIRED #REQUIRED