Skip to content

Commit

Permalink
Add documentation and error if a CAP is requested outside the handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
belak committed Dec 12, 2017
1 parent edc8aca commit c9560c2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 16 additions & 1 deletion client.go
Expand Up @@ -280,17 +280,33 @@ func (c *Client) sendError(err error) {
}
}

// CapRequest allows you to request IRCv3 capabilities from the server during
// the handshake. This must be called before the handshake completes and so it
// is recommended that this be called before Run. If the CAP is marked as
// required, the client will exit if that CAP could not be negotiated during the
// handshake.
func (c *Client) CapRequest(capName string, required bool) {
if c.finishedHandshake {
c.sendError(errors.New("CAP requested after CAP handshake"))
return
}

cap := c.caps[capName]
cap.Requested = true
cap.Required = cap.Required || required
c.caps[capName] = cap
}

// CapEnabled allows you to check if a CAP is enabled for this connection. Note
// that it will not be populated until after the CAP handshake is done, so it is
// recommended to wait to check this until after a message like 001.
func (c *Client) CapEnabled(capName string) bool {
return c.caps[capName].Enabled
}

// CapAvailable allows you to check if a CAP is available on this server. Note
// that it will not be populated until after the CAP handshake is done, so it is
// recommended to wait to check this until after a message like 001.
func (c *Client) CapAvailable(capName string) bool {
return c.caps[capName].Available
}
Expand Down Expand Up @@ -341,7 +357,6 @@ func (c *Client) Run() error {

m, err := c.ReadMessage()
if err != nil {
fmt.Println("READ EOF")
c.sendError(err)
break
}
Expand Down
7 changes: 7 additions & 0 deletions client_test.go
Expand Up @@ -149,6 +149,13 @@ func TestCapReq(t *testing.T) {
assert.False(t, c.CapEnabled("multi-prefix"))
assert.False(t, c.CapAvailable("random-thing"))
assert.True(t, c.CapAvailable("multi-prefix"))

runClientTest(t, config, errors.New("CAP requested after CAP handshake"), func(c *Client) {
assert.False(t, c.CapAvailable("random-thing"))
assert.False(t, c.CapAvailable("multi-prefix"))
c.finishedHandshake = true
c.CapRequest("multi-prefix", true)
}, []TestAction{})
}

func TestClient(t *testing.T) {
Expand Down

0 comments on commit c9560c2

Please sign in to comment.