Implement nick changing.

This commit is contained in:
Jeremy Latt 2012-04-17 21:13:12 -07:00
parent 99364e8b5f
commit e7734f572b
4 changed files with 34 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package irc
import ( import (
"net" "net"
"strings"
) )
type Client struct { type Client struct {
@ -46,3 +47,20 @@ func (c *Client) UModeString() string {
} }
return "" return ""
} }
func (c *Client) HasNick() bool {
return c.nick != ""
}
func (c *Client) HasUser() bool {
return c.username != ""
}
func (c *Client) Hostname() string {
addr := c.conn.RemoteAddr().String()
index := strings.LastIndex(addr, ":")
if index != -1 {
return addr[0:index]
}
return addr
}

View File

@ -9,12 +9,17 @@ func (m *NickMessage) Handle(s *Server, c *Client) {
c.send <- ErrNickNameInUse(m.nickname) c.send <- ErrNickNameInUse(m.nickname)
return return
} }
oldNick := c.nick
if c.nick != "" { if c.nick != "" {
delete(s.nicks, c.nick) delete(s.nicks, c.nick)
} }
c.nick = m.nickname c.nick = m.nickname
s.nicks[c.nick] = c s.nicks[c.nick] = c
tryRegister(s, c) if c.registered {
c.send <- ReplyNick(oldNick, c)
} else {
tryRegister(s, c)
}
} }
func (m *UserMessage) Handle(s *Server, c *Client) { func (m *UserMessage) Handle(s *Server, c *Client) {
@ -56,9 +61,9 @@ func (m *ModeMessage) Handle(s *Server, c *Client) {
} }
func tryRegister(s *Server, c *Client) { func tryRegister(s *Server, c *Client) {
if (!c.registered && c.nick != "" && c.username != "") { if (!c.registered && c.HasNick() && c.HasUser()) {
c.registered = true c.registered = true
c.send <- ReplyWelcome(c.Nick(), c.username, "localhost") c.send <- ReplyWelcome(c)
c.send <- ReplyYourHost(c.Nick(), "irc.jlatt.com") c.send <- ReplyYourHost(c.Nick(), "irc.jlatt.com")
c.send <- ReplyCreated(c.Nick(), "2012/04/07") c.send <- ReplyCreated(c.Nick(), "2012/04/07")
c.send <- ReplyMyInfo(c.Nick(), "irc.jlatt.com") c.send <- ReplyMyInfo(c.Nick(), "irc.jlatt.com")

View File

@ -11,7 +11,8 @@ const (
RPL_CREATED = "003" RPL_CREATED = "003"
RPL_MYINFO = "004" RPL_MYINFO = "004"
RPL_UMODEIS = "221" RPL_UMODEIS = "221"
RPL_NONE = "300" RPL_INFO = "371"
RPL_NICK = "NICK"
) )
const ( const (

View File

@ -4,8 +4,12 @@ import (
"fmt" "fmt"
) )
func ReplyWelcome(nick string, user string, host string) string { func ReplyNick(oldNick string, c *Client) string {
return fmt.Sprintf("%s %s Welcome to the Internet Relay Network %s!%s@%s", RPL_WELCOME, nick, nick, user, host) return fmt.Sprintf(":%s!%s@%s %s :%s", oldNick, c.username, c.Hostname(), RPL_NICK, c.Nick())
}
func ReplyWelcome(c *Client) string {
return fmt.Sprintf("%s %s Welcome to the Internet Relay Network %s!%s@%s", RPL_WELCOME, c.Nick(), c.Nick(), c.username, c.Hostname())
} }
func ReplyYourHost(nick string, server string) string { func ReplyYourHost(nick string, server string) string {