mirror of
https://github.com/42wim/matterbridge.git
synced 2024-12-24 04:02:37 +01:00
Refactor
This commit is contained in:
parent
02d7e2db65
commit
2e4d58cb92
@ -30,14 +30,12 @@ type Bridge struct {
|
|||||||
Name string
|
Name string
|
||||||
Account string
|
Account string
|
||||||
Protocol string
|
Protocol string
|
||||||
ChannelsIn map[string]config.ChannelOptions
|
Channels map[string]config.ChannelInfo
|
||||||
ChannelsOut map[string]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.Channels = make(map[string]config.ChannelInfo)
|
||||||
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]
|
||||||
@ -84,32 +82,28 @@ 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)
|
||||||
err := b.joinChannels(b.ChannelsIn, exists)
|
err := b.joinChannels(b.Channels, exists)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = b.joinChannels(b.ChannelsOut, exists)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bridge) joinChannels(cMap map[string]config.ChannelOptions, exists map[string]bool) error {
|
func (b *Bridge) joinChannels(channels map[string]config.ChannelInfo, exists map[string]bool) error {
|
||||||
mychannel := ""
|
mychannel := ""
|
||||||
for channel, info := range cMap {
|
for ID, channel := range channels {
|
||||||
if !exists[channel] {
|
if !exists[ID] {
|
||||||
mychannel = channel
|
mychannel = channel.Name
|
||||||
log.Infof("%s: joining %s", b.Account, channel)
|
log.Infof("%s: joining %s %s", b.Account, channel.Name, ID)
|
||||||
if b.Protocol == "irc" && info.Key != "" {
|
if b.Protocol == "irc" && channel.Options.Key != "" {
|
||||||
log.Debugf("using key %s for channel %s", info.Key, channel)
|
log.Debugf("using key %s for channel %s", channel.Options.Key, channel.Name)
|
||||||
mychannel = mychannel + " " + info.Key
|
mychannel = mychannel + " " + channel.Options.Key
|
||||||
}
|
}
|
||||||
err := b.JoinChannel(mychannel)
|
err := b.JoinChannel(channel.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
exists[channel] = true
|
exists[ID] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -25,6 +25,14 @@ type Message struct {
|
|||||||
Timestamp time.Time
|
Timestamp time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChannelInfo struct {
|
||||||
|
Name string
|
||||||
|
Account string
|
||||||
|
Direction string
|
||||||
|
ID string
|
||||||
|
Options ChannelOptions
|
||||||
|
}
|
||||||
|
|
||||||
type Protocol struct {
|
type Protocol struct {
|
||||||
BindAddress string // mattermost, slack
|
BindAddress string // mattermost, slack
|
||||||
Buffer int // api
|
Buffer int // api
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"github.com/42wim/matterbridge/bridge"
|
"github.com/42wim/matterbridge/bridge"
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -14,12 +13,11 @@ type Gateway struct {
|
|||||||
*config.Config
|
*config.Config
|
||||||
MyConfig *config.Gateway
|
MyConfig *config.Gateway
|
||||||
Bridges map[string]*bridge.Bridge
|
Bridges map[string]*bridge.Bridge
|
||||||
ChannelsOut map[string][]string
|
Channels map[string]*config.ChannelInfo
|
||||||
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
|
DestChannelFunc func(msg *config.Message, dest string) []config.ChannelInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
|
func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
|
||||||
@ -27,6 +25,7 @@ func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
|
|||||||
gw.Name = gateway.Name
|
gw.Name = gateway.Name
|
||||||
gw.Config = cfg
|
gw.Config = cfg
|
||||||
gw.MyConfig = gateway
|
gw.MyConfig = gateway
|
||||||
|
gw.Channels = make(map[string]*config.ChannelInfo)
|
||||||
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
|
gw.DestChannelFunc = gw.getDestChannel
|
||||||
@ -41,8 +40,7 @@ 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)
|
||||||
gw.mapChannelsToBridge(br, gw.ChannelsOut)
|
gw.mapChannelsToBridge(br)
|
||||||
gw.mapChannelsToBridge(br, gw.ChannelsIn)
|
|
||||||
gw.Bridges[cfg.Account] = br
|
gw.Bridges[cfg.Account] = br
|
||||||
err := br.Connect()
|
err := br.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -55,12 +53,10 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gw *Gateway) mapChannelsToBridge(br *bridge.Bridge, cMap map[string][]string) {
|
func (gw *Gateway) mapChannelsToBridge(br *bridge.Bridge) {
|
||||||
for _, channel := range cMap[br.Account] {
|
for ID, channel := range gw.Channels {
|
||||||
if _, ok := gw.ChannelOptions[br.Account+channel]; ok {
|
if br.Account == channel.Account {
|
||||||
br.ChannelsOut[channel] = gw.ChannelOptions[br.Account+channel]
|
br.Channels[ID] = *channel
|
||||||
} else {
|
|
||||||
br.ChannelsOut[channel] = config.ChannelOptions{}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -113,61 +109,53 @@ RECONNECT:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gw *Gateway) mapChannels() error {
|
func (gw *Gateway) mapChannels() error {
|
||||||
options := make(map[string]config.ChannelOptions)
|
gw.Channels = make(map[string]*config.ChannelInfo)
|
||||||
m := make(map[string][]string)
|
for _, br := range append(gw.MyConfig.Out, gw.MyConfig.InOut...) {
|
||||||
for _, br := range gw.MyConfig.Out {
|
ID := br.Channel + br.Account
|
||||||
m[br.Account] = append(m[br.Account], br.Channel)
|
_, ok := gw.Channels[ID]
|
||||||
options[br.Account+br.Channel] = br.Options
|
if !ok {
|
||||||
|
channel := &config.ChannelInfo{Name: br.Channel, Direction: "out", ID: ID, Options: br.Options, Account: br.Account}
|
||||||
|
gw.Channels[channel.ID] = channel
|
||||||
}
|
}
|
||||||
gw.ChannelsOut = m
|
|
||||||
m = nil
|
|
||||||
m = make(map[string][]string)
|
|
||||||
for _, br := range gw.MyConfig.In {
|
|
||||||
m[br.Account] = append(m[br.Account], br.Channel)
|
|
||||||
options[br.Account+br.Channel] = br.Options
|
|
||||||
}
|
}
|
||||||
gw.ChannelsIn = m
|
|
||||||
for _, br := range gw.MyConfig.InOut {
|
for _, br := range append(gw.MyConfig.In, gw.MyConfig.InOut...) {
|
||||||
gw.ChannelsIn[br.Account] = append(gw.ChannelsIn[br.Account], br.Channel)
|
ID := br.Channel + br.Account
|
||||||
gw.ChannelsOut[br.Account] = append(gw.ChannelsOut[br.Account], br.Channel)
|
_, ok := gw.Channels[ID]
|
||||||
options[br.Account+br.Channel] = br.Options
|
if !ok {
|
||||||
|
channel := &config.ChannelInfo{Name: br.Channel, Direction: "in", ID: ID, Options: br.Options, Account: br.Account}
|
||||||
|
gw.Channels[channel.ID] = channel
|
||||||
|
}
|
||||||
}
|
}
|
||||||
gw.ChannelOptions = options
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gw *Gateway) getDestChannel(msg *config.Message, dest string) []string {
|
func (gw *Gateway) getDestChannel(msg *config.Message, dest string) []config.ChannelInfo {
|
||||||
channels := gw.ChannelsIn[msg.Account]
|
var channels []config.ChannelInfo
|
||||||
// broadcast to every out channel (irc QUIT)
|
for _, channel := range gw.Channels {
|
||||||
if msg.Event == config.EVENT_JOIN_LEAVE && msg.Channel == "" {
|
if channel.Direction == "out" && channel.Account == dest {
|
||||||
return gw.ChannelsOut[dest]
|
channels = append(channels, *channel)
|
||||||
}
|
|
||||||
for _, channel := range channels {
|
|
||||||
if channel == msg.Channel {
|
|
||||||
return gw.ChannelsOut[dest]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return []string{}
|
return channels
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) {
|
func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) {
|
||||||
// only relay join/part when configged
|
// broadcast to every out channel (irc QUIT)
|
||||||
if msg.Event == config.EVENT_JOIN_LEAVE && !gw.Bridges[dest.Account].Config.ShowJoinPart {
|
if msg.Channel == "" && msg.Event != config.EVENT_JOIN_LEAVE {
|
||||||
return
|
|
||||||
}
|
|
||||||
originchannel := msg.Channel
|
|
||||||
channels := gw.DestChannelFunc(&msg, dest.Account)
|
|
||||||
for _, channel := range channels {
|
|
||||||
// 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 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
msg.Channel = channel
|
|
||||||
if msg.Channel == "" {
|
|
||||||
log.Debug("empty channel")
|
log.Debug("empty channel")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel)
|
originchannel := msg.Channel
|
||||||
|
for _, channel := range gw.DestChannelFunc(&msg, dest.Account) {
|
||||||
|
// do not send to ourself
|
||||||
|
if channel.ID == getChannelID(msg) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// outgoing channels for this account
|
||||||
|
//if channel.Direction == "out" && channel.Account == dest.Account {
|
||||||
|
log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel.Name)
|
||||||
|
msg.Channel = channel.Name
|
||||||
gw.modifyUsername(&msg, dest)
|
gw.modifyUsername(&msg, dest)
|
||||||
// for api we need originchannel as channel
|
// for api we need originchannel as channel
|
||||||
if dest.Protocol == "api" {
|
if dest.Protocol == "api" {
|
||||||
@ -194,21 +182,6 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gw *Gateway) modifyMessage(msg *config.Message, dest *bridge.Bridge) {
|
|
||||||
val := reflect.ValueOf(gw.Config).Elem()
|
|
||||||
for i := 0; i < val.NumField(); i++ {
|
|
||||||
typeField := val.Type().Field(i)
|
|
||||||
// look for the protocol map (both lowercase)
|
|
||||||
if strings.ToLower(typeField.Name) == dest.Protocol {
|
|
||||||
// get the Protocol struct from the map
|
|
||||||
protoCfg := val.Field(i).MapIndex(reflect.ValueOf(dest.Name))
|
|
||||||
//config.SetNickFormat(msg, protoCfg.Interface().(config.Protocol))
|
|
||||||
val.Field(i).SetMapIndex(reflect.ValueOf(dest.Name), protoCfg)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gw *Gateway) modifyUsername(msg *config.Message, dest *bridge.Bridge) {
|
func (gw *Gateway) modifyUsername(msg *config.Message, dest *bridge.Bridge) {
|
||||||
br := gw.Bridges[msg.Account]
|
br := gw.Bridges[msg.Account]
|
||||||
msg.Protocol = br.Protocol
|
msg.Protocol = br.Protocol
|
||||||
@ -221,3 +194,7 @@ func (gw *Gateway) modifyUsername(msg *config.Message, dest *bridge.Bridge) {
|
|||||||
nick = strings.Replace(nick, "{PROTOCOL}", br.Protocol, -1)
|
nick = strings.Replace(nick, "{PROTOCOL}", br.Protocol, -1)
|
||||||
msg.Username = nick
|
msg.Username = nick
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getChannelID(msg config.Message) string {
|
||||||
|
return msg.Channel + msg.Account
|
||||||
|
}
|
||||||
|
@ -41,9 +41,11 @@ func (sgw *SameChannelGateway) validChannel(channel string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sgw *SameChannelGateway) getDestChannel(msg *config.Message, dest string) []string {
|
func (sgw *SameChannelGateway) getDestChannel(msg *config.Message, dest string) []config.ChannelInfo {
|
||||||
|
var channels []config.ChannelInfo
|
||||||
if sgw.validChannel(msg.Channel) {
|
if sgw.validChannel(msg.Channel) {
|
||||||
return []string{msg.Channel}
|
channels = append(channels, config.ChannelInfo{Name: msg.Channel, Account: dest, ID: msg.Channel + dest})
|
||||||
|
return channels
|
||||||
}
|
}
|
||||||
return []string{}
|
return channels
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user