Thumbnail

rani/matterbridge.git

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

commit bda811b099526a294c58c39efe31274c98ad38ca Author: Wim <wim@42.be> Date: Tue Dec 19 23:15:03 2017 +0000 Refactor and add MediaDownloadSize to General diff --git a/bridge/api/api.go b/bridge/api/api.go index 8bae128..45bc11d 100644 --- a/bridge/api/api.go +++ b/bridge/api/api.go @@ -1311 +139 @@ import (  )    type Api struct { - Config *config.Protocol - Remote chan config.Message - Account string   Messages ring.Ring   sync.RWMutex + *config.BridgeConfig  }    type ApiMessage struct { @@ -3514 +3311 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Api { - b := &Api{} +func New(cfg *config.BridgeConfig) *Api { + b := &Api{BridgeConfig: cfg}   e := echo.New()   b.Messages = ring.Ring{} - b.Messages.SetCapacity(cfg.Buffer) - b.Config = &cfg - b.Account = account - b.Remote = c + b.Messages.SetCapacity(b.Config.Buffer)   if b.Config.Token != "" {   e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {   return key == b.Config.Token, nil @@ -527 +477 @@ func New(cfg config.Protocol, account string, c chan config.Message) *Api {   e.GET("/api/stream", b.handleStream)   e.POST("/api/message", b.handlePostMessage)   go func() { - flog.Fatal(e.Start(cfg.BindAddress)) + flog.Fatal(e.Start(b.Config.BindAddress))   }()   return b  } diff --git a/bridge/bridge.go b/bridge/bridge.go index 4c0540f..8c39b23 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -4646 +4647 @@ func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Brid   b.Protocol = protocol   b.Account = bridge.Account   b.Joined = make(map[string]bool) + bridgeConfig := &config.BridgeConfig{General: &cfg.General, Account: bridge.Account, Remote: c}     // override config from environment   config.OverrideCfgFromEnv(cfg, protocol, name)   switch protocol {   case "mattermost": - b.Config = cfg.Mattermost[name] - b.Bridger = bmattermost.New(cfg.Mattermost[name], bridge.Account, c) + bridgeConfig.Config = cfg.Mattermost[name] + b.Bridger = bmattermost.New(bridgeConfig)   case "irc": - b.Config = cfg.IRC[name] - b.Bridger = birc.New(cfg.IRC[name], bridge.Account, c) + bridgeConfig.Config = cfg.IRC[name] + b.Bridger = birc.New(bridgeConfig)   case "gitter": - b.Config = cfg.Gitter[name] - b.Bridger = bgitter.New(cfg.Gitter[name], bridge.Account, c) + bridgeConfig.Config = cfg.Gitter[name] + b.Bridger = bgitter.New(bridgeConfig)   case "slack": - b.Config = cfg.Slack[name] - b.Bridger = bslack.New(cfg.Slack[name], bridge.Account, c) + bridgeConfig.Config = cfg.Slack[name] + b.Bridger = bslack.New(bridgeConfig)   case "xmpp": - b.Config = cfg.Xmpp[name] - b.Bridger = bxmpp.New(cfg.Xmpp[name], bridge.Account, c) + bridgeConfig.Config = cfg.Xmpp[name] + b.Bridger = bxmpp.New(bridgeConfig)   case "discord": - b.Config = cfg.Discord[name] - b.Bridger = bdiscord.New(cfg.Discord[name], bridge.Account, c) + bridgeConfig.Config = cfg.Discord[name] + b.Bridger = bdiscord.New(bridgeConfig)   case "telegram": - b.Config = cfg.Telegram[name] - b.Bridger = btelegram.New(cfg.Telegram[name], bridge.Account, c) + bridgeConfig.Config = cfg.Telegram[name] + b.Bridger = btelegram.New(bridgeConfig)   case "rocketchat": - b.Config = cfg.Rocketchat[name] - b.Bridger = brocketchat.New(cfg.Rocketchat[name], bridge.Account, c) + bridgeConfig.Config = cfg.Rocketchat[name] + b.Bridger = brocketchat.New(bridgeConfig)   case "matrix": - b.Config = cfg.Matrix[name] - b.Bridger = bmatrix.New(cfg.Matrix[name], bridge.Account, c) + bridgeConfig.Config = cfg.Matrix[name] + b.Bridger = bmatrix.New(bridgeConfig)   case "steam": - b.Config = cfg.Steam[name] - b.Bridger = bsteam.New(cfg.Steam[name], bridge.Account, c) + bridgeConfig.Config = cfg.Steam[name] + b.Bridger = bsteam.New(bridgeConfig)   case "sshchat": - b.Config = cfg.Sshchat[name] - b.Bridger = bsshchat.New(cfg.Sshchat[name], bridge.Account, c) + bridgeConfig.Config = cfg.Sshchat[name] + b.Bridger = bsshchat.New(bridgeConfig)   case "api": - b.Config = cfg.Api[name] - b.Bridger = api.New(cfg.Api[name], bridge.Account, c) + bridgeConfig.Config = cfg.Api[name] + b.Bridger = api.New(bridgeConfig)   }   return b  } diff --git a/bridge/config/config.go b/bridge/config/config.go index 6344fa5..10f1d5f 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -606 +607 @@ type Protocol struct {   IgnoreMessages string // all protocols   Jid string // xmpp   Login string // mattermost, matrix + MediaDownloadSize int // all protocols   MediaServerDownload string   MediaServerUpload string   MessageDelay int // IRC, time in millisecond to wait between messages @@ -1476 +14813 @@ type Config struct {   SameChannelGateway []SameChannelGateway  }   +type BridgeConfig struct { + Config Protocol + General *Protocol + Account string + Remote chan Message +} +  func NewConfig(cfgfile string) *Config {   var cfg Config   if _, err := toml.DecodeFile(cfgfile, &cfg); err != nil { diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 183b119..a5e2075 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -129 +126 @@ import (    type bdiscord struct {   c *discordgo.Session - Config *config.Protocol - Remote chan config.Message - Account string   Channels []*discordgo.Channel   Nick string   UseChannelID bool @@ -246 +217 @@ type bdiscord struct {   webhookToken string   channelInfoMap map[string]*config.ChannelInfo   sync.RWMutex + *config.BridgeConfig  }    var flog *log.Entry @@ -3311 +318 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *bdiscord { - b := &bdiscord{} - b.Config = &cfg - b.Remote = c - b.Account = account +func New(cfg *config.BridgeConfig) *bdiscord { + b := &bdiscord{BridgeConfig: cfg}   b.userMemberMap = make(map[string]*discordgo.Member)   b.channelInfoMap = make(map[string]*config.ChannelInfo)   if b.Config.WebhookURL != "" { diff --git a/bridge/gitter/gitter.go b/bridge/gitter/gitter.go index 64b6344..fce58c1 100644 --- a/bridge/gitter/gitter.go +++ b/bridge/gitter/gitter.go @@ -913 +911 @@ import (  )    type Bgitter struct { - c *gitter.Gitter - Config *config.Protocol - Remote chan config.Message - Account string - User *gitter.User - Users []gitter.User - Rooms []gitter.Room + c *gitter.Gitter + User *gitter.User + Users []gitter.User + Rooms []gitter.Room + *config.BridgeConfig  }    var flog *log.Entry @@ -2512 +238 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Bgitter { - b := &Bgitter{} - b.Config = &cfg - b.Remote = c - b.Account = account - return b +func New(cfg *config.BridgeConfig) *Bgitter { + return &Bgitter{BridgeConfig: cfg}  }    func (b *Bgitter) Connect() error { diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go index b493997..c9c8cbb 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -2512 +2511 @@ type Birc struct {   i *girc.Client   Nick string   names map[string][]string - Config *config.Protocol - Remote chan config.Message   connected chan struct{}   Local chan config.Message // local queue for flood control - Account string   FirstConnection bool + + *config.BridgeConfig  }    var flog *log.Entry @@ -4013 +3911 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Birc { +func New(cfg *config.BridgeConfig) *Birc {   b := &Birc{} - b.Config = &cfg + b.BridgeConfig = cfg   b.Nick = b.Config.Nick - b.Remote = c   b.names = make(map[string][]string) - b.Account = account   b.connected = make(chan struct{})   if b.Config.MessageDelay == 0 {   b.Config.MessageDelay = 1300 diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index dee301b..03e493f 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -1512 +1510 @@ import (    type Bmatrix struct {   mc *matrix.Client - Config *config.Protocol - Remote chan config.Message - Account string   UserID string   RoomMap map[string]string   sync.RWMutex + *config.BridgeConfig  }    var flog *log.Entry @@ -3012 +289 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Bmatrix { - b := &Bmatrix{} +func New(cfg *config.BridgeConfig) *Bmatrix { + b := &Bmatrix{BridgeConfig: cfg}   b.RoomMap = make(map[string]string) - b.Config = &cfg - b.Account = account - b.Remote = c   return b  }   diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index 9d9990b..7d1da4f 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -366 +367 @@ type Bmattermost struct {   Remote chan config.Message   TeamId string   Account string + *config.BridgeConfig  }    var flog *log.Entry @@ -4511 +468 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Bmattermost { - b := &Bmattermost{} - b.Config = &cfg - b.Remote = c - b.Account = account +func New(cfg *config.BridgeConfig) *Bmattermost { + b := &Bmattermost{BridgeConfig: cfg}   b.mmMap = make(map[string]string)   return b  } diff --git a/bridge/rocketchat/rocketchat.go b/bridge/rocketchat/rocketchat.go index eac754f..0523853 100644 --- a/bridge/rocketchat/rocketchat.go +++ b/bridge/rocketchat/rocketchat.go @@ -149 +147 @@ type MMhook struct {    type Brocketchat struct {   MMhook - Config *config.Protocol - Remote chan config.Message - Account string + *config.BridgeConfig  }    var flog *log.Entry @@ -2612 +248 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Brocketchat { - b := &Brocketchat{} - b.Config = &cfg - b.Remote = c - b.Account = account - return b +func New(cfg *config.BridgeConfig) *Brocketchat { + return &Brocketchat{BridgeConfig: cfg}  }    func (b *Brocketchat) Command(cmd string) string { diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 7206bfd..d6f73ff 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -2714 +2712 @@ type MMMessage struct {  type Bslack struct {   mh *matterhook.Client   sc *slack.Client - Config *config.Protocol   rtm *slack.RTM   Plus bool - Remote chan config.Message   Users []slack.User - Account string   si *slack.Info   channels []slack.Channel + *config.BridgeConfig  }    var flog *log.Entry @@ -4412 +428 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Bslack { - b := &Bslack{} - b.Config = &cfg - b.Remote = c - b.Account = account - return b +func New(cfg *config.BridgeConfig) *Bslack { + return &Bslack{BridgeConfig: cfg}  }    func (b *Bslack) Command(cmd string) string { @@ -1617 +1557 @@ func (b *Bslack) Send(msg config.Message) (string, error) {   np.AsUser = true   }   np.Username = nick - np.IconURL = config.GetIconURL(&msg, b.Config) + np.IconURL = config.GetIconURL(&msg, &b.Config)   if msg.Avatar != "" {   np.IconURL = msg.Avatar   } diff --git a/bridge/sshchat/sshchat.go b/bridge/sshchat/sshchat.go index becb688..81e8324 100644 --- a/bridge/sshchat/sshchat.go +++ b/bridge/sshchat/sshchat.go @@ -1011 +109 @@ import (  )    type Bsshchat struct { - r *bufio.Scanner - w io.WriteCloser - Config *config.Protocol - Remote chan config.Message - Account string + r *bufio.Scanner + w io.WriteCloser + *config.BridgeConfig  }    var flog *log.Entry @@ -2412 +228 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Bsshchat { - b := &Bsshchat{} - b.Config = &cfg - b.Account = account - b.Remote = c - return b +func New(cfg *config.BridgeConfig) *Bsshchat { + return &Bsshchat{BridgeConfig: cfg}  }    func (b *Bsshchat) Connect() error { diff --git a/bridge/steam/steam.go b/bridge/steam/steam.go index 7eaef46..51e15c5 100644 --- a/bridge/steam/steam.go +++ b/bridge/steam/steam.go @@ -1611 +169 @@ import (  type Bsteam struct {   c *steam.Client   connected chan struct{} - Config *config.Protocol - Remote chan config.Message - Account string   userMap map[steamid.SteamId]string   sync.RWMutex + *config.BridgeConfig  }    var flog *log.Entry @@ -3011 +288 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Bsteam { - b := &Bsteam{} - b.Config = &cfg - b.Remote = c - b.Account = account +func New(cfg *config.BridgeConfig) *Bsteam { + b := &Bsteam{BridgeConfig: cfg}   b.userMap = make(map[steamid.SteamId]string)   b.connected = make(chan struct{})   return b diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 650eec1..95492a8 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -1210 +128 @@ import (  )    type Btelegram struct { - c *tgbotapi.BotAPI - Config *config.Protocol - Remote chan config.Message - Account string + c *tgbotapi.BotAPI + *config.BridgeConfig  }    var flog *log.Entry @@ -2512 +238 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Btelegram { - b := &Btelegram{} - b.Config = &cfg - b.Remote = c - b.Account = account - return b +func New(cfg *config.BridgeConfig) *Btelegram { + return &Btelegram{BridgeConfig: cfg}  }    func (b *Btelegram) Connect() error { diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index b5429b1..9cb6f6a 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -149 +147 @@ import (  type Bxmpp struct {   xc *xmpp.Client   xmppMap map[string]string - Config *config.Protocol - Remote chan config.Message - Account string + *config.BridgeConfig  }    var flog *log.Entry @@ -2612 +249 @@ func init() {   flog = log.WithFields(log.Fields{"module": protocol})  }   -func New(cfg config.Protocol, account string, c chan config.Message) *Bxmpp { - b := &Bxmpp{} +func New(cfg *config.BridgeConfig) *Bxmpp { + b := &Bxmpp{BridgeConfig: cfg}   b.xmppMap = make(map[string]string) - b.Config = &cfg - b.Account = account - b.Remote = c   return b  }