Skip to content

Commit

Permalink
Merge pull request #40 from fleaz/feature/handle_irc_disconnects
Browse files Browse the repository at this point in the history
Track state of IRC client
  • Loading branch information
fleaz authored Mar 8, 2019
2 parents 473ba9d + b020cde commit 99dc0c4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/x509"
"io/ioutil"
"strings"
"sync"
"time"

log "github.com/sirupsen/logrus"
Expand All @@ -14,6 +15,7 @@ import (
)

var client *girc.Client
var clientLock = &sync.RWMutex{}

func ircConnection(config *viper.Viper, channelList []string) {
clientConfig := girc.Config{
Expand Down Expand Up @@ -77,9 +79,11 @@ func ircConnection(config *viper.Viper, channelList []string) {
}
}

clientLock.Lock()
client = girc.New(clientConfig)

client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
clientLock.Unlock()
for _, name := range channelList {
joinChannel(name)
}
Expand All @@ -90,8 +94,9 @@ func ircConnection(config *viper.Viper, channelList []string) {

for {
if err := client.Connect(); err != nil {
clientLock.Lock()
log.Warnf("Connection to %s terminated: %s", client.Server(), err)
log.Warn("Reconnecting to in 30 seconds...")
log.Warn("Reconnecting in 30 seconds...")
time.Sleep(30 * time.Second)
}
}
Expand All @@ -105,7 +110,9 @@ func channelReceiver() {
log.Debug("Took IRC event out of channel.")
joinChannel(elem.Channel)
for _, message := range elem.Messages {
clientLock.RLock()
client.Cmd.Message(elem.Channel, message)
clientLock.RUnlock()
}
}
}
Expand All @@ -121,5 +128,7 @@ func joinChannel(newChannel string) {
"channel": newChannel,
}).Debug("Need to join new channel")

clientLock.RLock()
client.Cmd.Join(newChannel)
clientLock.RUnlock()
}

0 comments on commit 99dc0c4

Please sign in to comment.