diff options
author | Wim <wim@42.be> | 2017-02-17 22:12:53 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2017-02-17 22:12:53 +0100 |
commit | 7558a2162e79ffa29b11881fe249ec836a8c8b3d (patch) | |
tree | c64a7e3898d7ac9e49523ed9c0b2a4eaac492886 /gateway/samechannel | |
parent | fe258e1b6752f2751d7aec742a4ee20a5594dcdb (diff) | |
parent | 62b165c0b4052f96ab89c358301bf246d239eba7 (diff) | |
download | matterbridge-msglm-7558a2162e79ffa29b11881fe249ec836a8c8b3d.tar.gz matterbridge-msglm-7558a2162e79ffa29b11881fe249ec836a8c8b3d.tar.bz2 matterbridge-msglm-7558a2162e79ffa29b11881fe249ec836a8c8b3d.zip |
Merge branch 'status'
Diffstat (limited to 'gateway/samechannel')
-rw-r--r-- | gateway/samechannel/samechannel.go | 106 |
1 files changed, 25 insertions, 81 deletions
diff --git a/gateway/samechannel/samechannel.go b/gateway/samechannel/samechannel.go index bd8e3607..47bdfca1 100644 --- a/gateway/samechannel/samechannel.go +++ b/gateway/samechannel/samechannel.go @@ -1,105 +1,49 @@ package samechannelgateway import ( - "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/config" - log "github.com/Sirupsen/logrus" - "strings" + "github.com/42wim/matterbridge/gateway" ) type SameChannelGateway struct { *config.Config - MyConfig *config.SameChannelGateway - Bridges map[string]*bridge.Bridge - Channels []string - ignoreNicks map[string][]string - Name string + MyConfig *config.SameChannelGateway + Channels []string + Name string } -func New(cfg *config.Config, gateway *config.SameChannelGateway) error { - c := make(chan config.Message) - gw := &SameChannelGateway{} - gw.Bridges = make(map[string]*bridge.Bridge) - gw.Name = gateway.Name - gw.Config = cfg - gw.MyConfig = gateway - gw.Channels = gateway.Channels - for _, account := range gateway.Accounts { - br := config.Bridge{Account: account} - log.Infof("Starting bridge: %s", account) - gw.Bridges[account] = bridge.New(cfg, &br, c) - } - for _, br := range gw.Bridges { - err := br.Connect() - if err != nil { - log.Fatalf("Bridge %s failed to start: %v", br.Account, err) - } - for _, channel := range gw.Channels { - log.Infof("%s: joining %s", br.Account, channel) - br.JoinChannel(channel) - } - } - gw.handleReceive(c) - return nil +func New(cfg *config.Config, gatewayCfg *config.SameChannelGateway) *SameChannelGateway { + return &SameChannelGateway{ + MyConfig: gatewayCfg, + Channels: gatewayCfg.Channels, + Name: gatewayCfg.Name, + Config: cfg} } -func (gw *SameChannelGateway) handleReceive(c chan config.Message) { - for { - select { - case msg := <-c: - if !gw.ignoreMessage(&msg) { - for _, br := range gw.Bridges { - gw.handleMessage(msg, br) - } - } +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) } } + return gw.Start() } -func (gw *SameChannelGateway) handleMessage(msg config.Message, dest *bridge.Bridge) { - // is this a configured channel - if !gw.validChannel(msg.Channel) { - return - } - // do not send the message to the bridge we come from if also the channel is the same - if msg.Account == dest.Account { - return - } - gw.modifyUsername(&msg, dest) - log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, msg.Channel, dest.Account, msg.Channel) - err := dest.Send(msg) - if err != nil { - log.Error(err) - } -} - -func (gw *SameChannelGateway) ignoreMessage(msg *config.Message) bool { - for _, entry := range strings.Fields(gw.Bridges[msg.Account].Config.IgnoreNicks) { - if msg.Username == entry { - log.Debugf("ignoring %s from %s", msg.Username, msg.Account) +func (sgw *SameChannelGateway) validChannel(channel string) bool { + for _, c := range sgw.Channels { + if c == channel { return true } } return false } -func (gw *SameChannelGateway) modifyUsername(msg *config.Message, dest *bridge.Bridge) { - br := gw.Bridges[msg.Account] - nick := gw.Config.General.RemoteNickFormat - if nick == "" { - nick = dest.Config.RemoteNickFormat - } - nick = strings.Replace(nick, "{NICK}", msg.Username, -1) - nick = strings.Replace(nick, "{BRIDGE}", br.Name, -1) - nick = strings.Replace(nick, "{PROTOCOL}", br.Protocol, -1) - msg.Username = nick -} - -func (gw *SameChannelGateway) validChannel(channel string) bool { - for _, c := range gw.Channels { - if c == channel { - return true - } +func (sgw *SameChannelGateway) getDestChannel(msg *config.Message, dest string) []string { + if sgw.validChannel(msg.Channel) { + return []string{msg.Channel} } - return false + return []string{} } |