Browse Source

user registration

tags/v0.1.0
Jeremy Latt 12 years ago
parent
commit
53e098067a
3 changed files with 45 additions and 19 deletions
  1. 8
    0
      src/irc/client.go
  2. 8
    1
      src/irc/protocol.go
  3. 29
    18
      src/irc/server.go

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

@@ -19,6 +19,7 @@ type Client struct {
19 19
 	username   string
20 20
 	realname   string
21 21
 	nick       string
22
+	registered bool
22 23
 }
23 24
 
24 25
 func NewClient(conn net.Conn) *Client {
@@ -45,3 +46,10 @@ func (c *Client) Send(lines ...string) {
45 46
 		c.send <- line
46 47
 	}
47 48
 }
49
+
50
+func (c *Client) Nick() string {
51
+	if c.nick != "" {
52
+		return c.nick
53
+	}
54
+	return "<guest>"
55
+}

+ 8
- 1
src/irc/protocol.go View File

@@ -63,7 +63,7 @@ const (
63 63
 	RE_OPER     = "(?P<name>\\S+) (?P<password>\\S+)"
64 64
 	RE_MODE     = "(?P<nickname>\\S+)(?: (?P<mode>[-+][iwroOs]+))*"
65 65
 	RE_SERVICE  = "(?P<nickname>\\S+) (?P<reserved1>\\S+) (?P<distribution>\\S+) (?P<type>\\S+) (?P<reserved2>\\S+) :(?P<info>.+)"
66
-	RE_QUIT     = "(?P<message>.*)"
66
+	RE_QUIT     = ":(?P<message>.*)"
67 67
 	RE_SQUIT    = "(?P<server>\\S+) :(?P<comment>.+)"
68 68
 	RE_JOIN     = "0|(?:(?P<channels>\\S+(?:,\\S+)*)(?: (?P<keys>\\S+(?:,\\S+)*))?)"
69 69
 	RE_PART     = "(?P<channels>\\S+(?:,\\S+)*)(?: :(?P<message>.+))?"
@@ -105,3 +105,10 @@ const (
105 105
 	RE_ISON     = "(?P<nicknames>\\S+(?: \\S+)*)"
106 106
 )
107 107
 
108
+func MessagePong() string {
109
+	return "PONG"
110
+}
111
+
112
+func MessageError() string {
113
+	return "ERROR :Bye"
114
+}

+ 29
- 18
src/irc/server.go View File

@@ -3,17 +3,17 @@ package irc
3 3
 import (
4 4
 	"log"
5 5
 	"net"
6
+	"regexp"
6 7
 	"strings"
7 8
 )
8 9
 
9 10
 type Server struct {
10 11
 	ch chan Message
11
-	users map[string]*Client
12 12
 	nicks map[string]*Client
13 13
 }
14 14
 
15 15
 func NewServer() *Server {
16
-	server := Server{make(chan Message), make(map[string]*Client), make(map[string]*Client)}
16
+	server := Server{make(chan Message), make(map[string]*Client)}
17 17
 	go server.Receive()
18 18
 	return &server
19 19
 }
@@ -39,15 +39,15 @@ func (s *Server) Receive() {
39 39
 		log.Printf("C -> S: %s %s", message.command, message.args)
40 40
 		switch message.command {
41 41
 		case "PING":
42
-			message.client.Send("PONG")
43
-		case "PASS":
44
-			s.PassCommand(message.client, message.args)
42
+			message.client.Send(MessagePong())
45 43
 		case "USER":
46 44
 			s.UserCommand(message.client, message.args)
47 45
 		case "NICK":
48 46
 			s.NickCommand(message.client, message.args)
47
+		case "QUIT":
48
+			s.QuitCommand(message.client, message.args)
49 49
 		default:
50
-			message.client.Send(ErrUnknownCommand(message.client.nick, message.command))
50
+			message.client.Send(ErrUnknownCommand(message.client.Nick(), message.command))
51 51
 		}
52 52
 	}
53 53
 }
@@ -58,25 +58,15 @@ func (s *Server) Send(m Message) {
58 58
 
59 59
 // commands
60 60
 
61
-func (s *Server) PassCommand(c *Client, args string) {
62
-}
63
-
64 61
 func (s *Server) UserCommand(c *Client, args string) {
65 62
 	parts := strings.SplitN(args, " ", 4)
66 63
 	username, _, _, realname := parts[0], parts[1], parts[2], parts[3]
67
-	if s.users[username] != nil {
64
+	if c.username != "" {
68 65
 		c.Send(ErrAlreadyRegistered(c.nick))
69 66
 		return
70 67
 	}
71 68
 	c.username, c.realname = username, realname
72
-	s.users[username] = c
73
-	if c.nick != "" {
74
-		c.Send(
75
-			ReplyWelcome(c.nick, c.username, "localhost"),
76
-			ReplyYourHost(c.nick, "irc.jlatt.com"),
77
-			ReplyCreated(c.nick, "2012/04/07"),
78
-			ReplyMyInfo(c.nick, "irc.jlatt.com"))
79
-	}
69
+	s.TryRegister(c)
80 70
 }
81 71
 
82 72
 func (s *Server) NickCommand(c *Client, nick string) {
@@ -86,4 +76,25 @@ func (s *Server) NickCommand(c *Client, nick string) {
86 76
 	}
87 77
 	c.nick = nick
88 78
 	s.nicks[nick] = c
79
+	s.TryRegister(c)
80
+}
81
+
82
+func (s *Server) TryRegister(c *Client) {
83
+	if (!c.registered && c.nick != "" && c.username != "") {
84
+		c.registered = true
85
+		c.Send(
86
+			ReplyWelcome(c.Nick(), c.username, "localhost"),
87
+			ReplyYourHost(c.Nick(), "irc.jlatt.com"),
88
+			ReplyCreated(c.Nick(), "2012/04/07"),
89
+			ReplyMyInfo(c.Nick(), "irc.jlatt.com"))
90
+	}
91
+}
92
+
93
+func (s *Server) QuitCommand(c *Client, args string) {
94
+	re := regexp.MustCompile("^" + RE_QUIT + "$")
95
+	matches := re.FindAllStringSubmatch(args, -1)
96
+	if matches != nil {
97
+		c.Send(MessageError())
98
+	}
99
+	delete(s.nicks, c.nick)
89 100
 }

Loading…
Cancel
Save