|
@@ -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
|
}
|