From 889b6debc4f339975076668b6ab7d85f7dd08922 Mon Sep 17 00:00:00 2001 From: Wim Date: Mon, 15 Aug 2016 23:16:07 +0200 Subject: [PATCH] Add Connect() to Bridger interface --- bridge/bridge.go | 3 ++ bridge/irc/irc.go | 52 ++++++++++++++------------------- bridge/mattermost/mattermost.go | 16 ++++++---- bridge/xmpp/xmpp.go | 14 ++++----- 4 files changed, 42 insertions(+), 43 deletions(-) diff --git a/bridge/bridge.go b/bridge/bridge.go index a849ba67..2a9ea7c7 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -41,6 +41,9 @@ func NewBridge(cfg *config.Config) error { if len(b.Bridges) < 2 { log.Fatalf("only %d sections enabled. Need at least 2 sections enabled (eg [IRC] and [mattermost]", len(b.Bridges)) } + for _, br := range b.Bridges { + br.Connect() + } b.mapChannels() b.mapIgnores() b.handleReceive(c) diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go index d37149a0..9ed5202b 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -20,38 +20,27 @@ type Birc struct { names map[string][]string ircIgnoreNicks []string *config.Config - kind string Remote chan config.Message } type FancyLog struct { - irc *log.Entry - mm *log.Entry - xmpp *log.Entry + irc *log.Entry } var flog FancyLog -const Legacy = "legacy" - func init() { flog.irc = log.WithFields(log.Fields{"module": "irc"}) - flog.mm = log.WithFields(log.Fields{"module": "mattermost"}) - flog.xmpp = log.WithFields(log.Fields{"module": "xmpp"}) } func New(config *config.Config, c chan config.Message) *Birc { b := &Birc{} b.Config = config - b.kind = "legacy" b.Remote = c b.ircNick = b.Config.IRC.Nick b.ircMap = make(map[string]string) b.names = make(map[string][]string) b.ircIgnoreNicks = strings.Fields(b.Config.IRC.IgnoreNicks) - flog.irc.Info("Trying IRC connection") - b.i = b.connect() - flog.irc.Info("Connection succeeded") return b } @@ -63,6 +52,27 @@ func (b *Birc) Command(msg *config.Message) string { return "" } +func (b *Birc) Connect() error { + flog.irc.Info("Trying IRC connection") + i := irc.IRC(b.Config.IRC.Nick, b.Config.IRC.Nick) + i.UseTLS = b.Config.IRC.UseTLS + i.UseSASL = b.Config.IRC.UseSASL + i.SASLLogin = b.Config.IRC.NickServNick + i.SASLPassword = b.Config.IRC.NickServPassword + i.TLSConfig = &tls.Config{InsecureSkipVerify: b.Config.IRC.SkipTLSVerify} + if b.Config.IRC.Password != "" { + i.Password = b.Config.IRC.Password + } + i.AddCallback(ircm.RPL_WELCOME, b.handleNewConnection) + err := i.Connect(b.Config.IRC.Server) + if err != nil { + return err + } + flog.irc.Info("Connection succeeded") + b.i = i + return nil +} + func (b *Birc) Name() string { return "irc" } @@ -80,24 +90,6 @@ func (b *Birc) Send(msg config.Message) error { return nil } -func (b *Birc) connect() *irc.Connection { - i := irc.IRC(b.Config.IRC.Nick, b.Config.IRC.Nick) - i.UseTLS = b.Config.IRC.UseTLS - i.UseSASL = b.Config.IRC.UseSASL - i.SASLLogin = b.Config.IRC.NickServNick - i.SASLPassword = b.Config.IRC.NickServPassword - i.TLSConfig = &tls.Config{InsecureSkipVerify: b.Config.IRC.SkipTLSVerify} - if b.Config.IRC.Password != "" { - i.Password = b.Config.IRC.Password - } - i.AddCallback(ircm.RPL_WELCOME, b.handleNewConnection) - err := i.Connect(b.Config.IRC.Server) - if err != nil { - flog.irc.Fatal(err) - } - return i -} - func (b *Birc) endNames(event *irc.Event) { channel := event.Arguments[1] sort.Strings(b.names[channel]) diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index 63f38dc5..9dc0cebb 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -55,6 +55,14 @@ func New(cfg *config.Config, c chan config.Message) *Bmattermost { b.Remote = c b.Plus = cfg.General.Plus b.mmMap = make(map[string]string) + return b +} + +func (b *Bmattermost) Command(cmd string) string { + return "" +} + +func (b *Bmattermost) Connect() error { if !b.Plus { b.mh = matterhook.New(b.Config.Mattermost.URL, matterhook.Config{InsecureSkipVerify: b.Config.Mattermost.SkipTLSVerify, @@ -67,7 +75,7 @@ func New(cfg *config.Config, c chan config.Message) *Bmattermost { flog.mm.Infof("Trying login %s (team: %s) on %s", b.Config.Mattermost.Login, b.Config.Mattermost.Team, b.Config.Mattermost.Server) err := b.mc.Login() if err != nil { - flog.mm.Fatal("Can not connect", err) + return err } flog.mm.Info("Login ok") b.mc.JoinChannel(b.Config.Mattermost.Channel) @@ -77,11 +85,7 @@ func New(cfg *config.Config, c chan config.Message) *Bmattermost { go b.mc.WsReceiver() } go b.handleMatter() - return b -} - -func (b *Bmattermost) Command(cmd string) string { - return "" + return nil } func (b *Bmattermost) Name() string { diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index 32cf999b..5d10f9bd 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -17,8 +17,6 @@ type Bxmpp struct { } type FancyLog struct { - irc *log.Entry - mm *log.Entry xmpp *log.Entry } @@ -31,27 +29,29 @@ type Message struct { var flog FancyLog func init() { - flog.irc = log.WithFields(log.Fields{"module": "irc"}) - flog.mm = log.WithFields(log.Fields{"module": "mattermost"}) flog.xmpp = log.WithFields(log.Fields{"module": "xmpp"}) } func New(config *config.Config, c chan config.Message) *Bxmpp { b := &Bxmpp{} b.xmppMap = make(map[string]string) - var err error b.Config = config b.Remote = c + return b +} + +func (b *Bxmpp) Connect() error { + var err error flog.xmpp.Info("Trying XMPP connection") b.xc, err = b.createXMPP() if err != nil { flog.xmpp.Debugf("%#v", err) - panic("xmpp failure") + return err } flog.xmpp.Info("Connection succeeded") b.setupChannels() go b.handleXmpp() - return b + return nil } func (b *Bxmpp) Name() string {