mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-22 12:50:27 +00:00
Refactor samechannelgateway
This commit is contained in:
parent
dc37232100
commit
62b165c0b4
@ -12,14 +12,14 @@ import (
|
|||||||
|
|
||||||
type Gateway struct {
|
type Gateway struct {
|
||||||
*config.Config
|
*config.Config
|
||||||
MyConfig *config.Gateway
|
MyConfig *config.Gateway
|
||||||
//Bridges []*bridge.Bridge
|
Bridges map[string]*bridge.Bridge
|
||||||
Bridges map[string]*bridge.Bridge
|
ChannelsOut map[string][]string
|
||||||
ChannelsOut map[string][]string
|
ChannelsIn map[string][]string
|
||||||
ChannelsIn map[string][]string
|
ChannelOptions map[string]config.ChannelOptions
|
||||||
ChannelOptions map[string]config.ChannelOptions
|
Name string
|
||||||
Name string
|
Message chan config.Message
|
||||||
Message chan config.Message
|
DestChannelFunc func(msg *config.Message, dest string) []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
|
func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
|
||||||
@ -29,6 +29,7 @@ func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
|
|||||||
gw.MyConfig = gateway
|
gw.MyConfig = gateway
|
||||||
gw.Message = make(chan config.Message)
|
gw.Message = make(chan config.Message)
|
||||||
gw.Bridges = make(map[string]*bridge.Bridge)
|
gw.Bridges = make(map[string]*bridge.Bridge)
|
||||||
|
gw.DestChannelFunc = gw.getDestChannel
|
||||||
return gw
|
return gw
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +152,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
originchannel := msg.Channel
|
originchannel := msg.Channel
|
||||||
channels := gw.getDestChannel(&msg, dest.Account)
|
channels := gw.DestChannelFunc(&msg, dest.Account)
|
||||||
for _, channel := range channels {
|
for _, channel := range channels {
|
||||||
// do not send the message to the bridge we come from if also the channel is the same
|
// do not send the message to the bridge we come from if also the channel is the same
|
||||||
if msg.Account == dest.Account && channel == originchannel {
|
if msg.Account == dest.Account && channel == originchannel {
|
||||||
|
@ -1,105 +1,49 @@
|
|||||||
package samechannelgateway
|
package samechannelgateway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/42wim/matterbridge/bridge"
|
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
log "github.com/Sirupsen/logrus"
|
"github.com/42wim/matterbridge/gateway"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SameChannelGateway struct {
|
type SameChannelGateway struct {
|
||||||
*config.Config
|
*config.Config
|
||||||
MyConfig *config.SameChannelGateway
|
MyConfig *config.SameChannelGateway
|
||||||
Bridges map[string]*bridge.Bridge
|
Channels []string
|
||||||
Channels []string
|
Name string
|
||||||
ignoreNicks map[string][]string
|
|
||||||
Name string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.Config, gateway *config.SameChannelGateway) error {
|
func New(cfg *config.Config, gatewayCfg *config.SameChannelGateway) *SameChannelGateway {
|
||||||
c := make(chan config.Message)
|
return &SameChannelGateway{
|
||||||
gw := &SameChannelGateway{}
|
MyConfig: gatewayCfg,
|
||||||
gw.Bridges = make(map[string]*bridge.Bridge)
|
Channels: gatewayCfg.Channels,
|
||||||
gw.Name = gateway.Name
|
Name: gatewayCfg.Name,
|
||||||
gw.Config = cfg
|
Config: cfg}
|
||||||
gw.MyConfig = gateway
|
}
|
||||||
gw.Channels = gateway.Channels
|
|
||||||
for _, account := range gateway.Accounts {
|
func (sgw *SameChannelGateway) Start() error {
|
||||||
br := config.Bridge{Account: account}
|
gw := gateway.New(sgw.Config, &config.Gateway{Name: sgw.Name})
|
||||||
log.Infof("Starting bridge: %s", account)
|
gw.DestChannelFunc = sgw.getDestChannel
|
||||||
gw.Bridges[account] = bridge.New(cfg, &br, c)
|
for _, account := range sgw.MyConfig.Accounts {
|
||||||
}
|
for _, channel := range sgw.Channels {
|
||||||
for _, br := range gw.Bridges {
|
br := config.Bridge{Account: account, Channel: channel}
|
||||||
err := br.Connect()
|
gw.MyConfig.InOut = append(gw.MyConfig.InOut, br)
|
||||||
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 gw.Start()
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gw *SameChannelGateway) handleReceive(c chan config.Message) {
|
func (sgw *SameChannelGateway) validChannel(channel string) bool {
|
||||||
for {
|
for _, c := range sgw.Channels {
|
||||||
select {
|
|
||||||
case msg := <-c:
|
|
||||||
if !gw.ignoreMessage(&msg) {
|
|
||||||
for _, br := range gw.Bridges {
|
|
||||||
gw.handleMessage(msg, br)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
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 {
|
if c == channel {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sgw *SameChannelGateway) getDestChannel(msg *config.Message, dest string) []string {
|
||||||
|
if sgw.validChannel(msg.Channel) {
|
||||||
|
return []string{msg.Channel}
|
||||||
|
}
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
@ -36,12 +36,11 @@ func main() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("starting samechannel gateway %#v\n", gw.Name)
|
fmt.Printf("starting samechannel gateway %#v\n", gw.Name)
|
||||||
go func(gw config.SameChannelGateway) {
|
g := samechannelgateway.New(cfg, &gw)
|
||||||
err := samechannelgateway.New(cfg, &gw)
|
err := g.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("starting gateway failed %#v", err)
|
log.Fatalf("starting gateway failed %#v", err)
|
||||||
}
|
}
|
||||||
}(gw)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, gw := range cfg.Gateway {
|
for _, gw := range cfg.Gateway {
|
||||||
|
Loading…
Reference in New Issue
Block a user