//Join adds a webclient to the list of active clients. It blocks until webclient socket closes or chat ends func (c ircchat) Join(sessionID string, webclient irc.Conn) error { if !c.Active() { return errors.New("The chat session is not active or enabled. Check settings") } webclient.Write(irc.NickMessage(c.settings.Login.Nick)) //Send open channels to client for _, ch := range c.client.ChannelNames() { webclient.Write(irc.JoinMessage(ch)) //Send users in the rooms as a names reply commands (353 to indicate start, 366 to indicate end) //Webclient doesn't care about length, but a traditional IRC client will //TODO: Indicate if channel is public, private or secret ( "=" / "*" / "@" ), current sends as pub //:tepper.freenode.net 353 goirctest @ #gotest :goirctest @Oooska //:tepper.freenode.net 366 goirctest #gotest :End of /NAMES list. users, _ := c.client.Users(ch) namesRepl := fmt.Sprintf("353 %s = %s :%s", c.settings.Login.Nick, ch, strings.Join(users, " ")) namesEndRepl := fmt.Sprintf("366 %s %s", c.settings.Login.Nick, ch) webclient.Write(irc.NewMessage(namesRepl)) webclient.Write(irc.NewMessage(namesEndRepl)) messages, err := persistenceInstance.messages(c.account, ch, time.Now(), 200) if err != nil { log.Printf("Error retrieving message logs: %s", err.Error()) } for _, msg := range messages { webclient.Write(msg) } } //Register as a listener c.registerClient(sessionID, webclient) for { select { case <-c.quit: fmt.Println("Exiting ircClientListener") return errors.New("IRC Session has ended") default: msg, err := webclient.Read() if err != nil { c.unregisterClient(sessionID) return err } c.toServer <- clientMessage{SessionID: sessionID, Message: msg} } } }
//serverListener listens for messages from the IRC server. //When one is receives, it sends the message into the msg channel. func serverListener(ircConn irc.Conn, msgChan chan<- irc.Message, errChan chan<- error, quitChan <-chan bool) { for { select { case <-quitChan: return default: msg, err := ircConn.Read() if err != nil { errChan <- err return } msgChan <- msg } } }