Refactor. Make extra options easier for other protocols

This commit is contained in:
Wim 2017-02-14 23:52:45 +01:00
parent 163f55f9c2
commit dc37232100
2 changed files with 33 additions and 12 deletions

View File

@ -25,16 +25,17 @@ type Bridger interface {
type Bridge struct { type Bridge struct {
Config config.Protocol Config config.Protocol
Bridger Bridger
Name string Name string
Account string Account string
Protocol string Protocol string
ChannelsOut []string ChannelsIn map[string]config.ChannelOptions
ChannelsIn []string ChannelsOut map[string]config.ChannelOptions
ChannelOptions config.ChannelOptions
} }
func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Bridge { func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Bridge {
b := new(Bridge) b := new(Bridge)
b.ChannelsIn = make(map[string]config.ChannelOptions)
b.ChannelsOut = make(map[string]config.ChannelOptions)
accInfo := strings.Split(bridge.Account, ".") accInfo := strings.Split(bridge.Account, ".")
protocol := accInfo[0] protocol := accInfo[0]
name := accInfo[1] name := accInfo[1]
@ -75,10 +76,22 @@ func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Brid
func (b *Bridge) JoinChannels() error { func (b *Bridge) JoinChannels() error {
exists := make(map[string]bool) exists := make(map[string]bool)
for _, channel := range append(b.ChannelsIn, b.ChannelsOut...) { b.joinChannels(b.ChannelsIn, exists)
b.joinChannels(b.ChannelsOut, exists)
return nil
}
func (b *Bridge) joinChannels(cMap map[string]config.ChannelOptions, exists map[string]bool) error {
mychannel := ""
for channel, info := range cMap {
if !exists[channel] { if !exists[channel] {
mychannel = channel
log.Infof("%s: joining %s", b.Account, channel) log.Infof("%s: joining %s", b.Account, channel)
b.JoinChannel(channel) if b.Protocol == "irc" && info.Key != "" {
log.Debugf("using key %s for channel %s", info.Key, channel)
mychannel = mychannel + " " + info.Key
}
b.JoinChannel(mychannel)
exists[channel] = true exists[channel] = true
} }
} }

View File

@ -40,10 +40,8 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
} }
log.Infof("Starting bridge: %s ", cfg.Account) log.Infof("Starting bridge: %s ", cfg.Account)
br := bridge.New(gw.Config, cfg, gw.Message) br := bridge.New(gw.Config, cfg, gw.Message)
br.ChannelsOut = gw.ChannelsOut[br.Account] gw.mapChannelsToBridge(br, gw.ChannelsOut)
br.ChannelsIn = gw.ChannelsIn[br.Account] gw.mapChannelsToBridge(br, gw.ChannelsIn)
br.ChannelOptions = gw.ChannelOptions[br.Account]
gw.Bridges[cfg.Account] = br gw.Bridges[cfg.Account] = br
err := br.Connect() err := br.Connect()
if err != nil { if err != nil {
@ -53,6 +51,16 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
return nil return nil
} }
func (gw *Gateway) mapChannelsToBridge(br *bridge.Bridge, cMap map[string][]string) {
for _, channel := range cMap[br.Account] {
if _, ok := gw.ChannelOptions[br.Account+channel]; ok {
br.ChannelsOut[channel] = gw.ChannelOptions[br.Account+channel]
} else {
br.ChannelsOut[channel] = config.ChannelOptions{}
}
}
}
func (gw *Gateway) Start() error { func (gw *Gateway) Start() error {
gw.mapChannels() gw.mapChannels()
for _, br := range append(gw.MyConfig.In, append(gw.MyConfig.InOut, gw.MyConfig.Out...)...) { for _, br := range append(gw.MyConfig.In, append(gw.MyConfig.InOut, gw.MyConfig.Out...)...) {