summaryrefslogtreecommitdiffstats
path: root/gateway
diff options
context:
space:
mode:
authorWim <wim@42.be>2016-11-13 23:06:37 +0100
committerWim <wim@42.be>2016-11-13 23:06:37 +0100
commit99d130d1ed4c389a76d4fe7f5ea8fccb78bad444 (patch)
tree8799742a6713d28050f5af14ba8d2b1d62cd766a /gateway
parent14830d9f1c4e9435ee7a15e0839876ad1f4d1a9f (diff)
downloadmatterbridge-msglm-99d130d1ed4c389a76d4fe7f5ea8fccb78bad444.tar.gz
matterbridge-msglm-99d130d1ed4c389a76d4fe7f5ea8fccb78bad444.tar.bz2
matterbridge-msglm-99d130d1ed4c389a76d4fe7f5ea8fccb78bad444.zip
Refactor
Diffstat (limited to 'gateway')
-rw-r--r--gateway/gateway.go50
-rw-r--r--gateway/samechannel/samechannel.go45
2 files changed, 47 insertions, 48 deletions
diff --git a/gateway/gateway.go b/gateway/gateway.go
index 07fcdafc..580e129d 100644
--- a/gateway/gateway.go
+++ b/gateway/gateway.go
@@ -11,8 +11,9 @@ import (
type Gateway struct {
*config.Config
- MyConfig *config.Gateway
- Bridges []bridge.Bridge
+ MyConfig *config.Gateway
+ //Bridges []*bridge.Bridge
+ Bridges map[string]*bridge.Bridge
ChannelsOut map[string][]string
ChannelsIn map[string][]string
ignoreNicks map[string][]string
@@ -26,28 +27,29 @@ func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
gw.Config = cfg
gw.MyConfig = gateway
gw.Message = make(chan config.Message)
+ gw.Bridges = make(map[string]*bridge.Bridge)
return gw
}
func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
for _, br := range gw.Bridges {
- if br.FullOrigin() == cfg.Account {
+ if br.Account == cfg.Account {
return nil
}
}
log.Infof("Starting bridge: %s ", cfg.Account)
br := bridge.New(gw.Config, cfg, gw.Message)
- gw.Bridges = append(gw.Bridges, br)
+ gw.Bridges[cfg.Account] = br
err := br.Connect()
if err != nil {
- return fmt.Errorf("Bridge %s failed to start: %v", br.FullOrigin(), err)
+ return fmt.Errorf("Bridge %s failed to start: %v", br.Account, err)
}
exists := make(map[string]bool)
- for _, channel := range append(gw.ChannelsOut[br.FullOrigin()], gw.ChannelsIn[br.FullOrigin()]...) {
- if !exists[br.FullOrigin()+channel] {
- log.Infof("%s: joining %s", br.FullOrigin(), channel)
+ for _, channel := range append(gw.ChannelsOut[br.Account], gw.ChannelsIn[br.Account]...) {
+ if !exists[br.Account+channel] {
+ log.Infof("%s: joining %s", br.Account, channel)
br.JoinChannel(channel)
- exists[br.FullOrigin()+channel] = true
+ exists[br.Account+channel] = true
}
}
return nil
@@ -103,7 +105,7 @@ func (gw *Gateway) mapIgnores() {
}
func (gw *Gateway) getDestChannel(msg *config.Message, dest string) []string {
- channels := gw.ChannelsIn[msg.FullOrigin]
+ channels := gw.ChannelsIn[msg.Account]
for _, channel := range channels {
if channel == msg.Channel {
return gw.ChannelsOut[dest]
@@ -112,15 +114,15 @@ func (gw *Gateway) getDestChannel(msg *config.Message, dest string) []string {
return []string{}
}
-func (gw *Gateway) handleMessage(msg config.Message, dest bridge.Bridge) {
+func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) {
if gw.ignoreMessage(&msg) {
return
}
originchannel := msg.Channel
- channels := gw.getDestChannel(&msg, dest.FullOrigin())
+ channels := gw.getDestChannel(&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.FullOrigin == dest.FullOrigin() && channel == originchannel {
+ if msg.Account == dest.Account && channel == originchannel {
continue
}
msg.Channel = channel
@@ -128,7 +130,8 @@ func (gw *Gateway) handleMessage(msg config.Message, dest bridge.Bridge) {
log.Debug("empty channel")
return
}
- log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.FullOrigin, originchannel, dest.FullOrigin(), channel)
+ log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel)
+ gw.modifyUsername(&msg, dest)
err := dest.Send(msg)
if err != nil {
fmt.Println(err)
@@ -138,7 +141,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest bridge.Bridge) {
func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
// should we discard messages ?
- for _, entry := range gw.ignoreNicks[msg.FullOrigin] {
+ for _, entry := range gw.ignoreNicks[msg.Account] {
if msg.Username == entry {
return true
}
@@ -146,17 +149,26 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
return false
}
-func (gw *Gateway) modifyMessage(msg *config.Message, dest bridge.Bridge) {
+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() {
+ if strings.ToLower(typeField.Name) == dest.Protocol {
// get the Protocol struct from the map
- protoCfg := val.Field(i).MapIndex(reflect.ValueOf(dest.Origin()))
+ protoCfg := val.Field(i).MapIndex(reflect.ValueOf(dest.Name))
//config.SetNickFormat(msg, protoCfg.Interface().(config.Protocol))
- val.Field(i).SetMapIndex(reflect.ValueOf(dest.Origin()), protoCfg)
+ 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]
+ nick := dest.Config.RemoteNickFormat
+ nick = strings.Replace(nick, "{NICK}", msg.Username, -1)
+ nick = strings.Replace(nick, "{BRIDGE}", br.Name, -1)
+ nick = strings.Replace(nick, "{PROTOCOL}", br.Protocol, -1)
+ msg.Username = nick
+}
diff --git a/gateway/samechannel/samechannel.go b/gateway/samechannel/samechannel.go
index 5cdd59ae..23c4152b 100644
--- a/gateway/samechannel/samechannel.go
+++ b/gateway/samechannel/samechannel.go
@@ -10,7 +10,7 @@ import (
type SameChannelGateway struct {
*config.Config
MyConfig *config.SameChannelGateway
- Bridges []bridge.Bridge
+ Bridges map[string]*bridge.Bridge
Channels []string
ignoreNicks map[string][]string
Name string
@@ -19,6 +19,7 @@ type SameChannelGateway struct {
func New(cfg *config.Config, gateway *config.SameChannelGateway) error {
c := make(chan config.Message)
gw := &SameChannelGateway{}
+ gw.Bridges = make(map[string]*bridge.Bridge)
gw.Name = gateway.Name
gw.Config = cfg
gw.MyConfig = gateway
@@ -26,15 +27,15 @@ func New(cfg *config.Config, gateway *config.SameChannelGateway) error {
for _, account := range gateway.Accounts {
br := config.Bridge{Account: account}
log.Infof("Starting bridge: %s", account)
- gw.Bridges = append(gw.Bridges, bridge.New(cfg, &br, c))
+ gw.Bridges[account] = bridge.New(cfg, &br, c)
}
for _, br := range gw.Bridges {
err := br.Connect()
if err != nil {
- log.Fatalf("Bridge %s failed to start: %v", br.FullOrigin(), err)
+ log.Fatalf("Bridge %s failed to start: %v", br.Account, err)
}
for _, channel := range gw.Channels {
- log.Infof("%s: joining %s", br.FullOrigin(), channel)
+ log.Infof("%s: joining %s", br.Account, channel)
br.JoinChannel(channel)
}
}
@@ -53,38 +54,24 @@ func (gw *SameChannelGateway) handleReceive(c chan config.Message) {
}
}
-func (gw *SameChannelGateway) handleMessage(msg config.Message, dest bridge.Bridge) {
+func (gw *SameChannelGateway) handleMessage(msg config.Message, dest *bridge.Bridge) {
// do not send the message to the bridge we come from if also the channel is the same
- if msg.FullOrigin == dest.FullOrigin() {
+ if msg.Account == dest.Account {
return
}
- gw.modifyMessage(&msg, dest)
- log.Debugf("Sending %#v from %s to %s", msg, msg.FullOrigin, dest.FullOrigin())
+ gw.modifyUsername(&msg, dest)
+ log.Debugf("Sending %#v from %s to %s", msg, msg.Account, dest.Account)
err := dest.Send(msg)
if err != nil {
log.Error(err)
}
}
-func setNickFormat(msg *config.Message, format string) {
- if format == "" {
- msg.Username = msg.Protocol + "." + msg.Origin + "-" + msg.Username + ": "
- return
- }
- msg.Username = strings.Replace(format, "{NICK}", msg.Username, -1)
- msg.Username = strings.Replace(msg.Username, "{BRIDGE}", msg.Origin, -1)
- msg.Username = strings.Replace(msg.Username, "{PROTOCOL}", msg.Protocol, -1)
-}
-
-func (gw *SameChannelGateway) modifyMessage(msg *config.Message, dest bridge.Bridge) {
- switch dest.Protocol() {
- case "irc":
- setNickFormat(msg, gw.Config.IRC[dest.Origin()].RemoteNickFormat)
- case "mattermost":
- setNickFormat(msg, gw.Config.Mattermost[dest.Origin()].RemoteNickFormat)
- case "slack":
- setNickFormat(msg, gw.Config.Slack[dest.Origin()].RemoteNickFormat)
- case "discord":
- setNickFormat(msg, gw.Config.Discord[dest.Origin()].RemoteNickFormat)
- }
+func (gw *SameChannelGateway) modifyUsername(msg *config.Message, dest *bridge.Bridge) {
+ br := gw.Bridges[msg.Account]
+ nick := dest.Config.RemoteNickFormat
+ nick = strings.Replace(nick, "{NICK}", msg.Username, -1)
+ nick = strings.Replace(nick, "{BRIDGE}", br.Name, -1)
+ nick = strings.Replace(nick, "{PROTOCOL}", br.Protocol, -1)
+ msg.Username = nick
}