Selaa lähdekoodia

send proper replies for cap protocol

tags/v0.1.0
Jeremy Latt 10 vuotta sitten
vanhempi
commit
0874692aa8
4 muutettua tiedostoa jossa 39 lisäystä ja 24 poistoa
  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 Näytä tiedosto

@@ -235,7 +235,10 @@ func (client *Client) ChangeNickname(nickname string) {
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 242
 	client.socket.Write(reply)
240 243
 }
241 244
 

+ 13
- 14
irc/commands.go Näytä tiedosto

@@ -708,21 +708,13 @@ func NewOperCommand(args []string) (editableCommand, error) {
708 708
 
709 709
 type CapCommand struct {
710 710
 	BaseCommand
711
-	subCommand CapSubCommand
712
-	args       []string
711
+	subCommand   CapSubCommand
712
+	capabilities CapabilitySet
713 713
 }
714 714
 
715 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 720
 func NewCapCommand(args []string) (editableCommand, error) {
@@ -731,8 +723,15 @@ func NewCapCommand(args []string) (editableCommand, error) {
731 723
 	}
732 724
 
733 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 736
 	return cmd, nil
738 737
 }

+ 17
- 9
irc/server.go Näytä tiedosto

@@ -301,28 +301,36 @@ func (msg *CapCommand) HandleRegServer(server *Server) {
301 301
 	switch msg.subCommand {
302 302
 	case CAP_LS:
303 303
 		client.capState = CapNegotiating
304
-		client.Reply(fmt.Sprintf("CAP LS :%d", MultiPrefix))
304
+		client.Reply("CAP LS * :%s", SupportedCapabilities)
305 305
 
306 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 309
 	case CAP_REQ:
310 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 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 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 327
 		for capability := range client.capabilities {
328
+			args[index] = Disable
329
+			args[index+1] = capability
330
+			index += 2
323 331
 			delete(client.capabilities, capability)
324 332
 		}
325
-		client.Reply("CAP ACK :")
333
+		client.Reply("CAP ACK * :"+format, args...)
326 334
 
327 335
 	case CAP_END:
328 336
 		client.capState = CapNegotiated

+ 5
- 0
irc/types.go Näytä tiedosto

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

Loading…
Peruuta
Tallenna