2016-07-11 19:23:33 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
2016-08-14 19:48:51 +00:00
|
|
|
//"fmt"
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2016-09-04 18:03:07 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/gitter"
|
2016-08-14 19:48:51 +00:00
|
|
|
"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 {
|
2016-08-14 19:48:51 +00:00
|
|
|
*config.Config
|
|
|
|
Source string
|
|
|
|
Bridges []Bridger
|
|
|
|
Channels []map[string]string
|
|
|
|
ignoreNicks map[string][]string
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 19:48:51 +00:00
|
|
|
type Bridger interface {
|
|
|
|
Send(msg config.Message) error
|
|
|
|
Name() string
|
2016-08-15 21:15:22 +00:00
|
|
|
Connect() error
|
2016-08-14 19:48:51 +00:00
|
|
|
//Command(cmd string) string
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:11:50 +00:00
|
|
|
func NewBridge(cfg *config.Config) error {
|
2016-08-14 19:48:51 +00:00
|
|
|
c := make(chan config.Message)
|
2016-07-11 19:23:33 +00:00
|
|
|
b := &Bridge{}
|
2016-08-14 19:48:51 +00:00
|
|
|
b.Config = cfg
|
2016-08-15 21:15:22 +00:00
|
|
|
if cfg.IRC.Enable {
|
2016-08-14 19:48:51 +00:00
|
|
|
b.Bridges = append(b.Bridges, birc.New(cfg, c))
|
2016-07-13 22:23:50 +00:00
|
|
|
}
|
2016-08-15 21:15:22 +00:00
|
|
|
if cfg.Mattermost.Enable {
|
2016-08-14 19:48:51 +00:00
|
|
|
b.Bridges = append(b.Bridges, bmattermost.New(cfg, c))
|
2016-07-11 20:55:58 +00:00
|
|
|
}
|
2016-08-15 21:15:22 +00:00
|
|
|
if cfg.Xmpp.Enable {
|
2016-08-14 19:48:51 +00:00
|
|
|
b.Bridges = append(b.Bridges, bxmpp.New(cfg, c))
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
2016-09-04 18:03:07 +00:00
|
|
|
if cfg.Gitter.Enable {
|
|
|
|
b.Bridges = append(b.Bridges, bgitter.New(cfg, c))
|
|
|
|
}
|
2016-08-15 21:15:22 +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()
|
|
|
|
}
|
2016-08-14 19:48:51 +00:00
|
|
|
b.mapChannels()
|
|
|
|
b.mapIgnores()
|
2016-08-15 19:11:50 +00:00
|
|
|
b.handleReceive(c)
|
|
|
|
return nil
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 19:48:51 +00:00
|
|
|
func (b *Bridge) handleReceive(c chan config.Message) {
|
2016-07-11 19:23:33 +00:00
|
|
|
for {
|
2016-08-14 19:48:51 +00:00
|
|
|
select {
|
|
|
|
case msg := <-c:
|
|
|
|
for _, br := range b.Bridges {
|
2016-08-20 15:21:40 +00:00
|
|
|
b.handleMessage(msg, br)
|
2016-07-13 22:23:50 +00:00
|
|
|
}
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 19:48:51 +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
|
2016-09-04 18:03:07 +00:00
|
|
|
m["gitter"] = val.Gitter
|
2016-08-14 19:48:51 +00:00
|
|
|
b.Channels = append(b.Channels, m)
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
2016-08-14 19:48:51 +00:00
|
|
|
return nil
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 19:48:51 +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)
|
2016-09-04 18:03:07 +00:00
|
|
|
m["xmpp"] = strings.Fields(b.Config.Xmpp.IgnoreNicks)
|
|
|
|
m["gitter"] = strings.Fields(b.Config.Gitter.IgnoreNicks)
|
2016-08-14 19:48:51 +00:00
|
|
|
b.ignoreNicks = m
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 15:21:40 +00:00
|
|
|
func (b *Bridge) getDestChannel(msg *config.Message, dest string) string {
|
2016-08-14 19:48:51 +00:00
|
|
|
for _, v := range b.Channels {
|
2016-08-20 15:21:40 +00:00
|
|
|
if v[msg.Origin] == msg.Channel {
|
|
|
|
return v[dest]
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-20 15:21:40 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) handleMessage(msg config.Message, dest Bridger) {
|
|
|
|
if b.ignoreMessage(&msg) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if dest.Name() != msg.Origin {
|
|
|
|
msg.Channel = b.getDestChannel(&msg, dest.Name())
|
|
|
|
if msg.Channel == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b.modifyMessage(&msg, dest.Name())
|
|
|
|
dest.Send(msg)
|
|
|
|
}
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 15:21:40 +00:00
|
|
|
func (b *Bridge) ignoreMessage(msg *config.Message) bool {
|
2016-07-11 19:23:33 +00:00
|
|
|
// should we discard messages ?
|
2016-08-20 15:21:40 +00:00
|
|
|
for _, entry := range b.ignoreNicks[msg.Origin] {
|
|
|
|
if msg.Username == entry {
|
2016-07-11 19:23:33 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-08-20 15:21:40 +00:00
|
|
|
func (b *Bridge) modifyMessage(msg *config.Message, dest string) {
|
2016-08-15 22:08:38 +00:00
|
|
|
switch dest {
|
|
|
|
case "irc":
|
2016-08-20 15:21:40 +00:00
|
|
|
setNickFormat(msg, b.Config.IRC.RemoteNickFormat)
|
2016-09-04 18:03:07 +00:00
|
|
|
case "gitter":
|
|
|
|
setNickFormat(msg, b.Config.Gitter.RemoteNickFormat)
|
2016-08-15 22:08:38 +00:00
|
|
|
case "xmpp":
|
2016-08-20 15:21:40 +00:00
|
|
|
setNickFormat(msg, b.Config.Xmpp.RemoteNickFormat)
|
2016-08-15 22:08:38 +00:00
|
|
|
case "mattermost":
|
2016-08-20 15:21:40 +00:00
|
|
|
setNickFormat(msg, b.Config.Mattermost.RemoteNickFormat)
|
2016-08-15 22:08:38 +00:00
|
|
|
}
|
|
|
|
}
|