Browse Source

Implement nick changing.

tags/v0.1.0
Jeremy Latt 12 years ago
parent
commit
e7734f572b
4 changed files with 34 additions and 6 deletions
  1. 18
    0
      src/irc/client.go
  2. 8
    3
      src/irc/commands.go
  3. 2
    1
      src/irc/constants.go
  4. 6
    2
      src/irc/responses.go

+ 18
- 0
src/irc/client.go View File

@@ -2,6 +2,7 @@ package irc
2 2
 
3 3
 import (
4 4
 	"net"
5
+	"strings"
5 6
 )
6 7
 
7 8
 type Client struct {
@@ -46,3 +47,20 @@ func (c *Client) UModeString() string {
46 47
 	}
47 48
 	return ""
48 49
 }
50
+
51
+func (c *Client) HasNick() bool {
52
+	return c.nick != ""
53
+}
54
+
55
+func (c *Client) HasUser() bool {
56
+	return c.username != ""
57
+}
58
+
59
+func (c *Client) Hostname() string {
60
+	addr := c.conn.RemoteAddr().String()
61
+	index := strings.LastIndex(addr, ":")
62
+	if index != -1 {
63
+		return addr[0:index]
64
+	}
65
+	return addr
66
+}

+ 8
- 3
src/irc/commands.go View File

@@ -9,12 +9,17 @@ func (m *NickMessage) Handle(s *Server, c *Client) {
9 9
 		c.send <- ErrNickNameInUse(m.nickname)
10 10
 		return
11 11
 	}
12
+	oldNick := c.nick
12 13
 	if c.nick != "" {
13 14
 		delete(s.nicks, c.nick)
14 15
 	}
15 16
 	c.nick = m.nickname
16 17
 	s.nicks[c.nick] = c
17
-	tryRegister(s, c)
18
+	if c.registered {
19
+		c.send <- ReplyNick(oldNick, c)
20
+	} else {
21
+		tryRegister(s, c)
22
+	}
18 23
 }
19 24
 
20 25
 func (m *UserMessage) Handle(s *Server, c *Client) {
@@ -56,9 +61,9 @@ func (m *ModeMessage) Handle(s *Server, c *Client) {
56 61
 }
57 62
 
58 63
 func tryRegister(s *Server, c *Client) {
59
-	if (!c.registered && c.nick != "" && c.username != "") {
64
+	if (!c.registered && c.HasNick() && c.HasUser()) {
60 65
 		c.registered = true
61
-		c.send <- ReplyWelcome(c.Nick(), c.username, "localhost")
66
+		c.send <- ReplyWelcome(c)
62 67
 		c.send <- ReplyYourHost(c.Nick(), "irc.jlatt.com")
63 68
 		c.send <- ReplyCreated(c.Nick(), "2012/04/07")
64 69
 		c.send <- ReplyMyInfo(c.Nick(), "irc.jlatt.com")

+ 2
- 1
src/irc/constants.go View File

@@ -11,7 +11,8 @@ const (
11 11
 	RPL_CREATED  = "003"
12 12
 	RPL_MYINFO   = "004"
13 13
 	RPL_UMODEIS  = "221"
14
-	RPL_NONE     = "300"
14
+	RPL_INFO     = "371"
15
+	RPL_NICK     = "NICK"
15 16
 )
16 17
 
17 18
 const (

+ 6
- 2
src/irc/responses.go View File

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

Loading…
Cancel
Save