瀏覽代碼

move capability handling code to a single file

tags/v0.1.0
Jeremy Latt 10 年之前
父節點
當前提交
0126edc7af
共有 4 個檔案被更改,包括 116 行新增102 行删除
  1. 116
    0
      irc/capability.go
  2. 0
    27
      irc/constants.go
  3. 0
    37
      irc/server.go
  4. 0
    38
      irc/types.go

+ 116
- 0
irc/capability.go 查看文件

@@ -0,0 +1,116 @@
1
+package irc
2
+
3
+import (
4
+	"strings"
5
+)
6
+
7
+type CapSubCommand string
8
+
9
+const (
10
+	CAP_LS    CapSubCommand = "LS"
11
+	CAP_LIST  CapSubCommand = "LIST"
12
+	CAP_REQ   CapSubCommand = "REQ"
13
+	CAP_ACK   CapSubCommand = "ACK"
14
+	CAP_NAK   CapSubCommand = "NAK"
15
+	CAP_CLEAR CapSubCommand = "CLEAR"
16
+	CAP_END   CapSubCommand = "END"
17
+)
18
+
19
+// Capabilities are optional features a client may request from a server.
20
+type Capability string
21
+
22
+const (
23
+	MultiPrefix Capability = "multi-prefix"
24
+	SASL        Capability = "sasl"
25
+)
26
+
27
+var (
28
+	SupportedCapabilities = CapabilitySet{
29
+		MultiPrefix: true,
30
+	}
31
+)
32
+
33
+func (capability Capability) String() string {
34
+	return string(capability)
35
+}
36
+
37
+// CapModifiers are indicators showing the state of a capability after a REQ or
38
+// ACK.
39
+type CapModifier rune
40
+
41
+const (
42
+	Ack     CapModifier = '~'
43
+	Disable CapModifier = '-'
44
+	Sticky  CapModifier = '='
45
+)
46
+
47
+func (mod CapModifier) String() string {
48
+	return string(mod)
49
+}
50
+
51
+type CapState uint
52
+
53
+const (
54
+	CapNone        CapState = iota
55
+	CapNegotiating CapState = iota
56
+	CapNegotiated  CapState = iota
57
+)
58
+
59
+type CapabilitySet map[Capability]bool
60
+
61
+func (set CapabilitySet) String() string {
62
+	strs := make([]string, len(set))
63
+	index := 0
64
+	for capability := range set {
65
+		strs[index] = string(capability)
66
+		index += 1
67
+	}
68
+	return strings.Join(strs, " ")
69
+}
70
+
71
+func (set CapabilitySet) DisableString() string {
72
+	parts := make([]string, len(set))
73
+	index := 0
74
+	for capability := range set {
75
+		parts[index] = Disable.String() + capability.String()
76
+		index += 1
77
+	}
78
+	return strings.Join(parts, " ")
79
+}
80
+
81
+func (msg *CapCommand) HandleRegServer(server *Server) {
82
+	client := msg.Client()
83
+
84
+	switch msg.subCommand {
85
+	case CAP_LS:
86
+		client.capState = CapNegotiating
87
+		client.Reply(RplCap(client, CAP_LS, SupportedCapabilities))
88
+
89
+	case CAP_LIST:
90
+		client.Reply(RplCap(client, CAP_LIST, client.capabilities))
91
+
92
+	case CAP_REQ:
93
+		for capability := range msg.capabilities {
94
+			if !SupportedCapabilities[capability] {
95
+				client.Reply(RplCap(client, CAP_NAK, msg.capabilities))
96
+				return
97
+			}
98
+		}
99
+		for capability := range msg.capabilities {
100
+			client.capabilities[capability] = true
101
+		}
102
+		client.Reply(RplCap(client, CAP_ACK, msg.capabilities))
103
+
104
+	case CAP_CLEAR:
105
+		reply := RplCap(client, CAP_ACK, client.capabilities.DisableString())
106
+		client.capabilities = make(CapabilitySet)
107
+		client.Reply(reply)
108
+
109
+	case CAP_END:
110
+		client.capState = CapNegotiated
111
+		server.tryRegister(client)
112
+
113
+	default:
114
+		client.ErrInvalidCapCmd(msg.subCommand)
115
+	}
116
+}

+ 0
- 27
irc/constants.go 查看文件

