Wait until the welcome message before connection is ok (irc). Fixes #62

This commit is contained in:
Wim 2016-10-29 18:59:12 +02:00
parent 4a336a6bba
commit 5249568b8e
1 changed files with 19 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package birc
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
ircm "github.com/sorcix/irc" ircm "github.com/sorcix/irc"
@ -14,13 +15,14 @@ import (
) )
type Birc struct { type Birc struct {
i *irc.Connection i *irc.Connection
Nick string Nick string
names map[string][]string names map[string][]string
Config *config.Protocol Config *config.Protocol
origin string origin string
protocol string protocol string
Remote chan config.Message Remote chan config.Message
connected chan struct{}
} }
var flog *log.Entry var flog *log.Entry
@ -38,6 +40,7 @@ func New(config config.Protocol, origin string, c chan config.Message) *Birc {
b.names = make(map[string][]string) b.names = make(map[string][]string)
b.origin = origin b.origin = origin
b.protocol = protocol b.protocol = protocol
b.connected = make(chan struct{})
return b return b
} }
@ -70,9 +73,14 @@ func (b *Birc) Connect() error {
if err != nil { if err != nil {
return err return err
} }
flog.Info("Connection succeeded")
i.Debug = false
b.i = i b.i = i
select {
case <-b.connected:
flog.Info("Connection succeeded")
case <-time.After(time.Second * 30):
return fmt.Errorf("connection timed out")
}
i.Debug = false
return nil return nil
} }
@ -143,6 +151,8 @@ func (b *Birc) handleNewConnection(event *irc.Event) {
flog.Debugf("PING/PONG") flog.Debugf("PING/PONG")
}) })
i.AddCallback("*", b.handleOther) i.AddCallback("*", b.handleOther)
// we are now fully connected
b.connected <- struct{}{}
} }
func (b *Birc) handleNotice(event *irc.Event) { func (b *Birc) handleNotice(event *irc.Event) {