Browse Source

break up types.go

- remove old interfaces, move to relevant files
- remove Phase in favor of a boolean flag
tags/v0.1.0
Jeremy Latt 10 years ago
parent
commit
2006aff9f7
6 changed files with 82 additions and 93 deletions
  1. 11
    3
      irc/client.go
  2. 7
    0
      irc/commands.go
  3. 0
    10
      irc/constants.go
  4. 22
    6
      irc/reply.go
  5. 41
    29
      irc/server.go
  6. 1
    45
      irc/types.go

+ 11
- 3
irc/client.go View File

@@ -6,6 +6,12 @@ import (
6 6
 	"time"
7 7
 )
8 8
 
9
+const (
10
+	LOGIN_TIMEOUT = time.Minute / 2 // how long the client has to login
11
+	IDLE_TIMEOUT  = time.Minute     // how long before a client is considered idle
12
+	QUIT_TIMEOUT  = time.Minute     // how long after idle before a client is kicked
13
+)
14
+
9 15
 func IsNickname(nick string) bool {
10 16
 	return NicknameExpr.MatchString(nick)
11 17
 }
@@ -26,9 +32,9 @@ type Client struct {
26 32
 	idleTimer    *time.Timer
27 33
 	loginTimer   *time.Timer
28 34
 	nick         string
29
-	phase        Phase
30 35
 	quitTimer    *time.Timer
31 36
 	realname     string
37
+	registered   bool
32 38
 	server       *Server
33 39
 	socket       *Socket
34 40
 	username     string
@@ -45,7 +51,6 @@ func NewClient(server *Server, conn net.Conn) *Client {
45 51
 		commands:     make(chan Command),
46 52
 		ctime:        now,
47 53
 		flags:        make(map[UserMode]bool),
48
-		phase:        Registration,
49 54
 		server:       server,
50 55
 	}
51 56
 	client.socket = NewSocket(conn, client.commands)
@@ -118,7 +123,10 @@ func (client *Client) Idle() {
118 123
 }
119 124
 
120 125
 func (client *Client) Register() {
121
-	client.phase = Normal
126
+	if client.registered {
127
+		return
128
+	}
129
+	client.registered = true
122 130
 	client.loginTimer.Stop()
123 131
 	client.Touch()
124 132
 }

+ 7
- 0
irc/commands.go View File

@@ -9,6 +9,13 @@ import (
9 9
 	"strings"
10 10
 )
11 11
 
12
+type Command interface {
13
+	Client() *Client
14
+	Code() StringCode
15
+	SetClient(*Client)
16
+	SetCode(StringCode)
17
+}
18
+
12 19
 type checkPasswordCommand interface {
13 20
 	LoadPassword(*Server)
14 21
 	CheckPassword()

+ 0
- 10
irc/constants.go View File

@@ -3,7 +3,6 @@ package irc
3 3
 import (
4 4
 	"errors"
5 5
 	"regexp"
6
-	"time"
7 6
 )
8 7
 
9 8
 var (
@@ -21,10 +20,6 @@ const (
21 20
 	CRLF          = "\r\n"
22 21
 	MAX_REPLY_LEN = 512 - len(CRLF)
23 22
 
24
-	LOGIN_TIMEOUT = time.Minute / 2 // how long the client has to login
25
-	IDLE_TIMEOUT  = time.Minute     // how long before a client is considered idle
26
-	QUIT_TIMEOUT  = time.Minute     // how long after idle before a client is kicked
27
-
28 23
 	// string codes
29 24
 	AWAY    StringCode = "AWAY"
30 25
 	CAP     StringCode = "CAP"
@@ -227,8 +222,3 @@ const (
227 222
 	UserLimit       ChannelMode = 'l' // flag arg
228 223
 	Voice           ChannelMode = 'v' // arg
229 224
 )
230
-
231
-const (
232
-	Registration Phase = iota
233
-	Normal       Phase = iota
234
-)

+ 22
- 6
irc/reply.go View File

@@ -6,7 +6,23 @@ import (
6 6
 	"time"
7 7
 )
8 8
 
9
-func NewStringReply(source Identifier, code StringCode,
9
+type ReplyCode interface {
10
+	String() string
11
+}
12
+
13
+type StringCode string
14
+
15
+func (code StringCode) String() string {
16
+	return string(code)
17
+}
18
+
19
+type NumericCode uint
20
+
21
+func (code NumericCode) String() string {
22
+	return fmt.Sprintf("%03d", code)
23
+}
24
+
25
+func NewStringReply(source Identifiable, code StringCode,
10 26
 	format string, args ...interface{}) string {
11 27
 	var header string
12 28
 	if source == nil {
@@ -79,15 +95,15 @@ func (target *Client) MultilineReply(names []string, code NumericCode, format st
79 95
 // messaging replies
80 96
 //
81 97
 
82
-func RplPrivMsg(source Identifier, target Identifier, message string) string {
98
+func RplPrivMsg(source Identifiable, target Identifiable, message string) string {
83 99
 	return NewStringReply(source, PRIVMSG, "%s :%s", target.Nick(), message)
84 100
 }
85 101
 
86
-func RplNotice(source Identifier, target Identifier, message string) string {
102
+func RplNotice(source Identifiable, target Identifiable, message string) string {
87 103
 	return NewStringReply(source, NOTICE, "%s :%s", target.Nick(), message)
88 104
 }
89 105
 
90
-func RplNick(source Identifier, newNick string) string {
106
+func RplNick(source Identifiable, newNick string) string {
91 107
 	return NewStringReply(source, NICK, newNick)
92 108
 }
93 109
 
@@ -108,11 +124,11 @@ func RplChannelMode(client *Client, channel *Channel,
108 124
 	return NewStringReply(client, MODE, "%s %s", channel, changes)
109 125
 }
110 126
 
111
-func RplTopicMsg(source Identifier, channel *Channel) string {
127
+func RplTopicMsg(source Identifiable, channel *Channel) string {
112 128
 	return NewStringReply(source, TOPIC, "%s :%s", channel, channel.topic)
113 129
 }
114 130
 
115
-func RplPing(target Identifier) string {
131
+func RplPing(target Identifiable) string {
116 132
 	return NewStringReply(nil, PING, ":%s", target.Nick())
117 133
 }
118 134
 

+ 41
- 29
irc/server.go View File

@@ -16,10 +16,15 @@ import (
16 16
 	"time"
17 17
 )
18 18
 
19
-var (
20
-	SERVER_SIGNALS = []os.Signal{syscall.SIGINT, syscall.SIGHUP,
21
-		syscall.SIGTERM, syscall.SIGQUIT}
22
-)
19
+type ServerCommand interface {
20
+	Command
21
+	HandleServer(*Server)
22
+}
23
+
24
+type RegServerCommand interface {
25
+	Command
26
+	HandleRegServer(*Server)
27
+}
23 28
 
24 29
 type Server struct {
25 30
 	channels  ChannelNameMap
@@ -37,6 +42,11 @@ type Server struct {
37 42
 	whoWas    *WhoWasList
38 43
 }
39 44
 
45
+var (
46
+	SERVER_SIGNALS = []os.Signal{syscall.SIGINT, syscall.SIGHUP,
47
+		syscall.SIGTERM, syscall.SIGQUIT}
48
+)
49
+
40 50
 func NewServer(config *Config) *Server {
41 51
 	server := &Server{
42 52
 		channels:  make(ChannelNameMap),
@@ -111,34 +121,33 @@ func (server *Server) processCommand(cmd Command) {
111 121
 	client := cmd.Client()
112 122
 	Log.debug.Printf("%s → %s %s", client, server, cmd)
113 123
 
114
-	switch client.phase {
115
-	case Registration:
124
+	if !client.registered {
116 125
 		regCmd, ok := cmd.(RegServerCommand)
117 126
 		if !ok {
118 127
 			client.Quit("unexpected command")
119 128
 			return
120 129
 		}
121 130
 		regCmd.HandleRegServer(server)
131
+		return
132
+	}
122 133
 
123
-	case Normal:
124
-		srvCmd, ok := cmd.(ServerCommand)
125
-		if !ok {
126
-			client.ErrUnknownCommand(cmd.Code())
127
-			return
128
-		}
129
-		switch srvCmd.(type) {
130
-		case *PingCommand, *PongCommand:
131
-			client.Touch()
134
+	srvCmd, ok := cmd.(ServerCommand)
135
+	if !ok {
136
+		client.ErrUnknownCommand(cmd.Code())
137
+		return
138
+	}
139
+	switch srvCmd.(type) {
140
+	case *PingCommand, *PongCommand:
141
+		client.Touch()
132 142
 
133
-		case *QuitCommand:
134
-			// no-op
143
+	case *QuitCommand:
144
+		// no-op
135 145
 
136
-		default:
137
-			client.Active()
138
-			client.Touch()
139
-		}
140
-		srvCmd.HandleServer(server)
146
+	default:
147
+		client.Active()
148
+		client.Touch()
141 149
 	}
150
+	srvCmd.HandleServer(server)
142 151
 }
143 152
 
144 153
 func (server *Server) Shutdown() {
@@ -197,14 +206,17 @@ func (s *Server) listen(addr string) {
197 206
 //
198 207
 
199 208
 func (s *Server) tryRegister(c *Client) {
200
-	if c.HasNick() && c.HasUsername() && (c.capState != CapNegotiating) {
201
-		c.Register()
202
-		c.RplWelcome()
203
-		c.RplYourHost()
204
-		c.RplCreated()
205
-		c.RplMyInfo()
206
-		s.MOTD(c)
209
+	if c.registered || !c.HasNick() || !c.HasUsername() ||
210
+		(c.capState == CapNegotiating) {
211
+		return
207 212
 	}
213
+
214
+	c.Register()
215
+	c.RplWelcome()
216
+	c.RplYourHost()
217
+	c.RplCreated()
218
+	c.RplMyInfo()
219
+	s.MOTD(c)
208 220
 }
209 221
 
210 222
 func (server *Server) MOTD(client *Client) {

+ 1
- 45
irc/types.go View File

@@ -23,24 +23,6 @@ func (mode UserMode) String() string {
23 23
 	return string(mode)
24 24
 }
25 25
 
26
-type Phase uint
27
-
28
-type ReplyCode interface {
29
-	String() string
30
-}
31
-
32
-type StringCode string
33
-
34
-func (code StringCode) String() string {
35
-	return string(code)
36
-}
37
-
38
-type NumericCode uint
39
-
40
-func (code NumericCode) String() string {
41
-	return fmt.Sprintf("%03d", code)
42
-}
43
-
44 26
 // channel mode flags
45 27
 type ChannelMode rune
46 28
 
@@ -143,33 +125,7 @@ func (channels ChannelSet) First() *Channel {
143 125
 // interfaces
144 126
 //
145 127
 
146
-type Identifier interface {
128
+type Identifiable interface {
147 129
 	Id() string
148 130
 	Nick() string
149 131
 }
150
-
151
-type Replier interface {
152
-	Reply(...string)
153
-}
154
-
155
-type Command interface {
156
-	Code() StringCode
157
-	Client() *Client
158
-	SetCode(StringCode)
159
-	SetClient(*Client)
160
-}
161
-
162
-type ServerCommand interface {
163
-	Command
164
-	HandleServer(*Server)
165
-}
166
-
167
-type AuthServerCommand interface {
168
-	Command
169
-	HandleAuthServer(*Server)
170
-}
171
-
172
-type RegServerCommand interface {
173
-	Command
174
-	HandleRegServer(*Server)
175
-}

Loading…
Cancel
Save