5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 09:20:27 +00:00

Add Connect() to Bridger interface

This commit is contained in:
Wim 2016-08-15 23:16:07 +02:00
parent 9cb3413d9c
commit 889b6debc4
4 changed files with 42 additions and 43 deletions

View File

@ -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)

View File

@ -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])

View File

@ -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 {

View File

@ -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 {