From 163f55f9c27e9e8a75774424d22598799e8306c6 Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 14 Feb 2017 21:12:02 +0100 Subject: Refactor to handle disconnects/reconnects better. Now try to reconnect every 60 seconds until forever. --- bridge/bridge.go | 24 +++++++++++++++++++++--- bridge/config/config.go | 1 + bridge/discord/discord.go | 4 ++++ bridge/gitter/gitter.go | 5 +++++ bridge/irc/irc.go | 14 ++++++++++++-- bridge/mattermost/mattermost.go | 4 ++++ bridge/rocketchat/rocketchat.go | 5 +++++ bridge/slack/slack.go | 5 +++++ bridge/telegram/telegram.go | 5 +++++ bridge/xmpp/xmpp.go | 16 ++++++++++------ 10 files changed, 72 insertions(+), 11 deletions(-) (limited to 'bridge') diff --git a/bridge/bridge.go b/bridge/bridge.go index b387812a..c8c6ac4e 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -10,6 +10,8 @@ import ( "github.com/42wim/matterbridge/bridge/slack" "github.com/42wim/matterbridge/bridge/telegram" "github.com/42wim/matterbridge/bridge/xmpp" + log "github.com/Sirupsen/logrus" + "strings" ) @@ -17,14 +19,18 @@ type Bridger interface { Send(msg config.Message) error Connect() error JoinChannel(channel string) error + Disconnect() error } type Bridge struct { Config config.Protocol Bridger - Name string - Account string - Protocol string + Name string + Account string + Protocol string + ChannelsOut []string + ChannelsIn []string + ChannelOptions config.ChannelOptions } func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Bridge { @@ -66,3 +72,15 @@ func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Brid } return b } + +func (b *Bridge) JoinChannels() error { + exists := make(map[string]bool) + for _, channel := range append(b.ChannelsIn, b.ChannelsOut...) { + if !exists[channel] { + log.Infof("%s: joining %s", b.Account, channel) + b.JoinChannel(channel) + exists[channel] = true + } + } + return nil +} diff --git a/bridge/config/config.go b/bridge/config/config.go index ac3e939b..811c97ae 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -10,6 +10,7 @@ import ( const ( EVENT_JOIN_LEAVE = "join_leave" + EVENT_FAILURE = "failure" ) type Message struct { diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 620493b2..06508b84 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -80,6 +80,10 @@ func (b *bdiscord) Connect() error { return nil } +func (b *bdiscord) Disconnect() error { + return nil +} + func (b *bdiscord) JoinChannel(channel string) error { idcheck := strings.Split(channel, "ID:") if len(idcheck) > 1 { diff --git a/bridge/gitter/gitter.go b/bridge/gitter/gitter.go index 0400b4ba..d1f4b401 100644 --- a/bridge/gitter/gitter.go +++ b/bridge/gitter/gitter.go @@ -45,6 +45,11 @@ func (b *Bgitter) Connect() error { return nil } +func (b *Bgitter) Disconnect() error { + return nil + +} + func (b *Bgitter) JoinChannel(channel string) error { room := channel roomID := b.getRoomID(room) diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go index fe8dc741..db430800 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -46,7 +46,6 @@ func New(cfg config.Protocol, account string, c chan config.Message) *Birc { if b.Config.MessageQueue == 0 { b.Config.MessageQueue = 30 } - b.Local = make(chan config.Message, b.Config.MessageQueue+10) return b } @@ -61,6 +60,7 @@ func (b *Birc) Command(msg *config.Message) string { } func (b *Birc) Connect() error { + b.Local = make(chan config.Message, b.Config.MessageQueue+10) flog.Infof("Connecting %s", b.Config.Server) i := irc.IRC(b.Config.Nick, b.Config.Nick) if log.GetLevel() == log.DebugLevel { @@ -91,6 +91,12 @@ func (b *Birc) Connect() error { return nil } +func (b *Birc) Disconnect() error { + b.i.Disconnect() + close(b.Local) + return nil +} + func (b *Birc) JoinChannel(channel string) error { b.i.Join(channel) return nil @@ -170,7 +176,11 @@ func (b *Birc) handleJoinPart(event *irc.Event) { flog.Debugf("Sending JOIN_LEAVE event from %s to gateway", b.Account) channel := event.Arguments[0] if event.Code == "QUIT" { - channel = "" + if event.Nick == b.Nick && strings.Contains(event.Raw, "Ping timeout") { + flog.Infof("%s reconnecting ..", b.Account) + b.Remote <- config.Message{Username: "system", Text: "reconnect", Channel: channel, Account: b.Account, Event: config.EVENT_FAILURE} + return + } } b.Remote <- config.Message{Username: "system", Text: event.Nick + " " + strings.ToLower(event.Code) + "s", Channel: channel, Account: b.Account, Event: config.EVENT_JOIN_LEAVE} flog.Debugf("handle %#v", event) diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index e2bf228d..126bab43 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -77,6 +77,10 @@ func (b *Bmattermost) Connect() error { return nil } +func (b *Bmattermost) Disconnect() error { + return nil +} + func (b *Bmattermost) JoinChannel(channel string) error { // we can only join channels using the API if b.Config.UseAPI { diff --git a/bridge/rocketchat/rocketchat.go b/bridge/rocketchat/rocketchat.go index d87450ec..4590a895 100644 --- a/bridge/rocketchat/rocketchat.go +++ b/bridge/rocketchat/rocketchat.go @@ -49,6 +49,11 @@ func (b *Brocketchat) Connect() error { return nil } +func (b *Brocketchat) Disconnect() error { + return nil + +} + func (b *Brocketchat) JoinChannel(channel string) error { return nil } diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 763231d8..0f8806a2 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -65,6 +65,11 @@ func (b *Bslack) Connect() error { return nil } +func (b *Bslack) Disconnect() error { + return nil + +} + func (b *Bslack) JoinChannel(channel string) error { // we can only join channels using the API if b.Config.UseAPI { diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 38d7fd12..aa637457 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -51,6 +51,11 @@ func (b *Btelegram) Connect() error { return nil } +func (b *Btelegram) Disconnect() error { + return nil + +} + func (b *Btelegram) JoinChannel(channel string) error { return nil } diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index 8899e718..4dcb8ef7 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -1,10 +1,10 @@ package bxmpp import ( + "crypto/tls" "github.com/42wim/matterbridge/bridge/config" log "github.com/Sirupsen/logrus" "github.com/mattn/go-xmpp" - "crypto/tls" "strings" "time" @@ -47,6 +47,10 @@ func (b *Bxmpp) Connect() error { return nil } +func (b *Bxmpp) Disconnect() error { + return nil +} + func (b *Bxmpp) JoinChannel(channel string) error { b.xc.JoinMUCNoHistory(channel+"@"+b.Config.Muc, b.Config.Nick) return nil @@ -63,11 +67,11 @@ func (b *Bxmpp) createXMPP() (*xmpp.Client, error) { tc.InsecureSkipVerify = b.Config.SkipTLSVerify tc.ServerName = strings.Split(b.Config.Server, ":")[0] options := xmpp.Options{ - Host: b.Config.Server, - User: b.Config.Jid, - Password: b.Config.Password, - NoTLS: true, - StartTLS: true, + Host: b.Config.Server, + User: b.Config.Jid, + Password: b.Config.Password, + NoTLS: true, + StartTLS: true, TLSConfig: tc, //StartTLS: false, -- cgit v1.2.3 From dc3723210076d7f7fecdfbf98b5de2f4540900ea Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 14 Feb 2017 23:52:45 +0100 Subject: Refactor. Make extra options easier for other protocols --- bridge/bridge.go | 29 +++++++++++++++++++++-------- gateway/gateway.go | 16 ++++++++++++---- 2 files changed, 33 insertions(+), 12 deletions(-) (limited to 'bridge') diff --git a/bridge/bridge.go b/bridge/bridge.go index c8c6ac4e..db26c422 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -25,16 +25,17 @@ type Bridger interface { type Bridge struct { Config config.Protocol Bridger - Name string - Account string - Protocol string - ChannelsOut []string - ChannelsIn []string - ChannelOptions config.ChannelOptions + Name string + Account string + Protocol string + ChannelsIn map[string]config.ChannelOptions + ChannelsOut map[string]config.ChannelOptions } func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Bridge { b := new(Bridge) + b.ChannelsIn = make(map[string]config.ChannelOptions) + b.ChannelsOut = make(map[string]config.ChannelOptions) accInfo := strings.Split(bridge.Account, ".") protocol := accInfo[0] name := accInfo[1] @@ -75,10 +76,22 @@ func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Brid func (b *Bridge) JoinChannels() error { exists := make(map[string]bool) - for _, channel := range append(b.ChannelsIn, b.ChannelsOut...) { + b.joinChannels(b.ChannelsIn, exists) + b.joinChannels(b.ChannelsOut, exists) + return nil +} + +func (b *Bridge) joinChannels(cMap map[string]config.ChannelOptions, exists map[string]bool) error { + mychannel := "" + for channel, info := range cMap { if !exists[channel] { + mychannel = channel log.Infof("%s: joining %s", b.Account, channel) - b.JoinChannel(channel) + if b.Protocol == "irc" && info.Key != "" { + log.Debugf("using key %s for channel %s", info.Key, channel) + mychannel = mychannel + " " + info.Key + } + b.JoinChannel(mychannel) exists[channel] = true } } diff --git a/gateway/gateway.go b/gateway/gateway.go index 19571776..95bf902a 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -40,10 +40,8 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error { } log.Infof("Starting bridge: %s ", cfg.Account) br := bridge.New(gw.Config, cfg, gw.Message) - br.ChannelsOut = gw.ChannelsOut[br.Account] - br.ChannelsIn = gw.ChannelsIn[br.Account] - br.ChannelOptions = gw.ChannelOptions[br.Account] - + gw.mapChannelsToBridge(br, gw.ChannelsOut) + gw.mapChannelsToBridge(br, gw.ChannelsIn) gw.Bridges[cfg.Account] = br err := br.Connect() if err != nil { @@ -53,6 +51,16 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error { return nil } +func (gw *Gateway) mapChannelsToBridge(br *bridge.Bridge, cMap map[string][]string) { + for _, channel := range cMap[br.Account] { + if _, ok := gw.ChannelOptions[br.Account+channel]; ok { + br.ChannelsOut[channel] = gw.ChannelOptions[br.Account+channel] + } else { + br.ChannelsOut[channel] = config.ChannelOptions{} + } + } +} + func (gw *Gateway) Start() error { gw.mapChannels() for _, br := range append(gw.MyConfig.In, append(gw.MyConfig.InOut, gw.MyConfig.Out...)...) { -- cgit v1.2.3