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"
|
|
|
|
"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
|
|
|
|
kind string
|
|
|
|
Channels []map[string]string
|
|
|
|
ignoreNicks map[string][]string
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FancyLog struct {
|
2016-07-13 22:23:50 +00:00
|
|
|
irc *log.Entry
|
|
|
|
mm *log.Entry
|
|
|
|
xmpp *log.Entry
|
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
|
|
|
|
//Command(cmd string) string
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:23:33 +00:00
|
|
|
var flog FancyLog
|
|
|
|
|
|
|
|
const Legacy = "legacy"
|
|
|
|
|
|
|
|
func initFLog() {
|
|
|
|
flog.irc = log.WithFields(log.Fields{"module": "irc"})
|
|
|
|
flog.mm = log.WithFields(log.Fields{"module": "mattermost"})
|
2016-07-13 22:23:50 +00:00
|
|
|
flog.xmpp = log.WithFields(log.Fields{"module": "xmpp"})
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
initFLog()
|
|
|
|
b := &Bridge{}
|
2016-08-14 19:48:51 +00:00
|
|
|
b.Config = cfg
|
|
|
|
if cfg.General.Irc {
|
|
|
|
b.Bridges = append(b.Bridges, birc.New(cfg, c))
|
2016-07-13 22:23:50 +00:00
|
|
|
}
|
2016-08-14 19:48:51 +00:00
|
|
|
if cfg.General.Mattermost {
|
|
|
|
b.Bridges = append(b.Bridges, bmattermost.New(cfg, c))
|
2016-07-11 20:55:58 +00:00
|
|
|
}
|
2016-08-14 19:48:51 +00:00
|
|
|
if cfg.General.Xmpp {
|
|
|
|
b.Bridges = append(b.Bridges, bxmpp.New(cfg, c))
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
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:
|
|
|
|
m := b.getChannel(msg.Origin, msg.Channel)
|
|
|
|
if m == nil {
|
|
|
|
continue
|
2016-07-13 22:23:50 +00:00
|
|
|
}
|
2016-08-14 19:48:51 +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()]
|
|
|
|
br.Send(msg)
|
|
|
|
}
|
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
|
|
|
|
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)
|
|
|
|
m["xmpp"] = strings.Fields(b.Config.Mattermost.IgnoreNicks)
|
|
|
|
b.ignoreNicks = m
|
2016-07-11 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 19:48:51 +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 ?
|
2016-08-14 19:48:51 +00:00
|
|
|
for _, entry := range b.ignoreNicks[protocol] {
|
2016-07-11 19:23:33 +00:00
|
|
|
if nick == entry {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|