diff --git a/irc/client.go b/irc/client.go index 998a58d3..54185709 100644 --- a/irc/client.go +++ b/irc/client.go @@ -22,7 +22,8 @@ type Client struct { Messages chan *Message ConnectionChanged chan bool - nick string + nick string + channels []string conn net.Conn connected bool @@ -149,3 +150,18 @@ func (c *Client) register() { c.writeNick(c.nick) c.writeUser(c.Username, c.Realname) } + +func (c *Client) addChannel(channel string) { + c.lock.Lock() + c.channels = append(c.channels, channel) + c.lock.Unlock() +} + +func (c *Client) flushChannels() { + c.lock.Lock() + if len(c.channels) > 0 { + c.Join(c.channels...) + c.channels = []string{} + } + c.lock.Unlock() +} diff --git a/irc/client_test.go b/irc/client_test.go index e46fdf86..7ef02541 100644 --- a/irc/client_test.go +++ b/irc/client_test.go @@ -152,3 +152,14 @@ func TestRegister(t *testing.T) { assert.Equal(t, "NICK nick\r\n", <-out) assert.Equal(t, "USER user 0 * :rn\r\n", <-out) } + +func TestFlushChannels(t *testing.T) { + c, out := testClientSend() + c.addChannel("#chan1") + c.flushChannels() + assert.Equal(t, <-out, "JOIN #chan1\r\n") + c.addChannel("#chan2") + c.addChannel("#chan3") + c.flushChannels() + assert.Equal(t, <-out, "JOIN #chan2,#chan3\r\n") +} diff --git a/irc/conn.go b/irc/conn.go index 15720bad..30339592 100644 --- a/irc/conn.go +++ b/irc/conn.go @@ -93,6 +93,8 @@ func (c *Client) tryConnect() { err := c.connect() if err == nil { c.backoff.Reset() + + c.flushChannels() return } @@ -179,6 +181,11 @@ func (c *Client) recv() { case Ping: go c.write("PONG :" + msg.Trailing) + case Join: + if msg.Nick == c.GetNick() { + c.addChannel(msg.Params[0]) + } + case ReplyWelcome: c.once.Do(c.ready.Done) }