2016-09-30 14:19:47 -07:00
|
|
|
package samechannelgateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2017-02-17 13:08:30 -08:00
|
|
|
"github.com/42wim/matterbridge/gateway"
|
2016-09-30 14:19:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type SameChannelGateway struct {
|
|
|
|
*config.Config
|
2017-02-17 13:08:30 -08:00
|
|
|
MyConfig *config.SameChannelGateway
|
|
|
|
Channels []string
|
|
|
|
Name string
|
2016-09-30 14:19:47 -07:00
|
|
|
}
|
|
|
|
|
2017-02-17 13:08:30 -08:00
|
|
|
func New(cfg *config.Config, gatewayCfg *config.SameChannelGateway) *SameChannelGateway {
|
|
|
|
return &SameChannelGateway{
|
|
|
|
MyConfig: gatewayCfg,
|
|
|
|
Channels: gatewayCfg.Channels,
|
|
|
|
Name: gatewayCfg.Name,
|
|
|
|
Config: cfg}
|
2016-09-30 14:19:47 -07:00
|
|
|
}
|
|
|
|
|
2017-02-17 13:08:30 -08:00
|
|
|
func (sgw *SameChannelGateway) Start() error {
|
|
|
|
gw := gateway.New(sgw.Config, &config.Gateway{Name: sgw.Name})
|
|
|
|
gw.DestChannelFunc = sgw.getDestChannel
|
|
|
|
for _, account := range sgw.MyConfig.Accounts {
|
|
|
|
for _, channel := range sgw.Channels {
|
|
|
|
br := config.Bridge{Account: account, Channel: channel}
|
|
|
|
gw.MyConfig.InOut = append(gw.MyConfig.InOut, br)
|
2016-09-30 14:19:47 -07:00
|
|
|
}
|
|
|
|
}
|
2017-02-17 13:08:30 -08:00
|
|
|
return gw.Start()
|
2016-09-30 14:19:47 -07:00
|
|
|
}
|
|
|
|
|
2017-02-17 13:08:30 -08:00
|
|
|
func (sgw *SameChannelGateway) validChannel(channel string) bool {
|
|
|
|
for _, c := range sgw.Channels {
|
|
|
|
if c == channel {
|
2017-01-21 12:00:40 -08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-28 14:56:58 -07:00
|
|
|
func (sgw *SameChannelGateway) getDestChannel(msg *config.Message, dest string) []config.ChannelInfo {
|
|
|
|
var channels []config.ChannelInfo
|
2017-02-17 13:08:30 -08:00
|
|
|
if sgw.validChannel(msg.Channel) {
|
2017-03-28 14:56:58 -07:00
|
|
|
channels = append(channels, config.ChannelInfo{Name: msg.Channel, Account: dest, ID: msg.Channel + dest})
|
|
|
|
return channels
|
2016-11-11 06:23:22 -08:00
|
|
|
}
|
2017-03-28 14:56:58 -07:00
|
|
|
return channels
|
2016-11-11 06:23:22 -08:00
|
|
|
}
|