瀏覽代碼

fix CAP messages

tags/v0.1.0
Jeremy Latt 10 年之前
父節點
當前提交
33df043961
共有 4 個檔案被更改,包括 26 行新增16 行删除
  1. 1
    1
      irc/constants.go
  2. 4
    0
      irc/reply.go
  3. 7
    15
      irc/server.go
  4. 14
    0
      irc/types.go

+ 1
- 1
irc/constants.go 查看文件

@@ -23,7 +23,7 @@ var (
23 23
 )
24 24
 
25 25
 const (
26
-	SEM_VER       = "ergonomadic-1.2.14"
26
+	SEM_VER       = "ergonomadic-1.2.15"
27 27
 	CRLF          = "\r\n"
28 28
 	MAX_REPLY_LEN = 512 - len(CRLF)
29 29
 

+ 4
- 0
irc/reply.go 查看文件

@@ -142,6 +142,10 @@ func RplKill(client *Client, target *Client, comment string) string {
142 142
 		"%s :%s", target.Nick(), comment)
143 143
 }
144 144
 
145
+func RplCap(client *Client, subCommand CapSubCommand, arg interface{}) string {
146
+	return NewStringReply(nil, CAP, "%s %s :%s", client.Nick(), subCommand, arg)
147
+}
148
+
145 149
 // numeric replies
146 150
 
147 151
 func (target *Client) RplWelcome() {

+ 7
- 15
irc/server.go 查看文件

@@ -270,36 +270,28 @@ func (msg *CapCommand) HandleRegServer(server *Server) {
270 270
 	switch msg.subCommand {
271 271
 	case CAP_LS:
272 272
 		client.capState = CapNegotiating
273
-		client.Reply("CAP LS * :%s", SupportedCapabilities)
273
+		client.Reply(RplCap(client, CAP_LS, SupportedCapabilities))
274 274
 
275 275
 	case CAP_LIST:
276
-		client.Reply("CAP LIST * :%s", client.capabilities)
276
+		client.Reply(RplCap(client, CAP_LIST, client.capabilities))
277 277
 
278 278
 	case CAP_REQ:
279 279
 		client.capState = CapNegotiating
280 280
 		for capability := range msg.capabilities {
281 281
 			if !SupportedCapabilities[capability] {
282
-				client.Reply("CAP NAK * :%s", msg.capabilities)
282
+				client.Reply(RplCap(client, CAP_NAK, msg.capabilities))
283 283
 				return
284 284
 			}
285 285
 		}
286 286
 		for capability := range msg.capabilities {
287 287
 			client.capabilities[capability] = true
288 288
 		}
289
-		client.Reply("CAP ACK * :%s", msg.capabilities)
289
+		client.Reply(RplCap(client, CAP_ACK, msg.capabilities))
290 290
 
291 291
 	case CAP_CLEAR:
292
-		format := strings.TrimRight(
293
-			strings.Repeat("%s%s ", len(client.capabilities)), " ")
294
-		args := make([]interface{}, len(client.capabilities))
295
-		index := 0
296
-		for capability := range client.capabilities {
297
-			args[index] = Disable
298
-			args[index+1] = capability
299
-			index += 2
300
-			delete(client.capabilities, capability)
301
-		}
302
-		client.Reply("CAP ACK * :"+format, args...)
292
+		reply := RplCap(client, CAP_ACK, client.capabilities.DisableString())
293
+		client.capabilities = make(CapabilitySet)
294
+		client.Reply(reply)
303 295
 
304 296
 	case CAP_END:
305 297
 		client.capState = CapNegotiated

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

@@ -14,6 +14,10 @@ type CapSubCommand string
14 14
 
15 15
 type Capability string
16 16
 
17
+func (capability Capability) String() string {
18
+	return string(capability)
19
+}
20
+
17 21
 type CapModifier rune
18 22
 
19 23
 func (mod CapModifier) String() string {
@@ -34,6 +38,16 @@ func (set CapabilitySet) String() string {
34 38
 	return strings.Join(strs, " ")
35 39
 }
36 40
 
41
+func (set CapabilitySet) DisableString() string {
42
+	parts := make([]string, len(set))
43
+	index := 0
44
+	for capability := range set {
45
+		parts[index] = Disable.String() + capability.String()
46
+		index += 1
47
+	}
48
+	return strings.Join(parts, " ")
49
+}
50
+
37 51
 // a string with wildcards
38 52
 type Mask string
39 53
 

Loading…
取消
儲存