diff --git a/src/irc/client.go b/src/irc/client.go index 52221a3d..d7246b03 100644 --- a/src/irc/client.go +++ b/src/irc/client.go @@ -2,6 +2,7 @@ package irc import ( "net" + "strings" ) type Client struct { @@ -46,3 +47,20 @@ func (c *Client) UModeString() string { } 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 +} diff --git a/src/irc/commands.go b/src/irc/commands.go index 6afd061b..57d72e2a 100644 --- a/src/irc/commands.go +++ b/src/irc/commands.go @@ -9,12 +9,17 @@ func (m *NickMessage) Handle(s *Server, c *Client) { c.send <- ErrNickNameInUse(m.nickname) return } + oldNick := c.nick if c.nick != "" { delete(s.nicks, c.nick) } c.nick = m.nickname 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) { @@ -56,9 +61,9 @@ func (m *ModeMessage) Handle(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.send <- ReplyWelcome(c.Nick(), c.username, "localhost") + c.send <- ReplyWelcome(c) c.send <- ReplyYourHost(c.Nick(), "irc.jlatt.com") c.send <- ReplyCreated(c.Nick(), "2012/04/07") c.send <- ReplyMyInfo(c.Nick(), "irc.jlatt.com") diff --git a/src/irc/constants.go b/src/irc/constants.go index b7796e66..dc9ce40f 100644 --- a/src/irc/constants.go +++ b/src/irc/constants.go @@ -11,7 +11,8 @@ const ( RPL_CREATED = "003" RPL_MYINFO = "004" RPL_UMODEIS = "221" - RPL_NONE = "300" + RPL_INFO = "371" + RPL_NICK = "NICK" ) const ( diff --git a/src/irc/responses.go b/src/irc/responses.go index b444c213..4ab4681d 100644 --- a/src/irc/responses.go +++ b/src/irc/responses.go @@ -4,8 +4,12 @@ import ( "fmt" ) -func ReplyWelcome(nick string, user string, host string) string { - return fmt.Sprintf("%s %s Welcome to the Internet Relay Network %s!%s@%s", RPL_WELCOME, nick, nick, user, host) +func ReplyNick(oldNick string, c *Client) string { + 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 {