forked from lug/matterbridge
		
	Compare commits
	
		
			8 Commits
		
	
	
		
			v0.10.3
			...
			v0.11.0-be
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					33dffd5ea8 | ||
| 
						 | 
					57176dadd4 | ||
| 
						 | 
					dd449a8705 | ||
| 
						 | 
					587ad9f41d | ||
| 
						 | 
					a16ad8bf3b | ||
| 
						 | 
					1e0490bd36 | ||
| 
						 | 
					8afc641f0c | ||
| 
						 | 
					2e4d58cb92 | 
@@ -27,23 +27,23 @@ 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
 | 
			
		||||
	Joined   map[string]bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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]
 | 
			
		||||
	b.Name = name
 | 
			
		||||
	b.Protocol = protocol
 | 
			
		||||
	b.Account = bridge.Account
 | 
			
		||||
	b.Joined = make(map[string]bool)
 | 
			
		||||
 | 
			
		||||
	// override config from environment
 | 
			
		||||
	config.OverrideCfgFromEnv(cfg, protocol, name)
 | 
			
		||||
@@ -83,33 +83,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, b.Joined)
 | 
			
		||||
	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
 | 
			
		||||
 
 | 
			
		||||
@@ -25,6 +25,16 @@ type Message struct {
 | 
			
		||||
	Timestamp time.Time
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ChannelInfo struct {
 | 
			
		||||
	Name        string
 | 
			
		||||
	Account     string
 | 
			
		||||
	Direction   string
 | 
			
		||||
	ID          string
 | 
			
		||||
	GID         map[string]bool
 | 
			
		||||
	SameChannel map[string]bool
 | 
			
		||||
	Options     ChannelOptions
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Protocol struct {
 | 
			
		||||
	BindAddress            string // mattermost, slack
 | 
			
		||||
	Buffer                 int    // api
 | 
			
		||||
@@ -63,9 +73,10 @@ type ChannelOptions struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Bridge struct {
 | 
			
		||||
	Account string
 | 
			
		||||
	Channel string
 | 
			
		||||
	Options ChannelOptions
 | 
			
		||||
	Account     string
 | 
			
		||||
	Channel     string
 | 
			
		||||
	Options     ChannelOptions
 | 
			
		||||
	SameChannel bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Gateway struct {
 | 
			
		||||
 
 | 
			
		||||
@@ -92,7 +92,7 @@ func (b *Birc) Connect() error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (b *Birc) Disconnect() error {
 | 
			
		||||
	b.i.Disconnect()
 | 
			
		||||
	//b.i.Disconnect()
 | 
			
		||||
	close(b.Local)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -96,12 +96,7 @@ func (b *Bmattermost) Send(msg config.Message) error {
 | 
			
		||||
	channel := msg.Channel
 | 
			
		||||
 | 
			
		||||
	if b.Config.PrefixMessagesWithNick {
 | 
			
		||||
		/*if IsMarkup(message) {
 | 
			
		||||
			message = nick + "\n\n" + message
 | 
			
		||||
		} else {
 | 
			
		||||
		*/
 | 
			
		||||
		message = nick + " " + message
 | 
			
		||||
		//}
 | 
			
		||||
		message = nick + message
 | 
			
		||||
	}
 | 
			
		||||
	if !b.Config.UseAPI {
 | 
			
		||||
		matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
 | 
			
		||||
 
 | 
			
		||||
@@ -80,28 +80,30 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
 | 
			
		||||
	text := ""
 | 
			
		||||
	channel := ""
 | 
			
		||||
	for update := range updates {
 | 
			
		||||
		var message *tgbotapi.Message
 | 
			
		||||
		// handle channels
 | 
			
		||||
		if update.ChannelPost != nil {
 | 
			
		||||
			if update.ChannelPost.From != nil {
 | 
			
		||||
				username = update.ChannelPost.From.FirstName
 | 
			
		||||
				if username == "" {
 | 
			
		||||
					username = update.ChannelPost.From.UserName
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			text = update.ChannelPost.Text
 | 
			
		||||
			channel = strconv.FormatInt(update.ChannelPost.Chat.ID, 10)
 | 
			
		||||
			message = update.ChannelPost
 | 
			
		||||
		}
 | 
			
		||||
		if update.EditedChannelPost != nil {
 | 
			
		||||
			message = update.EditedChannelPost
 | 
			
		||||
		}
 | 
			
		||||
		// handle groups
 | 
			
		||||
		if update.Message != nil {
 | 
			
		||||
			if update.Message.From != nil {
 | 
			
		||||
				username = update.Message.From.FirstName
 | 
			
		||||
				if username == "" {
 | 
			
		||||
					username = update.Message.From.UserName
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			text = update.Message.Text
 | 
			
		||||
			channel = strconv.FormatInt(update.Message.Chat.ID, 10)
 | 
			
		||||
			message = update.Message
 | 
			
		||||
		}
 | 
			
		||||
		if update.EditedMessage != nil {
 | 
			
		||||
			message = update.EditedMessage
 | 
			
		||||
		}
 | 
			
		||||
		if message.From != nil {
 | 
			
		||||
			username = message.From.FirstName
 | 
			
		||||
			if username == "" {
 | 
			
		||||
				username = message.From.UserName
 | 
			
		||||
			}
 | 
			
		||||
			text = message.Text
 | 
			
		||||
			channel = strconv.FormatInt(message.Chat.ID, 10)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if username == "" {
 | 
			
		||||
			username = "unknown"
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,9 @@
 | 
			
		||||
# v0.11.0-dev
 | 
			
		||||
## New features
 | 
			
		||||
* general: reusing the same account on multiple gateways now also reuses the connection. 
 | 
			
		||||
  This is particuarly useful for irc. See #87
 | 
			
		||||
* general: the Name is now REQUIRED and needs to be UNIQUE for each gateway configuration
 | 
			
		||||
 | 
			
		||||
# v0.10.3
 | 
			
		||||
## Bugfix
 | 
			
		||||
* slack: Allow bot tokens for now without warning (slack). Closes #140 (fixes user_is_bot message on channel join)
 | 
			
		||||
 
 | 
			
		||||
@@ -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,21 +13,21 @@ 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
 | 
			
		||||
	Names           map[string]bool
 | 
			
		||||
	Name            string
 | 
			
		||||
	Message         chan config.Message
 | 
			
		||||
	DestChannelFunc func(msg *config.Message, dest string) []string
 | 
			
		||||
	DestChannelFunc func(msg *config.Message, dest bridge.Bridge) []config.ChannelInfo
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
 | 
			
		||||
func New(cfg *config.Config) *Gateway {
 | 
			
		||||
	gw := &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.Names = make(map[string]bool)
 | 
			
		||||
	gw.DestChannelFunc = gw.getDestChannel
 | 
			
		||||
	return gw
 | 
			
		||||
}
 | 
			
		||||
@@ -36,13 +35,17 @@ func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
 | 
			
		||||
func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
 | 
			
		||||
	for _, br := range gw.Bridges {
 | 
			
		||||
		if br.Account == cfg.Account {
 | 
			
		||||
			gw.mapChannelsToBridge(br)
 | 
			
		||||
			err := br.JoinChannels()
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return fmt.Errorf("Bridge %s failed to join channel: %v", br.Account, err)
 | 
			
		||||
			}
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	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,17 +58,17 @@ 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) AddConfig(cfg *config.Gateway) error {
 | 
			
		||||
	if gw.Names[cfg.Name] {
 | 
			
		||||
		return fmt.Errorf("Gateway with name %s already exists", cfg.Name)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (gw *Gateway) Start() error {
 | 
			
		||||
	if cfg.Name == "" {
 | 
			
		||||
		return fmt.Errorf("%s", "Gateway without name found")
 | 
			
		||||
	}
 | 
			
		||||
	log.Infof("Starting gateway: %s", cfg.Name)
 | 
			
		||||
	gw.Names[cfg.Name] = true
 | 
			
		||||
	gw.Name = cfg.Name
 | 
			
		||||
	gw.MyConfig = cfg
 | 
			
		||||
	gw.mapChannels()
 | 
			
		||||
	for _, br := range append(gw.MyConfig.In, append(gw.MyConfig.InOut, gw.MyConfig.Out...)...) {
 | 
			
		||||
		err := gw.AddBridge(&br)
 | 
			
		||||
@@ -73,6 +76,18 @@ func (gw *Gateway) Start() error {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (gw *Gateway) mapChannelsToBridge(br *bridge.Bridge) {
 | 
			
		||||
	for ID, channel := range gw.Channels {
 | 
			
		||||
		if br.Account == channel.Account {
 | 
			
		||||
			br.Channels[ID] = *channel
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (gw *Gateway) Start() error {
 | 
			
		||||
	go gw.handleReceive()
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
@@ -109,45 +124,52 @@ RECONNECT:
 | 
			
		||||
		time.Sleep(time.Second * 60)
 | 
			
		||||
		goto RECONNECT
 | 
			
		||||
	}
 | 
			
		||||
	br.Joined = make(map[string]bool)
 | 
			
		||||
	br.JoinChannels()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
	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,
 | 
			
		||||
				GID: make(map[string]bool), SameChannel: make(map[string]bool)}
 | 
			
		||||
			channel.GID[gw.Name] = true
 | 
			
		||||
			channel.SameChannel[gw.Name] = br.SameChannel
 | 
			
		||||
			gw.Channels[channel.ID] = channel
 | 
			
		||||
		}
 | 
			
		||||
		gw.Channels[ID].GID[gw.Name] = true
 | 
			
		||||
		gw.Channels[ID].SameChannel[gw.Name] = br.SameChannel
 | 
			
		||||
	}
 | 
			
		||||
	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,
 | 
			
		||||
				GID: make(map[string]bool), SameChannel: make(map[string]bool)}
 | 
			
		||||
			channel.GID[gw.Name] = true
 | 
			
		||||
			channel.SameChannel[gw.Name] = br.SameChannel
 | 
			
		||||
			gw.Channels[channel.ID] = channel
 | 
			
		||||
		}
 | 
			
		||||
		gw.Channels[ID].GID[gw.Name] = true
 | 
			
		||||
		gw.Channels[ID].SameChannel[gw.Name] = br.SameChannel
 | 
			
		||||
	}
 | 
			
		||||
	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 bridge.Bridge) []config.ChannelInfo {
 | 
			
		||||
	var channels []config.ChannelInfo
 | 
			
		||||
	for _, channel := range gw.Channels {
 | 
			
		||||
		if _, ok := gw.Channels[getChannelID(*msg)]; !ok {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		if channel.Direction == "out" && channel.Account == dest.Account && gw.validGatewayDest(*msg, channel) {
 | 
			
		||||
			channels = append(channels, *channel)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return []string{}
 | 
			
		||||
	return channels
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) {
 | 
			
		||||
@@ -155,19 +177,19 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) {
 | 
			
		||||
	if msg.Event == config.EVENT_JOIN_LEAVE && !gw.Bridges[dest.Account].Config.ShowJoinPart {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	// 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) {
 | 
			
		||||
		// 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)
 | 
			
		||||
		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 +216,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 +228,29 @@ 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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (gw *Gateway) validGatewayDest(msg config.Message, channel *config.ChannelInfo) bool {
 | 
			
		||||
	GIDmap := gw.Channels[getChannelID(msg)].GID
 | 
			
		||||
	// check if we are running a samechannelgateway.
 | 
			
		||||
	// if it is and the channel name matches it's ok, otherwise we shouldn't use this channel.
 | 
			
		||||
	for k, _ := range GIDmap {
 | 
			
		||||
		if channel.SameChannel[k] == true {
 | 
			
		||||
			if msg.Channel == channel.Name {
 | 
			
		||||
				return true
 | 
			
		||||
			} else {
 | 
			
		||||
				return false
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// check if we are in the correct gateway
 | 
			
		||||
	for k, _ := range GIDmap {
 | 
			
		||||
		if channel.GID[k] == true {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,48 +2,27 @@ package samechannelgateway
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/42wim/matterbridge/bridge/config"
 | 
			
		||||
	"github.com/42wim/matterbridge/gateway"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type SameChannelGateway struct {
 | 
			
		||||
	*config.Config
 | 
			
		||||
	MyConfig *config.SameChannelGateway
 | 
			
		||||
	Channels []string
 | 
			
		||||
	Name     string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func New(cfg *config.Config, gatewayCfg *config.SameChannelGateway) *SameChannelGateway {
 | 
			
		||||
	return &SameChannelGateway{
 | 
			
		||||
		MyConfig: gatewayCfg,
 | 
			
		||||
		Channels: gatewayCfg.Channels,
 | 
			
		||||
		Name:     gatewayCfg.Name,
 | 
			
		||||
		Config:   cfg}
 | 
			
		||||
func New(cfg *config.Config) *SameChannelGateway {
 | 
			
		||||
	return &SameChannelGateway{Config: cfg}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
func (sgw *SameChannelGateway) GetConfig() []config.Gateway {
 | 
			
		||||
	var gwconfigs []config.Gateway
 | 
			
		||||
	cfg := sgw.Config
 | 
			
		||||
	for _, gw := range cfg.SameChannelGateway {
 | 
			
		||||
		gwconfig := config.Gateway{Name: gw.Name, Enable: gw.Enable}
 | 
			
		||||
		for _, account := range gw.Accounts {
 | 
			
		||||
			for _, channel := range gw.Channels {
 | 
			
		||||
				gwconfig.InOut = append(gwconfig.InOut, config.Bridge{Account: account, Channel: channel, SameChannel: true})
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		gwconfigs = append(gwconfigs, gwconfig)
 | 
			
		||||
	}
 | 
			
		||||
	return gw.Start()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (sgw *SameChannelGateway) validChannel(channel string) bool {
 | 
			
		||||
	for _, c := range sgw.Channels {
 | 
			
		||||
		if c == channel {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (sgw *SameChannelGateway) getDestChannel(msg *config.Message, dest string) []string {
 | 
			
		||||
	if sgw.validChannel(msg.Channel) {
 | 
			
		||||
		return []string{msg.Channel}
 | 
			
		||||
	}
 | 
			
		||||
	return []string{}
 | 
			
		||||
	return gwconfigs
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,10 +8,11 @@ import (
 | 
			
		||||
	"github.com/42wim/matterbridge/gateway/samechannel"
 | 
			
		||||
	log "github.com/Sirupsen/logrus"
 | 
			
		||||
	"github.com/google/gops/agent"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	version = "0.10.3"
 | 
			
		||||
	version = "0.11.0-dev"
 | 
			
		||||
	githash string
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -39,31 +40,26 @@ func main() {
 | 
			
		||||
		log.SetLevel(log.DebugLevel)
 | 
			
		||||
	}
 | 
			
		||||
	log.Printf("Running version %s %s", version, githash)
 | 
			
		||||
	cfg := config.NewConfig(*flagConfig)
 | 
			
		||||
	for _, gw := range cfg.SameChannelGateway {
 | 
			
		||||
		if !gw.Enable {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		log.Printf("Starting samechannel gateway %#v", gw.Name)
 | 
			
		||||
		g := samechannelgateway.New(cfg, &gw)
 | 
			
		||||
		err := g.Start()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatalf("Starting gateway failed %#v", err)
 | 
			
		||||
		}
 | 
			
		||||
		log.Printf("Started samechannel gateway %#v", gw.Name)
 | 
			
		||||
	if strings.Contains(version, "-dev") {
 | 
			
		||||
		log.Println("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.")
 | 
			
		||||
	}
 | 
			
		||||
	cfg := config.NewConfig(*flagConfig)
 | 
			
		||||
 | 
			
		||||
	for _, gw := range cfg.Gateway {
 | 
			
		||||
	g := gateway.New(cfg)
 | 
			
		||||
	sgw := samechannelgateway.New(cfg)
 | 
			
		||||
	gwconfigs := sgw.GetConfig()
 | 
			
		||||
	for _, gw := range append(gwconfigs, cfg.Gateway...) {
 | 
			
		||||
		if !gw.Enable {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		log.Printf("Starting gateway %#v", gw.Name)
 | 
			
		||||
		g := gateway.New(cfg, &gw)
 | 
			
		||||
		err := g.Start()
 | 
			
		||||
		err := g.AddConfig(&gw)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatalf("Starting gateway failed %#v", err)
 | 
			
		||||
			log.Fatalf("Starting gateway failed: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
		log.Printf("Started gateway %#v", gw.Name)
 | 
			
		||||
	}
 | 
			
		||||
	err := g.Start()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalf("Starting gateway failed: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	log.Printf("Gateway(s) started succesfully. Now relaying messages")
 | 
			
		||||
	select {}
 | 
			
		||||
 
 | 
			
		||||
@@ -587,7 +587,7 @@ RemoteNickFormat="[{PROTOCOL}] <{NICK}> "
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
[[gateway]]
 | 
			
		||||
#OPTIONAL (not used for now)
 | 
			
		||||
#REQUIRED and UNIQUE
 | 
			
		||||
name="gateway1"
 | 
			
		||||
#Enable enables this gateway
 | 
			
		||||
##OPTIONAL (default false)
 | 
			
		||||
@@ -659,6 +659,7 @@ enable=true
 | 
			
		||||
#channel testing on slack and vice versa. (and for the channel testing2 and testing3)
 | 
			
		||||
 | 
			
		||||
[[samechannelgateway]]
 | 
			
		||||
   name="samechannel1"
 | 
			
		||||
   enable = false
 | 
			
		||||
   accounts = [ "mattermost.work","slack.hobby" ]
 | 
			
		||||
   channels = [ "testing","testing2","testing3"]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user