Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 5dfe29df3c5a40d915591673985b2c990cae9622 Author: Wim <wim@42.be> Date: Mon Aug 15 23:16:07 2016 +0000 Add Connect() to Bridger interface diff --git a/bridge/bridge.go b/bridge/bridge.go index a849ba6..2a9ea7c 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -416 +419 @@ 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 d37149a..9ed5202 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -2038 +2027 @@ 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  }   @@ -636 +5227 @@ 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"  } @@ -8024 +906 @@ 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 63f38dc..9dc0ceb 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -556 +5514 @@ 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, @@ -677 +757 @@ 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) @@ -7711 +857 @@ 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 32cf999..5d10f9b 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -178 +176 @@ type Bxmpp struct {  }    type FancyLog struct { - irc *log.Entry - mm *log.Entry   xmpp *log.Entry  }   @@ -3127 +2929 @@ 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 {