aboutsummaryrefslogtreecommitdiff
path: root/cmd_cap.go
blob: c76bd5751197f8537cca39521ab8bb187e4b2ab0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main

import (
	"strings"
)

func init() {
	CommandHandlers["CAP"] = handleClientCap
}

func handleClientCap(msg RMsg, client *Client) error {
	if len(msg.Params) < 1 {
		err := client.Send(MakeMsg(self, ERR_NEEDMOREPARAMS, "CAP", "Not enough parameters"))
		if err != nil {
			return err
		}
		return nil
	}
	if client.State == ClientStateRemote {
		return ErrRemoteClient
	}
	switch strings.ToUpper(msg.Params[0]) {
	case "LS":
		if client.State == ClientStatePreRegistration {
			client.State = ClientStateCapabilities
		}
		var err error
		if len(msg.Params) >= 2 && msg.Params[1] == "302" {
			err = client.Send(MakeMsg(self, "CAP", client.Nick, "LS", capls302))
		} else {
			err = client.Send(MakeMsg(self, "CAP", client.Nick, "LS", capls))
		}
		// TODO: Split when too long
		if err != nil {
			return err
		}
	case "REQ":
		if client.State == ClientStatePreRegistration {
			client.State = ClientStateCapabilities
		}
		caps := strings.Split(msg.Params[1], " ")
		for _, c := range caps {
			if c[0] == '-' {
				// TODO: Remove capability
				delete(client.Caps, c)
				continue
			}
			_, ok := Caps[c]
			if ok {
				err := client.Send(MakeMsg(self, "CAP", client.Nick, "ACK", c))
				if err != nil {
					return err
				}
				client.Caps[c] = struct{}{}
				// TODO: This is terrible
			} else {
				err := client.Send(MakeMsg(self, "CAP", client.Nick, "NAK", c))
				if err != nil {
					return err
				}
			}
		}
	case "END":
		if client.State != ClientStateCapabilities {
			// Just ignore it
			return nil
		}
		client.State = ClientStateCapabilitiesFinished
		err := client.checkRegistration()
		if err != nil {
			return err
		}
	}
	return nil
}