Refactor and add MediaDownloadSize to General

This commit is contained in:
Wim 2017-12-19 23:15:03 +01:00
parent 4a4a29c9f6
commit 265457b451
14 changed files with 80 additions and 131 deletions

View File

@ -13,11 +13,9 @@ import (
) )
type Api struct { type Api struct {
Config *config.Protocol
Remote chan config.Message
Account string
Messages ring.Ring Messages ring.Ring
sync.RWMutex sync.RWMutex
*config.BridgeConfig
} }
type ApiMessage struct { type ApiMessage struct {
@ -35,14 +33,11 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *Api { func New(cfg *config.BridgeConfig) *Api {
b := &Api{} b := &Api{BridgeConfig: cfg}
e := echo.New() e := echo.New()
b.Messages = ring.Ring{} b.Messages = ring.Ring{}
b.Messages.SetCapacity(cfg.Buffer) b.Messages.SetCapacity(b.Config.Buffer)
b.Config = &cfg
b.Account = account
b.Remote = c
if b.Config.Token != "" { if b.Config.Token != "" {
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) { e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
return key == b.Config.Token, nil 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.GET("/api/stream", b.handleStream)
e.POST("/api/message", b.handlePostMessage) e.POST("/api/message", b.handlePostMessage)
go func() { go func() {
flog.Fatal(e.Start(cfg.BindAddress)) flog.Fatal(e.Start(b.Config.BindAddress))
}() }()
return b return b
} }

View File

@ -46,46 +46,47 @@ func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Brid
b.Protocol = protocol b.Protocol = protocol
b.Account = bridge.Account b.Account = bridge.Account
b.Joined = make(map[string]bool) b.Joined = make(map[string]bool)
bridgeConfig := &config.BridgeConfig{General: &cfg.General, Account: bridge.Account, Remote: c}
// override config from environment // override config from environment
config.OverrideCfgFromEnv(cfg, protocol, name) config.OverrideCfgFromEnv(cfg, protocol, name)
switch protocol { switch protocol {
case "mattermost": case "mattermost":
b.Config = cfg.Mattermost[name] bridgeConfig.Config = cfg.Mattermost[name]
b.Bridger = bmattermost.New(cfg.Mattermost[name], bridge.Account, c) b.Bridger = bmattermost.New(bridgeConfig)
case "irc": case "irc":
b.Config = cfg.IRC[name] bridgeConfig.Config = cfg.IRC[name]
b.Bridger = birc.New(cfg.IRC[name], bridge.Account, c) b.Bridger = birc.New(bridgeConfig)
case "gitter": case "gitter":
b.Config = cfg.Gitter[name] bridgeConfig.Config = cfg.Gitter[name]
b.Bridger = bgitter.New(cfg.Gitter[name], bridge.Account, c) b.Bridger = bgitter.New(bridgeConfig)
case "slack": case "slack":
b.Config = cfg.Slack[name] bridgeConfig.Config = cfg.Slack[name]
b.Bridger = bslack.New(cfg.Slack[name], bridge.Account, c) b.Bridger = bslack.New(bridgeConfig)
case "xmpp": case "xmpp":
b.Config = cfg.Xmpp[name] bridgeConfig.Config = cfg.Xmpp[name]
b.Bridger = bxmpp.New(cfg.Xmpp[name], bridge.Account, c) b.Bridger = bxmpp.New(bridgeConfig)
case "discord": case "discord":
b.Config = cfg.Discord[name] bridgeConfig.Config = cfg.Discord[name]
b.Bridger = bdiscord.New(cfg.Discord[name], bridge.Account, c) b.Bridger = bdiscord.New(bridgeConfig)
case "telegram": case "telegram":
b.Config = cfg.Telegram[name] bridgeConfig.Config = cfg.Telegram[name]
b.Bridger = btelegram.New(cfg.Telegram[name], bridge.Account, c) b.Bridger = btelegram.New(bridgeConfig)
case "rocketchat": case "rocketchat":
b.Config = cfg.Rocketchat[name] bridgeConfig.Config = cfg.Rocketchat[name]
b.Bridger = brocketchat.New(cfg.Rocketchat[name], bridge.Account, c) b.Bridger = brocketchat.New(bridgeConfig)
case "matrix": case "matrix":
b.Config = cfg.Matrix[name] bridgeConfig.Config = cfg.Matrix[name]
b.Bridger = bmatrix.New(cfg.Matrix[name], bridge.Account, c) b.Bridger = bmatrix.New(bridgeConfig)
case "steam": case "steam":
b.Config = cfg.Steam[name] bridgeConfig.Config = cfg.Steam[name]
b.Bridger = bsteam.New(cfg.Steam[name], bridge.Account, c) b.Bridger = bsteam.New(bridgeConfig)
case "sshchat": case "sshchat":
b.Config = cfg.Sshchat[name] bridgeConfig.Config = cfg.Sshchat[name]
b.Bridger = bsshchat.New(cfg.Sshchat[name], bridge.Account, c) b.Bridger = bsshchat.New(bridgeConfig)
case "api": case "api":
b.Config = cfg.Api[name] bridgeConfig.Config = cfg.Api[name]
b.Bridger = api.New(cfg.Api[name], bridge.Account, c) b.Bridger = api.New(bridgeConfig)
} }
return b return b
} }

View File

@ -60,6 +60,7 @@ type Protocol struct {
IgnoreMessages string // all protocols IgnoreMessages string // all protocols
Jid string // xmpp Jid string // xmpp
Login string // mattermost, matrix Login string // mattermost, matrix
MediaDownloadSize int // all protocols
MediaServerDownload string MediaServerDownload string
MediaServerUpload string MediaServerUpload string
MessageDelay int // IRC, time in millisecond to wait between messages MessageDelay int // IRC, time in millisecond to wait between messages
@ -147,6 +148,13 @@ type Config struct {
SameChannelGateway []SameChannelGateway SameChannelGateway []SameChannelGateway
} }
type BridgeConfig struct {
Config Protocol
General *Protocol
Account string
Remote chan Message
}
func NewConfig(cfgfile string) *Config { func NewConfig(cfgfile string) *Config {
var cfg Config var cfg Config
if _, err := toml.DecodeFile(cfgfile, &cfg); err != nil { if _, err := toml.DecodeFile(cfgfile, &cfg); err != nil {

View File

@ -12,9 +12,6 @@ import (
type bdiscord struct { type bdiscord struct {
c *discordgo.Session c *discordgo.Session
Config *config.Protocol
Remote chan config.Message
Account string
Channels []*discordgo.Channel Channels []*discordgo.Channel
Nick string Nick string
UseChannelID bool UseChannelID bool
@ -24,6 +21,7 @@ type bdiscord struct {
webhookToken string webhookToken string
channelInfoMap map[string]*config.ChannelInfo channelInfoMap map[string]*config.ChannelInfo
sync.RWMutex sync.RWMutex
*config.BridgeConfig
} }
var flog *log.Entry var flog *log.Entry
@ -33,11 +31,8 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *bdiscord { func New(cfg *config.BridgeConfig) *bdiscord {
b := &bdiscord{} b := &bdiscord{BridgeConfig: cfg}
b.Config = &cfg
b.Remote = c
b.Account = account
b.userMemberMap = make(map[string]*discordgo.Member) b.userMemberMap = make(map[string]*discordgo.Member)
b.channelInfoMap = make(map[string]*config.ChannelInfo) b.channelInfoMap = make(map[string]*config.ChannelInfo)
if b.Config.WebhookURL != "" { if b.Config.WebhookURL != "" {

View File

@ -10,12 +10,10 @@ import (
type Bgitter struct { type Bgitter struct {
c *gitter.Gitter c *gitter.Gitter
Config *config.Protocol
Remote chan config.Message
Account string
User *gitter.User User *gitter.User
Users []gitter.User Users []gitter.User
Rooms []gitter.Room Rooms []gitter.Room
*config.BridgeConfig
} }
var flog *log.Entry var flog *log.Entry
@ -25,12 +23,8 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *Bgitter { func New(cfg *config.BridgeConfig) *Bgitter {
b := &Bgitter{} return &Bgitter{BridgeConfig: cfg}
b.Config = &cfg
b.Remote = c
b.Account = account
return b
} }
func (b *Bgitter) Connect() error { func (b *Bgitter) Connect() error {

View File

@ -25,12 +25,11 @@ type Birc struct {
i *girc.Client i *girc.Client
Nick string Nick string
names map[string][]string names map[string][]string
Config *config.Protocol
Remote chan config.Message
connected chan struct{} connected chan struct{}
Local chan config.Message // local queue for flood control Local chan config.Message // local queue for flood control
Account string
FirstConnection bool FirstConnection bool
*config.BridgeConfig
} }
var flog *log.Entry var flog *log.Entry
@ -40,13 +39,11 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) 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 := &Birc{}
b.Config = &cfg b.BridgeConfig = cfg
b.Nick = b.Config.Nick b.Nick = b.Config.Nick
b.Remote = c
b.names = make(map[string][]string) b.names = make(map[string][]string)
b.Account = account
b.connected = make(chan struct{}) b.connected = make(chan struct{})
if b.Config.MessageDelay == 0 { if b.Config.MessageDelay == 0 {
b.Config.MessageDelay = 1300 b.Config.MessageDelay = 1300

View File

@ -15,12 +15,10 @@ import (
type Bmatrix struct { type Bmatrix struct {
mc *matrix.Client mc *matrix.Client
Config *config.Protocol
Remote chan config.Message
Account string
UserID string UserID string
RoomMap map[string]string RoomMap map[string]string
sync.RWMutex sync.RWMutex
*config.BridgeConfig
} }
var flog *log.Entry var flog *log.Entry
@ -30,12 +28,9 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *Bmatrix { func New(cfg *config.BridgeConfig) *Bmatrix {
b := &Bmatrix{} b := &Bmatrix{BridgeConfig: cfg}
b.RoomMap = make(map[string]string) b.RoomMap = make(map[string]string)
b.Config = &cfg
b.Account = account
b.Remote = c
return b return b
} }

View File

@ -36,6 +36,7 @@ type Bmattermost struct {
Remote chan config.Message Remote chan config.Message
TeamId string TeamId string
Account string Account string
*config.BridgeConfig
} }
var flog *log.Entry var flog *log.Entry
@ -45,11 +46,8 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *Bmattermost { func New(cfg *config.BridgeConfig) *Bmattermost {
b := &Bmattermost{} b := &Bmattermost{BridgeConfig: cfg}
b.Config = &cfg
b.Remote = c
b.Account = account
b.mmMap = make(map[string]string) b.mmMap = make(map[string]string)
return b return b
} }

View File

@ -14,9 +14,7 @@ type MMhook struct {
type Brocketchat struct { type Brocketchat struct {
MMhook MMhook
Config *config.Protocol *config.BridgeConfig
Remote chan config.Message
Account string
} }
var flog *log.Entry var flog *log.Entry
@ -26,12 +24,8 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *Brocketchat { func New(cfg *config.BridgeConfig) *Brocketchat {
b := &Brocketchat{} return &Brocketchat{BridgeConfig: cfg}
b.Config = &cfg
b.Remote = c
b.Account = account
return b
} }
func (b *Brocketchat) Command(cmd string) string { func (b *Brocketchat) Command(cmd string) string {

View File

@ -27,14 +27,12 @@ type MMMessage struct {
type Bslack struct { type Bslack struct {
mh *matterhook.Client mh *matterhook.Client
sc *slack.Client sc *slack.Client
Config *config.Protocol
rtm *slack.RTM rtm *slack.RTM
Plus bool Plus bool
Remote chan config.Message
Users []slack.User Users []slack.User
Account string
si *slack.Info si *slack.Info
channels []slack.Channel channels []slack.Channel
*config.BridgeConfig
} }
var flog *log.Entry var flog *log.Entry
@ -44,12 +42,8 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *Bslack { func New(cfg *config.BridgeConfig) *Bslack {
b := &Bslack{} return &Bslack{BridgeConfig: cfg}
b.Config = &cfg
b.Remote = c
b.Account = account
return b
} }
func (b *Bslack) Command(cmd string) string { func (b *Bslack) Command(cmd string) string {
@ -161,7 +155,7 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
np.AsUser = true np.AsUser = true
} }
np.Username = nick np.Username = nick
np.IconURL = config.GetIconURL(&msg, b.Config) np.IconURL = config.GetIconURL(&msg, &b.Config)
if msg.Avatar != "" { if msg.Avatar != "" {
np.IconURL = msg.Avatar np.IconURL = msg.Avatar
} }

View File

@ -12,9 +12,7 @@ import (
type Bsshchat struct { type Bsshchat struct {
r *bufio.Scanner r *bufio.Scanner
w io.WriteCloser w io.WriteCloser
Config *config.Protocol *config.BridgeConfig
Remote chan config.Message
Account string
} }
var flog *log.Entry var flog *log.Entry
@ -24,12 +22,8 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *Bsshchat { func New(cfg *config.BridgeConfig) *Bsshchat {
b := &Bsshchat{} return &Bsshchat{BridgeConfig: cfg}
b.Config = &cfg
b.Account = account
b.Remote = c
return b
} }
func (b *Bsshchat) Connect() error { func (b *Bsshchat) Connect() error {

View File

@ -16,11 +16,9 @@ import (
type Bsteam struct { type Bsteam struct {
c *steam.Client c *steam.Client
connected chan struct{} connected chan struct{}
Config *config.Protocol
Remote chan config.Message
Account string
userMap map[steamid.SteamId]string userMap map[steamid.SteamId]string
sync.RWMutex sync.RWMutex
*config.BridgeConfig
} }
var flog *log.Entry var flog *log.Entry
@ -30,11 +28,8 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *Bsteam { func New(cfg *config.BridgeConfig) *Bsteam {
b := &Bsteam{} b := &Bsteam{BridgeConfig: cfg}
b.Config = &cfg
b.Remote = c
b.Account = account
b.userMap = make(map[steamid.SteamId]string) b.userMap = make(map[steamid.SteamId]string)
b.connected = make(chan struct{}) b.connected = make(chan struct{})
return b return b

View File

@ -13,9 +13,7 @@ import (
type Btelegram struct { type Btelegram struct {
c *tgbotapi.BotAPI c *tgbotapi.BotAPI
Config *config.Protocol *config.BridgeConfig
Remote chan config.Message
Account string
} }
var flog *log.Entry var flog *log.Entry
@ -25,12 +23,8 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *Btelegram { func New(cfg *config.BridgeConfig) *Btelegram {
b := &Btelegram{} return &Btelegram{BridgeConfig: cfg}
b.Config = &cfg
b.Remote = c
b.Account = account
return b
} }
func (b *Btelegram) Connect() error { func (b *Btelegram) Connect() error {

View File

@ -14,9 +14,7 @@ import (
type Bxmpp struct { type Bxmpp struct {
xc *xmpp.Client xc *xmpp.Client
xmppMap map[string]string xmppMap map[string]string
Config *config.Protocol *config.BridgeConfig
Remote chan config.Message
Account string
} }
var flog *log.Entry var flog *log.Entry
@ -26,12 +24,9 @@ func init() {
flog = log.WithFields(log.Fields{"module": protocol}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, account string, c chan config.Message) *Bxmpp { func New(cfg *config.BridgeConfig) *Bxmpp {
b := &Bxmpp{} b := &Bxmpp{BridgeConfig: cfg}
b.xmppMap = make(map[string]string) b.xmppMap = make(map[string]string)
b.Config = &cfg
b.Account = account
b.Remote = c
return b return b
} }