summaryrefslogtreecommitdiffstats
path: root/gateway/router.go
blob: eb8251edf27b60fd0ea590a4fdf0b9d0dc38381d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package gateway

import (
	"fmt"
	"github.com/42wim/matterbridge/bridge"
	"github.com/42wim/matterbridge/bridge/config"
	"github.com/42wim/matterbridge/gateway/samechannel"
	log "github.com/Sirupsen/logrus"
	//	"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) {
	r := &Router{}
	r.Config = cfg
	r.Message = make(chan config.Message)
	r.Gateways = make(map[string]*Gateway)
	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 {
		for _, br := range gw.Bridges {
			m[br.Account] = br
		}
	}
	for _, br := range m {
		log.Infof("Starting bridge: %s ", br.Account)
		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) getGatewayName(channelID string) []string {
	var names []string
	for _, gw := range r.Gateways {
		if _, ok := gw.Channels[channelID]; ok {
			names = append(names, gw.Name)
		}
	}
	return names
}

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 {
			if !gw.ignoreMessage(&msg) {
				msg.Timestamp = time.Now()
				gw.modifyMessage(&msg)
				for _, br := range gw.Bridges {
					gw.handleMessage(msg, br)
				}
			}
		}
	}
}