2017-07-25 20:11:52 +02:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-11-13 23:30:56 +01:00
|
|
|
"time"
|
2018-06-09 12:47:40 +02:00
|
|
|
|
2017-07-25 20:11:52 +02:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-06-09 12:47:40 +02:00
|
|
|
samechannelgateway "github.com/42wim/matterbridge/gateway/samechannel"
|
2017-07-25 20:11:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Router struct {
|
2018-11-13 23:30:56 +01:00
|
|
|
config.Config
|
|
|
|
|
2018-11-11 21:56:12 +01:00
|
|
|
Gateways map[string]*Gateway
|
|
|
|
Message chan config.Message
|
|
|
|
MattermostPlugin chan config.Message
|
2017-07-25 20:11:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:30:56 +01:00
|
|
|
func NewRouter(cfg config.Config) (*Router, error) {
|
|
|
|
r := &Router{
|
|
|
|
Config: cfg,
|
|
|
|
Message: make(chan config.Message),
|
|
|
|
MattermostPlugin: make(chan config.Message),
|
|
|
|
Gateways: make(map[string]*Gateway),
|
|
|
|
}
|
2017-07-25 20:11:52 +02:00
|
|
|
sgw := samechannelgateway.New(cfg)
|
|
|
|
gwconfigs := sgw.GetConfig()
|
|
|
|
|
2018-11-15 20:43:43 +01:00
|
|
|
for _, entry := range append(gwconfigs, cfg.BridgeValues().Gateway...) {
|
2017-07-25 20:11:52 +02:00
|
|
|
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-21 00:20:25 +01:00
|
|
|
flog.Infof("Parsing gateway %s", gw.Name)
|
2017-07-25 20:11:52 +02:00
|
|
|
for _, br := range gw.Bridges {
|
|
|
|
m[br.Account] = br
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, br := range m {
|
2018-02-21 00:20:25 +01:00
|
|
|
flog.Infof("Starting bridge: %s ", br.Account)
|
2017-07-25 20:11:52 +02:00
|
|
|
err := br.Connect()
|
|
|
|
if err != nil {
|
2018-11-25 10:35:35 +01:00
|
|
|
e := fmt.Errorf("Bridge %s failed to start: %v", br.Account, err)
|
|
|
|
if r.disableBridge(br, e) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return e
|
2017-07-25 20:11:52 +02:00
|
|
|
}
|
|
|
|
err = br.JoinChannels()
|
|
|
|
if err != nil {
|
2018-11-25 10:35:35 +01:00
|
|
|
e := fmt.Errorf("Bridge %s failed to join channel: %v", br.Account, err)
|
|
|
|
if r.disableBridge(br, e) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove unused bridges
|
|
|
|
for _, gw := range r.Gateways {
|
|
|
|
for i, br := range gw.Bridges {
|
|
|
|
if br.Bridger == nil {
|
|
|
|
flog.Errorf("removing failed bridge %s", i)
|
|
|
|
delete(gw.Bridges, i)
|
|
|
|
}
|
2017-07-25 20:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
go r.handleReceive()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-25 10:35:35 +01:00
|
|
|
// disableBridge returns true and empties a bridge if we have IgnoreFailureOnStart configured
|
|
|
|
// otherwise returns false
|
|
|
|
func (r *Router) disableBridge(br *bridge.Bridge, err error) bool {
|
|
|
|
if r.BridgeValues().General.IgnoreFailureOnStart {
|
|
|
|
flog.Error(err)
|
|
|
|
// setting this bridge empty
|
|
|
|
*br = bridge.Bridge{}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-25 20:11:52 +02:00
|
|
|
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 {
|
2018-11-08 22:29:34 +01:00
|
|
|
msg := msg // scopelint
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event == config.EventFailure {
|
2017-07-25 20:11:52 +02:00
|
|
|
Loop:
|
|
|
|
for _, gw := range r.Gateways {
|
|
|
|
for _, br := range gw.Bridges {
|
|
|
|
if msg.Account == br.Account {
|
|
|
|
go gw.reconnectBridge(br)
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event == config.EventRejoinChannels {
|
2017-07-25 20:11:52 +02:00
|
|
|
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-28 00:33:17 +02:00
|
|
|
// record all the message ID's of the different bridges
|
|
|
|
var msgIDs []*BrMsgID
|
2017-07-30 16:09:05 +02:00
|
|
|
if !gw.ignoreMessage(&msg) {
|
|
|
|
msg.Timestamp = time.Now()
|
|
|
|
gw.modifyMessage(&msg)
|
2017-11-24 22:36:19 +01:00
|
|
|
gw.handleFiles(&msg)
|
2017-07-30 16:09:05 +02:00
|
|
|
for _, br := range gw.Bridges {
|
2017-08-28 00:33:17 +02:00
|
|
|
msgIDs = append(msgIDs, gw.handleMessage(msg, br)...)
|
|
|
|
}
|
|
|
|
// only add the message ID if it doesn't already exists
|
2018-11-19 21:28:23 +01:00
|
|
|
if _, ok := gw.Messages.Get(msg.Protocol + " " + msg.ID); !ok && msg.ID != "" {
|
|
|
|
gw.Messages.Add(msg.Protocol+" "+msg.ID, msgIDs)
|
2017-07-25 20:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|