Merge branch 'channelinfo'

This commit is contained in:
Wim 2017-03-28 23:58:22 +02:00
commit 1e0490bd36
4 changed files with 73 additions and 92 deletions

View File

@ -27,17 +27,15 @@ type Bridger interface {
type Bridge struct {
Config config.Protocol
Bridger
Name string
Account string
Protocol string
ChannelsIn map[string]config.ChannelOptions
ChannelsOut map[string]config.ChannelOptions
Name string
Account string
Protocol string
Channels map[string]config.ChannelInfo
}
func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Bridge {
b := new(Bridge)
b.ChannelsIn = make(map[string]config.ChannelOptions)
b.ChannelsOut = make(map[string]config.ChannelOptions)
b.Channels = make(map[string]config.ChannelInfo)
accInfo := strings.Split(bridge.Account, ".")
protocol := accInfo[0]
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 {
exists := make(map[string]bool)
err := b.joinChannels(b.ChannelsIn, exists)
if err != nil {
return err
}
err = b.joinChannels(b.ChannelsOut, exists)
err := b.joinChannels(b.Channels, exists)
if err != nil {
return err
}
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 := ""
for channel, info := range cMap {
if !exists[channel] {
mychannel = channel
log.Infof("%s: joining %s", b.Account, channel)
if b.Protocol == "irc" && info.Key != "" {
log.Debugf("using key %s for channel %s", info.Key, channel)
mychannel = mychannel + " " + info.Key
for ID, channel := range channels {
if !exists[ID] {
mychannel = channel.Name
log.Infof("%s: joining %s %s", b.Account, channel.Name, ID)
if b.Protocol == "irc" && channel.Options.Key != "" {
log.Debugf("using key %s for channel %s", channel.Options.Key, channel.Name)
mychannel = mychannel + " " + channel.Options.Key
}
err := b.JoinChannel(mychannel)
err := b.JoinChannel(channel.Name)
if err != nil {
return err
}
exists[channel] = true
exists[ID] = true
}
}
return nil

View File

@ -25,6 +25,14 @@ type Message struct {
Timestamp time.Time
}
type ChannelInfo struct {
Name string
Account string
Direction string
ID string
Options ChannelOptions
}
type Protocol struct {
BindAddress string // mattermost, slack
Buffer int // api

View File

@ -5,7 +5,6 @@ import (
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
log "github.com/Sirupsen/logrus"
"reflect"
"strings"
"time"
)
@ -14,12 +13,11 @@ type Gateway struct {
*config.Config
MyConfig *config.Gateway
Bridges map[string]*bridge.Bridge
ChannelsOut map[string][]string
ChannelsIn map[string][]string
Channels map[string]*config.ChannelInfo
ChannelOptions map[string]config.ChannelOptions
Name string
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 {
@ -27,6 +25,7 @@ func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
gw.Name = gateway.Name
gw.Config = cfg
gw.MyConfig = gateway
gw.Channels = make(map[string]*config.ChannelInfo)
gw.Message = make(chan config.Message)
gw.Bridges = make(map[string]*bridge.Bridge)
gw.DestChannelFunc = gw.getDestChannel
@ -41,8 +40,7 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
}
log.Infof("Starting bridge: %s ", cfg.Account)
br := bridge.New(gw.Config, cfg, gw.Message)
gw.mapChannelsToBridge(br, gw.ChannelsOut)
gw.mapChannelsToBridge(br, gw.ChannelsIn)
gw.mapChannelsToBridge(br)
gw.Bridges[cfg.Account] = br
err := br.Connect()
if err != nil {
@ -55,12 +53,10 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
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) mapChannelsToBridge(br *bridge.Bridge) {
for ID, channel := range gw.Channels {
if br.Account == channel.Account {
br.Channels[ID] = *channel
}
}
}
@ -113,61 +109,53 @@ RECONNECT:
}
func (gw *Gateway) mapChannels() error {
options := make(map[string]config.ChannelOptions)
m := make(map[string][]string)
for _, br := range gw.MyConfig.Out {
m[br.Account] = append(m[br.Account], br.Channel)
options[br.Account+br.Channel] = br.Options
gw.Channels = make(map[string]*config.ChannelInfo)
for _, br := range append(gw.MyConfig.Out, gw.MyConfig.InOut...) {
ID := br.Channel + br.Account
_, ok := gw.Channels[ID]
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
for _, br := range append(gw.MyConfig.In, gw.MyConfig.InOut...) {
ID := br.Channel + br.Account
_, ok := gw.Channels[ID]
if !ok {
channel := &config.ChannelInfo{Name: br.Channel, Direction: "in", ID: ID, Options: br.Options, Account: br.Account}
gw.Channels[channel.ID] = channel
}
}
gw.ChannelsIn = m
for _, br := range gw.MyConfig.InOut {
gw.ChannelsIn[br.Account] = append(gw.ChannelsIn[br.Account], br.Channel)
gw.ChannelsOut[br.Account] = append(gw.ChannelsOut[br.Account], br.Channel)
options[br.Account+br.Channel] = br.Options
}
gw.ChannelOptions = options
return nil
}
func (gw *Gateway) getDestChannel(msg *config.Message, dest string) []string {
channels := gw.ChannelsIn[msg.Account]
// broadcast to every out channel (irc QUIT)
if msg.Event == config.EVENT_JOIN_LEAVE && msg.Channel == "" {
return gw.ChannelsOut[dest]
}
for _, channel := range channels {
if channel == msg.Channel {
return gw.ChannelsOut[dest]
func (gw *Gateway) getDestChannel(msg *config.Message, dest string) []config.ChannelInfo {
var channels []config.ChannelInfo
for _, channel := range gw.Channels {
if channel.Direction == "out" && channel.Account == dest {
channels = append(channels, *channel)
}
}
return []string{}
return channels
}
func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) {
// only relay join/part when configged
if msg.Event == config.EVENT_JOIN_LEAVE && !gw.Bridges[dest.Account].Config.ShowJoinPart {
// broadcast to every out channel (irc QUIT)
if msg.Channel == "" && msg.Event != config.EVENT_JOIN_LEAVE {
log.Debug("empty channel")
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 {
for _, channel := range gw.DestChannelFunc(&msg, dest.Account) {
// do not send to ourself
if channel.ID == getChannelID(msg) {
continue
}
msg.Channel = channel
if msg.Channel == "" {
log.Debug("empty channel")
return
}
log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel)
// 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)
// for api we need originchannel as channel
if dest.Protocol == "api" {
@ -194,21 +182,6 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
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) {
br := gw.Bridges[msg.Account]
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)
msg.Username = nick
}
func getChannelID(msg config.Message) string {
return msg.Channel + msg.Account
}

View File

@ -41,9 +41,11 @@ func (sgw *SameChannelGateway) validChannel(channel string) bool {
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) {
return []string{msg.Channel}
channels = append(channels, config.ChannelInfo{Name: msg.Channel, Account: dest, ID: msg.Channel + dest})
return channels
}
return []string{}
return channels
}