From 6fdea28236771ee1d90a6fc959075c79939ad566 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 8 Dec 2024 13:57:25 +0800 Subject: CAP: Primitive negotiation --- clients.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'clients.go') diff --git a/clients.go b/clients.go index 07dc532..2719b08 100644 --- a/clients.go +++ b/clients.go @@ -15,6 +15,7 @@ type Client struct { Ident string Gecos string Host string + Caps map[string]struct{} Server Server State ClientState } @@ -65,6 +66,7 @@ func NewLocalClient(conn *net.Conn) (*Client, error) { Server: self, State: ClientStatePreRegistration, Nick: "*", + Caps: make(map[string]struct{}), } for range 10 { uid_ := []byte(self.SID) @@ -86,22 +88,32 @@ func NewLocalClient(conn *net.Conn) (*Client, error) { } func (client *Client) checkRegistration() error { - if client.State != ClientStatePreRegistration { - slog.Error("spurious call to checkRegistration", "client", client) - return ErrCallState - } - if client.Nick != "*" && client.Ident != "" { - return client.Send(MakeMsg(self, RPL_WELCOME, client.Nick, "Welcome")) + switch client.State { + case ClientStatePreRegistration: + if client.Nick != "*" && client.Ident != "" { + client.State = ClientStateRegistered + return client.Send(MakeMsg(self, RPL_WELCOME, client.Nick, "Welcome")) + } + return nil // Incomplete for registration + case ClientStateCapabilitiesFinished: + if client.Nick != "*" && client.Ident != "" { + client.State = ClientStateRegistered + return client.Send(MakeMsg(self, RPL_WELCOME, client.Nick, "Welcome")) + } + return nil + default: + return nil } - return nil } type ClientState uint8 const ( - ClientStateRemote ClientState = iota - ClientStatePreRegistration + ClientStatePreRegistration ClientState = iota + ClientStateCapabilities + ClientStateCapabilitiesFinished ClientStateRegistered + ClientStateRemote ) var ( -- cgit v1.2.3