matterbridge/bridge/bridge.go

134 lines
3.1 KiB
Go
Raw Normal View History

2016-07-11 19:23:33 +00:00
package bridge
import (
//"fmt"
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/irc"
"github.com/42wim/matterbridge/bridge/mattermost"
"github.com/42wim/matterbridge/bridge/xmpp"
2016-07-11 19:23:33 +00:00
log "github.com/Sirupsen/logrus"
"strings"
)
type Bridge struct {
*config.Config
Source string
Bridges []Bridger
Channels []map[string]string
ignoreNicks map[string][]string
2016-07-11 19:23:33 +00:00
}
type Bridger interface {
Send(msg config.Message) error
Name() string
Connect() error
//Command(cmd string) string
}
func NewBridge(cfg *config.Config) error {
c := make(chan config.Message)
2016-07-11 19:23:33 +00:00
b := &Bridge{}
b.Config = cfg
if cfg.IRC.Enable {
b.Bridges = append(b.Bridges, birc.New(cfg, c))
2016-07-13 22:23:50 +00:00
}
if cfg.Mattermost.Enable {
b.Bridges = append(b.Bridges, bmattermost.New(cfg, c))
}
if cfg.Xmpp.Enable {
b.Bridges = append(b.Bridges, bxmpp.New(cfg, c))
2016-07-11 19:23:33 +00:00
}
if len(b.Bridges) < 2 {
log.Fatalf("only %d sections enabled. Need at least 2 sections enabled (eg [IRC] and [mattermost]", len(b.Bridges))
}
2016-08-15 21:16:07 +00:00
for _, br := range b.Bridges {
br.Connect()
}
b.mapChannels()
b.mapIgnores()
b.handleReceive(c)
return nil
2016-07-11 19:23:33 +00:00
}
func (b *Bridge) handleReceive(c chan config.Message) {
2016-07-11 19:23:33 +00:00
for {
select {
case msg := <-c:
m := b.getChannel(msg.Origin, msg.Channel)
if m == nil {
continue
2016-07-13 22:23:50 +00:00
}
for _, br := range b.Bridges {
if b.ignoreMessage(msg.Username, msg.Text, msg.Origin) {
continue
}
// do not send to originated bridge
if br.Name() != msg.Origin {
msg.Channel = m[br.Name()]
2016-08-15 22:08:38 +00:00
msgmod := b.modifyMessage(msg, br.Name())
br.Send(msgmod)
}
2016-07-13 22:23:50 +00:00
}
2016-07-11 19:23:33 +00:00
}
}
}
func (b *Bridge) mapChannels() error {
for _, val := range b.Config.Channel {
m := make(map[string]string)
m["irc"] = val.IRC
m["mattermost"] = val.Mattermost
m["xmpp"] = val.Xmpp
b.Channels = append(b.Channels, m)
2016-07-11 19:23:33 +00:00
}
return nil
2016-07-11 19:23:33 +00:00
}
func (b *Bridge) mapIgnores() {
m := make(map[string][]string)
m["irc"] = strings.Fields(b.Config.IRC.IgnoreNicks)
m["mattermost"] = strings.Fields(b.Config.Mattermost.IgnoreNicks)
m["xmpp"] = strings.Fields(b.Config.Mattermost.IgnoreNicks)
b.ignoreNicks = m
2016-07-11 19:23:33 +00:00
}
func (b *Bridge) getChannel(src, name string) map[string]string {
for _, v := range b.Channels {
if v[src] == name {
return v
}
}
return nil
2016-07-11 19:23:33 +00:00
}
func (b *Bridge) ignoreMessage(nick string, message string, protocol string) bool {
// should we discard messages ?
for _, entry := range b.ignoreNicks[protocol] {
2016-07-11 19:23:33 +00:00
if nick == entry {
return true
}
}
return false
}
2016-08-15 22:08:38 +00:00
func setNickFormat(msg *config.Message, format string) {
if format == "" {
2016-08-20 14:29:43 +00:00
msg.Username = msg.Origin + "-" + msg.Username + ": "
2016-08-15 22:08:38 +00:00
return
}
msg.Username = strings.Replace(format, "{NICK}", msg.Username, -1)
msg.Username = strings.Replace(msg.Username, "{BRIDGE}", msg.Origin, -1)
}
func (b *Bridge) modifyMessage(msg config.Message, dest string) config.Message {
switch dest {
case "irc":
setNickFormat(&msg, b.Config.IRC.RemoteNickFormat)
case "xmpp":
setNickFormat(&msg, b.Config.Xmpp.RemoteNickFormat)
case "mattermost":
setNickFormat(&msg, b.Config.Mattermost.RemoteNickFormat)
}
return msg
}