mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-22 12:50:27 +00:00
Refactor gateway
This commit is contained in:
parent
a3dd0f1345
commit
14830d9f1c
@ -17,49 +17,60 @@ type Gateway struct {
|
|||||||
ChannelsIn map[string][]string
|
ChannelsIn map[string][]string
|
||||||
ignoreNicks map[string][]string
|
ignoreNicks map[string][]string
|
||||||
Name string
|
Name string
|
||||||
|
Message chan config.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.Config, gateway *config.Gateway) error {
|
func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
|
||||||
c := make(chan config.Message)
|
|
||||||
gw := &Gateway{}
|
gw := &Gateway{}
|
||||||
gw.Name = gateway.Name
|
gw.Name = gateway.Name
|
||||||
gw.Config = cfg
|
gw.Config = cfg
|
||||||
gw.MyConfig = gateway
|
gw.MyConfig = gateway
|
||||||
exists := make(map[string]bool)
|
gw.Message = make(chan config.Message)
|
||||||
for _, br := range append(gateway.In, gateway.Out...) {
|
return gw
|
||||||
if exists[br.Account] {
|
}
|
||||||
continue
|
|
||||||
}
|
func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
|
||||||
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
|
|
||||||
}
|
|
||||||
gw.mapChannels()
|
|
||||||
//TODO fix mapIgnores
|
|
||||||
//gw.mapIgnores()
|
|
||||||
exists = make(map[string]bool)
|
|
||||||
for _, br := range gw.Bridges {
|
for _, br := range gw.Bridges {
|
||||||
|
if br.FullOrigin() == 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)
|
||||||
err := br.Connect()
|
err := br.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Bridge %s failed to start: %v", br.FullOrigin(), err)
|
return fmt.Errorf("Bridge %s failed to start: %v", br.FullOrigin(), err)
|
||||||
}
|
}
|
||||||
|
exists := make(map[string]bool)
|
||||||
for _, channel := range append(gw.ChannelsOut[br.FullOrigin()], gw.ChannelsIn[br.FullOrigin()]...) {
|
for _, channel := range append(gw.ChannelsOut[br.FullOrigin()], gw.ChannelsIn[br.FullOrigin()]...) {
|
||||||
if exists[br.FullOrigin()+channel] {
|
if !exists[br.FullOrigin()+channel] {
|
||||||
continue
|
|
||||||
}
|
|
||||||
log.Infof("%s: joining %s", br.FullOrigin(), channel)
|
log.Infof("%s: joining %s", br.FullOrigin(), channel)
|
||||||
br.JoinChannel(channel)
|
br.JoinChannel(channel)
|
||||||
exists[br.FullOrigin()+channel] = true
|
exists[br.FullOrigin()+channel] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gw.handleReceive(c)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gw *Gateway) handleReceive(c chan config.Message) {
|
func (gw *Gateway) Start() error {
|
||||||
|
gw.mapChannels()
|
||||||
|
for _, br := range append(gw.MyConfig.In, gw.MyConfig.Out...) {
|
||||||
|
err := gw.AddBridge(&br)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//TODO fix mapIgnores
|
||||||
|
//gw.mapIgnores()
|
||||||
|
go gw.handleReceive()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gw *Gateway) handleReceive() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case msg := <-c:
|
case msg := <-gw.Message:
|
||||||
for _, br := range gw.Bridges {
|
for _, br := range gw.Bridges {
|
||||||
gw.handleMessage(msg, br)
|
gw.handleMessage(msg, br)
|
||||||
}
|
}
|
||||||
|
@ -49,12 +49,11 @@ func main() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("starting gateway %#v\n", gw.Name)
|
fmt.Printf("starting gateway %#v\n", gw.Name)
|
||||||
go func(gw config.Gateway) {
|
g := gateway.New(cfg, &gw)
|
||||||
err := gateway.New(cfg, &gw)
|
err := g.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("starting gateway failed %#v", err)
|
log.Debugf("starting gateway failed %#v", err)
|
||||||
}
|
}
|
||||||
}(gw)
|
|
||||||
}
|
}
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user