summaryrefslogtreecommitdiffstats
path: root/bridge/bridge.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2016-09-18 19:21:15 +0200
committerWim <wim@42.be>2016-09-18 19:21:15 +0200
commit7baf386edea4c71919b257d99ca7f7e07897c412 (patch)
tree5a6f7f0f506c3ab2d15d91548b1d0479bb8fb1ba /bridge/bridge.go
parent6e410b096ef15e976f6a2d28f3412fe9e457f95a (diff)
downloadmatterbridge-msglm-7baf386edea4c71919b257d99ca7f7e07897c412.tar.gz
matterbridge-msglm-7baf386edea4c71919b257d99ca7f7e07897c412.tar.bz2
matterbridge-msglm-7baf386edea4c71919b257d99ca7f7e07897c412.zip
Refactor for more flexibility
* Move from gcfg to toml configuration because gcfg was too restrictive * Implemented gateway which has support multiple in and out bridges. * Allow for bridging the same bridges, which means eg you can now bridge between multiple mattermosts. * Support multiple gateways
Diffstat (limited to 'bridge/bridge.go')
-rw-r--r--bridge/bridge.go147
1 files changed, 18 insertions, 129 deletions
diff --git a/bridge/bridge.go b/bridge/bridge.go
index 45a0160a..719af4fb 100644
--- a/bridge/bridge.go
+++ b/bridge/bridge.go
@@ -1,151 +1,40 @@
package bridge
import (
- //"fmt"
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/gitter"
"github.com/42wim/matterbridge/bridge/irc"
"github.com/42wim/matterbridge/bridge/mattermost"
"github.com/42wim/matterbridge/bridge/slack"
"github.com/42wim/matterbridge/bridge/xmpp"
- log "github.com/Sirupsen/logrus"
"strings"
)
-type Bridge struct {
- *config.Config
- Source string
- Bridges []Bridger
- Channels []map[string]string
- ignoreNicks map[string][]string
-}
-
-type Bridger interface {
+type Bridge 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)
- b := &Bridge{}
- b.Config = cfg
- if cfg.IRC.Enable {
- b.Bridges = append(b.Bridges, birc.New(cfg, c))
- }
- 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))
- }
- if cfg.Gitter.Enable {
- b.Bridges = append(b.Bridges, bgitter.New(cfg, c))
- }
- if cfg.Slack.Enable {
- b.Bridges = append(b.Bridges, bslack.New(cfg, c))
- }
- if len(b.Bridges) < 2 {
- log.Fatalf("only %d sections enabled. Need at least 2 sections enabled (eg [IRC] and [mattermost]", len(b.Bridges))
- }
- for _, br := range b.Bridges {
- br.Connect()
- }
- b.mapChannels()
- b.mapIgnores()
- b.handleReceive(c)
- return nil
-}
-
-func (b *Bridge) handleReceive(c chan config.Message) {
- for {
- select {
- case msg := <-c:
- for _, br := range b.Bridges {
- b.handleMessage(msg, br)
- }
- }
- }
+ FullOrigin() string
+ Origin() string
+ Protocol() string
+ JoinChannel(channel string) error
}
-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
- m["gitter"] = val.Gitter
- m["slack"] = val.Slack
- b.Channels = append(b.Channels, m)
- }
- return nil
-}
-
-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.Xmpp.IgnoreNicks)
- m["gitter"] = strings.Fields(b.Config.Gitter.IgnoreNicks)
- m["slack"] = strings.Fields(b.Config.Slack.IgnoreNicks)
- b.ignoreNicks = m
-}
-
-func (b *Bridge) getDestChannel(msg *config.Message, dest string) string {
- for _, v := range b.Channels {
- if v[msg.Origin] == msg.Channel {
- return v[dest]
- }
- }
- 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())
- log.Debugf("sending %#v from %s to %s", msg, msg.Origin, dest.Name())
- dest.Send(msg)
- }
-}
-
-func (b *Bridge) ignoreMessage(msg *config.Message) bool {
- // should we discard messages ?
- for _, entry := range b.ignoreNicks[msg.Origin] {
- if msg.Username == entry {
- return true
- }
- }
- return false
-}
-
-func setNickFormat(msg *config.Message, format string) {
- if format == "" {
- msg.Username = msg.Origin + "-" + msg.Username + ": "
- 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) {
- switch dest {
+func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) Bridge {
+ accInfo := strings.Split(bridge.Account, ".")
+ protocol := accInfo[0]
+ name := accInfo[1]
+ switch protocol {
+ case "mattermost":
+ return bmattermost.New(cfg.Mattermost[name], name, c)
case "irc":
- setNickFormat(msg, b.Config.IRC.RemoteNickFormat)
+ return birc.New(cfg.IRC[name], name, c)
case "gitter":
- setNickFormat(msg, b.Config.Gitter.RemoteNickFormat)
- case "xmpp":
- setNickFormat(msg, b.Config.Xmpp.RemoteNickFormat)
- case "mattermost":
- setNickFormat(msg, b.Config.Mattermost.RemoteNickFormat)
+ return bgitter.New(cfg.Gitter[name], name, c)
case "slack":
- setNickFormat(msg, b.Config.Slack.RemoteNickFormat)
+ return bslack.New(cfg.Slack[name], name, c)
+ case "xmpp":
+ return bxmpp.New(cfg.Xmpp[name], name, c)
}
+ return nil
}