Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit ea731a552dd1c381d4d6b1853fdbcd820c5d9d56 Author: Wim <wim@42.be> Date: Sat Jun 24 19:36:10 2017 +0000 Deprecate URL,useAPI,BindAddress (slack,mattermost,rocketchat) diff --git a/bridge/config/config.go b/bridge/config/config.go index 6244dd6..fc41fe0 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -407 +407 @@ type ChannelInfo struct {    type Protocol struct {   AuthCode string // steam - BindAddress string // mattermost, slack + BindAddress string // mattermost, slack // DEPRECATED   Buffer int // api   EditSuffix string // mattermost, slack, discord, telegram, gitter   EditDisable bool // mattermost, slack, discord, telegram, gitter @@ -7212 +7214 @@ type Protocol struct {   SkipTLSVerify bool // IRC, mattermost   Team string // mattermost   Token string // gitter, slack, discord, api - URL string // mattermost, slack, matrix + URL string // mattermost, slack // DEPRECATED   UseAPI bool // mattermost, slack   UseSASL bool // IRC   UseTLS bool // IRC   UseFirstName bool // telegram - WebhookURL string // discord + WebhookBindAddress string // mattermost, slack + WebhookURL string // mattermost, slack + WebhookUse string // mattermost, slack, discord  }    type ChannelOptions struct { @@ -1286 +13028 @@ func NewConfig(cfgfile string) *Config {   if _, err := toml.DecodeFile(cfgfile, &cfg); err != nil {   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  }   @@ -1783 +20217 @@ func GetIconURL(msg *Message, cfg *Protocol) string {   iconURL = strings.Replace(iconURL, "{PROTOCOL}", protocol, -1)   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) +} diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 1acbb19..e3ea9f9 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -496 +4911 @@ func New(cfg config.Protocol, account string, c chan config.Message) *bdiscord {  func (b *bdiscord) Connect() error {   var err error   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 ") {   b.Config.Token = "Bot " + b.Config.Token   } @@ -1107 +1157 @@ func (b *bdiscord) Send(msg config.Message) error {   return nil   }   if b.Config.WebhookURL == "" { - flog.Debugf("Broadcasting using API") + flog.Debugf("Broadcasting using token (API)")   b.c.ChannelMessageSend(channelID, msg.Username+msg.Text)   } else {   flog.Debugf("Broadcasting using Webhook") diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index e88bf83..222522e 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -5512 +5518 @@ func (b *Bmattermost) Command(cmd string) string {  }    func (b *Bmattermost) Connect() error { - if !b.Config.UseAPI { - flog.Info("Connecting webhooks") - b.mh = matterhook.New(b.Config.URL, + if b.Config.WebhookURL != "" && b.Config.WebhookBindAddress != "" { + flog.Info("Connecting using webhookurl and webhookbindaddress") + b.mh = matterhook.New(b.Config.WebhookURL,   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 { + flog.Info("Connecting using token")   b.mc = matterclient.New(b.Config.Login, b.Config.Password,   b.Config.Team, b.Config.Server)   b.mc.SkipTLSVerify = b.Config.SkipTLSVerify @@ -857 +917 @@ func (b *Bmattermost) Disconnect() error {    func (b *Bmattermost) JoinChannel(channel string) error {   // 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 nil @@ -1007 +1067 @@ func (b *Bmattermost) Send(msg config.Message) error {   if b.Config.PrefixMessagesWithNick {   message = nick + message   } - if !b.Config.UseAPI { + if b.Config.WebhookURL != "" {   matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}   matterMessage.IconURL = msg.Avatar   matterMessage.Channel = channel @@ -11912 +12513 @@ func (b *Bmattermost) Send(msg config.Message) error {  }    func (b *Bmattermost) handleMatter() { - flog.Debugf("Choosing API based Mattermost connection: %t", b.Config.UseAPI)   mchan := make(chan *MMMessage) - if b.Config.UseAPI { - go b.handleMatterClient(mchan) - } else { + if b.Config.WebhookBindAddress != "" && b.Config.WebhookURL != "" { + flog.Debugf("Choosing webhooks based receiving")   go b.handleMatterHook(mchan) + } else { + flog.Debugf("Choosing login (api) based receiving") + go b.handleMatterClient(mchan)   }   for message := range mchan {   flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.Account) diff --git a/bridge/rocketchat/rocketchat.go b/bridge/rocketchat/rocketchat.go index 15cac55..8eb788f 100644 --- a/bridge/rocketchat/rocketchat.go +++ b/bridge/rocketchat/rocketchat.go @@ -4110 +4110 @@ func (b *Brocketchat) Command(cmd string) string {    func (b *Brocketchat) Connect() error {   flog.Info("Connecting webhooks") - b.mh = matterhook.New(b.Config.URL, + b.mh = matterhook.New(b.Config.WebhookURL,   matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,   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()   return nil  } diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index ad6a2b6..94f74d7 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -5211 +5216 @@ func (b *Bslack) Command(cmd string) string {  }    func (b *Bslack) Connect() error { - flog.Info("Connecting") - if !b.Config.UseAPI { - b.mh = matterhook.New(b.Config.URL, - matterhook.Config{BindAddress: b.Config.BindAddress}) + if b.Config.WebhookURL != "" && b.Config.WebhookBindAddress != "" { + flog.Info("Connecting using webhookurl and webhookbindaddress") + b.mh = matterhook.New(b.Config.WebhookURL, + 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 { + flog.Info("Connecting using token")   b.sc = slack.New(b.Config.Token)   b.rtm = b.sc.NewRTM()   go b.rtm.ManageConnection() @@ -737 +787 @@ func (b *Bslack) Disconnect() error {    func (b *Bslack) JoinChannel(channel string) error {   // 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") {   // TODO check if bot has already joined channel   return nil @@ -967 +1017 @@ func (b *Bslack) Send(msg config.Message) error {   if b.Config.PrefixMessagesWithNick {   message = nick + " " + message   } - if !b.Config.UseAPI { + if b.Config.WebhookURL != "" {   matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}   matterMessage.Channel = channel   matterMessage.UserName = nick @@ -16918 +17419 @@ func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {  }    func (b *Bslack) handleSlack() { - flog.Debugf("Choosing API based slack connection: %t", b.Config.UseAPI)   mchan := make(chan *MMMessage) - if b.Config.UseAPI { - go b.handleSlackClient(mchan) - } else { + if b.Config.WebhookBindAddress != "" && b.Config.WebhookURL != "" { + flog.Debugf("Choosing webhooks based receiving")   go b.handleMatterHook(mchan) + } else { + flog.Debugf("Choosing token based receiving") + go b.handleSlackClient(mchan)   }   time.Sleep(time.Second)   flog.Debug("Start listening for Slack messages")   for message := range mchan {   // 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   }   texts := strings.Split(message.Text, "\n") diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index a5c523b..c725b14 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -19943 +19917 @@ ShowJoinPart=false  #REQUIRED    [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) -#REQUIRED (when useAPI=true) +#REQUIRED (when not using webhooks)  Server="yourmattermostserver.domain"    #Your team on mattermost. -#REQUIRED (when useAPI=true) +#REQUIRED (when not using webhooks)  Team="yourteam"    #login/pass of your bot.  #Use a dedicated user for this and not your own! -#REQUIRED (when useAPI=true) +#REQUIRED (when not using webhooks)  Login="yourlogin"  Password="yourpass"   @@ -2437 +21730 @@ Password="yourpass"  #OPTIONAL (default 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.  #e.g. when using selfsigned certificates @@ -34515 +34220 @@ ShowJoinPart=false  #In this example we use [slack.hobby]  #REQUIRED  [slack.hobby] -#### Settings for webhook matterbridge. -#### These settings will not be used when useAPI is enabled +#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 not using webhooks) +Token="yourslacktoken"   +#### Settings for webhook matterbridge.  #NOT RECOMMENDED TO USE INCOMING/OUTGOING WEBHOOK. USE SLACK API  #AND DEDICATED BOT USER WHEN POSSIBLE!  #Url is your incoming webhook url as specified in slack  #See account settings - integrations - incoming webhooks on slack -#REQUIRED (unless useAPI=true) -URL="https://hooks.slack.com/services/yourhook" +#OPTIONAL +WebhookURL="https://hooks.slack.com/services/yourhook"    #NOT RECOMMENDED TO USE INCOMING/OUTGOING WEBHOOK. USE SLACK API  #AND DEDICATED BOT USER WHEN POSSIBLE! @@ -36121 +3638 @@ URL="https://hooks.slack.com/services/yourhook"  #See account settings - integrations - outgoing webhooks on slack  #This setting will not be used when useAPI is eanbled  #webhooks -#REQUIRED (unless useAPI=true) -BindAddress="0.0.0.0:9999" - -#### Settings for using slack API (RECOMMENDED)  #OPTIONAL -useAPI=false - -#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 +WebhookBindAddress="0.0.0.0:9999"    #Icon that will be showed in slack  #The string "{NICK}" (case sensitive) will be replaced by the actual nick / username. @@ -55912 +54812 @@ ShowJoinPart=false  #Read #https://rocket.chat/docs/administrator-guides/integrations/#how-to-create-a-new-incoming-webhook  #See administration - integrations - new integration - incoming webhook  #REQUIRED -URL="https://yourdomain/hooks/yourhookkey" +WebhookURL="https://yourdomain/hooks/yourhookkey"    #Address to listen on for outgoing webhook requests from rocketchat.  #See administration - integrations - new integration - outgoing webhook  #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  #REQUIRED