From 265457b45171dcae96a1aac9454e3bda2cd0557a Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 19 Dec 2017 23:15:03 +0100 Subject: Refactor and add MediaDownloadSize to General --- bridge/api/api.go | 15 +++++-------- bridge/bridge.go | 49 +++++++++++++++++++++-------------------- bridge/config/config.go | 8 +++++++ bridge/discord/discord.go | 11 +++------ bridge/gitter/gitter.go | 20 ++++++----------- bridge/irc/irc.go | 11 ++++----- bridge/matrix/matrix.go | 11 +++------ bridge/mattermost/mattermost.go | 8 +++---- bridge/rocketchat/rocketchat.go | 12 +++------- bridge/slack/slack.go | 14 ++++-------- bridge/sshchat/sshchat.go | 16 +++++--------- bridge/steam/steam.go | 11 +++------ bridge/telegram/telegram.go | 14 ++++-------- bridge/xmpp/xmpp.go | 11 +++------ 14 files changed, 80 insertions(+), 131 deletions(-) diff --git a/bridge/api/api.go b/bridge/api/api.go index 8bae1281..45bc11d3 100644 --- a/bridge/api/api.go +++ b/bridge/api/api.go @@ -13,11 +13,9 @@ import ( ) type Api struct { - Config *config.Protocol - Remote chan config.Message - Account string Messages ring.Ring sync.RWMutex + *config.BridgeConfig } type ApiMessage struct { @@ -35,14 +33,11 @@ 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 @@ -52,7 +47,7 @@ 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 4c0540f6..8c39b230 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -46,46 +46,47 @@ 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 6344fa59..10f1d5f0 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -60,6 +60,7 @@ 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 @@ -147,6 +148,13 @@ 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 183b1193..a5e20758 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -12,9 +12,6 @@ import ( type bdiscord struct { c *discordgo.Session - Config *config.Protocol - Remote chan config.Message - Account string Channels []*discordgo.Channel Nick string UseChannelID bool @@ -24,6 +21,7 @@ type bdiscord struct { webhookToken string channelInfoMap map[string]*config.ChannelInfo sync.RWMutex + *config.BridgeConfig } var flog *log.Entry @@ -33,11 +31,8 @@ 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 64b6344f..fce58c1f 100644 --- a/bridge/gitter/gitter.go +++ b/bridge/gitter/gitter.go @@ -9,13 +9,11 @@ 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 @@ -25,12 +23,8 @@ 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 b493997a..c9c8cbb2 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -25,12 +25,11 @@ 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 @@ -40,13 +39,11 @@ 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 dee301be..03e493f7 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -15,12 +15,10 @@ 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 @@ -30,12 +28,9 @@ 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 9d9990b2..7d1da4f8 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -36,6 +36,7 @@ type Bmattermost struct { Remote chan config.Message TeamId string Account string + *config.BridgeConfig } var flog *log.Entry @@ -45,11 +46,8 @@ 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 eac754f5..05238539 100644 --- a/bridge/rocketchat/rocketchat.go +++ b/bridge/rocketchat/rocketchat.go @@ -14,9 +14,7 @@ type MMhook struct { type Brocketchat struct { MMhook - Config *config.Protocol - Remote chan config.Message - Account string + *config.BridgeConfig } var flog *log.Entry @@ -26,12 +24,8 @@ 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 7206bfdd..d6f73ffd 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -27,14 +27,12 @@ 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 @@ -44,12 +42,8 @@ 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 { @@ -161,7 +155,7 @@ 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 becb688c..81e83244 100644 --- a/bridge/sshchat/sshchat.go +++ b/bridge/sshchat/sshchat.go @@ -10,11 +10,9 @@ 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 @@ -24,12 +22,8 @@ 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 7eaef46a..51e15c5b 100644 --- a/bridge/steam/steam.go +++ b/bridge/steam/steam.go @@ -16,11 +16,9 @@ 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 @@ -30,11 +28,8 @@ 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 650eec12..95492a88 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -12,10 +12,8 @@ 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 @@ -25,12 +23,8 @@ 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 b5429b1f..9cb6f6ad 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -14,9 +14,7 @@ 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 @@ -26,12 +24,9 @@ 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 } -- cgit v1.2.3