diff --git a/server/irc.go b/server/irc.go index c87ee406..95c5ec21 100644 --- a/server/irc.go +++ b/server/irc.go @@ -62,6 +62,7 @@ type IRC struct { reader *bufio.Reader out chan string ready sync.WaitGroup + once sync.Once nick string nickLock sync.Mutex @@ -247,22 +248,23 @@ func (i *IRC) recv() { msg := parseMessage(line) msg.Prefix = parseUser(msg.Prefix) + i.Messages <- msg + switch msg.Command { case PING: - i.write("PONG :" + msg.Trailing) + go i.write("PONG :" + msg.Trailing) case RPL_WELCOME: - i.ready.Done() + i.once.Do(i.ready.Done) } - - i.Messages <- msg } } func (i *IRC) close() { i.conn.Close() - i.ready.Done() + i.once.Do(i.ready.Done) close(i.out) + close(i.Messages) } func parseMessage(line string) *Message { diff --git a/server/message_handler.go b/server/message_handler.go index 01672176..ce8abca6 100644 --- a/server/message_handler.go +++ b/server/message_handler.go @@ -12,7 +12,12 @@ func handleMessages(irc *IRC, session *Session) { userBuffers := make(map[string][]string) var motd MOTD - for msg := range irc.Messages { + for { + msg, ok := <-irc.Messages + if !ok { + return + } + switch msg.Command { case NICK: session.sendJSON("nick", Nick{