2017-07-25 18:11:52 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-06-09 10:47:40 +00:00
|
|
|
|
2017-07-25 18:11:52 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-06-09 10:47:40 +00:00
|
|
|
samechannelgateway "github.com/42wim/matterbridge/gateway/samechannel"
|
2017-07-25 18:11:52 +00:00
|
|
|
// "github.com/davecgh/go-spew/spew"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Router struct {
|
|
|
|
Gateways map[string]*Gateway
|
|
|
|
Message chan config.Message
|
|
|
|
*config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRouter(cfg *config.Config) (*Router, error) {
|
2018-02-27 21:16:44 +00:00
|
|
|
r := &Router{Message: make(chan config.Message), Gateways: make(map[string]*Gateway), Config: cfg}
|
2017-07-25 18:11:52 +00:00
|
|
|
sgw := samechannelgateway.New(cfg)
|
|
|
|
gwconfigs := sgw.GetConfig()
|
|
|
|
|
|
|
|
for _, entry := range append(gwconfigs, cfg.Gateway...) {
|
|
|
|
if !entry.Enable {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if entry.Name == "" {
|
|
|
|
return nil, fmt.Errorf("%s", "Gateway without name found")
|
|
|
|
}
|
|
|
|
if _, ok := r.Gateways[entry.Name]; ok {
|
|
|
|
return nil, fmt.Errorf("Gateway with name %s already exists", entry.Name)
|
|
|
|
}
|
|
|
|
r.Gateways[entry.Name] = New(entry, r)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) Start() error {
|
|
|
|
m := make(map[string]*bridge.Bridge)
|
|
|
|
for _, gw := range r.Gateways {
|
2018-02-20 23:20:25 +00:00
|
|
|
flog.Infof("Parsing gateway %s", gw.Name)
|
2017-07-25 18:11:52 +00:00
|
|
|
for _, br := range gw.Bridges {
|
|
|
|
m[br.Account] = br
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, br := range m {
|
2018-02-20 23:20:25 +00:00
|
|
|
flog.Infof("Starting bridge: %s ", br.Account)
|
2017-07-25 18:11:52 +00:00
|
|
|
err := br.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bridge %s failed to start: %v", br.Account, err)
|
|
|
|
}
|
|
|
|
err = br.JoinChannels()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bridge %s failed to join channel: %v", br.Account, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go r.handleReceive()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) getBridge(account string) *bridge.Bridge {
|
|
|
|
for _, gw := range r.Gateways {
|
|
|
|
if br, ok := gw.Bridges[account]; ok {
|
|
|
|
return br
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) handleReceive() {
|
|
|
|
for msg := range r.Message {
|
|
|
|
if msg.Event == config.EVENT_FAILURE {
|
|
|
|
Loop:
|
|
|
|
for _, gw := range r.Gateways {
|
|
|
|
for _, br := range gw.Bridges {
|
|
|
|
if msg.Account == br.Account {
|
|
|
|
go gw.reconnectBridge(br)
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if msg.Event == config.EVENT_REJOIN_CHANNELS {
|
|
|
|
for _, gw := range r.Gateways {
|
|
|
|
for _, br := range gw.Bridges {
|
|
|
|
if msg.Account == br.Account {
|
|
|
|
br.Joined = make(map[string]bool)
|
|
|
|
br.JoinChannels()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, gw := range r.Gateways {
|
2017-08-27 22:33:17 +00:00
|
|
|
// record all the message ID's of the different bridges
|
|
|
|
var msgIDs []*BrMsgID
|
2017-07-30 14:09:05 +00:00
|
|
|
if !gw.ignoreMessage(&msg) {
|
|
|
|
msg.Timestamp = time.Now()
|
|
|
|
gw.modifyMessage(&msg)
|
2017-11-24 21:36:19 +00:00
|
|
|
gw.handleFiles(&msg)
|
2017-07-30 14:09:05 +00:00
|
|
|
for _, br := range gw.Bridges {
|
2017-08-27 22:33:17 +00:00
|
|
|
msgIDs = append(msgIDs, gw.handleMessage(msg, br)...)
|
|
|
|
}
|
|
|
|
// only add the message ID if it doesn't already exists
|
2017-08-29 18:28:44 +00:00
|
|
|
if _, ok := gw.Messages.Get(msg.ID); !ok && msg.ID != "" {
|
2017-08-27 22:33:17 +00:00
|
|
|
gw.Messages.Add(msg.ID, msgIDs)
|
2017-07-25 18:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|