Skip to content

Commit

Permalink
Merge ff9535d into fa5e001
Browse files Browse the repository at this point in the history
  • Loading branch information
belak authored Jan 3, 2018
2 parents fa5e001 + ff9535d commit 7595152
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ func (c *Client) handlePing(timestamp int64, pongChan chan struct{}, wg *sync.Wa

// maybeStartCapHandshake will run a CAP LS and all the relevant CAP REQ
// commands if there are any CAPs requested.
func (c *Client) maybeStartCapHandshake() error {
func (c *Client) maybeStartCapHandshake() {
if len(c.caps) <= 0 {
return nil
return
}

c.Write("CAP LS")
Expand All @@ -293,8 +293,6 @@ func (c *Client) maybeStartCapHandshake() error {
c.remainingCapResponses++
}
}

return nil
}

// CapRequest allows you to request IRCv3 capabilities from the server during
Expand Down Expand Up @@ -330,6 +328,31 @@ func (c *Client) sendError(err error) {
}
}

func (c *Client) startReadLoop(wg *sync.WaitGroup) {
wg.Add(1)

go func() {
defer wg.Done()

for {
m, err := c.ReadMessage()
if err != nil {
c.sendError(err)
break
}

if f, ok := clientFilters[m.Command]; ok {
f(c, m)
}

if c.config.Handler != nil {
c.config.Handler.Handle(c, m)
}
}

}()
}

// Run starts the main loop for this IRC connection. Note that it may break in
// strange and unexpected ways if it is called again before the first connection
// exits.
Expand All @@ -355,21 +378,9 @@ func (c *Client) Run() error {
c.Writef("NICK :%s", c.config.Nick)
c.Writef("USER %s 0.0.0.0 0.0.0.0 :%s", c.config.User, c.config.Name)

for {
m, err := c.ReadMessage()
if err != nil {
c.sendError(err)
break
}

if f, ok := clientFilters[m.Command]; ok {
f(c, m)
}

if c.config.Handler != nil {
c.config.Handler.Handle(c, m)
}
}
// Now that the handshake is pretty much done, we can start listening for
// messages.
c.startReadLoop(&wg)

// Wait for an error from any goroutine, then signal we're exiting and wait
// for the goroutines to exit.
Expand Down

0 comments on commit 7595152

Please sign in to comment.