summaryrefslogtreecommitdiffstats
path: root/gateway/gateway.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2016-11-13 23:09:06 +0100
committerWim <wim@42.be>2016-11-13 23:09:06 +0100
commit497633867759601d658233257447b5b86aa1b01e (patch)
treebc732d5333ddcd0343e1c2d7fd0a025c23822196 /gateway/gateway.go
parent4fb0544b0e25bf6409811fc3e574bf45efea369d (diff)
parent99d130d1ed4c389a76d4fe7f5ea8fccb78bad444 (diff)
downloadmatterbridge-msglm-497633867759601d658233257447b5b86aa1b01e.tar.gz
matterbridge-msglm-497633867759601d658233257447b5b86aa1b01e.tar.bz2
matterbridge-msglm-497633867759601d658233257447b5b86aa1b01e.zip
Merge branch 'refactor'
Diffstat (limited to 'gateway/gateway.go')
-rw-r--r--gateway/gateway.go97
1 files changed, 60 insertions, 37 deletions
diff --git a/gateway/gateway.go b/gateway/gateway.go
index 13b6be1b..580e129d 100644
--- a/gateway/gateway.go
+++ b/gateway/gateway.go
@@ -11,55 +11,68 @@ 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
Name string
+ Message chan config.Message
}
-func New(cfg *config.Config, gateway *config.Gateway) error {
- c := make(chan config.Message)
+func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
gw := &Gateway{}
gw.Name = gateway.Name
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.Account == cfg.Account {
+ return nil
+ }
+ }
+ log.Infof("Starting bridge: %s ", cfg.Account)
+ br := bridge.New(gw.Config, cfg, gw.Message)
+ gw.Bridges[cfg.Account] = br
+ err := br.Connect()
+ if err != nil {
+ return fmt.Errorf("Bridge %s failed to start: %v", br.Account, err)
+ }
exists := make(map[string]bool)
- for _, br := range append(gateway.In, gateway.Out...) {
- if exists[br.Account] {
- continue
+ 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.Account+channel] = true
}
- log.Infof("Starting bridge: %s channel: %s", br.Account, br.Channel)
- gw.Bridges = append(gw.Bridges, bridge.New(cfg, &br, c))
- exists[br.Account] = true
}
+ return nil
+}
+
+func (gw *Gateway) Start() error {
gw.mapChannels()
- //TODO fix mapIgnores
- //gw.mapIgnores()
- exists = make(map[string]bool)
- for _, br := range gw.Bridges {
- err := br.Connect()
+ for _, br := range append(gw.MyConfig.In, gw.MyConfig.Out...) {
+ err := gw.AddBridge(&br)
if err != nil {
- log.Fatalf("Bridge %s failed to start: %v", br.FullOrigin(), err)
- }
- for _, channel := range append(gw.ChannelsOut[br.FullOrigin()], gw.ChannelsIn[br.FullOrigin()]...) {
- if exists[br.FullOrigin()+channel] {
- continue
- }
- log.Infof("%s: joining %s", br.FullOrigin(), channel)
- br.JoinChannel(channel)
- exists[br.FullOrigin()+channel] = true
+ return err
}
}
- gw.handleReceive(c)
+ //TODO fix mapIgnores
+ //gw.mapIgnores()
+ go gw.handleReceive()
return nil
}
-func (gw *Gateway) handleReceive(c chan config.Message) {
+func (gw *Gateway) handleReceive() {
for {
select {
- case msg := <-c:
+ case msg := <-gw.Message:
for _, br := range gw.Bridges {
gw.handleMessage(msg, br)
}
@@ -92,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]
@@ -101,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
@@ -117,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)
@@ -127,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
}
@@ -135,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
+}