@@ -196,14 +196,6 @@ const (
196 196
 	ERR_UMODEUNKNOWNFLAG  NumericCode = 501
197 197
 	ERR_USERSDONTMATCH    NumericCode = 502
198 198
 
199
-	CAP_LS    CapSubCommand = "LS"
200
-	CAP_LIST  CapSubCommand = "LIST"
201
-	CAP_REQ   CapSubCommand = "REQ"
202
-	CAP_ACK   CapSubCommand = "ACK"
203
-	CAP_NAK   CapSubCommand = "NAK"
204
-	CAP_CLEAR CapSubCommand = "CLEAR"
205
-	CAP_END   CapSubCommand = "END"
206
-
207 199
 	Add    ModeOp = '+'
208 200
 	List   ModeOp = '='
209 201
 	Remove ModeOp = '-'
@@ -234,28 +226,9 @@ const (
234 226
 	Secret          ChannelMode = 's' // flag, deprecated
235 227
 	UserLimit       ChannelMode = 'l' // flag arg
236 228
 	Voice           ChannelMode = 'v' // arg
237
-
238
-	MultiPrefix Capability = "multi-prefix"
239
-	SASL        Capability = "sasl"
240
-
241
-	Disable CapModifier = '-'
242
-	Ack     CapModifier = '~'
243
-	Sticky  CapModifier = '='
244
-)
245
-
246
-var (
247
-	SupportedCapabilities = CapabilitySet{
248
-		MultiPrefix: true,
249
-	}
250 229
 )
251 230
 
252 231
 const (
253 232
 	Registration Phase = iota
254 233
 	Normal       Phase = iota
255 234
 )
256
-
257
-const (
258
-	CapNone        CapState = iota
259
-	CapNegotiating CapState = iota
260
-	CapNegotiated  CapState = iota
261
-)

+ 0
- 37
irc/server.go 查看文件

@@ -275,43 +275,6 @@ func (msg *ProxyCommand) HandleRegServer(server *Server) {
275 275
 	msg.Client().hostname = msg.hostname
276 276
 }
277 277
 
278
-func (msg *CapCommand) HandleRegServer(server *Server) {
279
-	client := msg.Client()
280
-
281
-	switch msg.subCommand {
282
-	case CAP_LS:
283
-		client.capState = CapNegotiating
284
-		client.Reply(RplCap(client, CAP_LS, SupportedCapabilities))
285
-
286
-	case CAP_LIST:
287
-		client.Reply(RplCap(client, CAP_LIST, client.capabilities))
288
-
289
-	case CAP_REQ:
290
-		for capability := range msg.capabilities {
291
-			if !SupportedCapabilities[capability] {
292
-				client.Reply(RplCap(client, CAP_NAK, msg.capabilities))
293
-				return
294
-			}
295
-		}
296
-		for capability := range msg.capabilities {
297
-			client.capabilities[capability] = true
298
-		}
299
-		client.Reply(RplCap(client, CAP_ACK, msg.capabilities))
300
-
301
-	case CAP_CLEAR:
302
-		reply := RplCap(client, CAP_ACK, client.capabilities.DisableString())
303
-		client.capabilities = make(CapabilitySet)
304
-		client.Reply(reply)
305
-
306
-	case CAP_END:
307
-		client.capState = CapNegotiated
308
-		server.tryRegister(client)
309
-
310
-	default:
311
-		client.ErrInvalidCapCmd(msg.subCommand)
312
-	}
313
-}
314
-
315 278
 func (m *NickCommand) HandleRegServer(s *Server) {
316 279
 	client := m.Client()
317 280
 	if !client.authorized {

+ 0
- 38
irc/types.go 查看文件

@@ -9,44 +9,6 @@ import (
9 9
 // simple types
10 10
 //
11 11
 
12
-type CapSubCommand string
13
-
14
-type Capability string
15
-
16
-func (capability Capability) String() string {
17
-	return string(capability)
18
-}
19
-
20
-type CapModifier rune
21
-
22
-func (mod CapModifier) String() string {
23
-	return string(mod)
24
-}
25
-
26
-type CapState uint
27
-
28
-type CapabilitySet map[Capability]bool
29
-
30
-func (set CapabilitySet) String() string {
31
-	strs := make([]string, len(set))
32
-	index := 0
33
-	for capability := range set {
34
-		strs[index] = string(capability)
35
-		index += 1
36
-	}
37
-	return strings.Join(strs, " ")
38
-}
39
-
40
-func (set CapabilitySet) DisableString() string {
41
-	parts := make([]string, len(set))
42
-	index := 0
43
-	for capability := range set {
44
-		parts[index] = Disable.String() + capability.String()
45
-		index += 1
46
-	}
47
-	return strings.Join(parts, " ")
48
-}
49
-
50 12
 // add, remove, list modes
51 13
 type ModeOp rune
52 14
 

Loading…
取消
儲存