Procházet zdrojové kódy

send proper replies for cap protocol

tags/v0.1.0
Jeremy Latt před 10 roky
rodič
revize
0874692aa8
4 změnil soubory, kde provedl 39 přidání a 24 odebrání
  1. 4
    1
      irc/client.go
  2. 13
    14
      irc/commands.go
  3. 17
    9
      irc/server.go
  4. 5
    0
      irc/types.go

+ 4
- 1
irc/client.go Zobrazit soubor

235
 	}
235
 	}
236
 }
236
 }
237
 
237
 
238
-func (client *Client) Reply(reply string) {
238
+func (client *Client) Reply(reply string, args ...interface{}) {
239
+	if len(args) > 0 {
240
+		reply = fmt.Sprintf(reply, args...)
241
+	}
239
 	client.socket.Write(reply)
242
 	client.socket.Write(reply)
240
 }
243
 }
241
 
244
 

+ 13
- 14
irc/commands.go Zobrazit soubor

708
 
708
 
709
 type CapCommand struct {
709
 type CapCommand struct {
710
 	BaseCommand
710
 	BaseCommand
711
-	subCommand CapSubCommand
712
-	args       []string
711
+	subCommand   CapSubCommand
712
+	capabilities CapabilitySet
713
 }
713
 }
714
 
714
 
715
 func (msg *CapCommand) String() string {
715
 func (msg *CapCommand) String() string {
716
-	return fmt.Sprintf("CAP(subCommand=%s, args=%s)", msg.subCommand, msg.args)
717
-}
718
-
719
-func (msg *CapCommand) Capabilities() []Capability {
720
-	strs := strings.Split(msg.args[0], " ")
721
-	caps := make([]Capability, len(strs))
722
-	for index, str := range strs {
723
-		caps[index] = Capability(str)
724
-	}
725
-	return caps
716
+	return fmt.Sprintf("CAP(subCommand=%s, capabilities=%s)",
717
+		msg.subCommand, msg.capabilities)
726
 }
718
 }
727
 
719
 
728
 func NewCapCommand(args []string) (editableCommand, error) {
720
 func NewCapCommand(args []string) (editableCommand, error) {
731
 	}
723
 	}
732
 
724
 
733
 	cmd := &CapCommand{
725
 	cmd := &CapCommand{
734
-		subCommand: CapSubCommand(args[0]),
735
-		args:       args[1:],
726
+		subCommand:   CapSubCommand(strings.ToUpper(args[0])),
727
+		capabilities: make(CapabilitySet),
728
+	}
729
+
730
+	if len(args) > 1 {
731
+		strs := spacesExpr.Split(args[1], -1)
732
+		for _, str := range strs {
733
+			cmd.capabilities[Capability(str)] = true
734
+		}
736
 	}
735
 	}
737
 	return cmd, nil
736
 	return cmd, nil
738
 }
737
 }

+ 17
- 9
irc/server.go Zobrazit soubor

301
 	switch msg.subCommand {
301
 	switch msg.subCommand {
302
 	case CAP_LS:
302
 	case CAP_LS:
303
 		client.capState = CapNegotiating
303
 		client.capState = CapNegotiating
304
-		client.Reply(fmt.Sprintf("CAP LS :%d", MultiPrefix))
304
+		client.Reply("CAP LS * :%s", SupportedCapabilities)
305
 
305
 
306
 	case CAP_LIST:
306
 	case CAP_LIST:
307
-		client.Reply(fmt.Sprintf("CAP LIST :%s", client.capabilities))
307
+		client.Reply("CAP LIST * :%s", client.capabilities)
308
 
308
 
309
 	case CAP_REQ:
309
 	case CAP_REQ:
310
 		client.capState = CapNegotiating
310
 		client.capState = CapNegotiating
311
-		caps := msg.Capabilities()
312
-		if (len(caps) != 1) && (caps[0] != MultiPrefix) {
313
-			client.Reply("CAP NAK :" + msg.args[0])
314
-			return
311
+		for capability := range msg.capabilities {
312
+			if !SupportedCapabilities[capability] {
313
+				client.Reply("CAP NAK * :%s", msg.capabilities)
314
+				return
315
+			}
315
 		}
316
 		}
316
-		for _, capability := range caps {
317
+		for capability := range msg.capabilities {
317
 			client.capabilities[capability] = true
318
 			client.capabilities[capability] = true
318
 		}
319
 		}
319
-		client.Reply("CAP ACK :" + msg.args[0])
320
+		client.Reply("CAP ACK * :%s", msg.capabilities)
320
 
321
 
321
 	case CAP_CLEAR:
322
 	case CAP_CLEAR:
323
+		format := strings.TrimRight(
324
+			strings.Repeat("%s%s ", len(client.capabilities)), " ")
325
+		args := make([]interface{}, len(client.capabilities))
326
+		index := 0
322
 		for capability := range client.capabilities {
327
 		for capability := range client.capabilities {
328
+			args[index] = Disable
329
+			args[index+1] = capability
330
+			index += 2
323
 			delete(client.capabilities, capability)
331
 			delete(client.capabilities, capability)
324
 		}
332
 		}
325
-		client.Reply("CAP ACK :")
333
+		client.Reply("CAP ACK * :"+format, args...)
326
 
334
 
327
 	case CAP_END:
335
 	case CAP_END:
328
 		client.capState = CapNegotiated
336
 		client.capState = CapNegotiated

+ 5
- 0
irc/types.go Zobrazit soubor

16
 
16
 
17
 type CapModifier rune
17
 type CapModifier rune
18
 
18
 
19
+func (mod CapModifier) String() string {
20
+	return string(mod)
21
+}
22
+
19
 type CapState uint
23
 type CapState uint
20
 
24
 
21
 type CapabilitySet map[Capability]bool
25
 type CapabilitySet map[Capability]bool
25
 	index := 0
29
 	index := 0
26
 	for capability := range set {
30
 	for capability := range set {
27
 		strs[index] = string(capability)
31
 		strs[index] = string(capability)
32
+		index += 1
28
 	}
33
 	}
29
 	return strings.Join(strs, " ")
34
 	return strings.Join(strs, " ")
30
 }
35
 }

Načítá se…
Zrušit
Uložit